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