getting basic search functionality working
authorPaul J R <me@pjr.cc>
Wed, 19 Sep 2012 23:18:49 +0000 (09:18 +1000)
committerPaul J R <me@pjr.cc>
Wed, 19 Sep 2012 23:18:49 +0000 (09:18 +1000)
gwvpmini/gwvpmini_db.php
gwvpmini/gwvpmini_search.php

index 00f20b1..1995e7d 100644 (file)
@@ -55,6 +55,7 @@ function gwvpmini_getUser($username=null, $email=null, $id=null)
 \r
 }
 
+
 function gwvpmini_getRepo($ownerid=null, $name=null, $id=null)\r
 {\r
        $conn = gwvpmini_ConnectDB();
@@ -505,16 +506,19 @@ function gwvpmini_GetOwnedRepos($username)
         */
        $conn = gwvpmini_ConnectDB();
        
+       
        $uid = gwvpmini_GetUserId($username);
        $sql = "select * from repos where repos_owner='$uid'";
-       error_log("owned repos sql $sql");
+       error_log("owned repos sql $sql for username $username");
        $res = $conn->query($sql);
+       if($username == "") return false;\r
        
        $retval = false;
        foreach($res as $row) {
                $id = $row["repos_id"];
                $retval[$id]["name"] = $row["repos_name"];
                $retval[$id]["desc"] = $row["repos_description"];
+               $retval[$id]["id"] = $row["repos_id"];
                error_log(print_r($row, true));
        }
        
@@ -569,11 +573,43 @@ function gwvpmini_GetUsers($startat = 0, $num = 10)
                $retval[$id]["desc"] = $row["user_desc"];
                $retval[$id]["level"] = $row["user_level"];
                $retval[$id]["status"] = $row["user_status"];
+               $retval[$id]["id"] = $row["user_id"];
        }
        
        return $retval;
 }\r
 
+function gwvp_findPeopleLike($search)\r
+{\r
+       $conn = gwvpmini_ConnectDB();
+       
+       $sql = "select * from users where user_username like '%$search%' or user_full_name like '%$search%'";\r
+       \r
+       $res = $conn->query($sql);
+       
+       if(!$res) {
+               error_log("SERACHUSER: $sql returned false");
+               return false;
+       } else {
+               error_log("SERACHUSER: $sql returned true");
+       }\r
+       \r
+       $retval = false;\r
+       foreach($res as $row) {\r
+               $id = $row["user_id"];\r
+               $retval[$id]["fullname"] = $row["user_full_name"];\r
+               $retval[$id]["username"] = $row["user_username"];\r
+               $retval[$id]["email"] = $row["user_email"];\r
+               $retval[$id]["desc"] = $row["user_desc"];\r
+               $retval[$id]["level"] = $row["user_level"];\r
+               $retval[$id]["status"] = $row["user_status"];\r
+               $retval[$id]["id"] = $row["user_id"];\r
+       }\r
+       
+       error_log("SEARCHUSER: array is ".print_r($retval, true));\r
+       return $retval;
+}
+
 function gwvpmini_GetRepos($startat=0, $num=200)\r
 {\r
        $conn = gwvpmini_ConnectDB();\r
@@ -598,6 +634,7 @@ function gwvpmini_GetRepos($startat=0, $num=200)
                $retval[$id]["name"] = $row["repos_name"];\r
                $retval[$id]["desc"] = $row["repos_description"];\r
                $retval[$id]["owner"] = $row["repos_owner"];\r
+               $retval[$id]["id"] = $row["repos_id"];
        }\r
        \r
        return $retval;\r
@@ -605,6 +642,39 @@ function gwvpmini_GetRepos($startat=0, $num=200)
        \r
 }\r
 
