add prev/next pictures for easier slide show mode
[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 if(isset($_REQUEST["pic"]))
23   $pic = intval(htmlentities($_REQUEST["pic"]));
24 else
25   $pic = -1;
26 /* end parse flags */
27
28 /* The basic layout */
29 ?>
30
31 <html>
32 <title><?php echo htmlspecialchars($title) ?></title>
33 <script src="<?php echo $webbase?>/d3.min.js"></script>
34 <link rel="stylesheet" type="text/css" href="<?php echo $webbase?>/normalize.css" />
35 <link rel="stylesheet" type="text/css" href="<?php echo $webbase?>/style.css" />
36
37 <body>
38
39 <div class="debug">test</div>
40 <h1><?php echo htmlspecialchars($title) ?></h1>
41
42 <nav>
43 <span class="index"></span>
44 <button class="prev" type="button" disabled="disabled" onclick="left()"> prev </button>
45 <button class="next" type="button" onclick="right()">next </button>
46 <button class="all"  type="submit" onclick="document.location.href='<?php echo $webbase?>'">all</button>
47 </nav>
48
49 <div class="permalink"></div>
50
51 <div class="tagsearch">
52 <form method="get" action="">
53  Add tag: <input list="MyTags" id="MyTagsInput" type="text" value="" />
54   <datalist id="MyTags">
55   </datalist>
56 </form>
57   Current tags:<span id="currenttags"></span>
58   <button class="next" type="button" onclick="cloud()">tag cloud</button>
59 </div>
60
61 <div class="nextprev"> <ul></ul></div>
62
63 <div class="pics"><ul></ul> </div>
64
65 <footer>
66   This gallery belongs to <?php echo htmlspecialchars($admin) ?>.
67   <div class="copyright"> photo-tags: copyright 2011 Arun Persaud arun@nubati.net, code available at <a href="http://source.nubati.net/projects/photo-tags">source.nubati.net/projects/photo-tags</a></div>
68 </footer>
69
70
71 <script type="text/javascript" >
72
73 var pics = d3.select(".pics").select("ul");
74
75 var page=<?php echo $page ?>;
76 var N=<?php echo $N ?>;
77 var T="<?php echo $tags ?>";
78 var ID=<?php echo $pic ?>;
79 var count=0;
80
81 /* populate data list with tags*/
82 d3.json("<?php echo $webbase?>/getjson.php?S", function(json) {
83     d3.select("#MyTags").selectAll("option").data(json)
84       .enter().append("option").attr("value",function(d) {return d.name});
85   });
86
87 /* update form to point to new link */
88 d3.select("input").on("keyup", function(d) {
89     d3.select('form').attr("action","<?php echo $webbase?>/tag/"+document.getElementById('MyTagsInput').value.replace(" ","+"));
90 });
91
92 if (T!="")
93   {
94     var mycurrenttags = T.split(",");
95
96     d3.select("#currenttags").select("button").remove();
97     d3.select("#currenttags").selectAll("button")
98       .data(mycurrenttags).enter()
99       .append("button").attr("type","button").text( function(d) {return d;} );
100   }
101  else
102   {
103     d3.select("#currenttags").select("button").remove();
104     d3.select("#currenttags").append("span").text( ' none');
105   };
106
107 function load_content(a) {
108   //  d3.select(".debug").text("T,P,N = *"+T+"* *"+a+"* *"+N+"*");
109
110   update_page_index(a);
111
112   if (ID>=0)
113     url = "<?php echo $webbase?>/getjson.php?ID="+ID;
114   else if(T!="")
115     url = "<?php echo $webbase?>/getjson.php?T="+T+"&P="+a;
116   else
117     url = "<?php echo $webbase?>/getjson.php?P="+a;
118
119   /* update pics */
120   d3.json(url, function(json) {
121       count=0;
122       pics.selectAll("li").remove();
123       picdata=json;
124
125       /* if ID is set, just show one pictures, else create an array of pictures */
126       if (ID>=0)
127         {
128           pics.selectAll("li").data(picdata)
129             .enter().append("li")
130             .append("img")
131             .attr("class","large")
132             .attr("src",function(d) {
133                 s= d.base_uri+'/'+d.filename;
134                 s = s.replace('file:\/\/<?php echo "".str_replace("/","\/",$dbprefix); ?>','<?php echo $webbase?>/Photos-small/');
135                 return s;
136               });
137
138           /* update thumbnails */
139           if(T!="")
140             url2 = "<?php echo $webbase?>/getjson.php?NP=1&T="+T+"&ID="+ID;
141           else
142             url2 = "<?php echo $webbase?>/getjson.php?NP=1&ID="+ID;
143
144           d3.json(url2, function(json2) {
145               var thumbs= d3.select(".nextprev").select("ul").selectAll("li").data(json2);
146               thumbs.enter().append("li")
147                 .append("a")
148                 .attr("href",function(d) {
149                     s = '<?php echo $webbase; ?>';
150                     if(T!="")
151                       s = s + '/tag/' + T;
152                     s = s + '/pic/' + d.id;
153                     return s;
154                   })
155                 .append("img")
156                 .attr("src",function(d) {
157                     s= d.base_uri+'/'+d.filename;
158                     s = s.replace('file:\/\/<?php echo "".str_replace("/","\/",$dbprefix); ?>','<?php echo $webbase?>/Photos-tiny/');
159                     return s;
160                   });
161
162               thumbs.exit().remove();
163
164
165             });
166         }
167       else
168         {
169           d3.select(".nextprev").select("ul").selectAll("li").remove();
170           pics.selectAll("li").data(picdata)
171             .enter().append("li")
172             .append("a")
173             .attr("href",function(d) {
174                 s = '<?php echo $webbase; ?>';
175                 if(T!="")
176                   s = s + '/tag/' + T;
177                 if(a!=1)
178                   s = s + '/page/' + a;
179                 s = s + '/pic/' + d.id;
180                 return s;
181               })
182             .append("img")
183             .attr("src",function(d) {
184                 count++;
185                 s= d.base_uri+'/'+d.filename;
186                 s = s.replace('file:\/\/<?php echo "".str_replace("/","\/",$dbprefix); ?>','<?php echo $webbase?>/Photos-tiny/');
187                 return s;
188               });
189         };
190
191      checkbutton();
192     });
193
194   /* update permalink */
195
196   permalink="<?php echo $webbase ?>";
197   if(T!="")
198     permalink += '/tag/' + T;
199   if(a!=1)
200     permalink += '/page/' + a;
201
202
203   d3.select(".permalink").html("Permalink: <a href=\""+permalink+"\">"+permalink+"</a>");
204 }
205
206 function left() {
207   if (page>=2) page=page-1;
208   load_content(page);
209 }
210
211 function right() {
212   page=page+1;
213   load_content(page);
214 }
215
216 function cloud() {
217
218   url = "<?php echo $webbase?>/getjson.php?CLOUD=1";
219
220   pics.selectAll("li").remove();
221
222   var svgelement=pics.append("li")
223     .append("svg").attr("width",400).attr("height",400);
224
225   /* update pics */
226   d3.json(url, function(json) {
227       svgelement.selectAll("text").data(json).enter().append("text")
228         .style("font-size", function(d){return (Math.log(d.count+1)/2.0)+"em"})
229         .text(function(d) { return d.name+" "; })
230         .on("mouseover", function(d){ d3.select(this).style("color","red")} )
231         .on("mouseout", function(d){ d3.select(this).style("color","white")} )
232         .on("click", function(d) { document.location.href='<?php echo $webbase?>/tag/'+d.name })
233     });
234 }
235
236 function checkbutton()
237 {
238
239   if (page==1)
240     { d3.select("button.prev").attr("disabled","disabled");}
241   else
242     { d3.select("button.prev").attr("disabled", null);};
243
244   if (count<N)
245     { d3.select("button.next").attr("disabled","disabled");}
246   else
247     { d3.select("button.next").attr("disabled",null);}
248 }
249
250 function update_page_index(mypage)
251 {
252   /* load number of pictures */
253
254   if(T!="")
255     url = "<?php echo $webbase?>/getjson.php?C=1&T="+T;
256   else
257     url = "<?php echo $webbase?>/getjson.php?C=1";
258
259   d3.json(url, function(json) {
260     /* update index, show only page +-5 pages max */
261     n = Math.floor(json[0].total/N);
262     s = "";
263
264     if(n>0)
265       {
266         s="page ";
267
268         if(mypage>7)
269           {
270             s+=" <a href=\"<?php echo $webbase?>";
271             if(T!="")
272               s+="/tag/"+T;
273             s+="/page/1\">1</a>...";
274             start = mypage-5;
275           }
276         else
277           start=1;
278
279         for(i=start;i<=Math.min(n+1,mypage+5);i++)
280           {
281             if(i==mypage)
282               s+= " "+i+" ";
283             else
284               {
285                 s+=" <a href=\"<?php echo $webbase?>";
286                 if(T!="")
287                   s+="/tag/"+T;
288                 s+="/page/"+i+"\">"+i+"</a>";
289               }
290           }
291
292         if(mypage+5<n)
293           {
294             s+="... <a href=\"<?php echo $webbase?>";
295             if(T!="")
296               s+="/tag/"+T;
297             s+="/page/"+(n+1)+"\">"+(n+1)+"</a>";
298           }
299         else if(mypage+5==n)
300           {
301             s+=" <a href=\"<?php echo $webbase?>";
302             if(T!="")
303               s+="/tag/"+T;
304             s+="/page/"+(n+1)+"\">"+(n+1)+"</a>";
305           };
306       };
307     d3.select(".index").html(s);
308     } );
309 }
310
311 load_content(page);
312
313 </script>
314
315 </body>
316 </html>