83360e6118387d957fdf4a6b139e5c079ce20ab8
[photo-tags.git] / getjson.php
1 <?php
2
3 $N=30;
4
5 /* parse ini -file */
6 $iniarray=parse_ini_file("config.ini");
7 $DBFILE=$iniarray["fspotdb"];
8 $usePDO=$iniarray["usePDO"];
9 $N=$iniarray["pics_per_page"];
10 /* end parse ini-file */
11
12 if($usePDO)
13   $DB = new PDO("sqlite:$DBFILE");
14 else
15   $DB = new SQlite3($DBFILE);
16
17
18 /* do database query */
19 if (isset($_REQUEST["S"]))
20   {
21     /* single tag or part of tag */
22     $tag = sqlite_escape_string($_REQUEST["S"]);
23
24     $result = $DB->query("SELECT name FROM tags");
25   }
26  else if (isset($_REQUEST["ID"]))
27   {
28     $id  = intval($_REQUEST["ID"]);
29     $result = $DB->query("SELECT base_uri, filename, id FROM photos".
30                          " WHERE id=$id");
31   }
32  else
33   {
34     if (isset($_REQUEST["P"]))
35       $OFFSET = "".(intval($_REQUEST["P"])*$N-$N);
36     else
37       $OFFSET = "0";
38
39     if (isset($_REQUEST["T"]))
40       {
41         /* single tag or part of tag */
42         $tags = $_REQUEST["T"];
43         $tags = explode(",",$tags);
44         $nrtags = count($tags);
45
46         foreach ($tags as $key => $value)
47           $tags[$key]=sqlite_escape_string(trim($value));
48         $tags = "'".implode("','",$tags)."'";
49
50         /* individual tags are seperated by ',' */
51
52         /* use and AND query between tags as a default
53          a good explanation on different ways of doing this can be found at:
54          http://www.pui.ch/phred/archives/2005/04/tags-database-schemas.html
55         */
56
57         if (isset($_REQUEST["C"]))
58           {
59             $result = $DB->query("SELECT count(*) as total from (SELECT p.id FROM photo_tags pt, photos p, tags t".
60                                 " WHERE pt.tag_id = t.id".
61                                 " AND (t.name COLLATE NOCASE IN ($tags))".
62                                 " AND p.id = pt.photo_id ".
63                                 " GROUP BY p.id HAVING COUNT( p.id )=$nrtags)");
64           }
65         else
66           {
67             $result = $DB->query("SELECT base_uri, filename, p.id as id  FROM photo_tags pt, photos p, tags t".
68                                  " WHERE pt.tag_id = t.id".
69                                  " AND (t.name COLLATE NOCASE IN ($tags))".
70                                  " AND p.id = pt.photo_id ".
71                                  " GROUP BY p.id HAVING COUNT( p.id )=$nrtags".
72                                  "    LIMIT $OFFSET, $N");
73
74           }
75       }
76     else
77       {
78         if (isset($_REQUEST["C"]))
79           $result = $DB->query("SELECT count(*) as total FROM photos");
80         else
81           $result = $DB->query("SELECT * FROM photos LIMIT $OFFSET, $N");
82       }
83   }
84
85 /* encode result as an array */
86 $tmp=array();
87 if(!$usePDO)
88   {
89     /* convert results into array */
90     while($res = $result->fetchArray(SQLITE3_ASSOC))
91       $tmp[]=$res;
92   }
93 else
94   {
95     foreach($result as $res)
96       $tmp[]=$res;
97   }
98 $result=$tmp;
99
100 echo json_encode($result);
101
102 /* close the database */
103 if($usePDO)
104   $DB=null;
105 else
106   $DB->close();
107
108
109 ?>
110