More style related refactoring:
authorMichael J. Rubinsky <mrubinsk@horde.org>
Sat, 18 Sep 2010 02:14:33 +0000 (22:14 -0400)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Sat, 18 Sep 2010 02:14:33 +0000 (22:14 -0400)
Ansel now automatically picks up and makes available any new image
generators placed in lib/ImageGenerator. Will look for a matching "Stack"
generator for generating image stacks, if no match found, reverts to standard
thumbnail.  Ditto for gallery view styles placed in lib/View/GalleryRenderer.

Gallery property page now displays and allows changing current gallery style settings.

Some other phpdoc, and cleanup.

15 files changed:
ansel/config/conf.xml
ansel/config/styles.php.dist
ansel/lib/Gallery.php
ansel/lib/ImageGenerator.php
ansel/lib/ImageGenerator/PolaroidThumb.php
ansel/lib/ImageGenerator/RoundedThumb.php
ansel/lib/ImageGenerator/ShadowThumb.php
ansel/lib/ImageGenerator/Thumb.php
ansel/lib/Injector/Binder/Styles.php
ansel/lib/View/GalleryProperties.php
ansel/lib/View/GalleryRenderer/Base.php
ansel/lib/View/GalleryRenderer/Gallery.php
ansel/lib/View/GalleryRenderer/GalleryLightbox.php
ansel/templates/gallery/partial/_styles.html.php
ansel/templates/gallery/properties.html.php

index b434138..0f369ea 100644 (file)
@@ -84,7 +84,8 @@
    0</configstring>
    <configboolean name="prettythumbs" desc="Allow the selection of gallery
    themes and generation of 'pretty' thumbnails?  This can be a performance
-    hit the first time the thumbnails are generated.">false</configboolean>
+    hit on some installations but is required for the generation of all but the
+    most basic image thumbnails." required="false">false</configboolean>
    <configstring name="num_uploads" desc="How many photos should a user be
    allowed to upload at once?">5</configstring>
    <configstring name="font" required="false" desc="What font would you like
index d703074..cca404b 100644 (file)
@@ -1,16 +1,20 @@
 <?php
 /**
- * Configuration for the available gallery style
+ * Predefined gallery styles.  These are used to select a predefined set of
+ * styles on the gallery's property page. Style may then be tweaked by the
+ * gallery owner. The 'name' attribute is the name of they style that should be
+ * used if requesting an explicit style to overide the gallery's existing style
+ * when rendering via the API.
  *
  * Each entry *must* have:
- *
+ * <pre>
  * 'name'        =  The internal name of the style (this should match the
  *                  hash key).
  *
  * 'title'       =  This is the title to be displayed to the users.
  *
  * 'thumbstyle'  =  This is the type of thumbnail to use. This must match
- *                  an available Ansel_ImageView object in lib/ImageView.
+ *                  an available Ansel_ImageGenerator object.
  *
  * 'background'  =  The desired background color of the style. This will
  *                  set the background of both the photo display area inside
  *
  * The following are optional:
  *
- * requires_png               =  Specifies that the style should only be
- *                               available if both the Horde install uses PNG
- *                               photos *and* the currently requesting browser
- *                               has PNG transparency support.
- *
- * default_galleryimage_type  =  How to represent each gallery's default photo.
- *                               This must match an available Ansel_ImageView
- *                               object in lib/ImageView
- *                          create_serialized_styles.php
- * gallery_view               =  The Ansel_View_* subclass to use for gallery
- *                               views.
+ * gallery_view               =  The Ansel_View_GalleryRenderer to use for
+ *                               gallery rendering. [Gallery]
  *
  * widgets                    =  An array describing any Ansel_Widgets to
  *                               display on this gallery along with any
  *                               parameters the widget may need.
- *
- * hide                      =   Hide this style in the gallery propery page.
- *
- * falback                   -   Fall back to this gallery style if unable to
- *                               use this style (like if we don't have PNG
- *                               support)
- *
- * $Id$
+ * </pre>
  */
 
 // Just a time saver...
