issue95, new sort order to group by tags
[phpfspot.git] / phpfspot.class.php
index 05532044dbfa2015d63bdc8f6082c712238312e7..329371fa1dbfa19201e9c64f87af5f72b6960d6f 100644 (file)
@@ -47,13 +47,15 @@ class PHPFSPOT {
 
       /* set application name and version information */
       $this->cfg->product = "phpfspot";
-      $this->cfg->version = "1.2";
+      $this->cfg->version = "1.3";
 
       $this->sort_orders= array(
          'date_asc' => 'Date ↑',
          'date_desc' => 'Date ↓',
          'name_asc' => 'Name ↑',
-         'name_desc' => 'Name ↓'
+         'name_desc' => 'Name ↓',
+         'tags_asc' => 'Tags ↑',
+         'tags_desc' => 'Tags ↓',
       );
 
       /* Check necessary requirements */
@@ -112,8 +114,8 @@ class PHPFSPOT {
       if(!isset($_SESSION['searchfor']))
          $_SESSION['searchfor'] = '';
 
-      // if begin_with is still set but rows_per_page is now 0, unset it
-      if(isset($_SESSION['begin_with']) && $this->cfg->rows_per_page == 0)
+      // if begin_with is still set but thumbs_per_page is now 0, unset it
+      if(isset($_SESSION['begin_with']) && $this->cfg->thumbs_per_page == 0)
          unset($_SESSION['begin_with']);
 
    } // __construct()
@@ -165,10 +167,10 @@ class PHPFSPOT {
                   $_SESSION['start_action'] = 'showp';
                }
                if(isset($_GET['from_date']) && $this->isValidDate($_GET['from_date'])) {
-                  $_SESSION['from_date'] = strtotime($_GET['from_date']);
+                  $_SESSION['from_date'] = strtotime($_GET['from_date'] ." 00:00:00");
                }
                if(isset($_GET['to_date']) && $this->isValidDate($_GET['to_date'])) {
-                  $_SESSION['to_date'] = strtotime($_GET['to_date']);
+                  $_SESSION['to_date'] = strtotime($_GET['to_date'] ." 23:59:59");
                }
                break;
             case 'export':
@@ -472,30 +474,29 @@ class PHPFSPOT {
 
       $this->tmpl->assign('extern_link', $extern_link);
 
-      if(file_exists($thumb_path)) {
+      if(!file_exists($thumb_path)) {
+         $this->_error("Can't open file ". $thumb_path ."\n");
+         return;
+      }
 
-         $info = getimagesize($thumb_path);
+      $info = getimagesize($thumb_path);
 
-         $this->tmpl->assign('description', $details['description']);
-         $this->tmpl->assign('image_name', $this->parse_uri($details['uri'], 'filename'));
+      $this->tmpl->assign('description', $details['description']);
+      $this->tmpl->assign('image_name', $this->parse_uri($details['uri'], 'filename'));
 
-         $this->tmpl->assign('width', $info[0]);
-         $this->tmpl->assign('height', $info[1]);
-         $this->tmpl->assign('ExifMadeOn', $meta_date);
-         $this->tmpl->assign('ExifMadeWith', $meta_make);
-         $this->tmpl->assign('ExifOrigResolution', $meta_res);
-         $this->tmpl->assign('ExifFileSize', $meta_size);
-    
-         $this->tmpl->assign('image_url', 'phpfspot_img.php?idx='. $photo ."&width=". $this->cfg->photo_width);
-         $this->tmpl->assign('image_url_full', 'phpfspot_img.php?idx='. $photo);
+      $this->tmpl->assign('width', $info[0]);
+      $this->tmpl->assign('height', $info[1]);
+      $this->tmpl->assign('ExifMadeOn', $meta_date);
+      $this->tmpl->assign('ExifMadeWith', $meta_make);
+      $this->tmpl->assign('ExifOrigResolution', $meta_res);
+      $this->tmpl->assign('ExifFileSize', $meta_size);
+      $this->tmpl->assign('image_url', 'phpfspot_img.php?idx='. $photo ."&width=". $this->cfg->photo_width);
+      $this->tmpl->assign('image_url_full', 'phpfspot_img.php?idx='. $photo);
+      $this->tmpl->assign('image_filename', $this->parse_uri($details['uri'], 'filename'));
 
-         $this->tmpl->assign('tags', $this->get_photo_tags($photo));
-         $this->tmpl->assign('current', $current);
-      }
-      else {
-         $this->_error("Can't open file ". $thumb_path ."\n");
-         return;
-      }
+      $this->tmpl->assign('tags', $this->get_photo_tags($photo));
+      $this->tmpl->assign('current', $current);
 
       if($previous_img) {
          $this->tmpl->assign('previous_url', "javascript:showImage(". $previous_img .");");
@@ -878,7 +879,10 @@ class PHPFSPOT {
          $query_str.= "WHERE ". $additional_where_cond ." ";
 
       if(isset($this->cfg->show_tags) && !empty($this->cfg->show_tags)) {
-         $query_str.= "AND t.name IN ('".implode("','",$this->cfg->show_tags). "')";
+        if(isset($additional_where_cond))
+           $query_str.= "AND t.name IN ('".implode("','",$this->cfg->show_tags). "')";
+        else
+           $query_str.= "WHERE t.name IN ('".implode("','",$this->cfg->show_tags). "')";
       }
  
       if(isset($order_str))
@@ -907,79 +911,54 @@ class PHPFSPOT {
       if(isset($_SESSION['begin_with']) && $_SESSION['begin_with'] != "")
          $anchor = $_SESSION['begin_with'];
 
-      if(!isset($this->cfg->rows_per_page) || $this->cfg->rows_per_page == 0) {
+      if(!isset($this->cfg->thumbs_per_page) || $this->cfg->thumbs_per_page == 0) {
 
          $begin_with = 0;
          $end_with = $count;
 
       }
-      elseif($this->cfg->rows_per_page > 0) {
+      elseif($this->cfg->thumbs_per_page > 0) {
 
          if(!isset($_SESSION['begin_with']) || $_SESSION['begin_with'] == 0) {
             $begin_with = 0;
          }
          else {
-
             $begin_with = $_SESSION['begin_with'];
-
-            // verify $begin_with - perhaps the thumbs-per-rows or
-            // rows-per-page variables have changed or the jump back
-            // from a photo wasn't exact - so calculate the real new
-            // starting point
-            $multiplicator = $this->cfg->rows_per_page * $this->cfg->thumbs_per_row;
-            for($i = 0; $i <= $count; $i+=$multiplicator) {
-               if($begin_with >= $i && $begin_with < $i+$multiplicator) {
-                  $begin_with = $i;
-                  break;
-               }
-            }
          }
 
-         $end_with = $begin_with + ($this->cfg->rows_per_page * $this->cfg->thumbs_per_row);
+         $end_with = $begin_with + $this->cfg->thumbs_per_page;
       }
 
-   
-      $rows = 0;
-      $cols = 0;
-      $images[$rows] = Array();
-      $img_height[$rows] = Array();
-      $img_width[$rows] = Array();
-      $img_id[$rows] = Array();
-      $img_name[$rows] = Array();
+      $thumbs = 0;
+      $images[$thumbs] = Array();
+      $img_height[$thumbs] = Array();
+      $img_width[$thumbs] = Array();
+      $img_id[$thumbs] = Array();
+      $img_name[$thumbs] = Array();
       $img_title = Array();
 
       for($i = $begin_with; $i < $end_with; $i++) {
 
          if(isset($photos[$i])) {
 
-            $images[$rows][$cols] = $photos[$i];
-            $img_id[$rows][$cols] = $i;
-            $img_name[$rows][$cols] = htmlspecialchars($this->getPhotoName($photos[$i], 15));
-            $img_title[$rows][$cols] = "Click to view photo ". htmlspecialchars($this->getPhotoName($photos[$i], 0));
+            $images[$thumbs] = $photos[$i];
+            $img_id[$thumbs] = $i;
+            $img_name[$thumbs] = htmlspecialchars($this->getPhotoName($photos[$i], 15));
+            $img_title[$thumbs] = "Click to view photo ". htmlspecialchars($this->getPhotoName($photos[$i], 0));
 
             $thumb_path = $this->get_thumb_path($this->cfg->thumb_width, $photos[$i]);
 
             if(file_exists($thumb_path)) {
                $info = getimagesize($thumb_path); 
-               $img_width[$rows][$cols] = $info[0];
-               $img_height[$rows][$cols] = $info[1];
-            }
-
-            if($cols == $this->cfg->thumbs_per_row-1) {
-               $cols = 0;
-               $rows++;
-               $images[$rows] = Array();
-               $img_width[$rows] = Array();
-               $img_height[$rows] = Array();
-            }
-            else {
-               $cols++;
+               $img_width[$thumbs] = $info[0];
+               $img_height[$thumbs] = $info[1];
             }
+            $thumbs++;
          } 
       }
 
       // +1 for for smarty's selection iteration
-      $rows++;
+      $thumbs++;
 
       if(isset($_SESSION['searchfor']) && $_SESSION['searchfor'] != '')
          $this->tmpl->assign('searchfor', $_SESSION['searchfor']);
@@ -994,20 +973,20 @@ class PHPFSPOT {
       }
 
       /* do we have to display the page selector ? */
-      if($this->cfg->rows_per_page != 0) {
+      if($this->cfg->thumbs_per_page != 0) {
 
          $page_select = "";
       
          /* calculate the page switchers */
-         $previous_start = $begin_with - ($this->cfg->rows_per_page * $this->cfg->thumbs_per_row);
-         $next_start = $begin_with + ($this->cfg->rows_per_page * $this->cfg->thumbs_per_row);
+         $previous_start = $begin_with - $this->cfg->thumbs_per_page;
+         $next_start = $begin_with + $this->cfg->thumbs_per_page;
 
          if($begin_with != 0) 
             $this->tmpl->assign("previous_url", "javascript:showPhotoIndex(". $previous_start .");"); 
          if($end_with < $count)
             $this->tmpl->assign("next_url", "javascript:showPhotoIndex(". $next_start .");"); 
 
-         $photo_per_page  = $this->cfg->rows_per_page * $this->cfg->thumbs_per_row;
+         $photo_per_page  = $this->cfg->thumbs_per_page;
          $last_page = ceil($count / $photo_per_page);
 
          /* get the current selected page */
@@ -1095,16 +1074,15 @@ class PHPFSPOT {
       $this->tmpl->assign('rss_link', $rss_link);
       $this->tmpl->assign('count', $count);
       $this->tmpl->assign('width', $this->cfg->thumb_width);
-      $this->tmpl->assign('thumb_container_width', $this->cfg->thumb_width+20);
-      $this->tmpl->assign('thumb_container_height', $this->cfg->thumb_height);
+      $this->tmpl->assign('thumb_container_width', $this->cfg->thumb_width);
+      $this->tmpl->assign('thumb_container_height', $this->cfg->thumb_height+20);
       $this->tmpl->assign('images', $images);
       $this->tmpl->assign('img_width', $img_width);
       $this->tmpl->assign('img_height', $img_height);
       $this->tmpl->assign('img_id', $img_id);
       $this->tmpl->assign('img_name', $img_name);
       $this->tmpl->assign('img_title', $img_title);
-      $this->tmpl->assign('rows', $rows);
-      $this->tmpl->assign('columns', $this->cfg->thumbs_per_row);
+      $this->tmpl->assign('thumbs', $thumbs);
 
       $this->tmpl->show("photo_index.tpl");
 
@@ -1148,26 +1126,26 @@ class PHPFSPOT {
       $meta = $this->get_meta_informations($orig_image);
 
       $rotate = 0;
-      $flip = false;
+      $flip_hori = false;
+      $flip_vert = false;
 
       switch($meta['Orientation']) {
-
          case 1: /* top, left */
-            $rotate = 0; $flip = false; break;
+            /* nothing to do */ break;
          case 2: /* top, right */
-            $rotate = 0; $flip = true; break;
+            $rotate = 0; $flip_hori = true; break;
          case 3: /* bottom, left */
-            $rotate = 180; $flip = false; break;
+            $rotate = 180; break;
          case 4: /* bottom, right */
-            $rotate = 180; $flip = true; break;
+            $flip_vert = true; break;
          case 5: /* left side, top */
-            $rotate = 90; $flip = true; break;
+            $rotate = 90; $flip_vert = true; break;
          case 6: /* right side, top */
-            $rotate = 90; $flip = false; break;
+            $rotate = 90; break;
          case 7: /* left side, bottom */
-            $rotate = 270; $flip = true; break;
+            $rotate = 270; $flip_vert = true; break;
          case 8: /* right side, bottom */
-            $rotate = 270; $flip = false; break;
+            $rotate = 270; break;
       }
 
       $src_img = @imagecreatefromjpeg($orig_image);
@@ -1239,12 +1217,14 @@ class PHPFSPOT {
       imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_w, $new_h, imagesx($src_img), imagesy($src_img));
 
       /* needs the image to be flipped horizontal? */
-      if($flip) {
-         print "(FLIP)";
-         $image = $dst_img;
-         for($x = 0; $x < $new_w; $x++) {
-            imagecopy($dst_img, $image, $x, 0, $w - $x - 1, 0, 1, $h);
-         }
+      if($flip_hori) {
+         $this->_debug("(FLIP)");
+         $dst_img = $this->flipImage($dst_img, 'hori');
+      }
+      /* needs the image to be flipped vertical? */
+      if($flip_vert) {
+         $this->_debug("(FLIP)");
+         $dst_img = $this->flipImage($dst_img, 'vert');
       }
 
       if($rotate) {
@@ -1444,12 +1424,14 @@ class PHPFSPOT {
       $this->get_tags();
 
       $_SESSION['searchfor'] = $searchfor;
+
       if($from != 0)
-         $_SESSION['from_date'] = strtotime($from);
+         $_SESSION['from_date'] = strtotime($from ." 00:00:00");
       else
          unset($_SESSION['from_date']);
+
       if($to != 0)
-         $_SESSION['to_date'] = strtotime($to);
+         $_SESSION['to_date'] = strtotime($to ." 23:59:59");
       else
          unset($_SESSION['to_date']);
 
@@ -1558,6 +1540,35 @@ class PHPFSPOT {
 
    } // rotateImage()
 
+   /**
+    * returns flipped image
+    *
+    * this function will return an either horizontal or
+    * vertical flipped truecolor image.
+    */
+   private function flipImage($image, $mode)
+   {
+      $w = imagesx($image);
+      $h = imagesy($image);
+      $flipped = imagecreatetruecolor($w, $h);
+
+      switch($mode) {
+         case 'vert':
+            for ($y = 0; $y < $h; $y++) {
+               imagecopy($flipped, $image, 0, $y, 0, $h - $y - 1, $w, 1);
+            }
+            break;
+         case 'hori':
+            for ($x = 0; $x < $w; $x++) {
+               imagecopy($flipped, $image, $x, 0, $w - $x - 1, 0, 1, $h);
+            }
+            break;
+      }
+
+      return $flipped;
+
+   } // flipImage()
+
    /**
     * return all assigned tags for the specified photo
     */
@@ -2013,6 +2024,12 @@ class PHPFSPOT {
                return " ORDER BY basename(p.uri) DESC";
             }
             break;
+         case 'tags_asc':
+            return " ORDER BY t.name ASC ,p.time ASC";
+            break;
+         case 'tags_desc':
+            return " ORDER BY t.name DESC ,p.time ASC";
+            break;
       }
 
    } // get_sort_order()