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