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