All redirects done in Const'r
authorMichael J. Rubinsky <mrubinsk@horde.org>
Tue, 16 Feb 2010 16:28:42 +0000 (11:28 -0500)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Tue, 16 Feb 2010 16:31:07 +0000 (11:31 -0500)
Make sure all redirects Ansel_Views need are done in the Const'r since
most likely headers will have been sent by the time html() is called.
These still need to be cleaned up, probably some of the globals
are gettable from $injector etc... but now at least Ansel views work again.

ansel/lib/View/Base.php
ansel/lib/View/List.php
ansel/lib/View/Results.php
ansel/templates/view/list.inc
ansel/templates/view/results.inc
ansel/view.php

index b71df07..9092fa1 100644 (file)
@@ -35,7 +35,9 @@ abstract class Ansel_View_Base
      * Const'r
      *
      * Any javascript files needed by the (non-api) view should be included
-     * within this method.
+     * within this method. Additionally, any redirects need to be done in the
+     * cont'r since when ::html() is called, headers will have already been
+     * sent.
      *
      * @param array $params  Any parameters that the view might need.
      * <pre>
index abf27c2..55a27c3 100644 (file)
  */
 class Ansel_View_List extends Ansel_View_Base
 {
+    private $_groupby;
+    private $_owner;
+    private $_category;
+    private $_special;
+    private $_page;
+    private $_start;
+    private $_g_perPage;
+    private $_numGalleries;
+    private $_galleryList;
+    private $_sortBy;
+    private $_sortDir;
+
     /**
-     * Const'r - Not much going on here, leave this here for now as a place
-     * to document the additional parameters this View takes.
+     * Const'r
      *
      * @param array $params  Any parameters that the view might need.
      * <pre>
@@ -32,153 +43,150 @@ class Ansel_View_List extends Ansel_View_Base
      */
     public function __construct($params = array())
     {
-        parent::__construct($params);
-    }
+        global $prefs, $ansel_storage, $notification;
 
-    /**
-     * Get this view's title.
-     *
-     * @return string  The gallery's title.
-     */
-    public function getTitle()
-    {
-        return _("Gallery List");
-    }
-
-    /**
-     * Return the HTML representing this view.
-     *
-     * @return string  The HTML.
-     *
-     */
-    public function html()
-    {
-        global $conf, $prefs, $registry, $ansel_storage, $notification;
+        parent::__construct($params);
 
-        // If we aren't supplied with a page number, default to page 0.
-        if (isset($this->_params['page'])) {
-            $page = $this->_params['page'];
-        } else {
-            $page = Horde_Util::getFormData('page', 0);
-        }
-        $galleries_perpage = $prefs->getValue('tilesperpage');
+        // We'll need this in the template.
+        $this->_sortBy = !empty($this->_params['sort']) ? $this->_params['sort'] : 'name';
+        $this->_sortDir = isset($this->_params['sort_dir']) ? $this->_params['sort_dir'] : 0;
 
         // Check for grouping.
         if (empty($this->_params['groupby'])) {
-            $groupby = Horde_Util::getFormData('groupby', $prefs->getValue('groupby'));
+            $this->_groupby = Horde_Util::getFormData('groupby', $prefs->getValue('groupby'));
         } else {
-            $groupby = $this->_params['groupby'];
+            $this->_groupby = $this->_params['groupby'];
         }
 
         if (empty($this->_params['owner'])) {
-            $owner = Horde_Util::getFormData('owner');
-            $owner = empty($owner) ? null : $owner;
+            $this->_owner = Horde_Util::getFormData('owner');
+            $this->_owner = empty($this->_owner) ? null : $this->_owner;
         } else {
-            $owner = $this->_params['owner'];
+            $this->_owner = $this->_params['owner'];
         }
 
-        $special = Horde_Util::getFormData('special');
-
+        $this->_special = Horde_Util::getFormData('special');
         if (empty($this->_params['category'])) {
-            $category = Horde_Util::getFormData('category');
-            $category = empty($category) ? null : $category;
+            $this->_category = Horde_Util::getFormData('category');
+            $this->_category = empty($this->_category) ? null : $this->_category;
         } else {
-            $category = $this->_params['category'];
+            $this->_category = $this->_params['category'];
         }
-        if (!$owner && !$category && !$special && $groupby != 'none' ) {
-            header('Location: ' . Ansel::getUrlFor('group', array('groupby' => $groupby), true));
+        if (!$this->_owner && !$this->_category && !$this->_special && $this->_groupby != 'none' ) {
+            header('Location: ' . Ansel::getUrlFor('group', array('groupby' => $this->_groupby), true));
             exit;
         }
 
-        // We'll need this in the template.
-        $sortby = !empty($this->_params['sort']) ? $this->_params['sort'] : 'name';
-        $sortdir = isset($this->_params['sort_dir']) ? $this->_params['sort_dir'] : 0;
-
+        // If we aren't supplied with a page number, default to page 0.
+        if (isset($this->_params['page'])) {
+            $this->_page = $this->_params['page'];
+        } else {
+            $this->_page = Horde_Util::getFormData('page', 0);
+        }
+        $this->_g_perPage = $prefs->getValue('tilesperpage');
+        
         // If we are calling from the api, we can just pass a list of gallery
         // ids instead of doing grouping stuff.
         if (!empty($this->_params['api']) &&
             !empty($this->_params['gallery_ids']) &&
             count($this->_params['gallery_ids'])) {
 
-            $start = $page * $galleries_perpage;
-            $num_galleries = count($this->_params['gallery_ids']);
-            if ($num_galleries > $start) {
-                $getThese = array_slice($this->_params['gallery_ids'], $start, $galleries_perpage);
+            $this->_start = $this->_page * $this->_g_perPage;
+            $this->_numGalleries = count($this->_params['gallery_ids']);
+            if ($this->_numGalleries > $this->_start) {
+                $getThese = array_slice($this->_params['gallery_ids'], $this->_start, $this->_g_perPage);
                 $try = $ansel_storage->getGalleries($getThese);
-                $gallerylist = array();
+                $this->_galleryList = array();
                 foreach ($try as $id => $gallery) {
                     if ($gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::SHOW)) {
-                        $gallerylist[$id] = $gallery;
+                        $this->_galleryList[$id] = $gallery;
                     }
                 }
             } else {
-                $gallerylist = array();
+                $this->_galleryList = array();
             }
         } else {
             // Set list filter/title
             $filter = array();
-            if (!is_null($owner)) {
-                $filter['owner'] = $owner;
+            if (!is_null($this->_owner)) {
+                $filter['owner'] = $this->_owner;
             }
 
-            if (!is_null($category)) {
-                $filter['category'] = $category;
+            if (!is_null($this->_category)) {
+                $filter['category'] = $this->_category;
             }
 
-            if ($owner) {
-                if ($owner == Horde_Auth::getAuth() && empty($this->_params['api'])) {
-                    $list_title = _("My Galleries");
-                } elseif (!empty($GLOBALS['conf']['gallery']['customlabel'])) {
-                    $uprefs = Horde_Prefs::singleton($GLOBALS['conf']['prefs']['driver'],
-                                                'ansel', $owner, '', null, false);
-                    $fullname = $uprefs->getValue('grouptitle');
-                    if (!$fullname) {
-                        $identity = Horde_Prefs_Identity::singleton('none', $owner);
-                        $fullname = $identity->getValue('fullname');
-                        if (!$fullname) {
-                            $fullname = $owner;
-                        }
-                        $list_title = sprintf(_("%s's Galleries"), $fullname);
-                    } else {
-                        $list_title = $fullname;
-                    }
-                } else {
-                    $list_title = sprintf(_("%s's Galleries"), $owner);
-                }
-            } elseif ($category || ($groupby == 'category' && $special)) {
-                if ($special == 'unfiled') {
-                    $list_title = sprintf(_("Galleries in category \"%s\""),
-                                          _("Unfiled"));
-                    $filter['category'] = '';
-                } else {
-                    $list_title = sprintf(_("Galleries in category \"%s\""), $category);
-                }
-            } else {
-                $list_title = _("Gallery List");
-            }
-
-            $num_galleries = $ansel_storage->countGalleries(
+            $this->_numGalleries = $ansel_storage->countGalleries(
                 Horde_Auth::getAuth(), Horde_Perms::SHOW, $filter, null, false);
-            if (is_a($num_galleries, 'PEAR_Error')) {
-                return $num_galleries->getMessage();
+            if (is_a($this->_numGalleries, 'PEAR_Error')) {
+                return $this->_numGalleries->getMessage();
             }
 
-            if ($num_galleries == 0 && empty($this->_params['api'])) {
-                if ($owner && $filter == $owner && $owner == Horde_Auth::getAuth()) {
+            if ($this->_numGalleries == 0 && empty($this->_params['api'])) {
+                if ($this->_owner && $filter == $this->_owner && $this->_owner == Horde_Auth::getAuth()) {
                     $notification->push(_("You have no photo galleries, add one!"),
                                         'horde.message');
                     header('Location: ' . Horde_Util::addParameter(Horde::applicationUrl('gallery.php'), 'actionID', 'add'));
                     exit;
                 }
                 $notification->push(_("There are no photo galleries available."), 'horde.message');
-                $gallerylist = array();
+                $this->_galleryList = array();
             } else {
-                $gallerylist = $ansel_storage->listGalleries(
-                    Horde_Perms::SHOW, $filter, null, false, $page * $galleries_perpage,
-                    $galleries_perpage, $sortby, $sortdir);
+                $this->_galleryList = $ansel_storage->listGalleries(
+                    Horde_Perms::SHOW, $filter, null, false, $this->_page * $this->_g_perPage,
+                    $this->_g_perPage, $this->_sortBy, $this->_sortDir);
             }
 
         }
+    }
+
+    /**
+     * Get this view's title.
+     *
+     * @return string  The gallery's title.
+     */
+    public function getTitle()
+    {
+        if ($this->_owner) {
+            if ($this->_owner == Horde_Auth::getAuth() && empty($this->_params['api'])) {
+                return  _("My Galleries");
+            } elseif (!empty($GLOBALS['conf']['gallery']['customlabel'])) {
+                $uprefs = Horde_Prefs::singleton($GLOBALS['conf']['prefs']['driver'],
+                                            'ansel', $this->_owner, '', null, false);
+                $fullname = $uprefs->getValue('grouptitle');
+                if (!$fullname) {
+                    $identity = Horde_Prefs_Identity::singleton('none', $this->_owner);
+                    $fullname = $identity->getValue('fullname');
+                    if (!$fullname) {
+                        $fullname = $this->_owner;
+                    }
+                    return sprintf(_("%s's Galleries"), $fullname);
+                } else {
+                    return $fullname;
+                }
+            } else {
+                return sprintf(_("%s's Galleries"), $this->_owner);
+            }
+        } elseif ($this->_category || ($this->_groupby == 'category' && $this->_special)) {
+            if ($this->_special == 'unfiled') {
+                return sprintf(_("Galleries in category \"%s\""), _("Unfiled"));
+            } else {
+                return sprintf(_("Galleries in category \"%s\""), $this->_category);
+            }
+        } else {
+            return _("Gallery List");
+        }
+    }
+
+    /**
+     * Return the HTML representing this view.
+     *
+     * @return string  The HTML.
+     *
+     */
+    public function html()
+    {
+        global $conf, $prefs, $registry, $ansel_storage, $notification;
 
         $vars = Horde_Variables::getDefaultVariables();
         if (!empty($this->_params['page'])) {
@@ -186,53 +194,53 @@ class Ansel_View_List extends Ansel_View_Base
         }
 
         if (!empty($this->_params['pager_url'])) {
-            $pagerurl = $this->_params['pager_url'];
+            $this->_pagerurl = $this->_params['pager_url'];
             $override = true;
         } else {
             $override = false;
-            $pagerurl = Ansel::getUrlFor('view',
-                                    array('owner' => $owner,
-                                          'category' => $category,
-                                          'special' => $special,
-                                          'groupby' => $groupby,
+            $this->_pagerurl = Ansel::getUrlFor('view',
+                                    array('owner' => $this->_owner,
+                                          'category' => $this->_category,
+                                          'special' => $this->_special,
+                                          'groupby' => $this->_groupby,
                                           'view' => 'List'));
         }
-        $p_params = array('num' => $num_galleries,
-                          'url' => $pagerurl,
-                          'perpage' => $galleries_perpage);
+        $p_params = array('num' => $this->_numGalleries,
+                          'url' => $this->_pagerurl,
+                          'perpage' => $this->_g_perPage);
 
         if ($override) {
             $p_params['url_callback'] = null;
         }
-        $pager = new Horde_Ui_Pager('page', $vars, $p_params);
-        $preserve = array('sort_dir' => $sortdir);
-        if (!empty($sortby)) {
-            $preserve['sort'] = $sortby;
+        $this->_pager = new Horde_Ui_Pager('page', $vars, $p_params);
+        $preserve = array('sort_dir' => $this->_sortDir);
+        if (!empty($this->_sortBy)) {
+            $preserve['sort'] = $this->_sortBy;
         }
-        $pager->preserve($preserve);
+        $this->_pager->preserve($preserve);
 
-        if ($num_galleries) {
-            $min = $page * $galleries_perpage;
-            $max = $min + $galleries_perpage;
-            if ($max > $num_galleries) {
-                $max = $num_galleries - $min;
+        if ($this->_numGalleries) {
+            $min = $this->_page * $this->_g_perPage;
+            $max = $min + $this->_g_perPage;
+            if ($max > $this->_numGalleries) {
+                $max = $this->_numGalleries - $min;
             }
-            $start = $min + 1;
-            $end = min($num_galleries, $min + $galleries_perpage);
+            $this->_start = $min + 1;
+            $end = min($this->_numGalleries, $min + $this->_g_perPage);
 
-            if ($owner) {
+            if ($this->_owner) {
                 $refresh_link = Ansel::getUrlFor('view',
-                                                 array('groupby' => $groupby,
-                                                       'owner' => $owner,
-                                                       'page' => $page,
+                                                 array('groupby' => $this->_groupby,
+                                                       'owner' => $this->_owner,
+                                                       'page' => $this->_page,
                                                        'view' => 'List'));
 
             } else {
                 $refresh_link = Ansel::getUrlFor('view',
                                                  array('view' => 'List',
-                                                       'groupby' => $groupby,
-                                                       'page' => $page,
-                                                       'category' => $category));
+                                                       'groupby' => $this->_groupby,
+                                                       'page' => $this->_page,
+                                                       'category' => $this->_category));
             }
 
             // Get top-level / default gallery style.
index 46d186f..1567d52 100644 (file)
@@ -22,6 +22,9 @@ class Ansel_View_Results extends Ansel_View_Base
      */
     protected $_owner;
 
+    private $_page;
+    private $_perPage;
+
     /**
      * Contructor - just set some instance variables.
      *
@@ -29,39 +32,18 @@ class Ansel_View_Results extends Ansel_View_Base
      */
     public function __construct()
     {
+        global $prefs, $conf, $ansel_storage;
+
         $this->_owner = Horde_Util::getFormData('owner', null);
         $this->_search = Ansel_Tags::getSearch(null, $this->_owner);
-    }
-
-    /**
-     * Return the title for this view.
-     *
-     * @return string The title for this view.
-     */
-    public function getTitle()
-    {
-        return (!empty($this->_owner))
-                ? sprintf(_("Searching %s's photos tagged: "), $this->_owner)
-                : _("Searching all photos tagged: ");
-    }
-
-    /**
-     * Get the HTML representing this view.
-     *
-     * @return string  The HTML
-     */
-    public function html()
-    {
-        global $conf, $prefs, $registry, $ansel_storage;
+        $this->_page = Horde_Util::getFormData('page', 0);
 
-        $page = Horde_Util::getFormData('page', 0);
         $action = Horde_Util::getFormData('actionID', '');
         $image_id = Horde_Util::getFormData('image');
-
         $vars = Horde_Variables::getDefaultVariables();
 
         // Number perpage from prefs or config.
-        $perpage = min($prefs->getValue('tilesperpage'),
+        $this->_perPage = min($prefs->getValue('tilesperpage'),
                        $conf['thumbnail']['perpage']);
 
         switch ($action) {
@@ -225,14 +207,37 @@ class Ansel_View_Results extends Ansel_View_Base
             header('Location: ' . Horde::applicationUrl('browse.php', true));
             exit;
         }
+    }
+
+    /**
+     * Return the title for this view.
+     *
+     * @return string The title for this view.
+     */
+    public function getTitle()
+    {
+        return (!empty($this->_owner))
+                ? sprintf(_("Searching %s's photos tagged: "), $this->_owner)
+                : _("Searching all photos tagged: ");
+    }
+
+    /**
+     * Get the HTML representing this view.
+     *
+     * @return string  The HTML
+     */
+    public function html()
+    {
+        global $conf, $prefs, $registry, $ansel_storage;
 
         // Get the slice of galleries/images to view on this page.
-        $results = $this->_search->getSlice($page, $perpage);
+        $results = $this->_search->getSlice($this->_page, $this->_perPage);
         $total = $this->_search->count();
         $total = $total['galleries'] + $total['images'];
 
         // The number of resources to display on this page.
         $numimages = count($results);
+        $tilesperrow = $prefs->getValue('tilesperrow');
 
         // Get any related tags to display.
         if ($conf['tags']['relatedtags']) {
@@ -263,11 +268,11 @@ class Ansel_View_Results extends Ansel_View_Base
         $option_move = $option_copy = $ansel_storage->countGalleries(Horde_Perms::EDIT);
 
 
-        $pagestart = ($page * $perpage) + 1;
-        $pageend = min($pagestart + $numimages - 1, $pagestart + $perpage - 1);
-        $pager = new Horde_Ui_Pager('page', $vars, array('num' => $total,
+        $this->_pagestart = ($this->_page * $this->_perPage) + 1;
+        $this->_pageend = min($this->_pagestart + $numimages - 1, $this->_pagestart + $this->_perPage - 1);
+        $this->_pager = new Horde_Ui_Pager('page', $vars, array('num' => $total,
                                                          'url' => $viewurl,
-                                                         'perpage' => $perpage));
+                                                         'perpage' => $this->_perPage));
         ob_start();
         include ANSEL_TEMPLATES . '/view/results.inc';
         return ob_get_clean();
index 5c187a6..d65f9d8 100644 (file)
@@ -1,12 +1,12 @@
 <div id="listHeader" class="header">
  <span id="listCounts" class="rightFloat">
-   <?php if ($galleries_perpage < $num_galleries) printf(_("%d to %d of %d Galleries"), $start, $end, $num_galleries); else printf(ngettext("%d Gallery", "%d Galleries", $num_galleries), $num_galleries) ?>
+   <?php if ($this->_g_perPage < $this->_numGalleries) printf(_("%d to %d of %d Galleries"), $this->_start, $end, $this->_numGalleries); else printf(ngettext("%d Gallery", "%d Galleries", $this->_numGalleries), $this->_numGalleries) ?>
  </span>
  <?php if (empty($this->_params['api'])) echo Ansel::getBreadcrumbs() . ' ' . Horde::link(Horde::applicationUrl($refresh_link), _("Refresh List")) . Horde::img('reload.png', _("Refresh List"), null, $registry->getImageDir('horde')) . '</a>' ?>
 </div>
 <?php if (empty($this->_params['api'])): ?>
 <div class="anselActions widget">
-  <?php if ($groupby == 'none'): ?>
+  <?php if ($this->_groupby == 'none'): ?>
   <div class="rightFloat">
     <?php echo _("Group by:") . ' '
        . Horde::link(Ansel::getUrlFor('group', array('actionID' => 'groupby', 'groupby' => 'owner')))
       . _("Date") . '</a> | '
       . Horde::link(Horde::applicationUrl(Horde_Util::addParameter($refresh_link, 'sort', 'owner')))
       . _("Owner") . '</a>';
-  if ($sortdir) {
-      echo Horde::link(Horde::applicationUrl(Horde_Util::addParameter($refresh_link, array('sort' => $sortby, 'sort_dir' => 0))))
+  if ($this->_sortDir) {
+      echo Horde::link(Horde::applicationUrl(Horde_Util::addParameter($refresh_link, array('sort' => $this->_sortBy, 'sort_dir' => 0))))
           . Horde::img('za.png', _("Ascending"), '', $registry->getImageDir('horde'))
           . '</a>';
   } else {
-      echo Horde::link(Horde::applicationUrl(Horde_Util::addParameter($refresh_link, array('sort' => $sortby, 'sort_dir' => 1))))
+      echo Horde::link(Horde::applicationUrl(Horde_Util::addParameter($refresh_link, array('sort' => $this->_sortBy, 'sort_dir' => 1))))
           . Horde::img('az.png', _("Descending"), '', $registry->getImageDir('horde'))
           . '</a>';
    }
 <div style="float:right;width:25%;"><?php echo $this->renderWidgets() ?></div>
 <div style="float:right:width:73%;">
 <?php endif; ?>
-<?php echo $pager->render() ?>
+<?php echo $this->_pager->render() ?>
 <table width="<?php echo ($this->countWidgets() ? 73 : 100) ?>%" style="background-color:<?php echo $style['background'] ?>;">
  <tr>
  <?php
  $tilesperrow = $prefs->getValue('tilesperrow');
  $cellwidth = round(100 / $tilesperrow);
  $count = 0;
- foreach ($gallerylist as $galleryId => $gallery) {
+ foreach ($this->_galleryList as $galleryId => $gallery) {
      echo '<td width="' . $width . '%" class="ansel-tile">' . $gallery->getTile(null, $style['name'], false, $this->_params) . '</td>';
      if (!(++$count % $prefs->getValue('tilesperrow'))) {
-         if ($count < $num_galleries) {
+         if ($count < $this->_numGalleries) {
              echo '</tr><tr>';
          }
      }
@@ -59,7 +59,7 @@
  </tr>
  <tr>
    <td align="center" colspan="<?php echo $tilesperrow?>">
-     <?php echo $pager->render() ?>
+     <?php echo $this->_pager->render() ?>
    </td>
  </tr>
 </table>
index 3cd88e9..fdcdc92 100644 (file)
@@ -64,7 +64,7 @@ function copySelected()
 /* Build link, if needed, for browsing all users' results */
 $allLink = (!empty($this->_owner)) ? Horde::link(Horde_Util::addParameter(Horde::selfUrl(), array('view' => 'Results')), _("View Results from All Users")) . ' (' . _("View All Results") . ')</a>' : '';
 if ($total) {
-    echo '<span class="rightFloat">' . ($total > $perpage ? sprintf(_("%d to %d of %d Items"), $pagestart, $pageend, $total) : sprintf(ngettext("%d Item", "%d Items", $total), $total)) . '<small>' . $allLink . '</small></span>';
+    echo '<span class="rightFloat">' . ($total > $this->_perPage ? sprintf(_("%d to %d of %d Items"), $this->_pagestart, $this->_pageend, $total) : sprintf(ngettext("%d Item", "%d Items", $total), $total)) . '<small>' . $allLink . '</small></span>';
 }
 echo htmlspecialchars($this->getTitle(), ENT_COMPAT, Horde_Nls::getCharset()) . $this->_search->getTagTrail();
 ?>
@@ -98,10 +98,9 @@ echo htmlspecialchars($this->getTitle(), ENT_COMPAT, Horde_Nls::getCharset()) .
     <?php if (!$total): ?>
      <div class="text"><em><?php echo _("There are no photos in this gallery.") ?></em></div>
     <?php else: ?>
-     <?php echo $pager->render() ?>
+     <?php echo $this->_pager->render() ?>
      <table width="100%" style="background-color:<?php echo $styleDef['background'] ?>;"><tr>
       <?php
-        $tilesperrow = $prefs->getValue('tilesperrow');
         $cellwidth = round(100 / $tilesperrow);
         $count = 0;
         foreach ($results as $result) {
@@ -118,7 +117,7 @@ echo htmlspecialchars($this->getTitle(), ENT_COMPAT, Horde_Nls::getCharset()) .
         } ?>
       </tr>
      </table>
-     <?php echo $pager->render() ?>
+     <?php echo $this->_pager->render() ?>
     <?php endif; ?>
    </td>
    <td width="20%" valign="top">
index 1740fbb..5c6b653 100644 (file)
@@ -46,7 +46,7 @@ try {
 
 $title = $view->getTitle();
 require ANSEL_TEMPLATES . '/common-header.inc';
-$view_html = $view->html();
 require ANSEL_TEMPLATES . '/menu.inc';
+$view_html = $view->html();
 echo $view_html;
 require $registry->get('templates', 'horde') . '/common-footer.inc';