359811bbba79880639d9cc19f25d6dffaced9e67
[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"></div>
40 <h1><?php echo htmlspecialchars($title) ?></h1>
41
42 <nav>
43 page <span class="index"></span>
44 <button class="prev" type="button" disabled="disabled" onclick="prev_page()"> prev </button>
45 <button class="next" type="button" onclick="next_page()">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  Search for 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="tagcloud()">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 page=<?php echo $page ?>;
74 var N=<?php echo $N ?>;
75 var T="<?php echo $tags ?>";
76 var ID=<?php echo $pic ?>;
77 var count=0;
78
79 var webbase = "<?php echo $webbase?>";
80 var pics = d3.select(".pics").select("ul");
81
82
83 /* populate data list with tags*/
84 d3.json(webbase+"/getjson.php?S", function(json) {
85     d3.select("#MyTags").selectAll("option").data(json)
86       .enter().append("option").attr("value",function(d) {return d.name});
87   });
88
89 /* update form to point to new link */
90 d3.select("input").on("keyup", function(d) {
91     d3.select('form').attr("action",webbase+"/tag/"+document.getElementById('MyTagsInput').value.replace(" ","+"));
92 });
93
94 if (T!="")
95   {
96     var mycurrenttags = T.split(",");
97
98     d3.select("#currenttags").select("button").remove();
99     d3.select("#currenttags").selectAll("button")
100       .data(mycurrenttags).enter()
101       .append("button").attr("type","button").text( function(d) {return d;} );
102   }
103  else
104   {
105     d3.select("#currenttags").select("button").remove();
106     d3.select("#currenttags").append("span").text( ' none');
107   };
108
109 function load_content() {
110   //  d3.select(".debug").text("T,P,N = *"+T+"* *"+page+"* *"+N+"*");
111
112   if (ID>=0)
113     url = webbase+"/getjson.php?ID="+ID;
114   else if(T!="")
115     url = webbase+"/getjson.php?T="+T+"&P="+page;
116   else
117     url = webbase+"/getjson.php?P="+page;
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           var singlepicspace=pics.selectAll("li").data(picdata, function(d){return ID;}).enter().append("li").append("div").attr("class","singlepic");
129           singlepicspace.append("div").attr("class","left").append("img").attr("src",webbase+"/left.png");
130           singlepicspace.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); ?>',webbase+'/Photos-small/');
135                 return s;
136               });
137           singlepicspace.append("div").attr("class","right").append("img").attr("src",webbase+"/right.png");
138
139           update_thumbnails();
140         }
141       else
142         {
143           d3.select(".nextprev").select("ul").selectAll("li").remove();
144           pics.selectAll("li").data(picdata)
145             .enter().append("li")
146             .append("a")
147             .on("click", function(d) { load_pic(d.id); })
148             .append("img")
149             .attr("src",function(d) {
150                 count++;
151                 s= d.base_uri+'/'+d.filename;
152                 s = s.replace('file:\/\/<?php echo "".str_replace("/","\/",$dbprefix); ?>',webbase+'/Photos-tiny/');
153                 return s;
154               });
155         };
156
157      checkbutton();
158     });
159
160    update_permalink()
161 }
162
163 function update_permalink() {
164   /* update permalink */
165
166   permalink = webbase;
167
168   if(T!="")
169     permalink += '/tag/' + T;
170   if(page!=1)
171     permalink += '/page/' + page;
172   if(ID>0)
173     permalink += '/pic/' + ID;
174
175   d3.select(".permalink").html("Permalink: <a href=\""+permalink+"\">"+permalink+"</a>");
176 }
177
178 function prev_page() {
179   if (page>=2) page=page-1;
180   load_content();
181 }
182
183 function next_page() {
184   page=page+1;
185   load_content();
186 }
187
188 function prev_pic() {
189 }
190
191 function next_pic() {
192 }
193
194 function load_pic(myid) {
195   ID=myid;
196   update_page_index();
197   update_thumbnails();
198   load_content();
199 }
200
201 function tagcloud() {
202
203   url = webbase+"/getjson.php?CLOUD=1";
204
205   pics.selectAll("li").remove();
206
207   var svgelement=pics.append("li")
208     .append("svg").attr("width",400).attr("height",400);
209
210   /* update pics */
211   d3.json(url, function(json) {
212       svgelement.selectAll("text").data(json).enter().append("text")
213         .style("font-size", function(d){return (Math.log(d.count+1)/2.0)+"em"})
214         .text(function(d) { return d.name+" "; })
215         .on("mouseover", function(d){ d3.select(this).style("color","red")} )
216         .on("mouseout", function(d){ d3.select(this).style("color","white")} )
217         .on("click", function(d) { document.location.href=webbase+'/tag/'+d.name })
218     });
219 }
220
221 function update_thumbnails(){
222   if(T!="")
223     url2 = webbase+"/getjson.php?NP=1&T="+T+"&ID="+ID;
224   else
225     url2 = webbase+"/getjson.php?NP=1&ID="+ID;
226
227   var IDprev=-1;
228   var IDnext=-1;
229   var IDcurr=-1;
230   d3.json(url2, function(json2) {
231       /* figure out where the arrows on the pic should link to */
232       all=""
233       for (var i in json2){
234         if( IDcurr != ID )
235           {
236             IDprev = IDcurr;
237             IDcurr = IDnext;
238             IDnext = json2[i].id;
239           };
240       }
241
242       var thumbs= d3.select(".nextprev").select("ul").selectAll("li").data(json2, function(d) {return d.id;});
243
244       thumbs.enter().append("li")
245         .append("a")
246         .on("click", function(d) {
247             load_pic(d.id); }
248           )
249         .append("img")
250         .attr("src",function(d) {
251             s= d.base_uri+'/'+d.filename;
252             s = s.replace('file:\/\/<?php echo "".str_replace("/","\/",$dbprefix); ?>',webbase+'/Photos-tiny/');
253             return s;
254           })
255         .style("height","0")
256         .transition().duration(1000)
257         .style("height","100px");
258
259       thumbs.exit().select("img").transition().duration(1000).style("height","0");
260       thumbs.exit().transition().duration(1050).remove();
261
262       /* resort elements */
263       d3.select(".nextprev").select("ul").selectAll("li").sort(function(a,b){return a.id-b.id;});
264       d3.select(".nextprev").select("ul").selectAll("li").select("a").select("img").classed("current",false);
265       d3.select(".nextprev").select("ul").selectAll("li").select("a").select("img").classed("current",function(d){return (d.id==IDcurr);});
266
267
268       /* add links for left/right arrows */
269       if (IDprev != -1 )
270         d3.select(".left").on("click", function() { load_pic(IDprev); });
271       if (IDnext != -1 )
272         d3.select(".right").on("click", function() { load_pic(IDnext); });
273
274     });
275 }
276
277 function checkbutton()
278 {
279   if (page==1)
280     { d3.select("button.prev").attr("disabled","disabled");}
281   else
282     { d3.select("button.prev").attr("disabled", null);};
283
284   if (count<N)
285     { d3.select("button.next").attr("disabled","disabled");}
286   else
287     { d3.select("button.next").attr("disabled",null);}
288 }
289
290 function update_page_index()
291 {
292   /* load number of pictures */
293   myID = "";
294   if(ID > 0)
295     myID = "&ID="+ID;
296
297   if(T!="")
298     url = webbase+"/getjson.php?C=1&T="+T+myID;
299   else
300     url = webbase+"/getjson.php?C=1"+myID;
301
302   d3.json(url, function(json) {
303     /* update index, show only page +-5 pages max */
304     n = Math.floor(json[0].total/N);
305     nr = Math.floor( (json[0].row-1)/N); /* rowid starts at 1 not 0 */
306
307     if(nr > 0)
308       page = nr+1;
309
310     var mydata = new Array();  // add json data  {page: <nr>, name: <name>} ; at end reform array into real json and use d3 to parse it
311
312     if(n>0)
313       {
314         if(page>7)
315           {
316             mydata.push('{ page:1, name:"1"}');
317             mydata.push('{ page:1.5, name:"..."}');
318             start = page-5;
319           }
320         else
321           start=1;
322
323         for(i=start;i<=Math.min(n+1,page+5);i++)
324           mydata.push('{ page:'+i+', name:"'+i+'"}');
325
326         if(page+5<n)
327           {
328             mydata.push('{ page:'+(n+0.5)+', name:"..."}');
329             mydata.push('{ page:'+(n+1)+', name:"'+(n+1)+'"}');
330           }
331         else if(page+5==n)
332           mydata.push('{ page:'+(n+1)+', name:"'+(n+1)+'"}');
333       };
334     mydata = "["+mydata.join(",")+"]";
335     mydata =  eval('(' + mydata + ')');
336
337     var pageindex = d3.select(".index").selectAll("button").data(mydata, function(d){ return d.page; });
338     pageindex.exit().remove();
339     pageindex.enter().append("button")
340       .on("click", function(d) { if(  (d.page - Math.floor(d.page)) ==0 )  {page=d.page; ID=-1;load_content(); update_page_index();} })
341       .text(function(d) {return " "+d.name+" "});
342     pageindex.sort( function(a,b) { return a.page- b.page;} );
343     } );
344
345   d3.select(".index").selectAll("button").classed("currentpage",false);
346   d3.select(".index").selectAll("button").classed("currentpage",function(d){return (d.page==page);});
347 }
348
349 load_content();
350 update_page_index();
351
352 </script>
353
354 </body>
355 </html>