d5dab8b20bb637b5a5afbd7c8487de377688093d
[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["NP"]))
27   {
28     /* get +- 5 pics from ordered list to show next to a large image */
29
30     /* first create a temp table with all images and then use rowid to get +-5 images */
31
32     if (isset($_REQUEST["T"]))
33       {
34         /* single tag or part of tag */
35         $tags = $_REQUEST["T"];
36         $tags = explode(",",$tags);
37         $nrtags = count($tags);
38
39         foreach ($tags as $key => $value)
40           $tags[$key]=sqlite_escape_string(trim($value));
41         $tags = "'".implode("','",$tags)."'";
42
43         $DB->query("CREATE TEMP TABLE NEXTPREV AS SELECT base_uri, filename, p.id as id  FROM photo_tags pt, photos p, tags t".
44                    " WHERE pt.tag_id = t.id".
45                    " AND (t.name COLLATE NOCASE IN ($tags))".
46                    " AND p.id = pt.photo_id ".
47                    " GROUP BY p.id HAVING COUNT( p.id )=$nrtags");
48
49       }
50     else
51       {
52         $DB->query("CREATE TEMP TABLE NEXTPREV AS SELECT base_uri, filename, p.id as id FROM  photos p");
53       };
54
55     if (isset($_REQUEST["ID"]))
56       {
57         $ID=intval($_REQUEST["ID"]);
58         $result = $DB->query("SELECT * FROM NEXTPREV".
59                              " WHERE rowid > (select rowid from NEXTPREV where id=$ID) -5".
60                              "   AND rowid < (select rowid from NEXTPREV where id=$ID) +5");
61       }
62     else
63       {
64         $result = $DB->query("SELECT 1 where 1=2");
65       }
66
67   }
68  else if (isset($_REQUEST["ID"]))
69   {
70     $id  = intval($_REQUEST["ID"]);
71     $result = $DB->query("SELECT base_uri, filename, id FROM photos".
72                          " WHERE id=$id");
73   }
74  else if (isset($_REQUEST["CLOUD"]))
75   {
76     $result = $DB->query("SELECT t.name as name, count(*) as count FROM photo_tags pt ".
77                          " LEFT JOIN tags t on t.id=pt.tag_id".
78                          " GROUP BY t.id");
79   }
80  else
81   {
82     if (isset($_REQUEST["P"]))
83       $OFFSET = "".(intval($_REQUEST["P"])*$N-$N);
84     else
85       $OFFSET = "0";
86
87     if (isset($_REQUEST["T"]))
88       {
89         /* single tag or part of tag */
90         $tags = $_REQUEST["T"];
91         $tags = explode(",",$tags);
92         $nrtags = count($tags);
93
94         foreach ($tags as $key => $value)
95           $tags[$key]=sqlite_escape_string(trim($value));
96         $tags = "'".implode("','",$tags)."'";
97
98         /* individual tags are seperated by ',' */
99
100         /* use and AND query between tags as a default
101          a good explanation on different ways of doing this can be found at:
102          http://www.pui.ch/phred/archives/2005/04/tags-database-schemas.html
103         */
104
105         if (isset($_REQUEST["C"]))
106           {
107             $result = $DB->query("SELECT count(*) as total from (SELECT p.id FROM photo_tags pt, photos p, tags t".
108                                 " WHERE pt.tag_id = t.id".
109                                 " AND (t.name COLLATE NOCASE IN ($tags))".
110                                 " AND p.id = pt.photo_id ".
111                                 " GROUP BY p.id HAVING COUNT( p.id )=$nrtags)");
112           }
113         else
114           {
115             $result = $DB->query("SELECT base_uri, filename, p.id as id  FROM photo_tags pt, photos p, tags t".
116                                  " WHERE pt.tag_id = t.id".
117                                  " AND (t.name COLLATE NOCASE IN ($tags))".
118                                  " AND p.id = pt.photo_id ".
119                                  " GROUP BY p.id HAVING COUNT( p.id )=$nrtags".
120                                  "    LIMIT $OFFSET, $N");
121
122           }
123       }
124     else
125       {
126         if (isset($_REQUEST["C"]))
127           $result = $DB->query("SELECT count(*) as total FROM photos");
128         else
129           $result = $DB->query("SELECT * FROM photos LIMIT $OFFSET, $N");
130       }
131   }
132
133 /* encode result as an array */
134 $tmp=array();
135 if(!$usePDO)
136   {
137     /* convert results into array */
138     while($res = $result->fetchArray(SQLITE3_ASSOC))
139       $tmp[]=$res;
140   }
141 else
142   {
143     foreach($result as $res)
144       $tmp[]=$res;
145   }
146 $result=$tmp;
147
148 echo json_encode($result);
149
150 /* close the database */
151 if($usePDO)
152   $DB=null;
153 else
154   $DB->close();
155
156
157 ?>
158