first implementation of "selected tag condition"
[phpfspot.git] / phpfspot.class.php
1 <?php
2
3 require_once "phpfspot_cfg.php";
4 require_once "phpfspot_db.php";
5 require_once "phpfspot_tmpl.php";
6
7 class PHPFSPOT {
8
9    var $cfg;
10    var $db;
11    var $cfg_db;
12    var $tmpl;
13    var $tags;
14    var $avail_tags;
15    var $current_tags;
16
17    public function __construct()
18    {
19       $this->cfg = new PHPFSPOT_CFG;
20
21       $this->db = new PHPFSPOT_DB(&$this, $this->cfg->fspot_db);
22
23       $this->cfg_db = new PHPFSPOT_DB(&$this, $this->cfg->phpfspot_db);
24       $this->check_config_table();
25
26       $this->tmpl = new PHPFSPOT_TMPL($this);
27
28       $this->get_tags();
29
30       session_start();
31
32       if(!isset($_SESSION['tag_condition']))
33          $_SESSION['tag_condition'] = 'or';
34
35    } // __construct()
36
37    public function __destruct()
38    {
39
40    } // __destruct()
41
42    public function show()
43    {
44       $this->tmpl->assign('page_title', $this->cfg->page_title);
45       $this->tmpl->assign('current_condition', $_SESSION['tag_condition']);
46       $this->tmpl->show("index.tpl");
47
48    } // show()
49
50    private function get_tags()
51    {
52       $this->avail_tags = Array();
53       $count = 0;
54    
55       $result = $this->db->db_query("
56          SELECT id,name
57          FROM tags
58          ORDER BY sort_priority ASC
59       ");
60       
61       while($row = $this->db->db_fetch_object($result)) {
62
63          $tag_id = $row['id'];
64          $tag_name = $row['name'];
65
66          /* check if config requests to ignore this tag */
67          if(in_array($row['name'], $this->cfg->hide_tags))
68             continue;
69
70          $this->tags[$tag_id] = $tag_name; 
71          $this->avail_tags[$count] = $tag_id;
72
73          $count++;
74
75       }
76
77    } // get_tags()
78
79    public function get_photo_details($idx)
80    {
81       $result = $this->db->db_query("
82          SELECT *
83          FROM photos
84          WHERE id='". $idx ."'
85       ");
86       
87       return $this->db->db_fetch_object($result);
88
89    } // get_photo_details
90
91    public function translate_path($path, $width = 0)
92    {  
93       return str_replace($this->cfg->path_replace_from, $this->cfg->path_replace_to, $path);
94
95    } // translate_path
96
97    public function showPhoto($photo)
98    {
99       $all_photos = $this->getAllTagPhotos();
100
101       foreach($all_photos as $all_photo) {
102          
103          if($get_next) {
104             $next_img = $all_photo;
105             break;
106          }
107
108          if($all_photo == $photo) {
109             $get_next = 1;
110          }
111          else {
112             $previous_img = $all_photo;
113          }
114       }
115
116
117       $details = $this->get_photo_details($photo);
118       $meta = $this->get_meta_informations($this->translate_path($details['directory_path']) ."/". $details['name']);
119       $info = getimagesize($this->translate_path($details['directory_path']) ."/thumbs/". $this->cfg->photo_width ."_". $details['name']);
120
121       $this->tmpl->assign('width', $info[0]);
122       $this->tmpl->assign('height', $info[1]);
123       $this->tmpl->assign('c_date', $meta['DateTime']);
124       $this->tmpl->assign('madewith', $meta['Make'] ." ". $meta['Model']);
125       $this->tmpl->assign('image_name', $details['name']);
126       $this->tmpl->assign('image_url', 'phpfspot_img.php?idx='. $photo ."&amp;width=". $this->cfg->photo_width);
127       $this->tmpl->assign('image_url_full', 'phpfspot_img.php?idx='. $photo);
128
129       if($previous_img) {
130          $this->tmpl->assign('previous_url', "javascript:showImage(". $previous_img .");");
131       }
132
133       if($next_img) {
134          $this->tmpl->assign('next_url', "javascript:showImage(". $next_img .");");
135       }
136
137       $this->tmpl->show("single_photo.tpl");
138
139    } // showPhoto()
140
141    public function getAvailableTags()
142    {
143       foreach($this->avail_tags as $tag)
144       {
145          if(isset($_SESSION['selected_tags']) && in_array($tag, $_SESSION['selected_tags']))
146             continue;
147
148          // return all available (= not selected) tags
149          print "<a href=\"javascript:Tags('add', ". $tag .");\">". $this->tags[$tag] ."</a>&nbsp;";
150
151       }
152
153    } // getAvailableTags()
154
155    public function getSelectedTags()
156    {
157       foreach($this->avail_tags as $tag)
158       {
159          // return all selected tags
160          if(isset($_SESSION['selected_tags']) && in_array($tag, $_SESSION['selected_tags'])) {
161             print "<a href=\"javascript:Tags('del', ". $tag .");\">". $this->tags[$tag] ."</a>&nbsp;";
162          }
163
164       }
165
166    } // getSelectedTags()
167
168    public function addTag($tag)
169    {
170       if(!isset($_SESSION['selected_tags']))
171          $_SESSION['selected_tags'] = Array();
172
173       array_push($_SESSION['selected_tags'], $tag);
174    
175    } // addTag()
176
177    public function delTag($tag)
178    {
179       if(isset($_SESSION['selected_tags'])) {
180          $key = array_search($tag, $_SESSION['selected_tags']);
181          unset($_SESSION['selected_tags'][$key]);
182       }
183
184    } // delTag()
185
186    public function resetTags()
187    {
188       unset($_SESSION['selected_tags']);
189
190    } // resetTags()
191
192    public function getAllTagPhotos()
193    {  
194       $tagged_photos = Array();
195
196       if(isset($_SESSION['selected_tags'])) {
197          $selected = "";
198          foreach($_SESSION['selected_tags'] as $tag)
199             $selected.= $tag .",";
200          $selected = substr($selected, 0, strlen($selected)-1);
201
202          if($_SESSION['tag_condition'] == 'or') {
203             $result = $this->db->db_query("
204                SELECT DISTINCT photo_id
205                   FROM photo_tags pt
206                INNER JOIN photos p
207                   ON p.id=pt.photo_id
208                WHERE pt.tag_id IN (". $selected .")
209                ORDER BY p.time ASC
210             ");
211             while($row = $this->db->db_fetch_object($result)) {
212                array_push($tagged_photos, $row['photo_id']);
213             }
214          }
215          elseif($_SESSION['tag_condition'] == 'and') {
216             $result = $this->db->db_query("
217                SELECT DISTINCT photo_id, tag_id
218                   FROM photo_tags pt
219                INNER JOIN photos p
220                   on p.id=pt.photo_id
221                WHERE pt.tag_id IN (". $selected .")
222                ORDER BY p.time ASC
223             ");
224
225             /* now we need to check if each object fulfills the condition
226                and has all the selected tags assigned
227             */
228             $match_object = Array();
229             $matches_needed = count($_SESSION['selected_tags']);
230             while($row = $this->db->db_fetch_object($result)) {
231                /* set the counter for this object */
232                if(!isset($match_object[$row['photo_id']]))
233                   $match_object[$row['photo_id']] = $matches_needed;
234                
235                /* we have a match? decrement the counter */
236                if(in_array($row['tag_id'], $_SESSION['selected_tags']))
237                   $match_object[$row['photo_id']]--;
238
239                /* if the object has all necessary tags, add it to the result */
240                if($match_object[$row['photo_id']] == 0)
241                   array_push($tagged_photos, $row['photo_id']);
242             }
243          }
244       }
245       else {
246          $result = $this->db->db_query("
247             SELECT DISTINCT photo_id
248                FROM photo_tags pt
249             INNER JOIN photos p
250                ON p.id=pt.photo_id
251             ORDER BY p.time ASC
252          ");
253          while($row = $this->db->db_fetch_object($result)) {
254             array_push($tagged_photos, $row['photo_id']);
255          }
256       }
257
258       return $tagged_photos;
259
260    } // getAllTagPhotos()
261
262    public function showPhotoIndex()
263    {
264       $photos = $this->getAllTagPhotos();
265       $count = count($photos);
266
267       $rows = 0;
268       $cols = 0;
269       $images[$rows] = Array();
270
271       for($i = 0; $i < $count; $i++) {
272
273          $images[$rows][$cols] = $photos[$i];
274
275          if($cols == $this->cfg->thumbs_per_row-1) {
276             $cols = 0;
277             $rows++;
278             $images[$rows] = Array();
279          }
280          else {
281             $cols++;
282          }
283       } 
284
285       // +1 for for smarty's selection iteration
286       $rows++;
287
288          //$images.= "<img src=\"phpfspot_img.php?idx=". $photo ."&amp;width=". $this->cfg->thumb_width ."\" /><br />\n";
289
290       $this->tmpl->assign('count', $count);
291       $this->tmpl->assign('width', $this->cfg->thumb_width);
292       $this->tmpl->assign('images', $images);
293       $this->tmpl->assign('rows', $rows);
294       $this->tmpl->assign('columns', $this->cfg->thumbs_per_row);
295       $this->tmpl->show("photo_index.tpl");
296
297
298    } // showPhotoIndex()
299
300    public function showBubbleDetails($photo, $direction)
301    {
302       if($direction == "up")
303          $direction = "bubbleimg_up";
304       else
305          $direction = "bubbleimg_down";
306
307       $details = $this->get_photo_details($photo);
308
309       $image_url = "phpfspot_img.php?idx=". $photo ."&amp;width=". $this->cfg->bubble_width;
310
311       $filesize = filesize($this->translate_path($details['directory_path'])  ."/". $details['name']);
312       $filesize = rand($filesize/1024, 2);
313
314       $img = getimagesize($this->translate_path($details['directory_path'])  ."/". $details['name']);
315
316       $this->tmpl->assign('file_size', $filesize);
317       $this->tmpl->assign('width', $img[0]);
318       $this->tmpl->assign('height', $img[1]);
319       $this->tmpl->assign('file_name', $details['name']);
320       $this->tmpl->assign('image_id', $direction);
321       $this->tmpl->assign('image_url', $image_url);
322       $this->tmpl->show("bubble_details.tpl");
323
324    } // showBubbleDetails()
325
326    public function showCredits()
327    {
328       $this->tmpl->assign('version', $this->cfg->version);
329       $this->tmpl->assign('product', $this->cfg->product);
330       $this->tmpl->show("credits.tpl");
331
332    } // showCredits()
333
334    public function create_thumbnail($image, $width)
335    {  
336       // if thumbnail already exists, don't recreate it
337       if(file_exists(dirname($image) ."/thumbs/". $width ."_". basename($image)))
338          return;
339
340       $src_img = @imagecreatefromjpeg($image);
341
342       if($src_img)
343       {  
344          /* grabs the height and width */
345          $new_w = imagesx($src_img);
346          $new_h = imagesy($src_img);
347
348          // If requested width is more then the actual image width,
349          // do not generate a thumbnail
350
351          if($width >= $new_w) {
352             imagedestroy($src_img);
353             return;
354          }
355
356          /* calculates aspect ratio */
357          $aspect_ratio = $new_h / $new_w;
358
359          /* sets new size */
360          $new_w = $width;
361          $new_h = abs($new_w * $aspect_ratio);
362
363          /* creates new image of that size */
364          $dst_img = imagecreatetruecolor($new_w,$new_h);
365
366          imagefill($dst_img, 0, 0, ImageColorAllocate($dst_img, 255, 255, 255));
367
368          /* copies resized portion of original image into new image */
369          imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
370
371          /* write down new generated file */
372
373          if(!file_exists(dirname($image) ."/thumbs"))
374             mkdir(dirname($image) ."/thumbs");
375
376          $newfile = dirname($image) ."/thumbs/". $width ."_". basename($image);
377          imagejpeg($dst_img, $newfile, 75);
378
379          /* free your mind */
380          imagedestroy($dst_img);
381          imagedestroy($src_img);
382       }
383
384    } // create_thumbnail()
385
386    public function get_meta_informations($file)
387    {
388
389       return exif_read_data($file);
390
391    } // get_meta_informations()
392
393    public function check_config_table()
394    {
395       // if the config table doesn't exist yet, create it
396       if(!$this->cfg_db->db_check_table_exists("images")) {
397          $this->cfg_db->db_exec("
398             CREATE TABLE images (
399                img_idx int primary key,
400                img_md5 varchar(32)
401             )
402             ");
403       }
404
405    } // check_config_table
406
407    public function gen_thumbs($idx = 0, $fromcmd = 0)
408    {
409       if(!$idx) {
410          /* get all available photos */
411          $all = $this->getAllTagPhotos();
412       }
413       else
414          $all = Array($idx);
415       
416       foreach($all as $photo) {
417
418          $details = $this->get_photo_details($photo);
419
420          $full_path = $this->translate_path($details['directory_path'])  ."/". $details['name'];
421          $file_md5 = md5_file($full_path);
422
423          if($fromcmd) print "Image ". $details['name'] ." Thumbnails:";
424
425          /* if the file hasn't changed there is no need to regen the thumb */
426          if($file_md5 == $this->getMD5($photo)) {
427             if($fromcmd) print " file has not changed - skipping\n";
428             continue;
429          }
430
431          /* set the new/changed MD5 sum for the current photo */
432          $this->setMD5($photo, $file_md5);
433
434          $resolutions = Array(
435                            $this->cfg->thumb_width,
436                            $this->cfg->bubble_width,
437                            $this->cfg->photo_width
438                         );
439
440          /* create thumbnails for the requested resolutions */
441          foreach($resolutions as $resolution) {
442             if($fromcmd) print " ". $resolution ."px";
443             $this->create_thumbnail($full_path, $resolution);
444          }
445
446          if($fromcmd) print "\n";
447
448       }
449
450    } // gen_thumbs()
451
452    private function getMD5($idx)
453    {
454       $result = $this->cfg_db->db_query("
455          SELECT img_md5 
456          FROM images
457          WHERE img_idx='". $idx ."'
458       ");
459
460       if(!$result)
461          return 0;
462
463       $img = $this->cfg_db->db_fetch_object($result);
464       return $img['img_md5'];
465       
466    } // getMD5()
467
468    private function setMD5($idx, $md5)
469    {
470       $result = $this->cfg_db->db_exec("
471          REPLACE INTO images (img_idx, img_md5)
472          VALUES ('". $idx ."', '". $md5 ."')
473       ");
474
475    } // setMD5()
476
477    public function setTagCondition($mode)
478    {
479       $_SESSION['tag_condition'] = $mode;
480
481    } // setTagCondition()
482
483 }
484
485 ?>