+function gwvp_findReposLike($search)\r
+{\r
+       $conn = gwvpmini_ConnectDB();
+       
+       /*
+        *      CREATE TABLE "repos" (
+       "repos_id" INTEGER PRIMARY KEY AUTOINCREMENT,
+       "repos_name" TEXT,
+       "repos_description" TEXT,
+       "repos_owner" INTEGER
+       )';
+       
+                       */
+       
+       $sql = "select * from repos where repos_name like '%$search%' or repos_description like '%$search%'";
+       
+       $res = $conn->query($sql);
+       
+       if(!$res) return false;
+       
+       $retval = false;
+       foreach($res as $row) {
+               $id = $row["repos_id"];
+               $retval[$id]["name"] = $row["repos_name"];
+               $retval[$id]["desc"] = $row["repos_description"];
+               $retval[$id]["owner"] = $row["repos_owner"];
+               $retval[$id]["id"] = $row["repos_id"];
+       }
+       
+       return $retval;
+       \r
+}\r
+\r
 function gwvpmini_GetNRepos()
 {
        $conn = gwvpmini_ConnectDB();
index 1583c3f..6bcf4c1 100644 (file)
@@ -45,7 +45,99 @@ function gwvpmini_SearchMainPage()
 
 function gwvpmini_SearchMainPageBody()
 {
-       echo "You searched for ".$_REQUEST["searchstring"];
+       global $BASE_URL;
+       
+       $search = $_REQUEST["searchstring"];
+       
+       $reps = gwvp_findReposLike($search);
+       $ppls = gwvp_findPeopleLike($search);
+       
+       
+       
+       /*echo "<pre>repos\n\n";
+       print_r($reps);
+       echo "\n\nppl\n\n";
+       print_r($ppls);
+       echo "</pre>";*/
+       
+       // we need to process the search string into tokens here
+       if($search == "") {
+               echo "You need to enter a search string<br>";
+               return;
+       }
+       
+       $vl = gwvpmini_SearchTokenizeString($search);
+       $search_str = $vl["str"];
+       
+       echo "<h2>Results</h2>";
+       echo "Searching for \"$search_str\"<br>Note: Search does not look INSIDE repos<br>";
+       echo "<table><tr><td bgcolor=\"#eeeeff\"><h3>Repo's</h3></td><td bgcolor=\"#eeffee\"><h3>People</h3></td></tr>";
+       // repos
+       echo "<tr valign=\"top\"><td>";
+       
+       
+       // get_gravatar( $email, $s = 80, $d = 'mm', $r = 'g', $img = false, $atts = array() ) {
+       if($reps != false) {
+               echo "<table border=\"1\">";
+               foreach($reps as $rep) {
+                       $ownerinfo = gwvpmini_getUser(null, null, $rep["owner"]);
+                       $userdets = get_gravatar($ownerinfo["email"], 40, 'mm', 'g', true);
+                       $userdets .= "<br><a href=\"$BASE_URL/user/".$ownerinfo["username"]."\">".$ownerinfo["username"]."</a>";
+                       
+                       $repodets = "<b>".$rep["name"]."</b><br>".$rep["desc"];
+                       echo "<tr><td>$userdets</td><td>$repodets</td></tr>";
+               }
+               echo "</table>";
+       } else echo "No Repo's Match";
+       
+       
+       echo "</td><td>";
+       // people
+       if($ppls != false) {\r
+               echo "<table border=\"1\">";
+               $ownedrepos = "BLAHBLAH";\r
+               foreach($ppls as $ppl) {\r
+                       $userdets = get_gravatar($ppl["email"], 40, 'mm', 'g', true);
+                       $userdets .= "<br><a href=\"$BASE_URL/user/".$ppl["username"]."\">".$ppl["username"]."</a>";
+                       $repos = gwvpmini_GetOwnedRepos($ppl["username"]);
+                       if($repos == false) $ownedrepos = "No Repos";
+                       else {
+                               $ownedrepos = "";
+                               foreach($repos as $repo) {
+                                       $ownedrepos .= "<b><a href=\"$BASE_URL/view/".$repo["name"]."\">".$repo["name"]."</a></b> - ".$repo["desc"]."<br>";
+                               }
+                       }
+                       echo "<tr><td>$userdets</td><td>$ownedrepos</td></tr>";\r
+               }\r
+               \r
+               echo "</table>";\r
+       } else echo "No People Match";\r
+       \r
+       
+       echo "</table>";
+}
+
+function gwvpmini_SearchTokenizeString($search)
+{
+       $inp = preg_replace("/[^a-zA-Z0-9 ]+/", "", $search);\r
+       \r
+       $res = preg_split("/ +/", trim($inp));\r
+
+       $ret["str"] = "";
+       $i = 0;
+       foreach($res as $r) {
+               if($i == 0) $ret["str"] = "<b>$r</b>";
+               else $ret["str"] .= " <i>and</i> <b>$r</b>";
+               $ret["words"][$i] = $r;
+               $i++;
+       }
+       
+       /*echo "<pre>";
+       print_r($ret);
+       echo "</pre>";*/
+       
+       return $ret;
+       
 }
 
 ?>
\ No newline at end of file