@@ -64,20 +52,16 @@ $styles['ansel_default'] = array(
 
 $styles['ansel_prettythumbs'] = array(
     'name' => 'ansel_prettythumbs',
-    'title' => _("Pretty Thumbnails (No Background)"),
-    'thumbstyle' => 'PrettyThumb',
-    'requires_png' => true,
-    // This really only looks good with ImageMagick, not GD.
-    'default_galleryimage_type' => 'RoundedStack',
+    'title' => _("Rounded Thumbnails (No Background)"),
+    'thumbstyle' => 'RoundedThumb',
     'background' => 'none',
     'widgets' => $widgets,
 );
 
 $styles['ansel_blackonwhite'] = array(
     'name' => 'ansel_blackonwhite',
-    'title' => _("Pretty Thumbnails (White Background)"),
-    'default_galleryimage_type' => 'RoundedStack',
-    'thumbstyle' => 'PrettyThumb',
+    'title' => _("Rounded Thumbnails (White Background)"),
+    'thumbstyle' => 'RoundedThumb',
     'background' => 'white',
     'widgets' => $widgets,
 );
@@ -85,9 +69,8 @@ $styles['ansel_blackonwhite'] = array(
 $styles['ansel_sharpshadowed'] = array(
     'name' => 'ansel_sharpshadowed',
     'title' => _("Shadowed Thumbnails (White Background)"),
-    'thumbstyle' => 'ShadowSharpThumb',
+    'thumbstyle' => 'ShadowThumb',
     'background' => 'white',
-    'default_galleryimage_type' => 'PlainStack',
     'widgets' => $widgets
 );
 
@@ -97,7 +80,6 @@ $styles['ansel_polaroid'] = array(
     'title' => _("Polaroid Style Thumbnails (White Background)"),
     'thumbstyle' => 'PolaroidThumb',
     'background' => 'white',
-    'default_galleryimage_type' => 'PolaroidStack',
     'widgets' => $widgets,
 );
 
@@ -108,10 +90,9 @@ $styles['ansel_lightbox'] = array(
     'thumbstyle' => 'Thumb',
     'background' => 'white',
     'gallery_view' => 'GalleryLightbox',
-    'widgets' => $widgets,
+    'widgets' => $widgets
 );
 
-
 /* Lightbox image views with no background
  * (requires PNG) */
 $styles['ansel_lightbox_png'] = array(
@@ -119,9 +100,8 @@ $styles['ansel_lightbox_png'] = array(
     'title' => _("A Lightbox Inspired Style (No Background)"),
     'thumbstyle' => 'Thumb',
     'background' => 'none',
-    'requires_png' => true,
     'gallery_view' => 'GalleryLightbox',
-    'widgets' => $widgets,
+    'widgets' => $widgets
 );
 
 /* Lightbox image views with no background and shadowed thumbs
@@ -129,15 +109,13 @@ $styles['ansel_lightbox_png'] = array(
 $styles['ansel_lightbox_shadowed_png'] = array(
     'name' => 'ansel_lightbox_shadowed_png',
     'title' => _("A Lightbox Inspired Style (Drop Shadows, No Background)"),
-    'thumbstyle' => 'ShadowSharpThumb',
+    'thumbstyle' => 'ShadowThumb',
     'background' => 'none',
-    'requires_png' => true,
     'gallery_view' => 'GalleryLightbox',
     'widgets' => $widgets,
     'fallback' => 'ansel_lightbox'
 );
 
-
 /* Same as above, but with Polaroid thumbnails/stacks
  * and no background (so required png support) */
 $styles['ansel_lightbox_polaroid'] = array(
@@ -145,10 +123,8 @@ $styles['ansel_lightbox_polaroid'] = array(
     'title' => _("Lightbox with Polaroids (No Background)"),
     'thumbstyle' => 'PolaroidThumb',
     'background' => 'none',
-    'requires_png' => true,
-    'default_galleryimage_type' => 'PolaroidStack',
     'gallery_view' => 'GalleryLightbox',
-    'widgets' => $widgets,
+    'widgets' => $widgets
 );
 
 /* Simple styles with no Ansel_Widgets useful for rendering  on external sites
@@ -160,8 +136,7 @@ $styles['ansel_simple'] = array(
     'name' => 'ansel_simple',
     'title' => _("Simple"),
     'thumbstyle' => 'Thumb',
-    'background' => 'none',
-    'hide' => true,
+    'background' => 'none'
 );
 
 /* An API friendly lightbox style */
@@ -170,10 +145,7 @@ $styles['ansel_lightbox_simple'] = array(
     'title' => _("Simple Lightbox"),
     'thumbstyle' => 'Thumb',
     'background' => 'none',
-    'requires_png' => true,
-    'gallery_view' => 'GalleryLightbox',
-    'hide' => true,
-    'fallback' => 'ansel_simple'
+    'gallery_view' => 'GalleryLightbox'
 );
 
 /* Same as above, but with polaroid thumbnails */
@@ -182,9 +154,5 @@ $styles['ansel_lightbox_simple_polaroid'] = array(
     'title' => _("Lightbox with Polaroids (No Background)"),
     'thumbstyle' => 'PolaroidThumb',
     'background' => 'none',
-    'requires_png' => true,
-    'default_galleryimage_type' => 'PolaroidStack',
-    'gallery_view' => 'GalleryLightbox',
-    'hide' => true,
-    'fallback' => 'ansel_simple'
+    'gallery_view' => 'GalleryLightbox'
 );
index 2acb9ec..c02fb83 100644 (file)
@@ -710,23 +710,34 @@ class Ansel_Gallery extends Horde_Share_Object_Sql_Hierarchical
     }
 
     /**
-     * Return the style definition for this gallery. Returns the first available
-     * style in this order: Explicitly configured style if available, if
-     * configured style is not available, use ansel_default.  If nothing has
-     * been configured, the user's selected default is attempted.
-     *
-     * @TODO: Verify availability of selected style and fallback to ansel_default
+     * Return the style definition for this gallery.
      *
      * @return array  The style definition array.
      */
     public function getStyle()
     {
+        // No styles allowed per admin.
+        if (!$GLOBALS['conf']['image']['prettythumbs']) {
+            return Ansel::getStyleDefinition('ansel_default');
+        }
+
         if (empty($this->data['attribute_style'])) {
+            // No style configured, use user's prefered default
             $style = Ansel::getStyleDefinition($GLOBALS['prefs']->getValue('default_gallerystyle'));
         } else {
+            // Explicitly defined style
             $style = unserialize($this->data['attribute_style']);
         }
 
+        // Check browser requirements. If we require PNG support, and do not
+        // have it, revert to the basic ansel_default style.
+        if ($style->requiresPng() &&
+            ($GLOBALS['browser']->hasQuirk('png_transparency') ||
+             $GLOBALS['conf']['image']['type'] != 'png')) {
+
+            return Ansel::getStyleDefinition('ansel_default');
+        }
+
         return $style;
     }
 
index 983d027..8e4f883 100644 (file)
@@ -1,6 +1,21 @@
 <?php
 /**
- * Class to abstract the creation of various image views.
+ * Base class for the creation of various image views.
+ *
+ * New thumbnail generators can be dropped in and will be made available by
+ * Ansel providing:
+ *
+ *   1. The class name is as: Ansel_ImageGenerator_{type}Thumb and filename
+ *      matches, i.e. {type}Thumb.php where {type} is the unique name for your
+ *      thumbnail type.
+ *
+ *   2. Implements a _create() method that applies the effects to the image
+ *      (see existing generators for how this works).
+ *
+ *   3. If a matching "stack" generator is desired, that should be named
+ *      similarly: Ansel_ImageGenerator_{type}ThumbStack with matching filename:
+ *      {type}ThumbStack.php
+ *
  *
  * Copyright 2007-2010 The Horde Project (http://www.horde.org/)
  *
@@ -109,8 +124,7 @@ class Ansel_ImageGenerator
         }
         if (class_exists($class)) {
             $view = new $class($params);
-            // Check that the image object supports what we need for the
-            // requested effect.
+            // Check that the image object supports what we need for the effect.
             foreach ($view->need as $need) {
                 if (!Ansel::isAvailable($need)) {
                     Horde::logMessage($err, 'ERR');
@@ -154,10 +168,10 @@ class Ansel_ImageGenerator
 
    /**
     * Utility function to return an array of Horde_Images to use in building a
-    * polaroid stack. Returns a random set of 5 images from the gallery, or the
+    * stack. Returns a random set of 5 images from the gallery, or the
     * explicitly set key image plus 4 others.
     *
-    * @return array of Horde_Images
+    * @return array An array of Horde_Image objects.
     */
     protected function _getStackImages()
     {
index 3b8b5a5..27a9b7f 100644 (file)
@@ -9,6 +9,12 @@ class Ansel_ImageGenerator_PolaroidThumb extends Ansel_ImageGenerator
 {
     public $need = array('PolaroidImage');
 
+    public function __construct($params)
+    {
+        parent::__construct($params);
+        $this->title = _("Polaroids");
+    }
+
     /**
      *
      * @return Horde_Image
index 72f08ab..c60ce13 100644 (file)
@@ -9,6 +9,12 @@ class Ansel_ImageGenerator_RoundedThumb extends Ansel_ImageGenerator
 {
     public $need = array('RoundCorners', 'DropShadow');
 
+    public function __construct($params)
+    {
+        parent::__construct($params);
+        $this->title = _("Rounded Corners");
+    }
+
     /**
      *
      * @return Horde_Image
index 98ce8ab..6be90f9 100644 (file)
@@ -9,6 +9,12 @@ class Ansel_ImageGenerator_ShadowThumb extends Ansel_ImageGenerator
 {
     public $need = array('DropShadow');
 
+    public function __construct($params)
+    {
+        parent::__construct($params);
+        $this->title = _("Drop Shadows");
+    }
+
     /**
      *
      * @return boolean
index d563b4f..fdb56da 100755 (executable)
@@ -7,6 +7,12 @@
  */
 class Ansel_ImageGenerator_Thumb extends Ansel_ImageGenerator
 {
+    public function __construct($params)
+    {
+        parent::__construct($params);
+        $this->title = _("Basic Thumbnails");
+    }
+
     /**
      *
      * @return Horde_Image
index b288c18..9a8d20a 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * Binder for all available Ansel styles
+ * Binder for getting list of all available pre-defined styles.
  *
  * @author Michael J. Rubinsky <mrubinsk@horde.org>
  * @category Horde
@@ -18,7 +18,7 @@ class Ansel_Injector_Binder_Styles Implements Horde_Injector_Binder
         if (empty($GLOBALS['conf']['image']['prettythumbs'])) {
             $test = $styles;
             foreach ($test as $key => $style) {
-                if ($style['thumbstyle'] != 'thumb') {
+                if ($style['thumbstyle'] != 'Thumb') {
                     unset($styles[$key]);
                 }
             }
index d94adab..62eb09c 100644 (file)
  * @author  Michael J. Rubinsky <mrubinsk@horde.org>
  * @package Ansel
  */
-
-/**
- * Description of GalleryProperties
- *
- * @author mrubinsk
- */
 class Ansel_View_GalleryProperties
 {
     /**
@@ -48,22 +42,6 @@ class Ansel_View_GalleryProperties
     public function __construct($params = array())
     {
         $this->_params = $params;
-
-        /*Gallery properties */
-        $this->_properties = array(
-            'name' => '',
-            'desc' => '',
-            'tags' => '',
-            'style' => '',
-            'slug' => '',
-            'age' => 0,
-            'download' => $GLOBALS['prefs']->getValue('default_download'),
-            'parent' => null,
-            'id' => null,
-            'mode' => 'Normal',
-            'passwd' => '',
-            'owner' => ''
-        );
     }
 
     /**
@@ -92,6 +70,25 @@ class Ansel_View_GalleryProperties
         }
     }
 
+    private function _loadDefaults()
+    {
+        /*Gallery properties */
+        $this->_properties = array(
+            'name' => '',
+            'desc' => '',
+            'tags' => '',
+            'style' => Ansel::getStyleDefinition($GLOBALS['prefs']->getValue('default_gallerystyle')),
+            'slug' => '',
+            'age' => 0,
+            'download' => $GLOBALS['prefs']->getValue('default_download'),
+            'parent' => null,
+            'id' => null,
+            'mode' => 'Normal',
+            'passwd' => '',
+            'owner' => ''
+        );
+    }
+
     /**
      * Outputs the view to the browser.
      *
@@ -107,6 +104,8 @@ class Ansel_View_GalleryProperties
         $view->title = $this->_title;
         $view->action = $this->_params['actionID'];
         $view->url = $this->_params['url'];
+        $view->availableThumbs = $this->_thumbStyles();
+        $view->galleryViews = $this->_galleryViewStyles();
 
         Horde::addInlineScript(array('$("gallery_name").focus()'), 'dom');
         Horde::addScriptFile('stripe.js', 'horde');
@@ -132,6 +131,7 @@ class Ansel_View_GalleryProperties
      */
     private function _runNew()
     {
+        $this->_loadDefaults();
         $this->_title = _("Adding a New Gallery");
         $this->_properties['owner'] = $GLOBALS['registry']->getAuth();
     }
@@ -143,6 +143,8 @@ class Ansel_View_GalleryProperties
      */
     private function _runNewChild()
     {
+        $this->_loadDefaults();
+
         // Get the parent and make sure that it exists and that we have
         // permissions to add to it.
         $parentId = $this->_params['gallery'];
@@ -237,7 +239,8 @@ class Ansel_View_GalleryProperties
         $style = new Ansel_Style(array(
             'thumbstyle' => Horde_Util::getFormData('thumbnail_style'),
             'background' => Horde_Util::getFormData('background_color'),
-            // temporary hack
+            'gallery_view' => Horde_Util::getFormData('gallery_view'),
+            // temporary hack until widgets are also configurable.
             'widgets' => array(
                  'Tags' => array('view' => 'gallery'),
                  'OtherGalleries' => array(),
@@ -274,7 +277,7 @@ class Ansel_View_GalleryProperties
                     $gallery->get('owner') == $GLOBALS['registry']->getAuth()) {
                     $gallery->set('passwd', $gallery_passwd);
                 }
-                
+
                 // Did the parent change?
                 $old_parent = $gallery->getParent();
                 if (!is_null($old_parent)) {
@@ -386,5 +389,50 @@ class Ansel_View_GalleryProperties
         }
         $url->redirect();
     }
+
+   /**
+    * Get a list of available, currently usable thumbnail styles.
+    *
+    * @return array  An array of Classnames => titles
+    */
+    protected function _thumbStyles()
+    {
+        // Iterate all available thumbstyles:
+        $dir = ANSEL_BASE . '/lib/ImageGenerator';
+        $files = scandir($dir);
+        $thumbs = array();
+        foreach ($files as $file) {
+            if (substr($file, -9) == 'Thumb.php') {
+                try {
+                    $generator = Ansel_ImageGenerator::factory(substr($file, 0, -4), array('style' => ''));
+                    $thumbs[substr($file, 0, -4)] = $generator->title;
+                } catch (Ansel_Exception $e) {}
+            }
+        }
+
+        return $thumbs;
+    }
+
+    /**
+     * Get a list of available Gallery View styles
+     *
+     * @return array
+     */
+    protected function _galleryViewStyles()
+    {
+        // Iterate all available thumbstyles:
+        $dir = ANSEL_BASE . '/lib/View/GalleryRenderer';
+        $files = scandir($dir);
+        $views = array();
+        foreach ($files as $file) {
+            if ($file != 'Base.php' && $file != '.' && $file != '..') {
+                $class = 'Ansel_View_GalleryRenderer_' . substr($file, 0, -4);
+                $view = new $class(null);
+                $views[substr($file, 0, -4)] = $view->title;
+            }
+        }
+
+        return $views;
+    }
 }
 
index 673ed32..564f2b8 100644 (file)
@@ -51,7 +51,7 @@ abstract class Ansel_View_GalleryRenderer_Base
     public $mode;
 
     /**
-     * The style definition array for this gallery.
+     * The style definition.
      *
      * @var Ansel_Style
      */
@@ -101,11 +101,18 @@ abstract class Ansel_View_GalleryRenderer_Base
     public $date = array();
 
     /**
+     * Human readable title for this view type
+     *
+     * @var string
+     */
+    public $title;
+
+    /**
      * Constructor
      *
      * @param Ansel_View_Gallery  The view object for this renderer.
      */
-    public function __construct(Ansel_View_Gallery $view)
+    public function __construct($view)
     {
         $this->view = $view;
     }
index db63dd9..7fe5cee 100644 (file)
  */
 class Ansel_View_GalleryRenderer_Gallery extends Ansel_View_GalleryRenderer_Base
 {
+
+    public function __construct($view)
+    {
+        parent::__construct($view);
+        $this->title = _("Standard Gallery");
+    }
+
     /**
      * Perform any tasks that should be performed before the view is rendered.
      *
index f77ee68..5fba62d 100644 (file)
  */
 class Ansel_View_GalleryRenderer_GalleryLightbox extends Ansel_View_GalleryRenderer_Base
 {
+
+    public function __construct($view)
+    {
+        parent::__construct($view);
+        $this->title = _("Lightbox Gallery");
+    }
+
     /**
      * Perform any tasks that should be performed before the view is rendered.
      *
index 17161eb..3823d12 100644 (file)
@@ -1,27 +1,15 @@
   <tr>
-   <td align="right" valign="top"><strong><?php echo _("Thumbnail Style"); ?></string></td>
+   <td align="right" valign="top"><strong><?php echo _("Thumbnail Style"); ?></strong></td>
    <td>
     <select name="thumbnail_style">
-      <option value="Thumb" selected="selected">Basic Thumbnails</option>
-      <option value="ShadowThumb">Drop Shadows</option>
-      <option value="RoundedThumb">Rounded Corners</option>
-      <option value="PolaroidThumb">Polaroids</option>
+      <?php foreach ($this->availableThumbs as $type => $title): ?>
+        <option value="<?php echo $type ?>" <?php if ($this->properties['style']->thumbstyle == $type): ?>selected="selected"<?php endif; ?>><?php echo $title ?></option>
+      <?php endforeach;?>
     </select>
    </td>
   </tr>
-<!--  <tr>
-   <td align="right" valign="top"><strong><?php echo _("Key Image Style"); ?></string></td>
-   <td>
-    <select name="keyimage_type">
-     <option value="Thumb">Basic Thumbnail</option>
-     <option value="PlainStack">Photo Stack</option>
-     <option value-="RoundedStack">Rounded Stack</option>
-     <option value="PolaroidStack">Polaroid Stack</option>
-    </select>
-   </td>
-  </tr>-->
   <tr>
-   <td align="right" valign="top"><strong><?php echo _("Backgrond Color"); ?></string></td>
+   <td align="right" valign="top"><strong><?php echo _("Backgrond Color"); ?></strong></td>
    <td>
     <select name="background_color">
       <option value="none" selected="selected">None</option>
    </td>
   </tr>
   <tr>
-   <td align="right" valign="top"><strong><?php echo _("View Style"); ?></string></td>
+   <td align="right" valign="top"><strong><?php echo _("View Style"); ?></strong></td>
    <td>
     <select name="gallery_view">
-     <option value="Gallery">Traditional</option>
-     <option value="GalleryLightbox">Lightbox</option>
+     <?php foreach ($this->galleryViews as $type => $title): ?>
+       <option value="<?php echo $type ?>"<?php if ($this->properties['style']->gallery_view == $type): ?>selected="selected"<?php endif; ?>><?php echo $title ?></option>
+     <?php endforeach; ?>
     </select>
    </td>
   </tr>
\ No newline at end of file
index 9102d8f..e9a2379 100644 (file)
@@ -69,7 +69,7 @@
   <td align="right" valign="top"><strong id="slug_flag"><?php echo _("Gallery Slug") ?></strong>&nbsp;</td>
   <td>
    <input name="gallery_slug" id="gallery_slug" type="text" value="<?php echo $this->h($this->properties['slug']) ?>" size="50" /><br />
-   <?php if ($GLOBALS['conf']['urls']['pretty'] == 'rewrite') echo _("Slugs allows direct access to this gallery by visiting:") . ': ' . Horde::url('gallery/slugname', true) ?><br />
+   <?php if ($GLOBALS['conf']['urls']['pretty'] == 'rewrite'): echo _("Slugs allows direct access to this gallery by visiting:") . ': ' . Horde::url('gallery/slugname', true) ?><br /> <?php endif; ?>
    <?php echo _("Slug names may contain only letters, numbers, @, or _ (underscore).") ?>
   </td>
  </tr>
 <?php endif; ?>
 
 <!-- Gallery Style -->
-<?php echo $this->renderPartial('styles'); ?>
+<?php if ($GLOBALS['conf']['image']['prettythumbs']) {echo $this->renderPartial('styles'); }?>
 
 <!-- Submission -->
 <tr>