Horde 4 refactoring for GalleryRenderer objects
authorMichael J. Rubinsky <mrubinsk@horde.org>
Fri, 31 Jul 2009 17:02:05 +0000 (13:02 -0400)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Fri, 31 Jul 2009 17:02:05 +0000 (13:02 -0400)
ansel/lib/View/GalleryRenderer.php [deleted file]
ansel/lib/View/GalleryRenderer/Base.php [new file with mode: 0644]
ansel/lib/View/GalleryRenderer/Gallery.php
ansel/lib/View/GalleryRenderer/GalleryLightbox.php

diff --git a/ansel/lib/View/GalleryRenderer.php b/ansel/lib/View/GalleryRenderer.php
deleted file mode 100644 (file)
index 3056fd4..0000000
+++ /dev/null
@@ -1,219 +0,0 @@
-<?php
-/**
- * Ansel_View_GalleryRenderer::  Base class for all gallery renderers.
- *
- * Copyright 2008-2009 The Horde Project (http://www.horde.org/)
- *
- * See the enclosed file COPYING for license information (GPL). If you
- * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
- *
- * @author  Michael J. Rubinsky <mrubinsk@horde.org>
- * @package Ansel
- */
-class Ansel_View_GalleryRenderer {
-
-    /**
-     * The Ansel_View_Gallery object that this Renderer belongs to.
-     *
-     * @var Ansel_View_Gallery
-     */
-    var $view;
-
-    /**
-     * The gallery id for this view's gallery
-     *
-     * @var integer
-     */
-    var $galleryId;
-
-    /**
-     * Gallery slug for current gallery.
-     *
-     * @var string
-     */
-    var $gallerySlug;
-
-    /**
-     * The current page we are viewing
-     *
-     * @var integer
-     */
-    var $page = 0;
-
-    /**
-     * The display mode of the current gallery.
-     * 0 == Normal
-     * 1 == Group by date
-     *
-     * @var integer
-     */
-    var $mode;
-
-    /**
-     * The style definition array for this gallery.
-     *
-     * @var array
-     */
-    var $style;
-
-    /**
-     * Holds number of tiles to display per page
-     *
-     * @var integer
-     */
-    var $perpage;
-
-    /**
-     * The tile number we are starting with on the current page.
-     *
-     * @var integer
-     */
-    var $pagestart;
-
-    /**
-     * The last tile number on the current page.
-     *
-     * @var integer
-     */
-    var $pageend;
-
-    /**
-     * The total number of tiles that this view contains
-     *
-     * @var integer
-     */
-    var $numTiles;
-
-    /**
-     * The Ansel_Image or Ansel_DateGallery objects that appear on the current
-     * page in the current view.
-     *
-     * @var array of Ansel_Image or Ansel_DateGallery objects.
-     */
-    var $children;
-
-    /**
-     * If we are grouping by date, this holds the currently selected date parts.
-     *
-     * @var array containing sufficient date parts for the current depth.
-     */
-    var $date = array();
-
-    /**
-     * Constructor
-     *
-     * @param Ansel_View_Gallery  The view object for this renderer.
-     *
-     * @return Ansel_View_Renderer_Gallery
-     */
-    function Ansel_View_GalleryRenderer($view)
-    {
-        $this->view = $view;
-    }
-
-    /**
-     * Initialize the renderer. This *must* be called before any attempt is made
-     * to display or otherwise interact with the renderer.
-     *
-     */
-    function init()
-    {
-        global $prefs, $conf;
-
-        $this->galleryId = $this->view->gallery->id;
-        $this->gallerySlug = $this->view->gallery->get('slug');
-        if (isset($this->view->_params['page'])) {
-            $this->page = $this->view->_params['page'];
-        }
-
-        /* Number perpage from prefs or config */
-        $this->perpage = min($prefs->getValue('tilesperpage'),
-                             $conf['thumbnail']['perpage']);
-
-        /* Calculate the starting and ending images on this page */
-        $this->pagestart = ($this->page * $this->perpage) + 1;
-
-        /* Fetch the children */
-        $this->fetchChildren($this->view->_params['force_grouping']);
-
-        /* Do we have an explicit style set? If not, use the gallery's */
-        if (!empty($this->view->_params['style'])) {
-            $this->style = Ansel::getStyleDefinition($this->view->_params['style']);
-        } else {
-            $this->style = $this->view->gallery->getStyle();
-        }
-
-        /* Include any widgets */
-        if (!empty($this->style['widgets'])) {
-            /* Special case widgets - these are built in */
-            if (array_key_exists('Actions', $this->style['widgets'])) {
-                /* Don't show action widget if no actions */
-                if (Horde_Auth::getAuth() ||
-                    !empty($conf['report_content']['driver']) &&
-                    (($conf['report_content']['allow'] == 'authenticated' && Horde_Auth::isAuthenticated()) ||
-                     $conf['report_content']['allow'] == 'all')) {
-
-                    $this->view->addWidget(Ansel_Widget::factory('Actions'));
-                }
-                unset($this->style['widgets']['Actions']);
-            }
-
-            // I *think* this is more efficient, iterate over the children
-            // since we already have them instead of calling listImages.
-            //$image_ids = $this->view->gallery->listImages($this->pagestart, $this->pagestart + $this->perpage);
-            $ids = array();
-            foreach ($this->children as $child) {
-                if (is_a($child, 'Ansel_Image')) {
-                    $ids[] = $child->id;
-                }
-            }
-            // Gallery widgets always receive an array of image ids for
-            // the current page.
-            foreach ($this->style['widgets'] as $wname => $wparams) {
-                $wparams = array_merge($wparams, array('images' => $ids));
-                $this->view->addWidget(Ansel_Widget::factory($wname, $wparams));
-            }
-        }
-
-        /* See if any renderer specific tasks need to be done as well */
-        $this->_init();
-    }
-
-    /**
-     * Default implementation for fetching children/images for this view.
-     * Other view classes can override this if they need anything special.
-     *
-     */
-    function fetchChildren($noauto)
-    {
-        /* Total number of tiles for this gallery view */
-        $this->numTiles = $this->view->gallery->countGalleryChildren(PERMS_SHOW, false, $noauto);
-
-        /* Children to display on this page */
-        $this->children = $this->view->gallery->getGalleryChildren(
-            PERMS_SHOW,
-            $this->page * $this->perpage,
-            $this->perpage,
-            !empty($this->view->_params['force_grouping']));
-
-        /* The last tile number to display on the current page */
-        $this->pageend = min($this->numTiles, $this->pagestart + $this->perpage - 1);
-    }
-
-    /**
-     * Return the HTML for this view. Done this way so we can override this in
-     * subclasses if desired.
-     *
-     * @return string
-     */
-    function html()
-    {
-        if (is_a($this->view->gallery, 'PEAR_Error')) {
-            echo htmlspecialchars($this->view->gallery->getMessage(), ENT_COMPAT, Horde_Nls::getCharset());
-            return;
-        }
-
-        return $this->_html();
-    }
-
-}
diff --git a/ansel/lib/View/GalleryRenderer/Base.php b/ansel/lib/View/GalleryRenderer/Base.php
new file mode 100644 (file)
index 0000000..1b3a66d
--- /dev/null
@@ -0,0 +1,213 @@
+<?php
+/**
+ * Ansel_View_GalleryRenderer::  Base class for all gallery renderers.
+ *
+ * Copyright 2008-2009 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (GPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
+ *
+ * @author  Michael J. Rubinsky <mrubinsk@horde.org>
+ * @package Ansel
+ */
+abstract class Ansel_View_GalleryRenderer_Base
+{
+    /**
+     * The Ansel_View_Gallery object that this Renderer belongs to.
+     *
+     * @var Ansel_View_Gallery
+     */
+    public $view;
+
+    /**
+     * The gallery id for this view's gallery
+     *
+     * @var integer
+     */
+    public $galleryId;   // TODO: probably can remove this (get the id from the view's gallery)
+
+    /**
+     * Gallery slug for current gallery.
+     *
+     * @var string
+     */
+    var $gallerySlug; // Ditto.
+
+    /**
+     * The current page we are viewing
+     * //TODO: use __get() for these type of things...
+     * @var integer
+     */
+    public $page = 0;
+
+    /**
+     * The display mode of the current gallery.
+     * 0 == Normal
+     * 1 == Group by date
+     *
+     * @var integer
+     */
+    public $mode;
+
+    /**
+     * The style definition array for this gallery.
+     *
+     * @var array
+     */
+    public $style;
+
+    /**
+     * Holds number of tiles to display per page
+     *
+     * @var integer
+     */
+    public $perpage;
+
+    /**
+     * The tile number we are starting with on the current page.
+     *
+     * @var integer
+     */
+    public $pagestart;
+
+    /**
+     * The last tile number on the current page.
+     *
+     * @var integer
+     */
+    public $pageend;
+
+    /**
+     * The total number of tiles that this view contains
+     *
+     * @var integer
+     */
+    public $numTiles;
+
+    /**
+     * The Ansel_Image or Ansel_DateGallery objects that appear on the current
+     * page in the current view.
+     *
+     * @var array of Ansel_Image or Ansel_DateGallery objects.
+     */
+    public $children;
+
+    /**
+     * If we are grouping by date, this holds the currently selected date parts.
+     *
+     * @var array containing sufficient date parts for the current depth.
+     */
+    public $date = array();
+
+    /**
+     * Constructor
+     *
+     * @param Ansel_View_Gallery  The view object for this renderer.
+     *
+     * @return Ansel_View_Renderer_Gallery
+     */
+    public function __construct($view)
+    {
+        $this->view = $view;
+    }
+
+    /**
+     * Initialize the renderer. This *must* be called before any attempt is made
+     * to display or otherwise interact with the renderer.
+     *
+     * @TODO: Not sure why I didn't put this in the const'r - try moving it.
+     *
+     */
+    public function init()
+    {
+        global $prefs, $conf;
+
+        $this->galleryId = $this->view->gallery->id;
+        $this->gallerySlug = $this->view->gallery->get('slug');
+        if (isset($this->view->_params['page'])) {
+            $this->page = $this->view->_params['page'];
+        }
+
+        /* Number perpage from prefs or config */
+        $this->perpage = min($prefs->getValue('tilesperpage'),
+                             $conf['thumbnail']['perpage']);
+
+        /* Calculate the starting and ending images on this page */
+        $this->pagestart = ($this->page * $this->perpage) + 1;
+
+        /* Fetch the children */
+        $this->fetchChildren($this->view->_params['force_grouping']);
+
+        /* Do we have an explicit style set? If not, use the gallery's */
+        if (!empty($this->view->_params['style'])) {
+            $this->style = Ansel::getStyleDefinition($this->view->_params['style']);
+        } else {
+            $this->style = $this->view->gallery->getStyle();
+        }
+
+        /* Include any widgets */
+        if (!empty($this->style['widgets'])) {
+            /* Special case widgets - these are built in */
+            if (array_key_exists('Actions', $this->style['widgets'])) {
+                /* Don't show action widget if no actions */
+                if (Horde_Auth::getAuth() ||
+                    !empty($conf['report_content']['driver']) &&
+                    (($conf['report_content']['allow'] == 'authenticated' && Horde_Auth::isAuthenticated()) ||
+                     $conf['report_content']['allow'] == 'all')) {
+
+                    $this->view->addWidget(Ansel_Widget::factory('Actions'));
+                }
+                unset($this->style['widgets']['Actions']);
+            }
+
+            // I *think* this is more efficient, iterate over the children
+            // since we already have them instead of calling listImages.
+            //$image_ids = $this->view->gallery->listImages($this->pagestart, $this->pagestart + $this->perpage);
+            $ids = array();
+            foreach ($this->children as $child) {
+                if (is_a($child, 'Ansel_Image')) {
+                    $ids[] = $child->id;
+                }
+            }
+            // Gallery widgets always receive an array of image ids for
+            // the current page.
+            foreach ($this->style['widgets'] as $wname => $wparams) {
+                $wparams = array_merge($wparams, array('images' => $ids));
+                $this->view->addWidget(Ansel_Widget::factory($wname, $wparams));
+            }
+        }
+
+        /* See if any renderer specific tasks need to be done as well */
+        $this->_init();
+    }
+
+    /**
+     * Default implementation for fetching children/images for this view.
+     * Other view classes can override this if they need anything special.
+     *
+     */
+    public function fetchChildren($noauto)
+    {
+        /* Total number of tiles for this gallery view */
+        $this->numTiles = $this->view->gallery->countGalleryChildren(PERMS_SHOW, false, $noauto);
+
+        /* Children to display on this page */
+        $this->children = $this->view->gallery->getGalleryChildren(
+            PERMS_SHOW,
+            $this->page * $this->perpage,
+            $this->perpage,
+            !empty($this->view->_params['force_grouping']));
+
+        /* The last tile number to display on the current page */
+        $this->pageend = min($this->numTiles, $this->pagestart + $this->perpage - 1);
+    }
+
+    /**
+     * Return the HTML for this view. Done this way so we can override this in
+     * subclasses if desired.
+     *
+     * @return string
+     */
+    abstract public function html();
+    abstract protected function _init();
+}
index f806a4b..b901ca2 100644 (file)
  * @author  Michael J. Rubinsky <mrubinsk@horde.org>
  * @package Ansel
  */
