add support to query multiple tags, use "," as a separation character
[photo-tags.git] / index.php
1 <?php
2 /* parse ini -file */
3 $iniarray=parse_ini_file("config.ini");
4 $webbase=$iniarray["webbase"];
5 $dbprefix=$iniarray["dbprefix"];
6 $admin=$iniarray["admin"];
7 $title=$iniarray["title"];
8 $N=$iniarray["pics_per_page"];
9 /* end parse ini-file */
10
11 /* parse flags */
12 if(isset($_REQUEST["page"]))
13   $page = intval($_REQUEST["page"]);
14 else
15   $page = 1;
16
17 if(isset($_REQUEST["tag"]))
18   $tags = htmlentities($_REQUEST["tag"]);
19 else
20   $tags = "";
21
22 ?>
23 <html>
24 <title><?php echo htmlspecialchars($title) ?></title>
25 <script src="<?php echo $webbase?>/d3.min.js"></script>
26 <link rel="stylesheet" type="text/css" href="<?php echo $webbase?>/normalize.css" />
27 <link rel="stylesheet" type="text/css" href="<?php echo $webbase?>/style.css" />
28
29 <body>
30
31 <div class="debug">test</div>
32 <h1><?php echo htmlspecialchars($title) ?></h1>
33
34 <button class="prev" disabled="disabled" onclick="left()"> prev </button>
35 <button class="next"   onclick="right()">next </button>
36
37 <div class="permalink"></div>
38
39 <div class="tagsearch">
40 <form method="get" action="">
41  Tags: <input list="MyTags" id="MyTagsInput" type="text" value="" />
42   <datalist id="MyTags">
43   </datalist>
44 </form>
45 </div>
46
47 <div class="index"></div>
48 <div class="pics"> </div>
49
50 <footer>
51   This gallery belongs to <?php echo htmlspecialchars($admin) ?>.
52   <div class="copyright"> code: copyright 2011 Arun Persaud arun@nubati.net, code available at nubati.net/git/f-spot-gallery</div>
53 </footer>
54
55
56 <script type="text/javascript" >
57
58 var pics = d3.select(".pics").append("ul");
59
60 var page=<?php echo $page ?>;
61 var N=<?php echo $N ?>;
62 var T="<?php echo $tags ?>";
63 var count=0;
64
65 /* populate data list with tags*/
66 d3.json("<?php echo $webbase?>/getjson.php?S", function(json) {
67     d3.select("#MyTags").selectAll("option").data(json[1])
68       .enter().append("option").attr("value",function(d) {return d.name});
69   });
70
71 /* update form to point to new link */
72 d3.select("input").on("keyup", function(d) {
73     d3.select('form').attr("action","<?php echo $webbase?>/tag/"+document.getElementById('MyTagsInput').value.replace(" ","+"));
74 });
75
76 function myreload(a) {
77   d3.select(".debug").text("T,P,N ="+T+" "+a+" "+N);
78
79   if(T!="")
80     url = "<?php echo $webbase?>/getjson.php?T="+T+"&P="+a;
81   else
82     url = "<?php echo $webbase?>/getjson.php?P="+a;
83
84   d3.json(url, function(json) {
85
86       /* update index, show only page +-5 pages max */
87       n = Math.floor(json[0][0].total/N);
88       s = "";
89
90       if(n>1)
91         {
92           s="page ";
93
94           if(a>7)
95             {
96               s+=" <a href=\"<?php echo $webbase?>";
97               if(T!="")
98                 s+="/tag/"+T;
99               s+="/page/1\">1</a>...";
100               start = a-5;
101             }
102           else
103             start=1;
104
105           for(i=start;i<=Math.min(n+1,a+5);i++)
106             {
107               if(i==a)
108                 s+= " "+i+" ";
109               else
110                 {
111                   s+=" <a href=\"<?php echo $webbase?>";
112                   if(T!="")
113                     s+="/tag/"+T;
114                   s+="/page/"+i+"\">"+i+"</a>";
115                 }
116             }
117
118           if(a+5<n)
119             {
120               s+="... <a href=\"<?php echo $webbase?>";
121               if(T!="")
122                 s+="/tag/"+T;
123               s+="/page/"+(n+1)+"\">"+(n+1)+"</a>";
124             }
125           else if(a+5==n)
126             {
127               s+=" <a href=\"<?php echo $webbase?>";
128               if(T!="")
129                 s+="/tag/"+T;
130               s+="/page/"+(n+1)+"\">"+(n+1)+"</a>";
131             };
132         };
133       d3.select(".index").html(s);
134
135       /* update pics */
136       count=0;
137       pics.selectAll("li").remove();
138       picdata=json[1];
139       pics.selectAll("li").data(picdata)
140         .enter().append("li")
141         .append("a")
142         .attr("href",function(d) {
143             s= d.base_uri+'/'+d.filename;
144             s = s.replace('file:\/\/<?php echo "".str_replace("/","\/",$dbprefix); ?>','<?php echo $webbase; ?>/Photos-small/');
145             return s;
146           })
147         .append("img")
148         .attr("src",function(d) {
149             count++;
150             s= d.base_uri+'/'+d.filename;
151             s = s.replace('file:\/\/<?php echo "".str_replace("/","\/",$dbprefix); ?>','<?php echo $webbase?>/Photos-tiny/');
152             return s;
153           });
154       checkbutton();
155     });
156
157   permalink="<?php echo $webbase ?>/page/"+page;
158   d3.select(".permalink").html("Permalink: <a href=\""+permalink+"\">"+permalink+"</a>");
159 }
160
161 function left() {
162   if (page>=2) page=page-1;
163   myreload(page);
164 }
165
166 function right() {
167   page=page+1;
168   myreload(page);
169 }
170
171 function checkbutton() {
172
173   if (page==1)
174     { d3.select("button.prev").attr("disabled","disabled");}
175   else
176     { d3.select("button.prev").attr("disabled", null);};
177
178   if (count<N)
179     { d3.select("button.next").attr("disabled","disabled");}
180   else
181     { d3.select("button.next").attr("disabled",null);}
182 }
183
184 myreload(page);
185
186 </script>
187
188 </body>
189 </html>