7237d283507d865fec76318b500ac7fa54125d29
[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 <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  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="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 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 var webbase = "<?php echo $webbase?>";
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(a) {
110   //  d3.select(".debug").text("T,P,N = *"+T+"* *"+a+"* *"+N+"*");
111
112   update_page_index(a);
113
114   if (ID>=0)
115     url = webbase+"/getjson.php?ID="+ID;
116   else if(T!="")
117     url = webbase+"/getjson.php?T="+T+"&P="+a;
118   else
119     url = webbase+"/getjson.php?P="+a;
120
121   /* update pics */
122   d3.json(url, function(json) {
123       count=0;
124       pics.selectAll("li").remove();
125       picdata=json;
126
127       /* if ID is set, just show one pictures, else create an array of pictures */
128       if (ID>=0)
129         {
130           var singlepicspace=pics.selectAll("li").data(picdata).enter().append("li").append("div").attr("class","singlepic");
131           singlepicspace.append("div").attr("class","left").append("img").attr("src",webbase+"/left.png");
132           singlepicspace.append("img")
133             .attr("class","large")
134             .attr("src",function(d) {
135                 s= d.base_uri+'/'+d.filename;
136                 s = s.replace('file:\/\/<?php echo "".str_replace("/","\/",$dbprefix); ?>',webbase+'/Photos-small/');
137                 return s;
138               });
139           singlepicspace.append("div").attr("class","right").append("img").attr("src",webbase+"/right.png");
140
141           /* update thumbnails */
142           if(T!="")
143             url2 = webbase+"/getjson.php?NP=1&T="+T+"&ID="+ID;
144           else
145             url2 = webbase+"/getjson.php?NP=1&ID="+ID;
146
147           var IDprev=-1;
148           var IDnext=-1;
149           var IDcurr=-1;
150           d3.json(url2, function(json2) {
151               var thumbs= d3.select(".nextprev").select("ul").selectAll("li").data(json2);
152               thumbs.enter().append("li")
153                 .append("a")
154                 .attr("href",function(d) {
155                     s = webbase;
156                     if(T!="")
157                       s = s + '/tag/' + T;
158                     s = s + '/pic/' + d.id;
159
160                     if( IDcurr != ID )
161                       {
162                         IDprev = IDcurr;
163                         IDcurr = IDnext;
164                         IDnext = d.id;
165                       }
166                     
167                     return s;
168                   })
169                 .append("img")
170                 .attr("src",function(d) {
171                     s= d.base_uri+'/'+d.filename;
172                     s = s.replace('file:\/\/<?php echo "".str_replace("/","\/",$dbprefix); ?>',webbase+'/Photos-tiny/');
173                     return s;
174                   });
175
176               thumbs.exit().remove();
177
178               if (IDprev != -1 )
179                 {
180                   s = webbase;
181                   if(T!="")
182                     s = s + '/tag/' + T;
183                   s = s + '/pic/' + IDprev;
184                   d3.select(".left").on("click", function(d) { document.location.href=s })
185                 }
186               if (IDnext != -1 )
187                 {
188                   s = webbase;
189                   if(T!="")
190                     s = s + '/tag/' + T;
191                   s = s + '/pic/' + IDnext;
192                   d3.select(".right").on("click", function(d) { document.location.href=s })
193                 }
194
195             });
196         }
197       else
198         {
199           d3.select(".nextprev").select("ul").selectAll("li").remove();
200           pics.selectAll("li").data(picdata)
201             .enter().append("li")
202             .append("a")
203             .attr("href",function(d) {
204                 s = webbase;
205                 if(T!="")
206                   s = s + '/tag/' + T;
207                 if(a!=1)
208                   s = s + '/page/' + a;
209                 s = s + '/pic/' + d.id;
210                 return s;
211               })
212             .append("img")
213             .attr("src",function(d) {
214                 count++;
215                 s= d.base_uri+'/'+d.filename;
216                 s = s.replace('file:\/\/<?php echo "".str_replace("/","\/",$dbprefix); ?>',webbase+'/Photos-tiny/');
217                 return s;
218               });
219         };
220
221      checkbutton();
222     });
223
224    update_permalink(a)
225 }
226
227 function update_permalink(page) {
228   /* update permalink */
229
230   permalink = webbase;
231
232   if(T!="")
233     permalink += '/tag/' + T;
234   if(page!=1)
235     permalink += '/page/' + page;
236   if(ID>0)
237     permalink += '/pic/' + ID;
238
239   d3.select(".permalink").html("Permalink: <a href=\""+permalink+"\">"+permalink+"</a>");
240 }
241
242 function prev_page() {
243   if (page>=2) page=page-1;
244   load_content(page);
245 }
246
247 function next_page() {
248   page=page+1;
249   load_content(page);
250 }
251
252 function tagcloud() {
253
254   url = webbase+"/getjson.php?CLOUD=1";
255
256   pics.selectAll("li").remove();
257
258   var svgelement=pics.append("li")
259     .append("svg").attr("width",400).attr("height",400);
260
261   /* update pics */
262   d3.json(url, function(json) {
263       svgelement.selectAll("text").data(json).enter().append("text")
264         .style("font-size", function(d){return (Math.log(d.count+1)/2.0)+"em"})
265         .text(function(d) { return d.name+" "; })
266         .on("mouseover", function(d){ d3.select(this).style("color","red")} )
267         .on("mouseout", function(d){ d3.select(this).style("color","white")} )
268         .on("click", function(d) { document.location.href=webbase+'/tag/'+d.name })
269     });
270 }
271
272 function checkbutton()
273 {
274   if (page==1)
275     { d3.select("button.prev").attr("disabled","disabled");}
276   else
277     { d3.select("button.prev").attr("disabled", null);};
278
279   if (count<N)
280     { d3.select("button.next").attr("disabled","disabled");}
281   else
282     { d3.select("button.next").attr("disabled",null);}
283 }
284
285 function update_page_index(mypage)
286 {
287   /* load number of pictures */
288   myID = "";
289   if(ID > 0)
290     myID = "&ID="+ID;
291
292   if(T!="")
293     url = webbase+"/getjson.php?C=1&T="+T+myID;
294   else
295     url = webbase+"/getjson.php?C=1"+myID;
296
297   d3.json(url, function(json) {
298     /* update index, show only page +-5 pages max */
299     n = Math.floor(json[0].total/N);
300     nr = Math.floor( (json[0].row-1)/N); /* rowid starts at 1 not 0 */
301
302     if(nr > 0) mypage = nr+1;
303     s = "";
304
305     if(n>0)
306       {
307         s="page ";
308
309         if(mypage>7)
310           {
311             s+=" <a href=\""+webbase;
312             if(T!="")
313               s+="/tag/"+T;
314             s+="/page/1\">1</a>...";
315             start = mypage-5;
316           }
317         else
318           start=1;
319
320         for(i=start;i<=Math.min(n+1,mypage+5);i++)
321           {
322             if(i==mypage)
323               s+= " "+i+" ";
324             else
325               {
326                 s+=" <a href=\""+webbase;
327                 if(T!="")
328                   s+="/tag/"+T;
329                 s+="/page/"+i+"\">"+i+"</a>";
330               }
331           }
332
333         if(mypage+5<n)
334           {
335             s+="... <a href=\""+webbase;
336             if(T!="")
337               s+="/tag/"+T;
338             s+="/page/"+(n+1)+"\">"+(n+1)+"</a>";
339           }
340         else if(mypage+5==n)
341           {
342             s+=" <a href=\""+webbase;
343             if(T!="")
344               s+="/tag/"+T;
345             s+="/page/"+(n+1)+"\">"+(n+1)+"</a>";
346           };
347       };
348     d3.select(".index").html(s);
349     } );
350 }
351
352 load_content(page);
353
354 </script>
355
356 </body>
357 </html>