-class Ansel_View_GalleryRenderer_Gallery extends Ansel_View_GalleryRenderer {
-
+class Ansel_View_GalleryRenderer_Gallery extends Ansel_View_GalleryRenderer_Base
+{
     /**
      * Perform any tasks that should be performed before the view is rendered.
      *
      */
-    function _init()
+    protected function _init()
     {
     }
 
@@ -27,7 +27,7 @@ class Ansel_View_GalleryRenderer_Gallery extends Ansel_View_GalleryRenderer {
      * @return string  The HTML.
      *
      */
-    function _html()
+    public function html()
     {
         global $conf, $prefs, $registry;
 
index d70051b..f4f062d 100644 (file)
  * @author  Michael J. Rubinsky <mrubinsk@horde.org>
  * @package Ansel
  */
-class Ansel_View_GalleryRenderer_GalleryLightbox extends Ansel_View_GalleryRenderer {
-
+class Ansel_View_GalleryRenderer_GalleryLightbox extends Ansel_View_GalleryRenderer_Base
+{
     /**
      * Perform any tasks that should be performed before the view is rendered.
      *
      */
-    function _init()
+    protected function _init()
     {
         if (empty($this->view->_params['image_onclick'])) {
             $this->view->_params['image_onclick'] = 'return lb.start(%i);';
@@ -36,7 +36,7 @@ class Ansel_View_GalleryRenderer_GalleryLightbox extends Ansel_View_GalleryRende
      *
      * @return string The HTML
      */
-    function _html()
+    public function html()
     {
         global $conf, $prefs, $registry;