14af09b3003bf7feb5f81a728e52b56f72228d79
[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 = sqlite_escape_string($_REQUEST["T"]);
38         $tags = explode("+",$tags);
39         $tags = "'".implode("','",$tags)."'";
40
41         /* individual tags are seperated by '+' */
42         $result = $DB->query("SELECT base_uri, filename FROM photos ".
43                              "    left join photo_tags on photos.id=photo_tags.photo_id ".
44                              "    left join tags on tags.id=photo_tags.tag_id ".
45                              "    where tags.name in ($tags) LIMIT $OFFSET, $N");
46
47         $count = $DB->query("SELECT count(*) as total FROM photos ".
48                             "    left join photo_tags on photos.id=photo_tags.photo_id ".
49                             "    left join tags on tags.id=photo_tags.tag_id ".
50                             "    where tags.name in ($tags)");
51
52       }
53     else
54       {
55         $result = $DB->query("SELECT * FROM photos LIMIT $OFFSET, $N");
56         $count = $DB->query("SELECT count(*) as total FROM photos");
57       }
58   }
59
60 /* encode result as an array */
61 $tmp=array();
62 if(!$usePDO)
63   {
64     /* convert results into array */
65     while($res = $result->fetchArray(SQLITE3_ASSOC))
66       $tmp[]=$res;
67   }
68 else
69   {
70     foreach($result as $res)
71       $tmp[]=$res;
72   }
73 $result=$tmp;
74
75 /* encode count as an array */
76 $tmp=array();
77 if(!$usePDO)
78   {
79     /* convert results into array */
80     while($res = $count->fetchArray(SQLITE3_ASSOC))
81       $tmp[]=$res;
82   }
83 else
84   {
85     foreach($count as $res)
86       $tmp[]=$res;
87   }
88 $count=$tmp;
89
90 $return=array($count,$result);
91
92 echo json_encode($return);
93
94 /* close the database */
95 if($usePDO)
96   $DB=null;
97 else
98   $DB->close();
99
100
101 ?>
102