fix displaying of tags that include a whitespace (use + instead)
[photo-tags.git] / getjson.php
index cbb9feea559cc144bf756dfb1a89e021e92dd65f..14af09b3003bf7feb5f81a728e52b56f72228d79 100644 (file)
 <?php
 
+$N=30;
+
 /* parse ini -file */
 $iniarray=parse_ini_file("config.ini");
 $DBFILE=$iniarray["fspotdb"];
 $usePDO=$iniarray["usePDO"];
+$N=$iniarray["pics_per_page"];
 /* end parse ini-file */
 
-if (isset($_REQUEST["O"]))
-  $O = "".$_REQUEST["O"].",";
-else
-  $O="";
-
-if (isset($_REQUEST["N"]))
-  $N = "".$_REQUEST["N"];
-else
-  $N= 25;
-
 if($usePDO)
   $DB = new PDO("sqlite:$DBFILE");
 else
   $DB = new SQlite3($DBFILE);
 
-$result = $DB->query("SELECT * FROM photos LIMIT $O $N");
 
-$row = array();
+/* do database query */
+if (isset($_REQUEST["S"]))
+  {
+    /* single tag or part of tag */
+    $tag = sqlite_escape_string($_REQUEST["S"]);
 
-$i = 0;
+    $result = $DB->query("SELECT name FROM tags");
+    $count = $DB->query("SELECT 1");
+  }
+else
+  {
+    if (isset($_REQUEST["P"]))
+      $OFFSET = "".(intval($_REQUEST["P"])*$N-$N);
+    else
+      $OFFSET = "0";
+
+    if (isset($_REQUEST["T"]))
+      {
+       /* single tag or part of tag */
+       $tags = sqlite_escape_string($_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)");
 
+      }
+    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;
-    }
-    $result=$tmp;
   }
+else
+  {
+    foreach($result as $res)
+      $tmp[]=$res;
+  }
+$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();
+
 
 ?>