issue61, theme support
[phpfspot.git] / phpfspot.class.php
index 265fa1d6c70b6e22d37695c09d1a461b469dfea9..825f92d098d55a8e602ee8c16bd5cf027ac89b31 100644 (file)
@@ -58,8 +58,13 @@ class PHPFSPOT {
       }
          
       $this->cfg_db = new PHPFSPOT_DB(&$this, $this->cfg->phpfspot_db);
+      if(!is_writeable($this->cfg->phpfspot_db)) {
+         print $this->cfg->phpfspot_db ." is not writeable for user ". $this->getuid() ."\n";
+         exit(1);
+      }
       $this->check_config_table();
 
+
       $this->tmpl = new PHPFSPOT_TMPL($this);
 
       $this->get_tags();
@@ -69,6 +74,9 @@ class PHPFSPOT {
       if(!isset($_SESSION['tag_condition']))
          $_SESSION['tag_condition'] = 'or';
 
+      if(!isset($_SESSION['sort_order']))
+         $_SESSION['sort_order'] = 'date_asc';
+
       if(!isset($_SESSION['searchfor']))
          $_SESSION['searchfor'] = '';
 
@@ -132,11 +140,17 @@ class PHPFSPOT {
             $this->tmpl->show("export.tpl");
             return;
             break;
+         case 'slideshow':
+            $this->tmpl->show("slideshow.tpl");
+            return;
+            break;
       }
 
       $this->tmpl->assign('from_date', $this->get_calendar('from'));
       $this->tmpl->assign('to_date', $this->get_calendar('to'));
+      $this->tmpl->assign('sort_field', $this->get_sort_field());
       $this->tmpl->assign('content_page', 'welcome.tpl');
+      $this->tmpl->assign('template_path', 'themes/'. $this->cfg->theme_name);
       $this->tmpl->show("index.tpl");
 
 
@@ -304,7 +318,7 @@ class PHPFSPOT {
       }
 
       $meta_date = isset($meta['FileDateTime']) ? strftime("%a %x %X", $meta['FileDateTime']) : "n/a";
-      $meta_make = isset($meta['Make']) ? $meta['Make'] ." ". $meta['Model'] : "n/a";
+      $meta_make = isset($meta['Make']) ? $meta['Make'] ." ". $meta['Model'] : "n/a";
       $meta_size = isset($meta['FileSize']) ? round($meta['FileSize']/1024, 1) ."kbyte" : "n/a";
 
       $extern_link = "index.php?mode=showp&id=". $photo;
@@ -353,6 +367,8 @@ class PHPFSPOT {
          $this->tmpl->assign('next_img', $next_img);
       }
       $this->tmpl->assign('mini_width', $this->cfg->mini_width);
+      $this->tmpl->assign('photo_number', $i);
+      $this->tmpl->assign('photo_count', count($all_photos));
 
       $this->tmpl->show("single_photo.tpl");
 
@@ -367,6 +383,8 @@ class PHPFSPOT {
     */
    public function getAvailableTags()
    {
+      $output = "";
+
       $result = $this->db->db_query("
          SELECT tag_id as id, count(tag_id) as quantity
          FROM photo_tags
@@ -414,10 +432,13 @@ class PHPFSPOT {
           // uncomment if you want sizes in whole %:
          $size = ceil($size);
 
-         print "<a href=\"javascript:Tags('add', ". $key .");\" class=\"tag\" style=\"font-size: ". $size ."%;\">". $this->tags[$key] ."</a>, ";
+         $output.= "<a href=\"javascript:Tags('add', ". $key .");\" class=\"tag\" style=\"font-size: ". $size ."%;\">". $this->tags[$key] ."</a>, ";
 
       }
 
+      $output = substr($output, 0, strlen($output)-2);
+      print $output;
+
    } // getAvailableTags()
 
    /**
@@ -452,9 +473,6 @@ class PHPFSPOT {
     */
    public function addTag($tag)
    {
-      // if the result of a date search are displayed, reset them
-      $this->resetDateSearch();
-
       if(!isset($_SESSION['selected_tags']))
          $_SESSION['selected_tags'] = Array();
 
@@ -545,18 +563,37 @@ class PHPFSPOT {
    {  
       $matched_photos = Array();
 
+      if(isset($_SESSION['from_date']) && isset($_SESSION['to_date'])) {
+         $from_date = strtotime($_SESSION['from_date']);
+         $to_date = strtotime($_SESSION['to_date']);
+         $additional_where_cond = "
+               p.time>='". $from_date ."'
+            AND
+               p.time<='". $to_date ."'
+         ";
+      } 
+
+      if(isset($_SESSION['sort_order'])) {
+         $order_str = $this->get_sort_order();
+      }
+
       /* return a search result */
       if(isset($_SESSION['searchfor']) && $_SESSION['searchfor'] != '') {
-         $result = $this->db->db_query("
+         $query_str = "
             SELECT DISTINCT photo_id
                FROM photo_tags pt
             INNER JOIN photos p
                ON p.id=pt.photo_id
             INNER JOIN tags t
                ON pt.tag_id=t.id
-            WHERE t.name LIKE '%". $_SESSION['searchfor'] ."%'
-               ORDER BY p.time ASC
-         ");
+            WHERE t.name LIKE '%". $_SESSION['searchfor'] ."%'";
+
+         if(isset($additional_where_cond))
+            $query_str.= "AND ". $additional_where_cond ." ";
+         if(isset($order_str))
+            $query_str.= $order_str;
+
+         $result = $this->db->db_query($query_str);
          while($row = $this->db->db_fetch_object($result)) {
             array_push($matched_photos, $row['photo_id']);
          }
@@ -571,14 +608,17 @@ class PHPFSPOT {
          $selected = substr($selected, 0, strlen($selected)-1);
 
          if($_SESSION['tag_condition'] == 'or') {
-            $result = $this->db->db_query("
+            $query_str = "
                SELECT DISTINCT photo_id
                   FROM photo_tags pt
                INNER JOIN photos p
                   ON p.id=pt.photo_id
                WHERE pt.tag_id IN (". $selected .")
-               ORDER BY p.time ASC
-            ");
+            ";
+            if(isset($additional_where_cond)) 
+               $query_str.= "AND ". $additional_where_cond ." ";
+            if(isset($order_str))
+               $query_str.= $order_str;
          }
          elseif($_SESSION['tag_condition'] == 'and') {
 
@@ -608,49 +648,42 @@ class PHPFSPOT {
                      ON pt1.photo_id=pt". ($i+2) .".photo_id
                ";
             }
+            $query_str.= "
+               INNER JOIN photos p
+                  ON pt1.photo_id=p.id
+            ";
             $query_str.= "WHERE pt1.tag_id=". $_SESSION['selected_tags'][0];
             for($i = 1; $i < count($_SESSION['selected_tags']); $i++) {
                $query_str.= "
                   AND pt". ($i+1) .".tag_id=". $_SESSION['selected_tags'][$i] ."
                "; 
             }
-            $result = $this->db->db_query($query_str);
+            if(isset($additional_where_cond)) 
+               $query_str.= "AND ". $additional_where_cond;
+            if(isset($order_str))
+               $query_str.= $order_str;
          }
 
+         $result = $this->db->db_query($query_str);
          while($row = $this->db->db_fetch_object($result)) {
             array_push($matched_photos, $row['photo_id']);
          }
          return $matched_photos;
       }
 
-      if(isset($_SESSION['from_date']) && isset($_SESSION['to_date'])) {
-         $from_date = strtotime($_SESSION['from_date']);
-         $to_date = strtotime($_SESSION['to_date']);
-         $result = $this->db->db_query("
-            SELECT DISTINCT photo_id
-               FROM photo_tags pt
-            INNER JOIN photos p
-               ON p.id=pt.photo_id
-            WHERE 
-               time>='". $from_date ."'
-            AND
-               time<='". $to_date ."'
-            ORDER BY p.time ASC
-         ");
-         while($row = $this->db->db_fetch_object($result)) {
-            array_push($matched_photos, $row['photo_id']);
-         }
-         return $matched_photos;
-      } 
-
       /* return all available photos */
-      $result = $this->db->db_query("
+      $query_str = "
          SELECT DISTINCT photo_id
             FROM photo_tags pt
          INNER JOIN photos p
             ON p.id=pt.photo_id
-         ORDER BY p.time ASC
-      ");
+      ";
+      if(isset($additional_where_cond)) 
+         $query_str.= "WHERE ". $additional_where_cond ." ";
+      if(isset($order_str))
+         $query_str.= $order_str;
+
+      $result = $this->db->db_query($query_str);
       while($row = $this->db->db_fetch_object($result)) {
          array_push($matched_photos, $row['photo_id']);
       }
@@ -746,6 +779,15 @@ class PHPFSPOT {
       if(isset($_SESSION['searchfor']) && $_SESSION['searchfor'] != '')
          $this->tmpl->assign('searchfor', $_SESSION['searchfor']);
 
+      if(isset($_SESSION['from_date']) && isset($_SESSION['to_date'])) {
+         $this->tmpl->assign('from_date', $_SESSION['from_date']);
+         $this->tmpl->assign('to_date', $_SESSION['to_date']);
+      }
+
+      if(isset($_SESSION['selected_tags']) && !empty($_SESSION['selected_tags'])) {
+         $this->tmpl->assign('tag_result', 1);
+      }
+
       /* do we have to display the page selector ? */
       if($this->cfg->rows_per_page != 0) {
       
@@ -771,6 +813,8 @@ class PHPFSPOT {
             }
          } 
 
+         $dotdot_made = 0;
+
          for($i = 1; $i <= $last_page; $i++) {
 
             if($current_page == $i)
@@ -805,12 +849,16 @@ class PHPFSPOT {
                   $i == $current_page+3 || $i == $current_page+2 || $i == $current_page+1 /* three after */) {
 
                   $page_select.= $select;
+                  $dotdot_made = 0;
                   continue;
 
                }
             }
 
-            $page_select.= ".";
+            if(!$dotdot_made) {
+               $page_select.= ".........&nbsp;";
+               $dotdot_made = 1;
+            }
          }
 
          /* only show the page selector if we have more then one page */
@@ -829,8 +877,10 @@ class PHPFSPOT {
       }
 
       $export_link = "index.php?mode=export";
+      $slideshow_link = "index.php?mode=slideshow";
 
       $this->tmpl->assign('extern_link', $extern_link);
+      $this->tmpl->assign('slideshow_link', $slideshow_link);
       $this->tmpl->assign('export_link', $export_link);
       $this->tmpl->assign('count', $count);
       $this->tmpl->assign('width', $this->cfg->thumb_width);
@@ -1152,39 +1202,30 @@ class PHPFSPOT {
    } // setTagCondition()
 
    /** 
-    * invoke tag search 
+    * invoke tag & date search 
     *
     * this function will return all matching tags and store
-    * them in the session variable selected_tags. 
+    * them in the session variable selected_tags. furthermore
+    * it also handles the date search.
     * getPhotoSelection() will then only return the matching
     * photos.
     */
-   public function startTagSearch($searchfor)
+   public function startSearch($searchfor, $from, $to, $sort_order)
    {
       $_SESSION['searchfor'] = $searchfor;
-      $_SESSION['selected_tags'] = Array();
-
-      foreach($this->avail_tags as $tag) {
-         if(preg_match('/'. $searchfor .'/i', $this->tags[$tag]))
-            array_push($_SESSION['selected_tags'], $tag);
-      }
-
-      $this->resetDateSearch();
-
-   } // startTagSearch()
-
-   /** 
-    * invoke date search 
-    *
-    * this function in fact does nothing then only setting
-    * the from- and to-date in the users session variables.
-    * the result is generated by getPhotoSelection().
-    */
-   public function startDateSearch($from, $to)
-   {
       $_SESSION['from_date'] = $from;
       $_SESSION['to_date'] = $to;
-   }
+      $_SESSION['sort_order'] = $sort_order;
+
+      if($searchfor != "") {
+         /* new search, reset the current selected tags */
+         $_SESSION['selected_tags'] = Array();
+         foreach($this->avail_tags as $tag) {
+            if(preg_match('/'. $searchfor .'/i', $this->tags[$tag]))
+               array_push($_SESSION['selected_tags'], $tag);
+         }
+      }
+   } // startSearch()
 
    /**
     * rotate image
@@ -1386,9 +1427,15 @@ class PHPFSPOT {
       $month = $_SESSION[$mode .'_date'] ? date("m", strtotime($_SESSION[$mode .'_date'])) : date("m");
       $day = $_SESSION[$mode .'_date'] ? date("d", strtotime($_SESSION[$mode .'_date'])) : date("d");
 
-      $output = "<input type=\"text\" size=\"3\" id=\"". $mode ."year\" value=\"". $year ."\" />\n";
-      $output.= "<input type=\"text\" size=\"1\" id=\"". $mode ."month\" value=\"". $month ."\" />\n";
-      $output.= "<input type=\"text\" size=\"1\" id=\"". $mode ."day\" value=\"". $day ."\" />\n";
+      $output = "<input type=\"text\" size=\"3\" id=\"". $mode ."year\" value=\"". $year ."\"";
+      if(!isset($_SESSION[$mode .'_date'])) $output.= " disabled=\"disabled\"";
+      $output.= " />\n";
+      $output.= "<input type=\"text\" size=\"1\" id=\"". $mode ."month\" value=\"". $month ."\"";
+      if(!isset($_SESSION[$mode .'_date'])) $output.= " disabled=\"disabled\"";
+      $output.= " />\n";
+      $output.= "<input type=\"text\" size=\"1\" id=\"". $mode ."day\" value=\"". $day ."\"";
+      if(!isset($_SESSION[$mode .'_date'])) $output.= " disabled=\"disabled\"";
+      $output.= " />\n";
       return $output;
 
    } // get_calendar()
@@ -1549,15 +1596,13 @@ class PHPFSPOT {
     */
    public function whatToDo()
    {
-      if(isset($_SESSION['selected_tags']) && !empty($_SESSION['selected_tags'])) {
-         return "showpi_tags";
-      }
-      elseif(isset($_SESSION['from_date']) && isset($_SESSION['to_date'])) {
-         return "showpi_date";
-      }
-      elseif(isset($_SESSION['current_photo'])) {
+      if(isset($_SESSION['current_photo']) && $_SESSION['start_action'] == 'showp') {
          return "show_photo";
       }
+      elseif((isset($_SESSION['selected_tags']) && !empty($_SESSION['selected_tags'])) ||
+         (isset($_SESSION['from_date']) && isset($_SESSION['to_date']))) {
+         return "showpi";
+      }
       elseif(isset($_SESSION['start_action']) && $_SESSION['start_action'] == 'showpi') {
          return "showpi";
       }
@@ -1581,6 +1626,96 @@ class PHPFSPOT {
    
    } // getuid()
 
+   /**
+    * returns a select-dropdown box to select photo index sort parameters
+    */
+   private function get_sort_field()
+   {
+      $output = "<select name=\"sort_order\">";
+      foreach(array('date_asc', 'date_desc', 'name_asc', 'name_desc') as $sort_order) {
+         $output.= "<option value=\"". $sort_order ."\"";
+         if($sort_order == $_SESSION['sort_order']) {
+            $output.= " selected=\"selected\"";
+         }
+         $output.= ">". $sort_order ."</option>";
+      }
+      $output.= "</select>";
+      return $output;
+
+   } // get_sort_field()
+
+   /**
+    * returns the currently selected sort order
+    */ 
+   private function get_sort_order()
+   {
+      switch($_SESSION['sort_order']) {
+         case 'date_asc':
+            return " ORDER BY p.time ASC";
+            break;
+         case 'date_desc':
+            return " ORDER BY p.time DESC";
+            break;
+         case 'name_asc':
+            return " ORDER BY p.name ASC";
+            break;
+         case 'name_desc':
+            return " ORDER BY p.name DESC";
+            break;
+      }
+
+   } // get_sort_order()
+
+   public function getNextSlideShowImage()
+   {
+      $all_photos = $this->getPhotoSelection();
+
+      if(!isset($_SESSION['slideshow_img']) || $_SESSION['slideshow_img'] == count($all_photos)-1) 
+         $_SESSION['slideshow_img'] = 0;
+      else
+         $_SESSION['slideshow_img']++;
+
+      $server_name = $_SERVER['SERVER_NAME'];
+      if(!isset($_SERVER['HTTPS'])) $protocol = "http";
+      else $protocol = "https";
+
+      return $protocol ."://". $server_name . $this->cfg->web_path ."phpfspot_img.php?idx=". $all_photos[$_SESSION['slideshow_img']] ."&width=". $this->cfg->photo_width;
+
+   } // getNextSlideShowImage()
+
+   public function resetSlideShow()
+   {
+      if(isset($_SESSION['slideshow_img']))
+         unset($_SESSION['slideshow_img']);
+   } // resetSlideShow()
+   
+   /***
+     * get random photo
+     *
+     * this function will get all photos from the fspot
+     * database and randomly return ONE entry
+     *
+     * saddly there is yet no sqlite3 function which returns
+     * the bulk result in array, so we have to fill up our
+     * own here.
+     */ 
+   public function get_random_photo()
+   {
+      $all = Array();
+
+      $result = $this->db->db_query("
+         SELECT id
+         FROM photos
+      ");
+      
+      while($row = $this->db->db_fetch_object($result)) {
+         array_push($all, $row[0]);
+      }
+
+      return array_rand($all);
+
+   } // get_random_photo()
+
 }
 
 ?>