some code cleanup
[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 +- 3 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     else
50       {
51         $DB->query("CREATE TEMP TABLE NEXTPREV AS SELECT base_uri, filename, p.id as id FROM  photos p");
52       };
53
54     if (isset($_REQUEST["ID"]))
55       {
56         $ID=intval($_REQUEST["ID"]);
57         $result = $DB->query("SELECT * FROM NEXTPREV".
58                              " WHERE rowid > (select rowid from NEXTPREV where id=$ID) -3".
59                              "   AND rowid < (select rowid from NEXTPREV where id=$ID) +3");
60       }
61     else
62       {
63         $result = $DB->query("SELECT 1 where 1=2");
64       }
65
66   }
67  else if (isset($_REQUEST["ID"]) && !isset($_REQUEST["C"]))
68   {
69     $id  = intval($_REQUEST["ID"]);
70     $result = $DB->query("SELECT base_uri, filename, id, description, time FROM photos".
71                          " WHERE id=$id");
72   }
73  else if (isset($_REQUEST["IDT"]))
74    {  /* tags of a single image */
75     $id  = intval($_REQUEST["IDT"]);
76     $result = $DB->query("SELECT t.name as name FROM photo_tags pt ".
77                          " LEFT JOIN tags t on t.id=pt.tag_id".
78                          " WHERE pt.photo_id=$id");
79   }
80  else if (isset($_REQUEST["CLOUD"]))
81   {
82     $result = $DB->query("SELECT t.name as name, count(*) as count FROM photo_tags pt ".
83                          " LEFT JOIN tags t on t.id=pt.tag_id".
84                          " GROUP BY t.id");
85   }
86  else
87   {
88     if (isset($_REQUEST["P"]))
89       $OFFSET = "".(intval($_REQUEST["P"])*$N-$N);
90     else
91       $OFFSET = "0";
92
93     if (isset($_REQUEST["T"]))
94       {
95         /* single tag or part of tag */
96         $tags = $_REQUEST["T"];
97         $tags = explode(",",$tags);
98         $nrtags = count($tags);
99
100         foreach ($tags as $key => $value)
101           $tags[$key]=sqlite_escape_string(trim($value));
102         $tags = "'".implode("','",$tags)."'";
103
104         /* individual tags are seperated by ',' */
105
106         /* use and AND query between tags as a default
107          a good explanation on different ways of doing this can be found at:
108          http://www.pui.ch/phred/archives/2005/04/tags-database-schemas.html
109         */
110
111         if (isset($_REQUEST["C"]))
112           {
113             $DB->query("CREATE TEMP TABLE TEMPPICS AS SELECT p.id as id FROM photo_tags pt, photos p, tags t".
114                        " WHERE pt.tag_id = t.id".
115                        " AND (t.name COLLATE NOCASE IN ($tags))".
116                        " AND p.id = pt.photo_id ".
117                        " GROUP BY p.id HAVING COUNT( p.id )=$nrtags");
118
119             if (isset($_REQUEST["ID"]))
120               {
121                 $ID = $_REQUEST["ID"];
122                 $result = $DB->query("SELECT count(*) as total, (SELECT rowid from TEMPPICS WHERE id = $ID) as row from TEMPPICS");
123               }
124             else
125               $result = $DB->query("SELECT count(*) as total, -1 as row from TEMPPICS");
126           }
127         else
128           {
129             $result = $DB->query("SELECT base_uri, filename, p.id as id  FROM photo_tags pt, photos p, tags t".
130                                  " WHERE pt.tag_id = t.id".
131                                  " AND (t.name COLLATE NOCASE IN ($tags))".
132                                  " AND p.id = pt.photo_id ".
133                                  " GROUP BY p.id HAVING COUNT( p.id )=$nrtags".
134                                  "    LIMIT $OFFSET, $N");
135           }
136       }
137     else
138       {
139         if (isset($_REQUEST["C"]))
140           {
141             $DB->query("CREATE TEMP TABLE TEMPPICS AS SELECT id from photos");
142
143             if (isset($_REQUEST["ID"]))
144               {
145                 $ID = $_REQUEST["ID"];
146                 $result = $DB->query("SELECT count(*) as total, (SELECT rowid FROM TEMPPICS WHERE id=$ID) as row FROM TEMPPICS");
147               }
148             else
149               $result = $DB->query("SELECT count(*) as total, -1 as row FROM TEMPPICS");
150           }
151         else
152           $result = $DB->query("SELECT * FROM photos LIMIT $OFFSET, $N");
153       }
154   }
155
156 /* encode result as an array */
157 $tmp=array();
158 if(!$usePDO)
159   {
160     /* convert results into array */
161     while($res = $result->fetchArray(SQLITE3_ASSOC))
162       $tmp[]=$res;
163   }
164 else
165   {
166     foreach($result as $res)
167       $tmp[]=$res;
168   }
169 $result=$tmp;
170
171 echo json_encode($result);
172
173 /* close the database */
174 if($usePDO)
175   $DB=null;
176 else
177   $DB->close();
178
179
180 ?>
181