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