better display of description and time for a single photo
[photo-tags.git] / getjson.php
1 <?php
2
3   /**
4     copyright 2012,2013 Arun Persaud <arun@nubati.net>
5
6     This file is part of photo-tags.
7
8     Photo-tags is free software: you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation, either version 3 of the License, or
11     (at your option) any later version.
12
13     Photo-tags is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17
18     You should have received a copy of the GNU General Public License
19     along with Photo-tags.  If not, see <http://www.gnu.org/licenses/>.
20
21   **/
22
23 $N=30;
24
25 /* parse ini -file */
26 $iniarray=parse_ini_file("config.ini");
27 $DBFILE=$iniarray["fspotdb"];
28 $usePDO=$iniarray["usePDO"];
29 $N=$iniarray["pics_per_page"];
30 /* end parse ini-file */
31
32 if($usePDO)
33   $DB = new PDO("sqlite:$DBFILE");
34 else
35   $DB = new SQlite3($DBFILE);
36
37
38 /* do database query */
39 if (isset($_REQUEST["S"]))
40   {
41     /* single tag or part of tag */
42     $tag = sqlite_escape_string($_REQUEST["S"]);
43
44     $result = $DB->query("SELECT name FROM tags");
45   }
46  else if (isset($_REQUEST["NP"]))
47   {
48     /* get +- 3 pics from ordered list to show next to a large image */
49
50     /* first create a temp table with all images and then use rowid to get +-5 images */
51
52     if (isset($_REQUEST["T"]))
53       {
54         /* single tag or part of tag */
55         $tags = $_REQUEST["T"];
56         $tags = explode(",",$tags);
57         $nrtags = count($tags);
58
59         foreach ($tags as $key => $value)
60           $tags[$key]=sqlite_escape_string(trim($value));
61         $tags = "'".implode("','",$tags)."'";
62
63         $DB->query("CREATE TEMP TABLE NEXTPREV AS SELECT base_uri, filename, p.id as id  FROM photo_tags pt, photos p, tags t".
64                    " WHERE pt.tag_id = t.id".
65                    " AND (t.name COLLATE NOCASE IN ($tags))".
66                    " AND p.id = pt.photo_id ".
67                    " GROUP BY p.id HAVING COUNT( p.id )=$nrtags");
68       }
69     else
70       {
71         $DB->query("CREATE TEMP TABLE NEXTPREV AS SELECT base_uri, filename, p.id as id FROM  photos p");
72       };
73
74     if (isset($_REQUEST["ID"]))
75       {
76         $ID=intval($_REQUEST["ID"]);
77         $result = $DB->query("SELECT * FROM NEXTPREV".
78                              " WHERE rowid > (select rowid from NEXTPREV where id=$ID) -3".
79                              "   AND rowid < (select rowid from NEXTPREV where id=$ID) +3");
80       }
81     else
82       {
83         $result = $DB->query("SELECT 1 where 1=2");
84       }
85
86   }
87  else if (isset($_REQUEST["ID"]) && !isset($_REQUEST["C"]))
88   {
89     $id  = intval($_REQUEST["ID"]);
90     $result = $DB->query("SELECT base_uri, filename, id, description, time FROM photos".
91                          " WHERE id=$id");
92   }
93  else if (isset($_REQUEST["IDT"]))
94    {  /* tags of a single image */
95     $id  = intval($_REQUEST["IDT"]);
96     $result = $DB->query("SELECT t.name as name FROM photo_tags pt ".
97                          " LEFT JOIN tags t on t.id=pt.tag_id".
98                          " WHERE pt.photo_id=$id");
99   }
100  else if (isset($_REQUEST["CLOUD"]))
101   {
102     $result = $DB->query("SELECT t.name as name, count(*) as count FROM photo_tags pt ".
103                          " LEFT JOIN tags t on t.id=pt.tag_id".
104                          " GROUP BY t.id");
105   }
106  else
107   {
108     if (isset($_REQUEST["P"]))
109       $OFFSET = "".(intval($_REQUEST["P"])*$N-$N);
110     else
111       $OFFSET = "0";
112
113     if (isset($_REQUEST["T"]))
114       {
115         /* single tag or part of tag */
116         $tags = $_REQUEST["T"];
117         $tags = explode(",",$tags);
118         $nrtags = count($tags);
119
120         foreach ($tags as $key => $value)
121           $tags[$key]=sqlite_escape_string(trim($value));
122         $tags = "'".implode("','",$tags)."'";
123
124         /* individual tags are seperated by ',' */
125
126         /* use and AND query between tags as a default
127          a good explanation on different ways of doing this can be found at:
128          http://www.pui.ch/phred/archives/2005/04/tags-database-schemas.html
129         */
130
131         if (isset($_REQUEST["C"]))
132           {
133             $DB->query("CREATE TEMP TABLE TEMPPICS AS SELECT p.id as id FROM photo_tags pt, photos p, tags t".
134                        " WHERE pt.tag_id = t.id".
135                        " AND (t.name COLLATE NOCASE IN ($tags))".
136                        " AND p.id = pt.photo_id ".
137                        " GROUP BY p.id HAVING COUNT( p.id )=$nrtags");
138
139             if (isset($_REQUEST["ID"]))
140               {
141                 $ID = $_REQUEST["ID"];
142                 $result = $DB->query("SELECT count(*) as total, (SELECT rowid from TEMPPICS WHERE id = $ID) as row from TEMPPICS");
143               }
144             else
145               $result = $DB->query("SELECT count(*) as total, -1 as row from TEMPPICS");
146           }
147         else
148           {
149             $result = $DB->query("SELECT base_uri, filename, p.id as id  FROM photo_tags pt, photos p, tags t".
150                                  " WHERE pt.tag_id = t.id".
151                                  " AND (t.name COLLATE NOCASE IN ($tags))".
152                                  " AND p.id = pt.photo_id ".
153                                  " GROUP BY p.id HAVING COUNT( p.id )=$nrtags".
154                                  "    LIMIT $OFFSET, $N");
155           }
156       }
157     else
158       {
159         if (isset($_REQUEST["C"]))
160           {
161             $DB->query("CREATE TEMP TABLE TEMPPICS AS SELECT id from photos");
162
163             if (isset($_REQUEST["ID"]))
164               {
165                 $ID = $_REQUEST["ID"];
166                 $result = $DB->query("SELECT count(*) as total, (SELECT rowid FROM TEMPPICS WHERE id=$ID) as row FROM TEMPPICS");
167               }
168             else
169               $result = $DB->query("SELECT count(*) as total, -1 as row FROM TEMPPICS");
170           }
171         else
172           $result = $DB->query("SELECT * FROM photos LIMIT $OFFSET, $N");
173       }
174   }
175
176 /* encode result as an array */
177 $tmp=array();
178 if(!$usePDO)
179   {
180     /* convert results into array */
181     while($res = $result->fetchArray(SQLITE3_ASSOC))
182       $tmp[]=$res;
183   }
184 else
185   {
186     foreach($result as $res)
187       $tmp[]=$res;
188   }
189 $result=$tmp;
190
191 echo json_encode($result);
192
193 /* close the database */
194 if($usePDO)
195   $DB=null;
196 else
197   $DB->close();
198
199
200 ?>
201