added a tag cloud
[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">test</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="pics"> </div>
62
63 <footer>
64   This gallery belongs to <?php echo htmlspecialchars($admin) ?>.
65   <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>
66 </footer>
67
68
69 <script type="text/javascript" >
70
71 var pics = d3.select(".pics").append("ul");
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 /* populate data list with tags*/
80 d3.json("<?php echo $webbase?>/getjson.php?S", function(json) {
81     d3.select("#MyTags").selectAll("option").data(json[1])
82       .enter().append("option").attr("value",function(d) {return d.name});
83   });
84
85 /* update form to point to new link */
86 d3.select("input").on("keyup", function(d) {
87     d3.select('form').attr("action","<?php echo $webbase?>/tag/"+document.getElementById('MyTagsInput').value.replace(" ","+"));
88 });
89
90 if (T!="")
91   {
92     var mycurrenttags = T.split(",");
93
94     d3.select("#currenttags").select("button").remove();
95     d3.select("#currenttags").selectAll("button")
96       .data(mycurrenttags).enter()
97       .append("button").attr("type","button").text( function(d) {return d;} );
98   }
99  else
100   {
101     d3.select("#currenttags").select("button").remove();
102     d3.select("#currenttags").append("span").text( ' none');
103   };
104
105 function load_content(a) {
106   //  d3.select(".debug").text("T,P,N = *"+T+"* *"+a+"* *"+N+"*");
107
108   update_page_index(a);
109
110   if (ID>=0)
111     url = "<?php echo $webbase?>/getjson.php?ID="+ID;
112   else if(T!="")
113     url = "<?php echo $webbase?>/getjson.php?T="+T+"&P="+a;
114   else
115     url = "<?php echo $webbase?>/getjson.php?P="+a;
116
117   /* update pics */
118   d3.json(url, function(json) {
119       count=0;
120       pics.selectAll("li").remove();
121       picdata=json;
122
123       /* if ID is set, just show one pictures, else create an array of pictures */
124       if (ID>=0)
125         {
126           pics.selectAll("li").data(picdata)
127             .enter().append("li")
128             .append("img")
129             .attr("class","large")
130             .attr("src",function(d) {
131                 s= d.base_uri+'/'+d.filename;
132                 s = s.replace('file:\/\/<?php echo "".str_replace("/","\/",$dbprefix); ?>','<?php echo $webbase?>/Photos-small/');
133                 return s;
134               });
135         }
136       else
137         {
138           pics.selectAll("li").data(picdata)
139             .enter().append("li")
140             .append("a")
141             .attr("href",function(d) {
142                 s = '<?php echo $webbase; ?>';
143                 if(T!="")
144                   s = s + '/tag/' + T;
145                 if(a!=1)
146                   s = s + '/page/' + a;
147                 s = s + '/pic/' + d.id;
148                 return s;
149               })
150             .append("img")
151             .attr("src",function(d) {
152                 count++;
153                 s= d.base_uri+'/'+d.filename;
154                 s = s.replace('file:\/\/<?php echo "".str_replace("/","\/",$dbprefix); ?>','<?php echo $webbase?>/Photos-tiny/');
155                 return s;
156               });
157         };
158
159      checkbutton();
160     });
161
162   /* update permalink */
163
164   permalink="<?php echo $webbase ?>";
165   if(T!="")
166     permalink += '/tag/' + T;
167   if(a!=1)
168     permalink += '/page/' + a;
169
170
171   d3.select(".permalink").html("Permalink: <a href=\""+permalink+"\">"+permalink+"</a>");
172 }
173
174 function left() {
175   if (page>=2) page=page-1;
176   load_content(page);
177 }
178
179 function right() {
180   page=page+1;
181   load_content(page);
182 }
183
184 function cloud() {
185
186   url = "<?php echo $webbase?>/getjson.php?CLOUD=1";
187
188   pics.selectAll("li").remove();
189
190   var svgelement=pics.append("li")
191     .append("svg").attr("width",400).attr("height",400);
192
193   /* update pics */
194   d3.json(url, function(json) {
195       svgelement.selectAll("text").data(json).enter().append("text")
196         .style("font-size", function(d){return Math.log(d.count+1)+"em"})
197         .text(function(d) { return d.name; })
198         .on("mouseover", function(d){ d3.select(this).style("color","red")} )
199         .on("mouseout", function(d){ d3.select(this).style("color","white")} )
200         .on("click", function(d) { document.location.href='<?php echo $webbase?>/tag/'+d.name })
201     });
202 }
203
204 function checkbutton()
205 {
206
207   if (page==1)
208     { d3.select("button.prev").attr("disabled","disabled");}
209   else
210     { d3.select("button.prev").attr("disabled", null);};
211
212   if (count<N)
213     { d3.select("button.next").attr("disabled","disabled");}
214   else
215     { d3.select("button.next").attr("disabled",null);}
216 }
217
218 function update_page_index(mypage)
219 {
220   /* load number of pictures */
221
222   if(T!="")
223     url = "<?php echo $webbase?>/getjson.php?C=1&T="+T;
224   else
225     url = "<?php echo $webbase?>/getjson.php?C=1";
226
227   d3.json(url, function(json) {
228     /* update index, show only page +-5 pages max */
229     n = Math.floor(json[0].total/N);
230     s = "";
231
232     if(n>0)
233       {
234         s="page ";
235
236         if(mypage>7)
237           {
238             s+=" <a href=\"<?php echo $webbase?>";
239             if(T!="")
240               s+="/tag/"+T;
241             s+="/page/1\">1</a>...";
242             start = mypage-5;
243           }
244         else
245           start=1;
246
247         for(i=start;i<=Math.min(n+1,mypage+5);i++)
248           {
249             if(i==mypage)
250               s+= " "+i+" ";
251             else
252               {
253                 s+=" <a href=\"<?php echo $webbase?>";
254                 if(T!="")
255                   s+="/tag/"+T;
256                 s+="/page/"+i+"\">"+i+"</a>";
257               }
258           }
259
260         if(mypage+5<n)
261           {
262             s+="... <a href=\"<?php echo $webbase?>";
263             if(T!="")
264               s+="/tag/"+T;
265             s+="/page/"+(n+1)+"\">"+(n+1)+"</a>";
266           }
267         else if(mypage+5==n)
268           {
269             s+=" <a href=\"<?php echo $webbase?>";
270             if(T!="")
271               s+="/tag/"+T;
272             s+="/page/"+(n+1)+"\">"+(n+1)+"</a>";
273           };
274       };
275     d3.select(".index").html(s);
276     } );
277 }
278
279 load_content(page);
280
281 </script>
282
283 </body>
284 </html>