some style improvements
[phpfspot.git] / phpfspot.class.php
index 14dff99eb0ed67e4681e97651c4f4b652bbb8505..31a39529776df42465141c2e718b2f8fa3a13fae 100644 (file)
@@ -12,7 +12,6 @@ class PHPFSPOT {
    var $tmpl;
    var $tags;
    var $avail_tags;
-   var $current_tags;
 
    public function __construct()
    {
@@ -55,8 +54,8 @@ class PHPFSPOT {
       $this->tmpl->assign('searchfor', $_SESSION['searchfor']);
       $this->tmpl->assign('page_title', $this->cfg->page_title);
       $this->tmpl->assign('current_condition', $_SESSION['tag_condition']);
-      $this->tmpl->assign('from_date', $this->get_calendar());
-      $this->tmpl->assign('to_date', $this->get_calendar());
+      $this->tmpl->assign('from_date', $this->get_calendar('from'));
+      $this->tmpl->assign('to_date', $this->get_calendar('to'));
 
       switch($_GET['mode']) {
          case 'showpi':
@@ -64,6 +63,14 @@ class PHPFSPOT {
                $_SESSION['selected_tags'] = split(',', $_GET['tags']);
             }
             break;
+         case 'showp':
+            if(isset($_GET['tags'])) {
+               $_SESSION['selected_tags'] = split(',', $_GET['tags']);
+            }
+            if(isset($_GET['id'])) {
+               $_SESSION['current_photo'] = $_GET['id'];
+            }
+            break;
          case 'export':
             $this->tmpl->show("export.tpl");
             return;
@@ -119,18 +126,14 @@ class PHPFSPOT {
 
    } // get_photo_details
 
-   public function getPhotoName($idx)
+   public function getPhotoName($idx, $limit = 0)
    {
       if($details = $this->get_photo_details($idx)) {
-
          $name = $details['name'];
-
-         if(strlen($name) > 15) {
-            $name = substr($name, 0, 10) ."...". substr($name, -10);
+         if($limit != 0 && strlen($name) > $limit) {
+            $name = substr($name, 0, $limit-5) ."...". substr($name, -($limit-5));
          }
-
          return $name;
-   
       }
 
    } // getPhotoName()
@@ -196,6 +199,14 @@ class PHPFSPOT {
       $meta_make = isset($meta['Make']) ? $meta['Make'] ." ". $meta['Model'] : "n/a";
       $meta_size = isset($meta['FileSize']) ? round($meta['FileSize']/1024, 1) ."kbyte" : "n/a";
 
+      $current_tags = $this->getCurrentTags();
+      $extern_link = "index.php?mode=showp&id=". $photo;
+      if($current_tags != "") {
+         $extern_link.= "&tags=". $current_tags;
+      }
+
+      $this->tmpl->assign('extern_link', $extern_link);
+
       if(file_exists($thumb_path)) {
 
          $info = getimagesize($thumb_path);
@@ -328,19 +339,37 @@ class PHPFSPOT {
 
    public function resetTags()
    {
-      unset($_SESSION['selected_tags']);
+      if(isset($_SESSION['selected_tags']))
+         unset($_SESSION['selected_tags']);
 
    } // resetTags()
 
+   public function resetPhotoView()
+   {
+      if(isset($_SESSION['current_photo']))
+         unset($_SESSION['current_photo']);
+
+   } // resetPhotoView();
+
    public function resetTagSearch()
    {
-      unset($_SESSION['searchfor']);
+      if(isset($_SESSION['searchfor']))
+         unset($_SESSION['searchfor']);
 
    } // resetTagSearch()
 
+   public function resetDateSearch()
+   {
+      if(isset($_SESSION['from_date']))
+         unset($_SESSION['from_date']);
+      if(isset($_SESSION['to_date']))
+         unset($_SESSION['to_date']);
+
+   } // resetDateSearch();
+
    public function getPhotoSelection()
    {  
-      $tagged_photos = Array();
+      $matched_photos = Array();
 
       /* return a search result */
       if(isset($_SESSION['searchfor']) && $_SESSION['searchfor'] != '') {
@@ -355,9 +384,9 @@ class PHPFSPOT {
                ORDER BY p.time ASC
          ");
          while($row = $this->db->db_fetch_object($result)) {
-            array_push($tagged_photos, $row['photo_id']);
+            array_push($matched_photos, $row['photo_id']);
          }
-         return $tagged_photos;
+         return $matched_photos;
       }
 
       /* return according the selected tags */
@@ -415,11 +444,31 @@ class PHPFSPOT {
          }
 
          while($row = $this->db->db_fetch_object($result)) {
-            array_push($tagged_photos, $row['photo_id']);
+            array_push($matched_photos, $row['photo_id']);
          }
-         return $tagged_photos;
+         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("
          SELECT DISTINCT photo_id
@@ -429,9 +478,9 @@ class PHPFSPOT {
          ORDER BY p.time ASC
       ");
       while($row = $this->db->db_fetch_object($result)) {
-         array_push($tagged_photos, $row['photo_id']);
+         array_push($matched_photos, $row['photo_id']);
       }
-      return $tagged_photos;
+      return $matched_photos;
 
    } // getPhotoSelection()
 
@@ -482,12 +531,14 @@ class PHPFSPOT {
       $img_width[$rows] = Array();
       $img_id[$rows] = Array();
       $img_name[$rows] = Array();
+      $img_title = Array();
 
       for($i = $begin_with; $i < $end_with; $i++) {
 
          $images[$rows][$cols] = $photos[$i];
          $img_id[$rows][$cols] = $i;
-         $img_name[$rows][$cols] = $this->getPhotoName($photos[$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));
 
          $thumb_path = $this->cfg->base_path ."/thumbs/". $this->cfg->thumb_width ."_". $this->getMD5($photos[$i]);
 
@@ -587,14 +638,9 @@ class PHPFSPOT {
             $this->tmpl->assign('page_selector', $page_select);
       }
 
-      $current_tags = "";
-      if($_SESSION['selected_tags'] != "") {
-         foreach($_SESSION['selected_tags'] as $tag)
-            $current_tags.= $tag .",";
-         $current_tags = substr($current_tags, 0, strlen($current_tags)-1);
-      }
-
-      $extern_link = "http://". $_SERVER['SERVER_NAME'] ."/index.php?mode=showpi";
+      
+      $current_tags = $this->getCurrentTags();
+      $extern_link = "index.php?mode=showpi";
       if($current_tags != "") {
          $extern_link.= "&tags=". $current_tags;
       }
@@ -610,6 +656,7 @@ class PHPFSPOT {
       $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);
 
@@ -620,43 +667,6 @@ class PHPFSPOT {
 
    } // showPhotoIndex()
 
-   public function showBubbleDetails($photo, $direction)
-   {
-      if($direction == "up")
-         $direction = "bubbleimg_up";
-      else
-         $direction = "bubbleimg_down";
-
-      $details = $this->get_photo_details($photo);
-      $orig_path = $this->translate_path($details['directory_path'])  ."/". $details['name'];
-
-      $image_url = "phpfspot_img.php?idx=". $photo ."&amp;width=". $this->cfg->bubble_width;
-
-      $filesize = filesize($orig_path);
-      $filesize = rand($filesize/1024, 2);
-
-      if(!file_exists($orig_path)) {
-         $this->_warning("Photo ". $orig_path ." does not exist!<br />\n");
-         return;
-      }
-      
-      if(!is_readable($orig_path)) {
-         $this->_warning("Photo ". $orig_path ." is not readable for user ". get_current_user() ."<br />\n");
-         return;
-      }
-
-      $img = getimagesize($orig_path);
-
-      $this->tmpl->assign('file_size', $filesize);
-      $this->tmpl->assign('width', $img[0]);
-      $this->tmpl->assign('height', $img[1]);
-      $this->tmpl->assign('file_name', $details['name']);
-      $this->tmpl->assign('image_id', $direction);
-      $this->tmpl->assign('image_url', $image_url);
-      $this->tmpl->show("bubble_details.tpl");
-
-   } // showBubbleDetails()
-
    public function showCredits()
    {
       $this->tmpl->assign('version', $this->cfg->version);
@@ -834,7 +844,6 @@ class PHPFSPOT {
 
       $resolutions = Array(
          $this->cfg->thumb_width,
-         $this->cfg->bubble_width,
          $this->cfg->photo_width,
          $this->cfg->mini_width,
       );
@@ -931,8 +940,16 @@ class PHPFSPOT {
             array_push($_SESSION['selected_tags'], $tag);
       }
 
+      $this->resetDateSearch();
+
    } // startTagSearch()
 
+   public function startDateSearch($from, $to)
+   {
+      $_SESSION['from_date'] = $from;
+      $_SESSION['to_date'] = $to;
+   }
+
    private function rotateImage($img, $degrees)
    {
       if(function_exists("imagerotate"))
@@ -1105,41 +1122,95 @@ class PHPFSPOT {
 
    } // _warning()
 
-   private function get_calendar()
+   private function get_calendar($mode)
    {
-//      require_once CALENDAR_ROOT.'Year.php';
-//      require_once CALENDAR_ROOT.'Month.php';
-//      require_once CALENDAR_ROOT.'Day.php';
+      $output = "<input type=\"text\" size=\"3\" id=\"". $mode ."year\" value=\"". date("Y") ."\" />\n";
+      $output.= "<input type=\"text\" size=\"1\" id=\"". $mode ."month\" value=\"". date("m") ."\" />\n";
+      $output.= "<input type=\"text\" size=\"1\" id=\"". $mode ."day\" value=\"". date("d") ."\" />\n";
+      return $output;
 
-//      $Year = new Calendar_Year();
-//      $Month = new Calendar_Month();
-//      $Day = new Calendar_Day();
+   } // get_calendar()
 
-      $output = "<select name=\"year\">\n";
-      for($year = 1990; $year <= date("Y"); $year++) {
-         $output.= "<option value=\"". $year ."\">". $year ."</option>\n";
-      }
-      $output.= "</select>\n";
+   public function get_calendar_matrix($year = 0, $month = 0, $day = 0)
+   {
+      if (!isset($year)) $year = date('Y');
+      if (!isset($month)) $month = date('m');
+      if (!isset($day)) $day = date('d');
+      $rows = 1;
+      $cols = 1;
+      $matrix = Array();
 
-      $output.= "<select name=\"month\">\n";
-      for($month = 1; $month <= 12; $month++) {
-         $output.= "<option value=\"". $month ."\">". $month ."</option>\n";
-      }
-      $output.= "</select>\n";
+      require_once CALENDAR_ROOT.'Month/Weekdays.php';
+      require_once CALENDAR_ROOT.'Day.php';
+
+
+      // Build the month
+      $month = new Calendar_Month_Weekdays($year,$month);
+
+      // Create links
+      $prevStamp = $month->prevMonth(true);
+      $prev = "javascript:setMonth(". date('Y',$prevStamp) .", ". date('n',$prevStamp) .", ". date('j',$prevStamp) .");";
+      $nextStamp = $month->nextMonth(true);
+      $next = "javascript:setMonth(". date('Y',$nextStamp) .", ". date('n',$nextStamp) .", ". date('j',$nextStamp) .");";
+
+      $selectedDays = array (
+         new Calendar_Day($year,$month,$day),
+         new Calendar_Day($year,12,25),
+      );
+
+      // Build the days in the month
+      $month->build($selectedDays);
+
+      $this->tmpl->assign('current_month', date('F Y',$month->getTimeStamp()));
+      $this->tmpl->assign('prev_month', $prev);
+      $this->tmpl->assign('next_month', $next);
+
+      while ( $day = $month->fetch() ) {
+   
+         if(!isset($matrix[$rows]))
+            $matrix[$rows] = Array();
+
+         $string = "";
+
+         $dayStamp = $day->thisDay(true);
+         $link = "javascript:setCalendarDate(". date('Y',$dayStamp) .", ". date('n',$dayStamp).", ". date('j',$dayStamp) .");";
 
-      $output.= "<select name=\"day\">\n";
-      for($day = 1; $day <= 31; $day++) {
-         $output.= "<option value=\"". $day ."\">". $day ."</option>\n";
+         // isFirst() to find start of week
+         if ( $day->isFirst() )
+            $string.= "<tr>\n";
+
+         if ( $day->isSelected() ) {
+            $string.= "<td class=\"selected\">".$day->thisDay()."</td>\n";
+         } else if ( $day->isEmpty() ) {
+            $string.= "<td>&nbsp;</td>\n";
+         } else {
+            $string.= "<td><a class=\"calendar\" href=\"".$link."\">".$day->thisDay()."</a></td>\n";
+         }
+
+         // isLast() to find end of week
+         if ( $day->isLast() )
+            $string.= "</tr>\n";
+
+         $matrix[$rows][$cols] = $string;
+
+         $cols++;
+
+         if($cols > 7) {
+            $cols = 1;
+            $rows++;
+         }
       }
-      $output.= "</select>\n";
 
-      return $output;
+      $this->tmpl->assign('matrix', $matrix);
+      $this->tmpl->assign('rows', $rows);
+      $this->tmpl->show("calendar.tpl");
 
-   } // get_calendar()
+   } // get_calendar_matrix()
 
    public function getExport($mode)
    {
       $pictures = $this->getPhotoSelection();
+      $current_tags = $this->getCurrentTags();  
 
       if(!isset($_SERVER['HTTPS'])) $protocol = "http";
       else $protocol = "https";
@@ -1148,7 +1219,11 @@ class PHPFSPOT {
 
       foreach($pictures as $picture) {
 
-         $orig_url = $protocol ."://". $server_name . $this->cfg->web_path ."phpfspot_img.php?idx=". $picture ."&width=". $this->cfg->photo_width;
+         $orig_url = $protocol ."://". $server_name . $this->cfg->web_path ."index.php?mode=showp&id=". $picture;
+         if($current_tags != "") {
+            $orig_url.= "&tags=". $current_tags;
+         } 
+
          $thumb_url = $protocol ."://". $server_name . $this->cfg->web_path ."phpfspot_img.php?idx=". $picture ."&width=". $this->cfg->thumb_width;
 
          switch($mode) {
@@ -1160,7 +1235,7 @@ class PHPFSPOT {
                
             case 'MoinMoin':
                // [%pictureurl% %thumbnailurl%]
-               print htmlspecialchars("[$orig_url $thumb_url]") ."<br />\n";
+               print htmlspecialchars(" * [".$orig_url." ".$thumb_url."&fake=1.jpg]") ."<br />\n";
                break;
          }
 
@@ -1168,6 +1243,25 @@ class PHPFSPOT {
 
    } // getExport()
 
+   private function getCurrentTags()
+   {
+      $current_tags = "";
+      if($_SESSION['selected_tags'] != "") {
+         foreach($_SESSION['selected_tags'] as $tag)
+            $current_tags.= $tag .",";
+         $current_tags = substr($current_tags, 0, strlen($current_tags)-1);
+      }
+      return $current_tags;
+
+   } // getCurrentTags()
+
+   public function getCurrentPhoto()
+   {
+      if(isset($_SESSION['current_photo'])) {
+         print $_SESSION['current_photo'];
+      }
+   } // getCurrentPhoto()
+
 }
 
 ?>