initial support for tag search
[photo-tags.git] / getjson.php
index 6026776775ea5a5278becd77e0289b806b0bfceb..cc93c7c352b95f0b6c42466c7e3cadf02a929155 100644 (file)
@@ -9,46 +9,94 @@ $usePDO=$iniarray["usePDO"];
 $N=$iniarray["pics_per_page"];
 /* end parse ini-file */
 
-if (isset($_REQUEST["P"]))
-  $OFFSET = "".($_REQUEST["P"]*$N-$N).",";
-else
-  $OFFSET = "";
-
 if($usePDO)
   $DB = new PDO("sqlite:$DBFILE");
 else
   $DB = new SQlite3($DBFILE);
 
-$result = $DB->query("SELECT * FROM photos LIMIT $OFFSET $N");
 
-$row = array();
+/* do database query */
+if (isset($_REQUEST["S"]))
+  {
+    /* single tag or part of tag */
+    $tag = $_REQUEST["S"];
+    /* individual tags are separated by '+' */
+    $result = $DB->query("SELECT name FROM tags where name like \"%$tag%\"");
+    $count = $DB->query("SELECT 1");
+  }
+else
+  {
+    if (isset($_REQUEST["P"]))
+      $OFFSET = "".($_REQUEST["P"]*$N-$N);
+    else
+      $OFFSET = "0";
+
+    if (isset($_REQUEST["T"]))
+      {
+       /* single tag or part of tag */
+       $tags = $_REQUEST["T"];
+       $tags = explode("+",$tags);
+       $tags = "'".implode("','",$tags)."'";
+
+       /* individual tags are seperated by '+' */
+       $result = $DB->query("SELECT base_uri, filename FROM photos ".
+                            "    left join photo_tags on photos.id=photo_tags.photo_id ".
+                            "    left join tags on tags.id=photo_tags.tag_id ".
+                            "    where tags.name in ($tags) LIMIT $OFFSET, $N");
+
+       $count = $DB->query("SELECT count(*) as total FROM photos ".
+                           "    left join photo_tags on photos.id=photo_tags.photo_id ".
+                           "    left join tags on tags.id=photo_tags.tag_id ".
+                           "    where tags.name in ($tags)");
 
-$i = 0;
+      }
+    else
+      {
+       $result = $DB->query("SELECT * FROM photos LIMIT $OFFSET, $N");
+       $count = $DB->query("SELECT count(*) as total FROM photos");
+      }
+  }
 
+/* encode result as an array */
+$tmp=array();
 if(!$usePDO)
   {
     /* convert results into array */
-    $tmp=array();
-    while($res = $result->fetchArray(SQLITE3_ASSOC)){
+    while($res = $result->fetchArray(SQLITE3_ASSOC))
+      $tmp[]=$res;
+  }
+else
+  {
+    foreach($result as $res)
       $tmp[]=$res;
-    }
-    $result=$tmp;
   }
+$result=$tmp;
 
-foreach ($result as $res)
-{
-  $row[$i] = $res;
-  $i++;
-}
+/* encode count as an array */
+$tmp=array();
+if(!$usePDO)
+  {
+    /* convert results into array */
+    while($res = $count->fetchArray(SQLITE3_ASSOC))
+      $tmp[]=$res;
+  }
+else
+  {
+    foreach($count as $res)
+      $tmp[]=$res;
+  }
+$count=$tmp;
 
+$return=array($count,$result);
 
-echo json_encode($row);
+echo json_encode($return);
 
 /* close the database */
 if($usePDO)
   $DB=null;
 else
-  sqlite_close($DB);
+  $DB->close();
+
 
 ?>