From 5d397db3e6ff4dc02bf989bb64945e990d2c9da2 Mon Sep 17 00:00:00 2001
From: Andreas Unterkircher <unki@netshadow.at>
Date: Sun, 1 Jul 2007 10:12:10 +0000
Subject: issue20, jumping back to the right multipage is now possible

git-svn-id: file:///var/lib/svn/phpfspot/trunk@155 fa6a889d-dae6-447d-9e79-4ba9a3039384
---
 phpfspot.class.php         | 50 ++++++++++++++++++++++++++++++++++++----------
 templates/single_photo.tpl |  2 +-
 2 files changed, 41 insertions(+), 11 deletions(-)

diff --git a/phpfspot.class.php b/phpfspot.class.php
index f0e2fb2..980dc81 100644
--- a/phpfspot.class.php
+++ b/phpfspot.class.php
@@ -39,6 +39,10 @@ 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)
+         unset($_SESSION['begin_with']);
+
    } // __construct()
 
    public function __destruct()
@@ -117,19 +121,24 @@ class PHPFSPOT {
    public function showPhoto($photo)
    {
       $all_photos = $this->getPhotoSelection();
+      $count = count($all_photos);
 
-      foreach($all_photos as $all_photo) {
+      for($i = 0; $i < $count; $i++) {
          
          if($get_next) {
-            $next_img = $all_photo;
+            $next_img = $all_photos[$i];
             break;
          }
 
-         if($all_photo == $photo) {
+         if($all_photos[$i] == $photo) {
             $get_next = 1;
          }
          else {
-            $previous_img = $all_photo;
+            $previous_img = $all_photos[$i];
+         }
+
+         if($photo == $all_photos[$i]) {
+               $current = $i;
          }
       }
 
@@ -175,6 +184,7 @@ class PHPFSPOT {
          $this->tmpl->assign('image_url_full', 'phpfspot_img.php?idx='. $photo);
 
          $this->tmpl->assign('tags', $this->get_photo_tags($photo));
+         $this->tmpl->assign('current', $current);
       }
       else {
          $this->_warning("Can't open file ". $thumb_path ."\n");
@@ -389,15 +399,35 @@ class PHPFSPOT {
 
       $count = count($photos);
 
-      if(!$_SESSION['begin_with'] || $_SESSION['begin_with'] == 0)
-         $begin_with = 0;
-      else
-         $begin_with = $_SESSION['begin_with'];
+      if(!isset($this->cfg->rows_per_page) || $this->cfg->rows_per_page == 0) {
 
-      if($this->cfg->rows_per_page == 0)
+         $begin_with = 0;
          $end_with = $count;
-      else
+
+      }
+      elseif($this->cfg->rows_per_page > 0) {
+
+         if(!$_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);
+      }
 
    
       $rows = 0;
diff --git a/templates/single_photo.tpl b/templates/single_photo.tpl
index 536f1db..b93803f 100644
--- a/templates/single_photo.tpl
+++ b/templates/single_photo.tpl
@@ -38,7 +38,7 @@
       { /if }
      </td>
      <td style="text-align: center; width: 33%;">
-      <a href="javascript:showPhotoIndex();">
+      <a href="javascript:showPhotoIndex({$current});">
        <img src="resources/arrow_up.png" alt="photo index" />
       </a>
      </td>
-- 
cgit v1.2.3-18-g5258