issue124, also if rate-sort is set to DESC, 2nd-sort by photo-name should be ASC
[phpfspot.git] / phpfspot.class.php
index 9a1106961f61041980594481519d1f0679c54f76..d7a76c3a50d9cfd80830dda1553a87f6007d9b52 100644 (file)
@@ -122,7 +122,7 @@ class PHPFSPOT {
 
       /* set application name and version information */
       $this->cfg->product = "phpfspot";
-      $this->cfg->version = "1.5";
+      $this->cfg->version = "1.6";
 
       $this->sort_orders= array(
          'date_asc' => 'Date ↑',
@@ -142,7 +142,9 @@ class PHPFSPOT {
 
       /* Check if database file is writeable */
       if(!is_writeable($this->cfg->fspot_db)) {
-         print $this->cfg->fspot_db ." is not writeable for user ". $this->getuid() ."\n";
+         print "Error: ". $this->cfg->fspot_db ." is not writeable for user ". $this->getuid() .".\n";
+         print "Please fix permissions so phpfspot can create indices within the F-Spot database to"
+            ." speed up some database operations.\n";
          exit(1);
       }
 
@@ -174,13 +176,15 @@ class PHPFSPOT {
 
       /* Check if directory where the database file is stored is writeable  */
       if(!is_writeable(dirname($this->cfg->phpfspot_db))) {
-         print dirname($this->cfg->phpfspot_db) .": directory is not writeable for user ". $this->getuid() ."\n";
+         print "Error: ". dirname($this->cfg->phpfspot_db) .": directory is not writeable for user ". $this->getuid() .".\n";
+         print "Please fix permissions so phpfspot can create its own sqlite database to store some settings.\n";
          exit(1);
       }
 
       /* Check if database file is writeable */
       if(!is_writeable($this->cfg->phpfspot_db)) {
-         print $this->cfg->phpfspot_db ." is not writeable for user ". $this->getuid() ."\n";
+         print "Error: ". $this->cfg->phpfspot_db ." is not writeable for user ". $this->getuid() .".\n";
+         print "Please fix permissions so phpfspot can create its own sqlite database to store some settings.\n";
          exit(1);
       }
 
@@ -223,8 +227,9 @@ class PHPFSPOT {
       if(!isset($_SESSION['tag_condition']))
          $_SESSION['tag_condition'] = 'or';
 
+      /* if sort-order has not been set yet, get the one specified in the config */
       if(!isset($_SESSION['sort_order']))
-         $_SESSION['sort_order'] = 'date_desc';
+         $_SESSION['sort_order'] = $this->cfg->sort_order;
 
       if(!isset($_SESSION['searchfor_tag']))
          $_SESSION['searchfor_tag'] = '';
@@ -299,7 +304,9 @@ class PHPFSPOT {
                }
                break;
             case 'export':
-               $this->tmpl->show("export.tpl");
+               /* fetch export template */
+               print $this->tmpl->fetch("export.tpl");
+               /* no further execution necessary. */
                return;
                break;
             case 'slideshow':
@@ -338,18 +345,29 @@ class PHPFSPOT {
       }
 
       $this->tmpl->register_function("sort_select_list", array(&$this, "smarty_sort_select_list"), false);
-      $this->tmpl->assign('search_from_date', $this->get_calendar('from'));
-      $this->tmpl->assign('search_to_date', $this->get_calendar('to'));
+      $this->tmpl->assign('search_from_date', $this->get_date_text_field('from'));
+      $this->tmpl->assign('search_to_date', $this->get_date_text_field('to'));
 
       $this->tmpl->assign('preset_selected_tags', $this->getSelectedTags());
       $this->tmpl->assign('preset_available_tags', $this->getAvailableTags());
       $this->tmpl->assign('rate_search', $this->get_rate_search());
 
+      /* if no site-content has been set yet... */
       if(!isset($content)) {
-         if(isset($_SESSION['selected_tags']) && !empty($_SESSION['selected_tags']))
+         /* if tags are already selected, we can immediately display photo-index */
+         if((isset($_SESSION['selected_tags']) && !empty($_SESSION['selected_tags']) &&
+             isset($_SESSION['start_action']) && $_SESSION['start_action'] != 'showp') ||
+            (isset($_SESSION['start_action']) && $_SESSION['start_action'] == 'showpi'))
             $this->tmpl->assign('initial_content', $this->showPhotoIndex());
-         else
-            $this->tmpl->assign('initial_content', $this->tmpl->fetch('welcome.tpl'));
+         else {
+            /* if a photo is already selected, we can immediately display single-photo */
+            if(isset($_SESSION['current_photo']) && !empty($_SESSION['current_photo']))
+               $this->tmpl->assign('initial_content', $this->showPhoto($_SESSION['current_photo']));
+            else {
+               /* ok, then let us show the welcome page... */
+               $this->tmpl->assign('initial_content', $this->tmpl->fetch('welcome.tpl'));
+            }
+         }
       }
       else
          $this->tmpl->assign('initial_content', $content);
@@ -374,6 +392,11 @@ class PHPFSPOT {
       $this->avail_tags = Array();
       $count = 0;
    
+      /* if show_tags has been set in the configuration (only show photos
+         which are tagged by these tags) they following will take care,
+         that only these other tags are displayed where the photo is also
+         tagged with one of show_tags.
+      */
       if(isset($this->cfg->show_tags) && !empty($this->cfg->show_tags)) {
          $query_str="
             SELECT
@@ -441,6 +464,7 @@ class PHPFSPOT {
     */
    public function get_photo_details($idx)
    {
+      /* ~ F-Spot version 0.3.x */
       if($this->dbver < 9) {
          $query_str = "
             SELECT p.id, p.name, p.time, p.directory_path, p.description
@@ -463,8 +487,8 @@ class PHPFSPOT {
          }
       }
 
-      /* if show_tags is set, only return details for photos which
-         are specified to be shown
+      /* if show_tags is set, only return details of photos which are
+         tagged with a tag that has been specified to be shown.
       */
       if(isset($this->cfg->show_tags) && !empty($this->cfg->show_tags)) {
          $query_str.= "
@@ -485,6 +509,9 @@ class PHPFSPOT {
 
          $row = $this->db->db_fetch_object($result);
 
+         /* before F-Spot db version 9 there was no uri column but seperated
+            columns for directory_path and name (= filename).
+         */
          if($this->dbver < 9) {
             $row['uri'] = "file://". $row['directory_path'] ."/". $row['name'];
          }
@@ -500,9 +527,10 @@ class PHPFSPOT {
    /**
     * returns aligned photo names 
     *
-    * this function returns aligned (length) names for
-    * an specific photo. If the length of the name exceeds
-    * $limit the name will be shrinked (...)
+    * this function returns aligned (length) names for a specific photo.
+    * If the length of the name exceeds $limit the name will bei
+    * shrinked (...).
+    *
     * @param integer $idx
     * @param integer $limit
     * @return string|null
@@ -523,11 +551,11 @@ class PHPFSPOT {
    /**
     * get photo rating level
     *
-    * this function will return the integer-based rating
-    * level of the photo. This can only be done, if the F-Spot
-    * database is at a specific level. If rating value can not
-    * be found, zero will be returned indicating no rating value
-    * is available.
+    * this function will return the integer-based rating level of a
+    * photo. This can only be done, if the F-Spot database is at a
+    * specific version. If rating value can not be found, zero will
+    * be returned indicating no rating value is available.
+    *
     * @param integer idx
     * @return integer
     */
@@ -545,8 +573,8 @@ class PHPFSPOT {
    /**
     * get rate-search bars
     *
-    * this function will return the rating-bars for the
-    * search field.
+    * this function will return the rating-bars for the search field.
+    *
     * @return string
     */
    public function get_rate_search()
@@ -592,9 +620,9 @@ class PHPFSPOT {
    /**
     * shrink text according provided limit
     *
-    * If the length of the name exceeds $limit the
-    * text will be shortend and some content in between
-    * will be replaced with "..." 
+    * If the length of the name exceeds $limit, text will be shortend
+    * and inner content will be replaced with "...".
+    *
     * @param string $ext
     * @param integer $limit
     * @return string
@@ -702,7 +730,6 @@ class PHPFSPOT {
          $meta_res = $info[0] ."x". $info[1]; 
       }
 
-      $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_size = isset($meta['FileSize']) ? round($meta['FileSize']/1024, 1) ."kbyte" : "n/a";
 
@@ -730,7 +757,7 @@ class PHPFSPOT {
 
       $this->tmpl->assign('width', $info_thumb[0]);
       $this->tmpl->assign('height', $info_thumb[1]);
-      $this->tmpl->assign('ExifMadeOn', $meta_date);
+      $this->tmpl->assign('ExifMadeOn', strftime("%a %x %X", $details['time']));
       $this->tmpl->assign('ExifMadeWith', $meta_make);
       $this->tmpl->assign('ExifOrigResolution', $meta_res);
       $this->tmpl->assign('ExifFileSize', $meta_size);
@@ -824,6 +851,9 @@ class PHPFSPOT {
       // loop through our tag array
       foreach ($tags as $key => $value) {
 
+         /* has the currently processed tag already been added to
+            the selected tag list? if so, ignore it here...
+         */
          if(isset($_SESSION['selected_tags']) && in_array($key, $_SESSION['selected_tags']))
             continue;
 
@@ -832,7 +862,7 @@ class PHPFSPOT {
          // multiply by the font-size increment ($size)
          // and add the $min_size set above
          $size = $min_size + (($value - $min_qty) * $step);
-          // uncomment if you want sizes in whole %:
+         // uncomment if you want sizes in whole %:
          $size = ceil($size);
 
          $color = $min_sat + ($value - $min_qty) * $step_sat;
@@ -842,10 +872,20 @@ class PHPFSPOT {
          $b = '88';
 
          if(isset($this->tags[$key])) {
-            if($this->is_user_friendly_url())
-               $output.= "<a href=\"". $this->cfg->web_path ."/tag/". $key ."\" onclick=\"Tags('add', ". $key ."); return false;\" class=\"tag\" style=\"font-size: ". $size ."%; color: #". $r.$g.$b .";\">". $this->tags[$key] ."</a>, ";
-            else
-               $output.= "<a href=\"". $this->cfg->web_path ."/index.php?mode=showpi\" onclick=\"Tags('add', ". $key ."); return false;\" class=\"tag\" style=\"font-size: ". $size ."%; color: #". $r.$g.$b .";\">". $this->tags[$key] ."</a>, ";
+            if($this->is_user_friendly_url()) {
+               $output.= "<a href=\"". $this->cfg->web_path ."/tag/". $key ."\"
+                  onclick=\"Tags('add', ". $key ."); return false;\"
+                  class=\"tag\"
+                  style=\"font-size: ". $size ."%; color: #". $r.$g.$b .";\"
+                  title=\"Tag ". $this->tags[$key] .", ". $tags[$key] ." picture(s)\">". $this->tags[$key] ."</a>, ";
+            }
+            else {
+               $output.= "<a href=\"". $this->cfg->web_path ."/index.php?mode=showpi\"
+                  onclick=\"Tags('add', ". $key ."); return false;\"
+                  class=\"tag\"
+                  style=\"font-size: ". $size ."%; color: #". $r.$g.$b .";\"
+                  title=\"Tag ". $this->tags[$key] .", ". $tags[$key] ." picture(s)\">". $this->tags[$key] ."</a>, ";
+            }
          }
       }
 
@@ -1992,11 +2032,11 @@ class PHPFSPOT {
    public function startSearch()
    {
       /* date search */
-      if(isset($_POST['from']) && $this->isValidDate($_POST['from'])) {
-         $from = $_POST['from'];
+      if(isset($_POST['date_from']) && $this->isValidDate($_POST['date_from'])) {
+         $date_from = $_POST['date_from'];
       }
-      if(isset($_POST['to']) && $this->isValidDate($_POST['to'])) {
-         $to = $_POST['to'];
+      if(isset($_POST['date_to']) && $this->isValidDate($_POST['date_to'])) {
+         $date_to = $_POST['date_to'];
       }
 
       /* tag-name search */
@@ -2032,13 +2072,13 @@ class PHPFSPOT {
 
       $this->get_tags();
 
-      if(isset($from) && !empty($from))
-         $_SESSION['from_date'] = strtotime($from ." 00:00:00");
+      if(isset($date_from) && !empty($date_from))
+         $_SESSION['from_date'] = strtotime($date_from ." 00:00:00");
       else
          unset($_SESSION['from_date']);
 
-      if(isset($to) && !empty($to))
-         $_SESSION['to_date'] = strtotime($to ." 23:59:59");
+      if(isset($date_to) && !empty($date_to))
+         $_SESSION['to_date'] = strtotime($date_to ." 23:59:59");
       else
          unset($_SESSION['to_date']);
 
@@ -2348,32 +2388,31 @@ class PHPFSPOT {
    } // _error()
 
    /**
-    * output calendard input fields
+    * get calendar input-text fields
+    *
+    * this function returns a text-field used for the data selection.
+    * Either it will be filled with the current date or, if available,
+    * filled with the date user entered previously.
+    *
     * @param string $mode
     * @return string
     */
-   private function get_calendar($mode)
+   private function get_date_text_field($mode)
    {
-      $year = isset($_SESSION[$mode .'_date']) ? date("Y", $_SESSION[$mode .'_date']) : date("Y");
-      $month = isset($_SESSION[$mode .'_date']) ? date("m", $_SESSION[$mode .'_date']) : date("m");
-      $day = isset($_SESSION[$mode .'_date']) ? date("d", $_SESSION[$mode .'_date']) : date("d");
+      $date = isset($_SESSION[$mode .'_date']) ? date("Y", $_SESSION[$mode .'_date']) : date("Y");
+      $date.= "-";
+      $date.= isset($_SESSION[$mode .'_date']) ? date("m", $_SESSION[$mode .'_date']) : date("m");
+      $date.= "-";
+      $date.= isset($_SESSION[$mode .'_date']) ? date("d", $_SESSION[$mode .'_date']) : date("d");
 
-      $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 ."\"";
+      $output = "<input type=\"text\" size=\"15\" id=\"date_". $mode ."\" value=\"". $date ."\"";
       if(!isset($_SESSION[$mode .'_date']))
          $output.= " disabled=\"disabled\"";
       $output.= " />\n";
 
       return $output;
 
-   } // get_calendar()
+   } // get_date_text_field()
 
    /**
     * output calendar matrix
@@ -2558,18 +2597,16 @@ class PHPFSPOT {
             $meta = $this->get_meta_informations($orig_path);
          }
 
-         $meta_date = isset($meta['FileDateTime']) ? $meta['FileDateTime'] : filemtime($orig_path);
-
 ?>
   <item>
    <title><?php print htmlspecialchars($this->parse_uri($details['uri'], 'filename')); ?></title>
    <link><?php print htmlspecialchars($orig_url); ?></link>
    <guid><?php print htmlspecialchars($orig_url); ?></guid>
-   <dc:date.Taken><?php print strftime("%Y-%m-%dT%H:%M:%S+00:00", $meta_date); ?></dc:date.Taken>
+   <dc:date.Taken><?php print strftime("%Y-%m-%dT%H:%M:%S+00:00", $details['time']); ?></dc:date.Taken>
    <description>
     <?php print $thumb_html; ?> 
    </description>
-   <pubDate><?php print strftime("%a, %d %b %Y %T %z", $meta_date); ?></pubDate>
+   <pubDate><?php print strftime("%a, %d %b %Y %T %z", $details['time']); ?></pubDate>
   </item>
 <?php
 
@@ -2709,10 +2746,10 @@ class PHPFSPOT {
             return " ORDER BY t.name DESC ,p.time ASC";
             break;
          case 'rate_asc':
-            return " ORDER BY t.name ASC, p.rating ASC";
+            return " ORDER BY p.rating ASC, t.name ASC";
             break;
          case 'rate_desc':
-            return " ORDER BY t.name DESC, p.rating DESC";
+            return " ORDER BY p.rating DESC, t.name ASC";
             break;
       }