added a tag cloud
[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 if (isset($_REQUEST["CLOUD"]))
33   {
34     $result = $DB->query("SELECT t.name as name, count(*) as count FROM photo_tags pt ".
35                          " LEFT JOIN tags t on t.id=pt.tag_id".
36                          " GROUP BY t.id");
37   }
38  else
39   {
40     if (isset($_REQUEST["P"]))
41       $OFFSET = "".(intval($_REQUEST["P"])*$N-$N);
42     else
43       $OFFSET = "0";
44
45     if (isset($_REQUEST["T"]))
46       {
47         /* single tag or part of tag */
48         $tags = $_REQUEST["T"];
49         $tags = explode(",",$tags);
50         $nrtags = count($tags);
51
52         foreach ($tags as $key => $value)
53           $tags[$key]=sqlite_escape_string(trim($value));
54         $tags = "'".implode("','",$tags)."'";
55
56         /* individual tags are seperated by ',' */
57
58         /* use and AND query between tags as a default
59          a good explanation on different ways of doing this can be found at:
60          http://www.pui.ch/phred/archives/2005/04/tags-database-schemas.html
61         */
62
63         if (isset($_REQUEST["C"]))
64           {
65             $result = $DB->query("SELECT count(*) as total from (SELECT p.id FROM photo_tags pt, photos p, tags t".
66                                 " WHERE pt.tag_id = t.id".
67                                 " AND (t.name COLLATE NOCASE IN ($tags))".
68                                 " AND p.id = pt.photo_id ".
69                                 " GROUP BY p.id HAVING COUNT( p.id )=$nrtags)");
70           }
71         else
72           {
73             $result = $DB->query("SELECT base_uri, filename, p.id as id  FROM photo_tags pt, photos p, tags t".
74                                  " WHERE pt.tag_id = t.id".
75                                  " AND (t.name COLLATE NOCASE IN ($tags))".
76                                  " AND p.id = pt.photo_id ".
77                                  " GROUP BY p.id HAVING COUNT( p.id )=$nrtags".
78                                  "    LIMIT $OFFSET, $N");
79
80           }
81       }
82     else
83       {
84         if (isset($_REQUEST["C"]))
85           $result = $DB->query("SELECT count(*) as total FROM photos");
86         else
87           $result = $DB->query("SELECT * FROM photos LIMIT $OFFSET, $N");
88       }
89   }
90
91 /* encode result as an array */
92 $tmp=array();
93 if(!$usePDO)
94   {
95     /* convert results into array */
96     while($res = $result->fetchArray(SQLITE3_ASSOC))
97       $tmp[]=$res;
98   }
99 else
100   {
101     foreach($result as $res)
102       $tmp[]=$res;
103   }
104 $result=$tmp;
105
106 echo json_encode($result);
107
108 /* close the database */
109 if($usePDO)
110   $DB=null;
111 else
112   $DB->close();
113
114
115 ?>
116