d178a41fec0b5530e309cf1aecafba45131d357c
[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   permalink="<?php echo $webbase ?>";
225   if(T!="")
226     permalink += '/tag/' + T;
227   if(a!=1)
228     permalink += '/page/' + a;
229
230
231   d3.select(".permalink").html("Permalink: <a href=\""+permalink+"\">"+permalink+"</a>");
232 }
233
234 function left() {
235   if (page>=2) page=page-1;
236   load_content(page);
237 }
238
239 function right() {
240   page=page+1;
241   load_content(page);
242 }
243
244 function cloud() {
245
246   url = "<?php echo $webbase?>/getjson.php?CLOUD=1";
247
248   pics.selectAll("li").remove();
249
250   var svgelement=pics.append("li")
251     .append("svg").attr("width",400).attr("height",400);
252
253   /* update pics */
254   d3.json(url, function(json) {
255       svgelement.selectAll("text").data(json).enter().append("text")
256         .style("font-size", function(d){return (Math.log(d.count+1)/2.0)+"em"})
257         .text(function(d) { return d.name+" "; })
258         .on("mouseover", function(d){ d3.select(this).style("color","red")} )
259         .on("mouseout", function(d){ d3.select(this).style("color","white")} )
260         .on("click", function(d) { document.location.href='<?php echo $webbase?>/tag/'+d.name })
261     });
262 }
263
264 function checkbutton()
265 {
266
267   if (page==1)
268     { d3.select("button.prev").attr("disabled","disabled");}
269   else
270     { d3.select("button.prev").attr("disabled", null);};
271
272   if (count<N)
273     { d3.select("button.next").attr("disabled","disabled");}
274   else
275     { d3.select("button.next").attr("disabled",null);}
276 }
277
278 function update_page_index(mypage)
279 {
280   /* load number of pictures */
281   
282   myID = "";
283   if(ID > 0)
284     myID = "&ID="+ID;
285
286   if(T!="")
287     url = "<?php echo $webbase?>/getjson.php?C=1&T="+T+myID;
288   else
289     url = "<?php echo $webbase?>/getjson.php?C=1"+myID;
290
291   d3.json(url, function(json) {
292     /* update index, show only page +-5 pages max */
293     n = Math.floor(json[0].total/N);
294     s = "";
295
296     if(n>0)
297       {
298         s="page ";
299
300         if(mypage>7)
301           {
302             s+=" <a href=\"<?php echo $webbase?>";
303             if(T!="")
304               s+="/tag/"+T;
305             s+="/page/1\">1</a>...";
306             start = mypage-5;
307           }
308         else
309           start=1;
310
311         for(i=start;i<=Math.min(n+1,mypage+5);i++)
312           {
313             if(i==mypage)
314               s+= " "+i+" ";
315             else
316               {
317                 s+=" <a href=\"<?php echo $webbase?>";
318                 if(T!="")
319                   s+="/tag/"+T;
320                 s+="/page/"+i+"\">"+i+"</a>";
321               }
322           }
323
324         if(mypage+5<n)
325           {
326             s+="... <a href=\"<?php echo $webbase?>";
327             if(T!="")
328               s+="/tag/"+T;
329             s+="/page/"+(n+1)+"\">"+(n+1)+"</a>";
330           }
331         else if(mypage+5==n)
332           {
333             s+=" <a href=\"<?php echo $webbase?>";
334             if(T!="")
335               s+="/tag/"+T;
336             s+="/page/"+(n+1)+"\">"+(n+1)+"</a>";
337           };
338       };
339     d3.select(".index").html(s);
340     } );
341 }
342
343 load_content(page);
344
345 </script>
346
347 </body>
348 </html>