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