X-Git-Url: https://git.nubati.net/cgi-bin/gitweb.cgi?p=photo-tags.git;a=blobdiff_plain;f=getjson.php;h=3e4cf4d4e9470245a2997c030c200ca27e8e9eb8;hp=028864652f2d282bea6814ad56c2f129d32f8ed0;hb=f28ee54de58df18b6436d127b6dc06fa95eec8ea;hpb=629e06724cde4ecd8d1e30fa04567cc1960410c5 diff --git a/getjson.php b/getjson.php index 0288646..3e4cf4d 100644 --- a/getjson.php +++ b/getjson.php @@ -20,11 +20,63 @@ if (isset($_REQUEST["S"])) { /* single tag or part of tag */ $tag = sqlite_escape_string($_REQUEST["S"]); - /* individual tags are separated by '+' */ - $result = $DB->query("SELECT name FROM tags where name like \"%$tag%\""); - $count = $DB->query("SELECT 1"); + + $result = $DB->query("SELECT name FROM tags"); } -else + else if (isset($_REQUEST["NP"])) + { + /* get +- 5 pics from ordered list to show next to a large image */ + + /* first create a temp table with all images and then use rowid to get +-5 images */ + + if (isset($_REQUEST["T"])) + { + /* single tag or part of tag */ + $tags = $_REQUEST["T"]; + $tags = explode(",",$tags); + $nrtags = count($tags); + + foreach ($tags as $key => $value) + $tags[$key]=sqlite_escape_string(trim($value)); + $tags = "'".implode("','",$tags)."'"; + + $DB->query("CREATE TEMP TABLE NEXTPREV AS SELECT base_uri, filename, p.id as id FROM photo_tags pt, photos p, tags t". + " WHERE pt.tag_id = t.id". + " AND (t.name COLLATE NOCASE IN ($tags))". + " AND p.id = pt.photo_id ". + " GROUP BY p.id HAVING COUNT( p.id )=$nrtags"); + } + else + { + $DB->query("CREATE TEMP TABLE NEXTPREV AS SELECT base_uri, filename, p.id as id FROM photos p"); + }; + + if (isset($_REQUEST["ID"])) + { + $ID=intval($_REQUEST["ID"]); + $result = $DB->query("SELECT * FROM NEXTPREV". + " WHERE rowid > (select rowid from NEXTPREV where id=$ID) -5". + " AND rowid < (select rowid from NEXTPREV where id=$ID) +5"); + } + else + { + $result = $DB->query("SELECT 1 where 1=2"); + } + + } + else if (isset($_REQUEST["ID"]) && !isset($_REQUEST["C"])) + { + $id = intval($_REQUEST["ID"]); + $result = $DB->query("SELECT base_uri, filename, id FROM photos". + " WHERE id=$id"); + } + else if (isset($_REQUEST["CLOUD"])) + { + $result = $DB->query("SELECT t.name as name, count(*) as count FROM photo_tags pt ". + " LEFT JOIN tags t on t.id=pt.tag_id". + " GROUP BY t.id"); + } + else { if (isset($_REQUEST["P"])) $OFFSET = "".(intval($_REQUEST["P"])*$N-$N); @@ -34,26 +86,63 @@ else 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"); + $tags = $_REQUEST["T"]; + $tags = explode(",",$tags); + $nrtags = count($tags); - $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)"); + foreach ($tags as $key => $value) + $tags[$key]=sqlite_escape_string(trim($value)); + $tags = "'".implode("','",$tags)."'"; + /* individual tags are seperated by ',' */ + + /* use and AND query between tags as a default + a good explanation on different ways of doing this can be found at: + http://www.pui.ch/phred/archives/2005/04/tags-database-schemas.html + */ + + if (isset($_REQUEST["C"])) + { + $DB->query("CREATE TEMP TABLE TEMPPICS AS SELECT p.id as id FROM photo_tags pt, photos p, tags t". + " WHERE pt.tag_id = t.id". + " AND (t.name COLLATE NOCASE IN ($tags))". + " AND p.id = pt.photo_id ". + " GROUP BY p.id HAVING COUNT( p.id )=$nrtags"); + + if (isset($_REQUEST["ID"])) + { + $ID = $_REQUEST["ID"]; + $result = $DB->query("SELECT count(*) as total, (SELECT rowid from TEMPPICS WHERE id = $ID) as row from TEMPPICS"); + } + else + $result = $DB->query("SELECT count(*) as total, -1 as row from TEMPPICS"); + } + else + { + $result = $DB->query("SELECT base_uri, filename, p.id as id FROM photo_tags pt, photos p, tags t". + " WHERE pt.tag_id = t.id". + " AND (t.name COLLATE NOCASE IN ($tags))". + " AND p.id = pt.photo_id ". + " GROUP BY p.id HAVING COUNT( p.id )=$nrtags". + " LIMIT $OFFSET, $N"); + } } else { - $result = $DB->query("SELECT * FROM photos LIMIT $OFFSET, $N"); - $count = $DB->query("SELECT count(*) as total FROM photos"); + if (isset($_REQUEST["C"])) + { + $DB->query("CREATE TEMP TABLE TEMPPICS AS SELECT id from photos"); + + if (isset($_REQUEST["ID"])) + { + $ID = $_REQUEST["ID"]; + $result = $DB->query("SELECT count(*) as total, (SELECT rowid FROM TEMPPICS WHERE id=$ID) as row FROM TEMPPICS"); + } + else + $result = $DB->query("SELECT count(*) as total, -1 as row FROM TEMPPICS"); + } + else + $result = $DB->query("SELECT * FROM photos LIMIT $OFFSET, $N"); } } @@ -72,24 +161,7 @@ else } $result=$tmp; -/* 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($return); +echo json_encode($result); /* close the database */ if($usePDO)