Add search box to filter projects on the front project list page
Add search box to filter projects on the front project list page

Enhancement #17

--- a/css/gitphp.css
+++ b/css/gitphp.css
@@ -159,4 +159,5 @@
 #blobData li, #blobData .li1 {font-weight: normal; vertical-align:top;}
 table.treeTable td.filesize { text-align: right; }
 table.treeTable td.expander { padding-right: 0px; }
+div.projectSearch { padding:8px; border:solid #d9d8d1; border-width:0px 0px 1px; }
 

--- a/include/controller/Controller_ProjectList.class.php
+++ b/include/controller/Controller_ProjectList.class.php
@@ -65,7 +65,7 @@
 		} else if (isset($this->params['txt']) && ($this->params['txt'] === true)) {
 			return '';
 		}
-		return $this->params['order'];
+		return $this->params['order'] . '|' . (isset($this->params['search']) ? $this->params['search'] : '');
 	}
 
 	/**
@@ -109,6 +109,8 @@
 			$this->params['order'] = $_GET['o'];
 		else
 			$this->params['order'] = 'project';
+		if (isset($_GET['s']))
+			$this->params['search'] = $_GET['s'];
 	}
 
 	/**
@@ -143,9 +145,19 @@
 		
 		$projectList = GitPHP_ProjectList::GetInstance();
 		$projectList->Sort($this->params['order']);
-		
-		if ($projectList->Count() > 0)
-			$this->tpl->assign('projectlist', $projectList);
+
+		if ((empty($this->params['opml']) || ($this->params['opml'] !== true)) &&
+		    (empty($this->params['txt']) || ($this->params['txt'] !== true)) &&
+		    (!empty($this->params['search']))) {
+		    	$this->tpl->assign('search', $this->params['search']);
+			$matches = $projectList->Filter($this->params['search']);
+			if (count($matches) > 0) {
+				$this->tpl->assign('projectlist', $matches);
+			}
+		} else {
+			if ($projectList->Count() > 0)
+				$this->tpl->assign('projectlist', $projectList);
+		}
 	}
 
 }

--- a/include/git/ProjectListBase.class.php
+++ b/include/git/ProjectListBase.class.php
@@ -226,6 +226,33 @@
 	}
 
 	/**
+	 * Filter
+	 *
+	 * Returns a filtered list of projects
+	 *
+	 * @access public
+	 * @param string $filter filter pattern
+	 * @return array array of filtered projects
+	 */
+	public function Filter($pattern = null)
+	{
+		if (empty($pattern))
+			return $this->projects;
+
+		$matches = array();
+
+		foreach ($this->projects as $proj) {
+			if ((stripos($proj->GetProject(), $pattern) !== false) ||
+			    (stripos($proj->GetDescription(), $pattern) !== false) ||
+			    (stripos($proj->GetOwner(), $pattern) !== false)) {
+			    	$matches[] = $proj;
+			}
+		}
+
+		return $matches;
+	}
+
+	/**
 	 * ApplyProjectSettings
 	 *
 	 * Applies override settings for a project

--- a/templates/projectlist.tpl
+++ b/templates/projectlist.tpl
@@ -16,6 +16,12 @@
 git source code archive
 </p>
 {/if}
+</div>
+
+<div class="projectSearch">
+<form method="get" action="index.php" enctype="application/x-www-form-urlencoded">
+{t}Search projects{/t}: <input type="text" name="s" {if $search}value="{$search}"{/if} /> <a href="index.php" {if !$search}style="display: none;"{/if}>X</a>
+</form>
 </div>
 
 <table cellspacing="0">

comments