issue115, display tag-selection in photo-index
authorAndreas Unterkircher <unki@netshadow.at>
Sat, 26 Apr 2008 08:18:50 +0000 (10:18 +0200)
committerAndreas Unterkircher <unki@netshadow.at>
Sun, 27 Apr 2008 12:20:25 +0000 (14:20 +0200)
Signed-off-by: Andreas Unterkircher <unki@netshadow.at>
phpfspot.class.php
phpfspot_img.php
themes/default/stylesheet.css
themes/default/templates/photo_index.tpl

index bae4ae74dbc40ce34899bb4270397a3860000abf..97deb968dacb5b6b98dd967d73a379480f4bf21d 100644 (file)
@@ -726,7 +726,7 @@ class PHPFSPOT {
     * session-variable $_SESSION['selected_tags']
     * @return string
     */
-   public function getSelectedTags()
+   public function getSelectedTags($type = 'link')
    {
       /* retrive tags from database */
       $this->get_tags();
@@ -737,7 +737,29 @@ class PHPFSPOT {
       {
          // return all selected tags
          if(isset($_SESSION['selected_tags']) && in_array($tag, $_SESSION['selected_tags'])) {
-            $output.= "<a href=\"javascript:Tags('del', ". $tag .");\" class=\"tag\">". $this->tags[$tag] ."</a>, ";
+
+            switch($type) {
+               default:
+               case 'link':
+                  $output.= "<a href=\"javascript:Tags('del', ". $tag .");\" class=\"tag\">". $this->tags[$tag] ."</a>, ";
+                  break;
+               case 'img':
+                  $output.= "
+                  <div style=\"display: table-cell;\">
+                   <div style=\"display: table-row; text-align: center;\">
+                    <a href=\"javascript:Tags('del', ". $tag .");\" title=\"". $this->tags[$tag] ."\">
+                     <img src=\"phpfspot_img.php?tagidx=". $tag ."\" />
+                    </a>
+                   </div>
+                   <div style=\"display: table-row; text-align: center;\">
+                    <a href=\"javascript:Tags('del', ". $tag .");\" title=\"". $this->tags[$tag] ."\">
+                     <img src=\"resources/underbar.png\" />
+                    </a>
+                   </div>
+                  </div>
+                  ";
+                  break;
+            }
          }
       }
 
@@ -1315,6 +1337,7 @@ class PHPFSPOT {
       $this->tmpl->assign('img_fullname', $img_fullname);
       $this->tmpl->assign('img_title', $img_title);
       $this->tmpl->assign('thumbs', $thumbs);
+      $this->tmpl->assign('selected_tags', $this->getSelectedTags('img'));
 
       $this->tmpl->show("photo_index.tpl");
 
@@ -1634,6 +1657,7 @@ class PHPFSPOT {
          $this->cfg->thumb_width,
          $this->cfg->photo_width,
          $this->cfg->mini_width,
+         30,
       );
 
       /* get details from F-Spot's database */
@@ -2563,6 +2587,56 @@ class PHPFSPOT {
 
    } // get_random_photo()
 
+   /**
+    * get random photo tag photo
+    *
+    * this function will get all photos tagged with the requested
+    * tag 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.
+    * @return array
+    */
+   public function get_random_tag_photo($tagidx)
+   {
+      $all = Array();
+
+      $query_str = "
+         SELECT p.id
+         FROM photos p
+         INNER JOIN photo_tags pt
+            ON p.id=pt.photo_id
+      ";
+
+      if(isset($this->cfg->show_tags) && !empty($this->cfg->show_tags)) {
+         $query_str.= "
+            INNER JOIN tags t
+               ON pt.tag_id=t.id
+         ";
+      }
+      $query_str.= "
+         WHERE
+            pt.tag_id LIKE '". $tagidx ."'
+      ";
+
+      if(isset($this->cfg->show_tags) && !empty($this->cfg->show_tags)) {
+         $query_str.= "
+           AND
+               t.name IN ('".implode("','",$this->cfg->show_tags)."')
+         ";
+      }
+
+      $result = $this->db->db_query($query_str);
+
+      while($row = $this->db->db_fetch_object($result)) {
+         array_push($all, $row['id']);
+      }
+
+      return $all[array_rand($all)];
+
+   } // get_random_tag_photo()
+
    /**
     * validates provided date
     *
index bc8d3d11badc4b22067eba044e8c15deaf50c8f4..3599b2b3a89dbf30e7406e389973df148a5d516f 100644 (file)
@@ -64,7 +64,7 @@ class PHPFSPOT_IMG {
     * @param integer $idx
     * @param integer $width
     */
-   public function show($idx, $width = 0)
+   public function showImg($idx, $width = 0)
    {
       if($idx == 'rand')
          $idx = $this->parent->get_random_photo();
@@ -118,12 +118,76 @@ class PHPFSPOT_IMG {
       Header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
       Header("Cache-Control: no-cache");
       Header("Pragma: no-cache");
-         
+
+      $file = fopen($fullpath, "rb");
+      fpassthru($file);
+      @fclose($file);
+
+   } // showImg()
+
+   /**
+    * sends a random photo of the requested tag to the browser
+    *
+    * this function will send a random photo to the client.
+    * It is selected out by the provided $tagidx. It also try's
+    * to create on-the-fly missing thumbnails via PHPFSPOT
+    * gen_thumbs function.
+    * @param integer $idx
+    */
+   public function showTagImg($tagidx)
+   {
+      $idx = $this->parent->get_random_tag_photo($tagidx);
+      $width = 30;
+
+      $details = $this->parent->get_photo_details($idx);
+
+      if(!$details) {
+         $this->parent->showTextImage("The image (". $idx .") you requested is unknown");
+         return;
+      }
+
+      /* if no entry for this photo is yet in the database, create thumb */
+      if(!$this->parent->getMD5($idx)) {
+         $this->parent->gen_thumb($idx);
+      }
+      $fullpath = $this->parent->get_thumb_path($width, $idx);
+      /* if the thumb file does not exist, create it */
+      if(!file_exists($fullpath)) {
+         $this->parent->gen_thumb($idx);
+      }
+
+      if(!file_exists($fullpath)) {
+         $this->parent->showTextImage("File ". basename($fullpath) ." does not exist");
+         return;
+      }
+      if(!is_readable($fullpath)) {
+         $this->parent->showTextImage("File ". basename($fullpath) ." is not readable. Check the permissions");
+         return;
+      }
+
+      $mime = $this->parent->get_mime_info($fullpath);
+
+      if(!$this->parent->checkifImageSupported($mime)) {
+         $this->parent->showTextImage("Unsupported Image Type");
+         return;
+      }
+
+      Header("Content-Type: ". $mime);
+      Header("Content-Length: ". filesize($fullpath));
+      Header("Content-Transfer-Encoding: binary\n");
+      Header("Content-Disposition: inline; filename=\"". $this->parent->parse_uri($details['uri'], 'filename') ."\"");
+      Header("Content-Description: ". $this->parent->parse_uri($details['uri'], 'filename'));
+      Header("Accept-Ranges: bytes");
+      Header("Connection: close");
+      Header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
+      Header("Cache-Control: no-cache");
+      Header("Pragma: no-cache");
+
       $file = fopen($fullpath, "rb");
       fpassthru($file);
       @fclose($file);
 
-   } // show()
+   } // showTagImg()
 
 } // PHPFSPOT_IMG()
 
@@ -132,9 +196,21 @@ if(isset($_GET['idx']) && (is_numeric($_GET['idx']) || $_GET['idx'] == 'rand'))
    $img = new PHPFSPOT_IMG;
 
    if(isset($_GET['width']) && is_numeric($_GET['width'])) 
-      $img->show($_GET['idx'], $_GET['width']);
+      $img->showImg($_GET['idx'], $_GET['width']);
    else
-      $img->show($_GET['idx']);
+      $img->showImg($_GET['idx']);
+
+   exit(0);
 }
 
+if(isset($_GET['tagidx']) && is_numeric($_GET['tagidx'])) {
+
+   $img = new PHPFSPOT_IMG;
+   $img->showTagImg($_GET['tagidx']);
+
+   exit(0);
+
+}
+
+
 ?>
index e9a505c7ec5b1368dde502ecdb9c4d9ae5e4ad53..7771736f1d10a466a693aaea33c7a7c7b6a860f5 100644 (file)
@@ -83,6 +83,13 @@ div.header {
    white-space:            nowrap;
 }
 
+div.subheader {
+   background-color:       #eeeeee;
+   padding:                5px 10px 5px 10px;
+   vertical-align:         middle;
+   white-space:            nowrap;
+}
+
 img {
    border:                none;
 }
index 82f49596576234d13ee386c1914415c3f7261eed..c01c6afc4a4190619a5a5572c0ff38eef655fc9f 100644 (file)
@@ -26,7 +26,7 @@
    Results are limited to a date within {$from_date} to {$to_date}.
   {/if}
  </div>
- <div style="text-align: right">
+<div style="text-align: right">
   {if $slideshow_link }
    <a href="{$slideshow_link}" title="Slideshow" target="_blank"><img src="resources/slideshow.png" /></a>
   {/if}
   {/if}
  </div>
 </div>
+{ if $tag_result }
+<div style="height: 1px; background-color: #ffffff;"></div>
+<div class="subheader">
+ <div style="display: table-cell; vertical-align: middle;">
+ Tags:
+ </div>
+ { $selected_tags }
+</div>
+{ /if }
 
 <div id="index">