More PHP5 stuff. Exceptions, public/private etc...
authorMichael J. Rubinsky <mrubinsk@horde.org>
Thu, 13 Aug 2009 20:14:53 +0000 (16:14 -0400)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Thu, 13 Aug 2009 20:14:53 +0000 (16:14 -0400)
ansel/lib/Ansel.php
ansel/lib/Gallery.php
ansel/lib/GalleryMode/Date.php
ansel/lib/GalleryMode/Normal.php
ansel/lib/Image.php
ansel/lib/ImageView.php
ansel/lib/Storage.php
ansel/rss.php

index 4958108..830a19f 100644 (file)
@@ -480,7 +480,7 @@ class Ansel
             if (is_a($result = $image->createView($view, $style, false), 'PEAR_Error')) {
                 return Ansel::getErrorImage($view);
             }
-            $viewHash = $image->_getViewHash($view, $style) . '/'
+            $viewHash = $image->getViewHash($view, $style) . '/'
                 . $image->getVFSName($view);
         }
 
@@ -895,6 +895,27 @@ class Ansel
     }
 
     /**
+     * Return a hash key for the given view and style.
+     *
+     * @param string $view   The view (thumb, prettythumb etc...)
+     * @param string $style  The named style.
+     *
+     * @return string  A md5 hash suitable for use as a key.
+     */
+    static public function getViewHash($view, $style)
+    {
+        $style = Ansel::getStyleDefinition($style);
+
+        if ($view != 'screen' && $view != 'thumb' && $view != 'mini' &&
+            $view != 'full') {
+
+            $view = md5($style['thumbstyle'] . '.' . $style['background']);
+        }
+
+        return $view;
+    }
+
+    /**
      * Add a custom stylesheet to the current page. Need our own implementation
      * since we want to be able to ouput specific CSS files at specific times
      * (like when rendering embedded content, or calling via the api etc...).
index 481eda5..cbc3ec0 100644 (file)
 class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
 {
     /**
-     * Cache the Gallery Id - to match the Ansel_Image interface
-     */
-    var $id;
-
-    /**
      * The gallery mode helper
      *
      * @var Ansel_Gallery_Mode object
      */
-    var $_modeHelper;
-
-    /**
-     *
-     */
-    function __sleep()
-    {
-        $properties = get_object_vars($this);
-        unset($properties['_shareOb']);
-        unset($properties['_modeHelper']);
-        $properties = array_keys($properties);
-        return $properties;
-    }
-
-    function __wakeup()
-    {
-        $this->setShareOb($GLOBALS['ansel_storage']->shares);
-        $mode = $this->get('view_mode');
-        $this->_setModeHelper($mode);
-    }
+    protected $_modeHelper;
 
     /**
      * The Ansel_Gallery constructor.
      *
      * @param string $name  The name of the gallery
      */
-    function Ansel_Gallery($attributes = array())
+    public function __construct($attributes = array())
     {
-        /* Existing gallery? */
-        if (!empty($attributes['share_id'])) {
-            $this->id = (int)$attributes['share_id'];
-        }
-
         /* Pass on up the chain */
         parent::Horde_Share_Object_sql_hierarchical($attributes);
         $this->setShareOb($GLOBALS['ansel_storage']->shares);
@@ -63,11 +34,21 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
         $this->_setModeHelper($mode);
     }
 
+    public function __get($property)
+    {
+        switch ($property) {
+        case 'id':
+            return $this->_getId();
+        default:
+            return null;
+        }
+    }
+
     /**
      * Check for special capabilities of this gallery.
      *
      */
-    function hasFeature($feature)
+    public function hasFeature($feature)
     {
 
         // First check for purely Ansel_Gallery features
@@ -79,13 +60,13 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
     }
 
     /**
-     * Simple factory to retrieve the proper mode object.
+     * Simple factory to set the proper mode object.
      *
      * @param string $type  The mode to use
      *
      * @return Ansel_Gallery_Mode object
      */
-    function _setModeHelper($type = 'Normal')
+    protected function _setModeHelper($type = 'Normal')
     {
         $type = basename($type);
         $class = 'Ansel_GalleryMode_' . $type;
@@ -98,7 +79,7 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
      *
      * @return boolean  Whether or not user can download full photos
      */
-    function canDownload()
+    public function canDownload()
     {
         if (Horde_Auth::getAuth() == $this->data['share_owner'] || Horde_Auth::isAdmin('ansel:admin')) {
             return true;
@@ -125,6 +106,10 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
     /**
      * Saves any changes to this object to the backend permanently.
      *
+     * @TODO: this needs to stay public b/c Horde_Share_Object (which this extends)
+     *        is not yet ported to PHP5. This will be fixed when we no longer
+     *        extend Horde_Share_Object here.
+     *
      * @return mixed true || PEAR_Error on failure.
      */
     function _save()
@@ -133,6 +118,8 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
         if (!empty($this->data['attribute_slug']) &&
             preg_match('/[^a-zA-Z0-9_@]/', $this->data['attribute_slug'])) {
 
+            // TODO: Need to keep the pear error here since Horde_Share still
+            // uses them.
             return PEAR::raiseError(
                 sprintf(_("Could not save gallery, the slug, \"%s\", contains invalid characters."),
                         $this->data['attribute_slug']));
@@ -158,10 +145,12 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
      * @param boolean $add         Action to take (add or remove)
      * @param integer $gallery_id  Gallery id to update images for
      */
-    function _updateImageCount($images, $add = true, $gallery_id = null)
+    public function updateImageCount($images, $add = true, $gallery_id = null)
     {
         // We do the query directly here to avoid having to instantiate a
         // gallery object just to increment/decrement one value in the table.
+        // TODO: Change this - should always use the appropriate object, not
+        // direct manipulation of the share table...
         $sql = 'UPDATE ' . $this->_shareOb->_table
             . ' SET attribute_images = attribute_images '
             . ($add ? ' + ' : ' - ') . $images . ' WHERE share_id = '
@@ -198,10 +187,11 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
      *
      * @return integer  The id of the new image.
      */
-    function addImage($image_data, $default = false)
+    public function addImage($image_data, $default = false)
     {
         global $conf;
 
+        //@TODO: Maybe addImage() gets moved to the modeHelper delegate?
         /* Normal is the only view mode that can accurately update gallery counts */
         $vMode = $this->get('view_mode');
         if ($vMode != 'Normal') {
@@ -218,12 +208,8 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
         /* Create the image object */
         $image = new Ansel_Image($image_data);
         $result = $image->save();
-        if (is_a($result, 'PEAR_Error')) {
-            return $result;
-        }
-
         if (empty($image_data['image_id'])) {
-            $this->_updateImageCount(1);
+            $this->updateImageCount(1);
             if ($this->countImages() < 5) {
                 $resetStack = true;
             }
@@ -262,8 +248,9 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
      * Clear all of this gallery's default image stacks from the VFS and the
      * gallery's data store.
      *
+     * @return void
      */
-    function clearStacks()
+    public function clearStacks()
     {
         $ids = @unserialize($this->data['attribute_default_prettythumb']);
         if (is_array($ids)) {
@@ -280,8 +267,9 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
      * Removes all generated and cached 'prettythumb' thumbnails for this
      * gallery
      *
+     * @return void
      */
-    function clearThumbs()
+    public function clearThumbs()
     {
         $images = $this->listImages();
         foreach ($images as $id) {
@@ -293,8 +281,9 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
     /**
      * Removes all generated and cached views for this gallery
      *
+     * @return void
      */
-    function clearViews()
+    public function clearViews()
     {
         $images = $this->listImages();
         foreach ($images as $id) {
@@ -311,7 +300,7 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
      *
      * @return integer | PEAR_Error The number of images moved, or an error message.
      */
-    function moveImagesTo($images, $gallery)
+    public function moveImagesTo($images, $gallery)
     {
         return $this->_modeHelper->moveImagesTo($images, $gallery);
     }
@@ -322,14 +311,13 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
      * @param array $images           An array of image ids.
      * @param Ansel_Gallery $gallery  The gallery to copy images to.
      *
-     * @return integer | PEAR_Error The number of images copied or error message
+     * @return integer The number of images copied
+     * @throws Horde_Exception
      */
-    function copyImagesTo($images, $gallery)
+    public function copyImagesTo($images, $gallery)
     {
         if (!$gallery->hasPermission(Horde_Auth::getAuth(), PERMS_EDIT)) {
-            return PEAR::raiseError(
-                sprintf(_("Access denied copying photos to \"%s\"."),
-                          $gallery->get('name')));
+            throw new Horde_Exception(sprintf(_("Access denied copying photos to \"%s\"."), $gallery->get('name')));
         }
 
         $db = $this->_shareOb->_write_db;
@@ -343,9 +331,6 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
                                'image_filename' => $img->filename,
                                'image_type' => $img->getType(),
                                'image_uploaded_date' => $img->uploaded));
-            if (is_a($newId, 'PEAR_Error')) {
-                return $newId;
-            }
             /* Copy any tags */
             // Since we know that the tags already exist, no need to
             // go through Ansel_Tags::writeTags() - this saves us a SELECT query
@@ -353,12 +338,12 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
             $tags = $img->getTags();
             $query = $this->_shareOb->_write_db->prepare('INSERT INTO ansel_images_tags (image_id, tag_id) VALUES(' . $newId . ',?);');
             if (is_a($query, 'PEAR_Error')) {
-                return $query;
+                throw new Horde_Exception($query->getMessage());
             }
             foreach ($tags as $tag_id => $tag_name) {
                 $result = $query->execute($tag_id);
                 if (is_a($result, 'PEAR_Error')) {
-                    return $result;
+                    throw new Horde_Exception($result->getMessge());
                 }
             }
             $query->free();
@@ -371,12 +356,12 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
                 if (is_array($exif) && count($exif) > 0) {
                     $insert = $db->prepare('INSERT INTO ansel_image_attributes (image_id, attr_name, attr_value) VALUES (?, ?, ?)');
                     if (is_a($insert, 'PEAR_Error')) {
-                        return $insert;
+                        throw new Horde_Exception($insert->getMessage());
                     }
                     foreach ($exif as $attr){
                         $result = $insert->execute(array($newId, $attr['attr_name'], $attr['attr_value']));
                         if (is_a($result, 'PEAR_Error')) {
-                            return $result;
+                            throw new Horde_Exception($result->getMessage());
                         }
                     }
                     $insert->free();
@@ -394,7 +379,7 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
      * @param integer $imageId The image to sort.
      * @param integer $pos     The sort position of the image.
      */
-    function setImageOrder($imageId, $pos)
+    public function setImageOrder($imageId, $pos)
     {
         return $this->_shareOb->_write_db->exec('UPDATE ansel_images SET image_sort = ' . (int)$pos . ' WHERE image_id = ' . (int)$imageId);
     }
@@ -407,7 +392,7 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
      *
      * @return boolean  True on success, false on failure.
      */
-    function removeImage($image, $isStack = false)
+    public function removeImage($image, $isStack = false)
     {
         return $this->_modeHelper->removeImage($image, $isStack);
     }
@@ -415,9 +400,12 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
     /**
      * Returns this share's owner's Identity object.
      *
+     * @TODO: Maybe rename this to getIdentity() to avoid confusion with
+     *        the share's owner attribute?
+     *
      * @return Identity object for the owner of this gallery.
      */
-    function getOwner()
+    public function getOwner()
     {
         require_once 'Horde/Identity.php';
         $identity = Identity::singleton('none', $this->data['share_owner']);
@@ -433,7 +421,7 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
      * @param array $params          Any additional parameters the Ansel_Tile
      *                               object may need.
      */
-    function getTile($parent = null, $style = null, $mini = false,
+    public function getTile($parent = null, $style = null, $mini = false,
                      $params = array())
     {
         if (!is_null($parent) && is_null($style)) {
@@ -460,7 +448,7 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
      * @return A mixed array of Ansel_Gallery and Ansel_Image objects that are
      *         children of this gallery.
      */
-    function getGalleryChildren($perm = PERMS_SHOW, $from = 0, $to = 0, $noauto = true)
+    public function getGalleryChildren($perm = PERMS_SHOW, $from = 0, $to = 0, $noauto = true)
     {
         return $this->_modeHelper->getGalleryChildren($perm, $from, $to, $noauto);
     }
@@ -474,7 +462,7 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
      *
      * @return integer The count of this gallery's children.
      */
-    function countGalleryChildren($perm = PERMS_SHOW, $galleries_only = false, $noauto = true)
+    public function countGalleryChildren($perm = PERMS_SHOW, $galleries_only = false, $noauto = true)
     {
         return $this->_modeHelper->countGalleryChildren($perm, $galleries_only, $noauto);
     }
@@ -485,9 +473,9 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
      * @param integer $from  The image to start listing.
      * @param integer $count The numer of images to list.
      *
-     * @return mixed  An array of image_ids | PEAR_Error
+     * @return array  An array of image_ids
      */
-    function listImages($from = 0, $count = 0)
+    public function listImages($from = 0, $count = 0)
     {
         return $this->_modeHelper->listImages($from, $count);
     }
@@ -500,7 +488,7 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
      *
      * @param mixed An array of Ansel_Image objects | PEAR_Error
      */
-    function getImages($from = 0, $count = 0)
+    public function getImages($from = 0, $count = 0)
     {
         return $this->_modeHelper->getImages($from, $count);
     }
@@ -512,7 +500,7 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
      *
      * @return mixed  An array of Ansel_Image objects | PEAR_Error
      */
-    function getRecentImages($limit = 10)
+    public function getRecentImages($limit = 10)
     {
         return $GLOBALS['ansel_storage']->getRecentImages(array($this->id),
                                                           $limit);
@@ -525,7 +513,7 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
      *
      * @return Ansel_Image  The image object corresponding to the given id.
      */
-    function &getImage($id)
+    public function &getImage($id)
     {
         return $GLOBALS['ansel_storage']->getImage($id);
     }
@@ -533,7 +521,7 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
     /**
      * Checks if the gallery has any subgallery
      */
-    function hasSubGalleries()
+    public function hasSubGalleries()
     {
         return $this->_modeHelper->hasSubGalleries();
     }
@@ -547,7 +535,7 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
      *
      * @return integer number of images in this gallery
      */
-    function countImages($subgalleries = false)
+    public function countImages($subgalleries = false)
     {
         return $this->_modeHelper->countImages($subgalleries);
     }
@@ -555,6 +543,9 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
     /**
      * Returns the default image for this gallery.
      *
+     * @TODO: Rename default images to 'key' images - they really are not
+     *        'default' in any sense.
+     *
      * @param string $style  Force the use of this style, if it's available
      *                       otherwise use whatever style is choosen for this
      *                       gallery. If prettythumbs are not available then
@@ -562,7 +553,7 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
      *
      * @return mixed  The image_id of the default image or false.
      */
-    function getDefaultImage($style = null)
+    public function getDefaultImage($style = null)
     {
        // Check for explicitly requested style
         if (!is_null($style)) {
@@ -579,7 +570,7 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
             $gal_style['default_galleryimage_type'] != 'plain') {
 
             $thumbstyle = $gal_style['default_galleryimage_type'];
-            $styleHash = $this->_getViewHash($thumbstyle, $style);
+            $styleHash = $this->getViewHash($thumbstyle, $gal_style['name']);
 
             // First check for the existence of a default image in the style
             // we are looking for.
@@ -596,30 +587,29 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
 
             // Don't already have one, must generate it.
             $params = array('gallery' => $this, 'style' => $gal_style);
-            $iview = Ansel_ImageView::factory(
-                $gal_style['default_galleryimage_type'], $params);
-
-            if (!is_a($iview, 'PEAR_Error')) {
+            try {
+                $iview = Ansel_ImageView::factory($gal_style['default_galleryimage_type'], $params);
                 $img = $iview->create();
-                if (!is_a($img, 'PEAR_Error')) {
-                     // Note the gallery_id is negative for generated stacks
-                     $iparams = array('image_filename' => $this->get('name'),
-                                      'image_caption' => $this->get('name'),
-                                      'data' => $img->raw(),
-                                      'image_sort' => 0,
-                                      'gallery_id' => -$this->id);
-                     $newImg = new Ansel_Image($iparams);
-                     $newImg->save();
-                     $prettyData = serialize(
-                         array_merge($thumbs,
-                                     array($styleHash => $newImg->id)));
-
-                     $this->set('default_prettythumb', $prettyData, true);
-                     return $newImg->id;
+                if ($img) {
+                    // Note the gallery_id is negative for generated stacks
+                    $iparams = array('image_filename' => $this->get('name'),
+                                     'image_caption' => $this->get('name'),
+                                     'data' => $img->raw(),
+                                     'image_sort' => 0,
+                                     'gallery_id' => -$this->id);
+                    $newImg = new Ansel_Image($iparams);
+                    $newImg->save();
+                    $prettyData = serialize(
+                        array_merge($thumbs,
+                                    array($styleHash => $newImg->id)));
+
+                    $this->set('default_prettythumb', $prettyData, true);
+                    return $newImg->id;
                 } else {
                     Horde::logMessage($img, __FILE__, __LINE__, PEAR_LOG_ERR);
                 }
-            } else {
+
+            } catch (Horde_Exception $e) {
                 // Might not support the requested style...try ansel_default
                 // but protect against infinite recursion.
                 Horde::logMessage($iview, __FILE__, __LINE__, PEAR_LOG_DEBUG);
@@ -628,6 +618,7 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
                 }
                 Horde::logMessage($iview, __FILE__, __LINE__, PEAR_LOG_ERR);
             }
+
         } else {
             // We are just using an image thumbnail for the gallery default.
             if ($this->countImages()) {
@@ -637,9 +628,6 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
                     return $this->data['attribute_default'];
                 }
                 $keys = $this->listImages();
-                if (is_a($keys, 'PEAR_Error')) {
-                    return $keys;
-                }
                 $this->data['attribute_default'] = $keys[count($keys) - 1];
                 $this->data['attribute_default_type'] = 'auto';
                 $this->save();
@@ -648,9 +636,12 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
 
             if ($this->hasSubGalleries()) {
                 // Fall through to a default image of a sub gallery.
-                $galleries = $GLOBALS['ansel_storage']->listGalleries(
-                    PERMS_SHOW, null, $this, false);
-                if ($galleries && !is_a($galleries, 'PEAR_Error')) {
+                try {
+                    $galleries = $GLOBALS['ansel_storage']->listGalleries(PERMS_SHOW, null, $this, false);
+                } catch (Horde_Exception $e) {
+                    return false;
+                }
+                if ($galleries) {
                     foreach ($galleries as $galleryId => $gallery) {
                         if ($default_img = $gallery->getDefaultImage($style)) {
                             return $default_img;
@@ -659,31 +650,38 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
                 }
             }
         }
+
         return false;
     }
 
     /**
      * Returns this gallery's tags.
+     *
+     * @return array of tag info
+     * @throws Horde_Exception
      */
-    function getTags() {
+    public function getTags() {
         if ($this->hasPermission(Horde_Auth::getAuth(), PERMS_READ)) {
             return Ansel_Tags::readTags($this->id, 'gallery');
         } else {
-            return PEAR::raiseError(_("Access denied viewing this gallery."));
+            throw new Horde_Exception(_("Access denied viewing this gallery."));
         }
     }
 
     /**
      * Set/replace this gallery's tags.
      *
-     * @param array $tags  AN array of tag names to associate with this image.
+     * @param array $tags  An array of tag names to associate with this image.
+     *
+     * @return true on success
+     * @throws Horde_Exception
      */
-    function setTags($tags)
+    public function setTags($tags)
     {
         if ($this->hasPermission(Horde_Auth::getAuth(), PERMS_EDIT)) {
             return Ansel_Tags::writeTags($this->id, $tags, 'gallery');
         } else {
-            return PEAR::raiseError(_("Access denied adding tags to this gallery."));
+            throw new Horde_Exception(_("Access denied adding tags to this gallery."));
         }
     }
 
@@ -695,7 +693,7 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
      *
      * @return array  The style definition array.
      */
-    function getStyle()
+    public function getStyle()
     {
         if (empty($this->data['attribute_style'])) {
             $style = $GLOBALS['prefs']->getValue('default_gallerystyle');
@@ -714,20 +712,15 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
      *
      * @return string  A md5 hash suitable for use as a key.
      */
-    function _getViewHash($view, $style = null)
+    public function getViewHash($view, $style = null)
     {
-        if (is_null($style)) {
-            $style = $this->getStyle();
-        } else {
-            $style = Ansel::getStyleDefinition($style);
+        if (empty($style)) {
+            $style = $this->data['attribute_style'];
         }
-        if ($view != 'screen' && $view != 'thumb' && $view != 'mini' &&
-            $view != 'full') {
 
-            $view = md5($style['thumbstyle'] . '.' . $style['background']);
-        }
-        return $view;
+        return Ansel::getViewHash($view, $style);
     }
+
     /**
      * Checks to see if a user has a given permission.
      *
@@ -737,7 +730,7 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
      *
      * @return boolean  Whether or not $userid has $permission.
      */
-    function hasPermission($userid, $permission, $creator = null)
+    public function hasPermission($userid, $permission, $creator = null)
     {
         if ($userid == $this->data['share_owner'] ||
             Horde_Auth::isAdmin('ansel:admin')) {
@@ -755,7 +748,7 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
      *
      * @return boolean
      */
-    function isOldEnough()
+    public function isOldEnough()
     {
         if ($this->data['share_owner'] == Horde_Auth::getAuth() ||
             empty($GLOBALS['conf']['ages']['limits']) ||
@@ -787,7 +780,7 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
      *
      * @return boolean
      */
-    function hasPasswd()
+    public function hasPasswd()
     {
         if (Horde_Auth::getAuth() == $this->get('owner') || Horde_Auth::isAdmin('ansel:admin')) {
             return false;
@@ -806,20 +799,16 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
     /**
      * Sets this gallery's parent gallery.
      *
-     * @TODO: Check how this interacts with date galleries - shouldn't be able
-     *        to remove a subgallery from a date gallery anyway, but just incase
-     * @param mixed $parent    An Ansel_Gallery or a gallery_id.
+     * @param mixed $parent  An Ansel_Gallery or a gallery_id.
      *
-     * @return mixed  Ture || PEAR_Error
+     * @return boolean true on sucess
+     * @throws Horde_Exception
      */
-    function setParent($parent)
+    public function setParent($parent)
     {
         /* Make sure we have a gallery object */
         if (!is_null($parent) && !is_a($parent, 'Ansel_Gallery')) {
             $parent = $GLOBALS['ansel_storage']->getGallery($parent);
-            if (is_a($parent, 'PEAR_Error')) {
-                return $parent;
-            }
         }
 
         /* Check this now since we don't know if we are updating the DB or not */
@@ -836,7 +825,8 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
         /* Call the parent class method */
         $result = parent::setParent($parent);
         if (is_a($result, 'PEAR_Error')) {
-            return $result;
+            // Horde_Share still uses PEAR_Error
+            throw new Horde_Exception($result->getMessage());
         }
 
         /* Tell the parent the good news */
@@ -864,7 +854,7 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
      * @return mixed  True if setting the attribute did succeed, a PEAR_Error
      *                otherwise.
      */
-    function set($attribute, $value, $update = false)
+    public function set($attribute, $value, $update = false)
     {
         /* Translate the keys */
         if ($attribute == 'owner') {
@@ -894,19 +884,21 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
             }
             $result = $query->execute(array($data[$driver_key], $this->id));
             $query->free();
+            if (is_a($result, 'PEAR_Error')) {
+                throw new Horde_Exception($result->getMessage());
+            }
 
-            return $result;
         }
 
         return true;
     }
 
-    function setDate($date)
+    public function setDate($date)
     {
         $this->_modeHelper->setDate($date);
     }
 
-    function getDate()
+    public function getDate()
     {
         return $this->_modeHelper->getDate();
     }
@@ -917,9 +909,28 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
      * @return  An array of 'title' and 'navdata' hashes with the [0] element
      *          being the deepest part.
      */
-    function getGalleryCrumbData()
+    public function getGalleryCrumbData()
     {
         return $this->_modeHelper->getGalleryCrumbData();
     }
 
+    /**
+     *
+     */
+    public function __sleep()
+    {
+        $properties = get_object_vars($this);
+        unset($properties['_shareOb']);
+        unset($properties['_modeHelper']);
+        $properties = array_keys($properties);
+        return $properties;
+    }
+
+    public function __wakeup()
+    {
+        $this->setShareOb($GLOBALS['ansel_storage']->shares);
+        $mode = $this->get('view_mode');
+        $this->_setModeHelper($mode);
+    }
+
 }
index 8160ae8..2a35340 100644 (file)
@@ -441,7 +441,7 @@ class Ansel_GalleryMode_Date {
          */
         if ($this->_gallery->get('has_subgalleries')) {
             $gallery_ids = array();
-            $images = $GLOBALS['ansel_storage']->getImages($ids);
+            $images = $GLOBALS['ansel_storage']->getImages(array('ids' => $ids));
             foreach ($images as $image) {
                 if (empty($gallery_ids[$image->gallery])) {
                     $gallery_ids[$image->gallery] = 1;
@@ -460,12 +460,12 @@ class Ansel_GalleryMode_Date {
         /* Update the gallery counts for each affected gallery */
         if ($this->_gallery->get('has_subgalleries')) {
             foreach ($gallery_ids as $id => $count) {
-                $this->_gallery->_updateImageCount($count, false, $id);
+                $this->_gallery->updateImageCount($count, false, $id);
             }
         } else {
-            $this->_gallery->_updateImageCount(count($ids), false);
+            $this->_gallery->updateImageCount(count($ids), false);
         }
-        $this->_gallery->_updateImageCount(count($ids), true, $gallery->id);
+        $this->_gallery->updateImageCount(count($ids), true, $gallery->id);
 
         /* Expire the cache since we have no reason to save() the gallery */
         if ($GLOBALS['conf']['ansel_cache']['usecache']) {
@@ -532,7 +532,7 @@ class Ansel_GalleryMode_Date {
         $this->_gallery->_shareOb->_write_db->exec('DELETE FROM ansel_images_geolocation WHERE image_id = ' . (int)$image->id);
 
         if (!$isStack) {
-            $this->_gallery->_updateImageCount(1, false, $image_gallery);
+            $this->_gallery->updateImageCount(1, false, $image_gallery);
         }
 
         /* Update the modified flag if we are not a stack image */
@@ -625,7 +625,7 @@ class Ansel_GalleryMode_Date {
                 $ids = array_merge($ids, $child->_images);
             }
             $ids = $this->_getArraySlice($ids, $from, $count);
-            $images = $GLOBALS['ansel_storage']->getImages($ids);
+            $images = $GLOBALS['ansel_storage']->getImages(array('ids' => $ids));
         }
 
         return $images;
@@ -951,9 +951,9 @@ class Ansel_Gallery_Date {
      *
      * @return string  A md5 hash suitable for use as a key.
      */
-    function _getViewHash($view, $style = null)
+    function getViewHash($view, $style = null)
     {
-        return $this->_gallery->_getViewHash($view, $style);
+        return $this->_gallery->getViewHash($view, $style);
     }
 
     /**
index 8b3263b..7cf1320 100644 (file)
@@ -188,8 +188,8 @@ class Ansel_GalleryMode_Normal {
             return $result;
         }
 
-        $this->_gallery->_updateImageCount(count($ids), false);
-        $this->_gallery->_updateImageCount(count($ids), true, $gallery->id);
+        $this->_gallery->updateImageCount(count($ids), false);
+        $this->_gallery->updateImageCount(count($ids), true, $gallery->id);
 
         /* Expire the cache since we have no reason to save() the gallery */
         if ($GLOBALS['conf']['ansel_cache']['usecache']) {
@@ -236,7 +236,7 @@ class Ansel_GalleryMode_Normal {
         $this->_gallery->_shareOb->_write_db->exec('DELETE FROM ansel_image_attributes WHERE image_id = ' . (int)$image->id);
 
         if (!$isStack) {
-            $this->_gallery->_updateImageCount(1, false);
+            $this->_gallery->updateImageCount(1, false);
         }
 
         /* Remove any geolocation data */
@@ -279,13 +279,14 @@ class Ansel_GalleryMode_Normal {
      * @param integer $from  The image to start fetching.
      * @param integer $count The numer of images to return.
      *
-     * @param mixed An array of Ansel_Image objects | PEAR_Error
+     * @param mixed An array of Ansel_Image objects
      */
     function getImages($from = 0, $count = 0)
     {
         $images = $GLOBALS['ansel_storage']->getImages(array('gallery_id' => $this->_gallery->id,
                                                              'count' => $count,
                                                              'from' => $from));
+
         return array_values($images);
     }
 
index 42c6b0a..e61db1b 100644 (file)
@@ -140,7 +140,7 @@ class Ansel_Image
      */
     function getVFSPath($view = 'full', $style = null)
     {
-        $view = $this->_getViewHash($view, $style);
+        $view = $this->getViewHash($view, $style);
         return '.horde/ansel/'
                . substr(str_pad($this->id, 2, 0, STR_PAD_LEFT), -2)
                . '/' . $view;
@@ -188,7 +188,7 @@ class Ansel_Image
             return true;
         }
 
-        $viewHash = $this->_getViewHash($view, $style);
+        $viewHash = $this->getViewHash($view, $style);
         /* If we've already loaded the data, just return now. */
         if (!empty($this->_loaded[$viewHash])) {
             return true;
@@ -246,7 +246,7 @@ class Ansel_Image
         }
 
         /* Get the VFS path. */
-        $view = Ansel_Gallery::_getViewHash($view, $style);
+        $view = Ansel_Gallery::getViewHash($view, $style);
 
         /* Can't call the various vfs methods here, since this method needs
         to be called statically */
@@ -326,7 +326,7 @@ class Ansel_Image
             return $res;
         }
 
-        $view = $this->_getViewHash($view, $style);
+        $view = $this->getViewHash($view, $style);
 
         $this->_data[$view] = $this->_image->raw();
         $this->_image->loadString($vfspath . '/' . $this->id,
@@ -564,7 +564,7 @@ class Ansel_Image
         if (is_a($imageFile, 'PEAR_Error')) {
             return $imageFile;
         }
-        $exif = Horde_Image_Exif::factory($GLOBALS['conf']['exif'], $GLOBALS['conf']['exif']['params']);
+        $exif = Horde_Image_Exif::factory($GLOBALS['conf']['exif']['driver'], $GLOBALS['conf']['exif']['params']);
         $exif_fields = $exif->getData($imageFile);
 
         /* Flag to determine if we need to resave the image data */
@@ -998,7 +998,7 @@ class Ansel_Image
      *
      * @return string  A md5 hash suitable for use as a key.
      */
-    function _getViewHash($view, $style = null)
+    function getViewHash($view, $style = null)
     {
         global $ansel_storage;
 
@@ -1008,6 +1008,7 @@ class Ansel_Image
 
             return $view;
         }
+
         if (is_null($style)) {
             $gallery = $ansel_storage->getGallery(abs($this->gallery));
             if (is_a($gallery, 'PEAR_Error')) {
@@ -1017,8 +1018,8 @@ class Ansel_Image
         } else {
             $style = Ansel::getStyleDefinition($style);
         }
-
        $view = md5($style['thumbstyle'] . '.' . $style['background']);
+
        return $view;
     }
 
index 34f0474..576a56b 100644 (file)
@@ -66,6 +66,14 @@ class Ansel_ImageView {
         return $this->_create();
     }
 
+    /**
+     *
+     * @param string $type   The type of concrete instance to return.
+     * @param array $params  Additional parameters needed for the instance.
+     *
+     * @return Ansel_ImageView
+     * @throws Horde_Exception
+     */
     function factory($type, $params = array())
     {
         $type = basename($type);
@@ -79,16 +87,14 @@ class Ansel_ImageView {
             // requested effect.
             foreach ($view->need as $need) {
                 if (!Ansel::isAvailable($need)) {
-                    $err = PEAR::raiseError(_("This install does not support the %s feature. Please contact your administrator."), $need);
                     Horde::logMessage($err, __FILE__, __LINE__, PEAR_LOG_ERR);
-                    return $err;
+                    throw new Horde_Exception(_("This install does not support the %s feature. Please contact your administrator."), $need);
                 }
             }
             return $view;
         } else {
-            $err = PEAR::raiseError(sprintf(_("Unable to load the definition of %s."), $class));
             Horde::logMessage($err, __FILE__, __LINE__, PEAR_LOG_ERR);
-            return $err;
+            throw new Horde_Exception(sprintf(_("Unable to load the definition of %s."), $class));
         }
 
     }
@@ -118,6 +124,7 @@ class Ansel_ImageView {
                 return $result;
             }
         }
+
         return $parent;
     }
 
@@ -139,19 +146,19 @@ class Ansel_ImageView {
         $cnt = min(5, $gallery->countImages());
         $default = $gallery->get('default');
         if (!empty($default) && $default > 0) {
-            $img = &$gallery->getImage($default);
-            if (!is_a($img, 'PEAR_Error')) {
+            try {
+                $img = &$gallery->getImage($default);
                 $images[] = &$gallery->getImage($default);
                 $cnt--;
-            }
+            } catch (Horde_Exception $e) {}
         }
 
         for ($i = 0; $i < $cnt; $i++) {
             $rnd = mt_rand(0, $cnt);
-            $temp = $gallery->getImages($rnd, 1);
-            if (!is_a($temp, 'PEAR_Error') && count($temp)) {
-                $images[] = array_shift($temp);
-            }
+            try {
+                $temp = $gallery->getImages($rnd, 1);
+                 $images[] = array_shift($temp);
+            } catch (Horde_Exception $e) {}
         }
 
         // Reverse the array to ensure the requested default image
index cbef194..bee705a 100644 (file)
@@ -94,7 +94,8 @@ class Ansel_Storage
     *                              defaults are not desirable.
     * @param mixed  $parent       The gallery id of the parent (if any)
     *
-    * @return Ansel_Gallery  A new gallery object or PEAR_Error.
+    * @return Ansel_Gallery  A new gallery object.
+    * @throws Horde_Exception
     */
     public function createGallery($attributes = array(), $perm = null, $parent = null)
     {
@@ -135,15 +136,14 @@ class Ansel_Storage
         /* Check for slug uniqueness */
         if (!empty($attributes['slug']) &&
             $this->slugExists($attributes['slug'])) {
-            return PEAR::raiseError(sprintf(_("The slug \"%s\" already exists."),
-                                            $attributes['slug']));
+            throw new Horde_Exception(sprintf(_("The slug \"%s\" already exists."), $attributes['slug']));
         }
 
         /* Create the gallery */
         $gallery = $this->_shares->newShare('');
-        if (is_a($gallery, 'PEAR_Error')) {
+        if ($gallery instanceof PEAR_Error) {
             Horde::logMessage($gallery, __FILE__, __LINE__, PEAR_LOG_ERR);
-            return $gallery;
+            throw new Horde_Exception($gallery->getMessage());
         }
         Horde::logMessage('New Ansel_Gallery object instantiated', __FILE__, __LINE__, PEAR_LOG_DEBUG);
 
@@ -155,10 +155,6 @@ class Ansel_Storage
             if ($GLOBALS['conf']['ansel_cache']['usecache']) {
                 $GLOBALS['cache']->expire('Ansel_Gallery' . $parent);
             }
-            if (is_a($result, 'PEAR_Error')) {
-                Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR);
-                return $result;
-            }
         }
 
         /* Fill up the new gallery */
@@ -171,11 +167,11 @@ class Ansel_Storage
 
         /* Save it to storage */
         $result = $this->_shares->addShare($gallery);
-        if (is_a($result, 'PEAR_Error')) {
+        if ($result instanceof PEAR_Error) {
             $error = sprintf(_("The gallery \"%s\" could not be created: %s"),
                              $attributes['name'], $result->getMessage());
             Horde::logMessage($error, __FILE__, __LINE__, PEAR_LOG_ERR);
-            return PEAR::raiseError($error);
+            throw new Horde_Exception($error);
         }
 
         /* Convenience */
@@ -231,7 +227,7 @@ class Ansel_Storage
             if ($perms) {
                 $groups = Group::singleton();
                 $group_list = $groups->getGroupMemberships(Horde_Auth::getAuth());
-                if (!is_a($group_list, 'PEAR_Error') && count($group_list)) {
+                if (!($group_list instanceof PEAR_Error) && count($group_list)) {
                     foreach ($group_list as $group_id => $group_name) {
                         $perm->addGroupPermission($group_id, $perms, false);
                     }
@@ -265,14 +261,15 @@ class Ansel_Storage
         $stmt = $this->_db->prepare('SELECT share_id FROM '
             . $this->_shares->_table . ' WHERE attribute_slug = ?');
 
-        if (is_a($stmt, 'PEAR_Error')) {
+        if ($stmt instanceof PEAR_Error) {
             Horde::logMessage($stmt, __FILE__, __LINE__, PEAR_LOG_ERR);
             return 0;
         }
 
         $result = $stmt->execute($slug);
-        if (is_a($result, 'PEAR_Error')) {
+        if ($result instanceof PEAR_Error) {
             Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR);
+            return 0;
         }
         if (!$result->numRows()) {
             return 0;
@@ -293,7 +290,8 @@ class Ansel_Storage
      * @param array $overrides  An array of attributes that should be overridden
      *                          when the gallery is returned.
      *
-     * @return mixed  Ansel_Gallery object | PEAR_Error
+     * @return Ansel_Gallery object
+     * @throws Horde_Exception
      */
     public function &getGalleryBySlug($slug, $overrides = array())
     {
@@ -301,7 +299,7 @@ class Ansel_Storage
         if ($id) {
             return $this->getGallery($id, $overrides);
         } else {
-            return PEAR::raiseError(sprintf(_("Gallery %s not found."), $slug));
+            throw new Horde_Exception(sprintf(_("Gallery %s not found."), $slug));
         }
      }
 
@@ -312,7 +310,8 @@ class Ansel_Storage
      * @param array $overrides     An array of attributes that should be
      *                             overridden when the gallery is returned.
      *
-     * @return mixed  Ansel_Gallery | PEAR_Error
+     * @return Ansel_Gallery
+     * @throws Horde_Exception
      */
     public function &getGallery($gallery_id, $overrides = array())
     {
@@ -330,8 +329,8 @@ class Ansel_Storage
        }
 
        $result = &$this->_shares->getShareById($gallery_id);
-       if (is_a($result, 'PEAR_Error')) {
-           return $result;
+       if ($result instanceof PEAR_Error) {
+           throw new Horde_Exception($result->getMessage());
        }
        $this->_galleries[$gallery_id] = &$result;
 
@@ -345,6 +344,7 @@ class Ansel_Storage
                $this->_galleries[$gallery_id]->set($key, $value, false);
            }
        }
+
         return $this->_galleries[$gallery_id];
     }
 
@@ -353,7 +353,8 @@ class Ansel_Storage
      *
      * @param array $slugs  The gallery slugs
      *
-     * @return mixed  Array of Ansel_Gallery objects | PEAR_Error
+     * @return array of Ansel_Gallery objects
+     * @throws Horde_Exception
      */
     public function getGalleriesBySlugs($slugs)
     {
@@ -361,12 +362,12 @@ class Ansel_Storage
             . ' WHERE attribute_slug IN (' . str_repeat('?, ', count($slugs) - 1) . '?)';
 
         $stmt = $this->_shares->_db->prepare($sql);
-        if (is_a($stmt, 'PEAR_Error')) {
-            return $stmt;
+        if ($stmt instanceof PEAR_Error) {
+            throw new Horde_Exception($stmt->getMessage());
         }
         $result = $stmt->execute($slugs);
-        if (is_a($result, 'PEAR_Error')) {
-            return $result;
+        if ($result instanceof PEAR_Error) {
+            throw new Horde_Exception($result->getMessage());
         }
         $ids = array_values($result->fetchCol());
         $shares = $this->_shares->getShares($ids);
@@ -379,6 +380,8 @@ class Ansel_Storage
 
     /**
      * Retrieve an array of Ansel_Gallery objects for the requested ids
+     *
+     * @return array of Ansel_Gallery objects
      */
     public function getGalleries($ids)
     {
@@ -405,7 +408,6 @@ class Ansel_Storage
         if ($GLOBALS['conf']['ansel_cache']['usecache']) {
             $GLOBALS['cache']->expire('Ansel_OtherGalleries' . $gallery->get('owner'));
         }
-
     }
 
     /**
@@ -413,15 +415,13 @@ class Ansel_Storage
      *
      * @param Ansel_Gallery $gallery  The gallery to delete
      *
-     * @return mixed  True || PEAR_Error
+     * @return boolean true on success
+     * @throws Horde_Exception
      */
     public function removeGallery($gallery)
     {
         /* Get any children and empty them */
         $children = $gallery->getChildren(null, true);
-        if (is_a($children, 'PEAR_Error')) {
-            return $children;
-        }
         foreach ($children as $child) {
             $this->emptyGallery($child);
             $child->setTags(array());
@@ -439,8 +439,8 @@ class Ansel_Storage
 
         /* Delete the gallery from storage */
         $result = $this->_shares->removeShare($gallery);
-        if (is_a($result, 'PEAR_Error')) {
-            return $result;
+        if ($result instanceof PEAR_Error) {
+            throw new Horde_Exception($result->getMessage());
         }
 
         /* Expire the cache */
@@ -450,10 +450,9 @@ class Ansel_Storage
         unset($this->_galleries[$id]);
 
         /* See if we need to clear the has_subgalleries field */
-        if (is_a($parent, 'Ansel_Gallery')) {
+        if ($parent instanceof Ansel_Gallery) {
             if (!$parent->countChildren(PERMS_SHOW, false)) {
                 $parent->set('has_subgalleries', 0, true);
-
                 if ($GLOBALS['conf']['ansel_cache']['usecache']) {
                     $GLOBALS['cache']->expire('Ansel_Gallery' . $parent->id);
                 }
@@ -470,6 +469,7 @@ class Ansel_Storage
      * @param integer $id  The ID of the image to retrieve.
      *
      * @return Ansel_Image  The image object corresponding to the given name.
+     * @throws Horde_Exception
      */
     public function &getImage($id)
     {
@@ -478,23 +478,23 @@ class Ansel_Storage
         }
 
         $q = $this->_db->prepare('SELECT ' . $this->_getImageFields() . ' FROM ansel_images WHERE image_id = ?');
-        if (is_a($q, 'PEAR_Error')) {
+        if ($q instanceof PEAR_Error) {
             Horde::logMessage($q, __FILE__, __LINE__, PEAR_LOG_ERR);
-            return $q;
+            throw new Horde_Exception($q->getMessage());
         }
         $result = $q->execute((int)$id);
-        if (is_a($result, 'PEAR_Error')) {
+        if ($result instanceof PEAR_Error) {
             Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR);
-            return $result;
+            throw new Horde_Exception($result->getMessage());
         }
         $image = $result->fetchRow(MDB2_FETCHMODE_ASSOC);
         $q->free();
         $result->free();
         if (is_null($image)) {
-            return PEAR::raiseError(_("Photo not found"));
-        } elseif (is_a($image, 'PEAR_Error')) {
+            throw new Horde_Exception(_("Photo not found"));
+        } elseif ($image instanceof PEAR_Error) {
             Horde::logMessage($image, __FILE__, __LINE__, PEAR_LOG_ERR);
-            return $image;
+            throw new Horde_Exception($image->getMessage());
         } else {
             $image['image_filename'] = Horde_String::convertCharset($image['image_filename'], $GLOBALS['conf']['sql']['charset']);
             $image['image_caption'] = Horde_String::convertCharset($image['image_caption'], $GLOBALS['conf']['sql']['charset']);
@@ -517,6 +517,7 @@ class Ansel_Storage
      *  </pre>
      *
      * @return array of Ansel_Image objects.
+     * @throws Horde_Exception
      */
     public function getImages($params = array())
     {
@@ -540,11 +541,13 @@ class Ansel_Storage
         }
 
         $images = $this->_db->query($sql);
-        if (is_a($images, 'PEAR_Error')) {
-            return $images;
-        } elseif ($images->numRows() == 0) {
+        if ($images instanceof PEAR_Error) {
+            throw new Horde_Exception($images->getMessage());
+        } elseif ($images->numRows() == 0 && empty($params['gallery_id'])) {
             $images->free();
-            return PEAR::raiseError(_("Photos not found"));
+            throw new Horde_Exception(_("Images not found"));
+        } elseif ($images->numRows() == 0) {
+            return array();
         }
 
         $return = array();
@@ -558,7 +561,7 @@ class Ansel_Storage
 
         /* Need to get comment counts if comments are enabled */
         $ccounts = $this->_getImageCommentCounts(array_keys($return));
-        if (!is_a($ccounts, 'PEAR_Error') && count($ccounts)) {
+        if (!($ccounts instanceof PEAR_Error) && count($ccounts)) {
             foreach ($return as $key => $image) {
                 $return[$key]->commentCount = (!empty($ccounts[$key]) ? $ccounts[$key] : 0);
             }
@@ -601,6 +604,7 @@ class Ansel_Storage
      * @param string $where     Additional where clause
      *
      * @return array An array of Ansel_Image objects
+     * @throws Horde_Exception
      */
     public function getRecentImages($galleries = array(), $limit = 10, $slugs = array())
     {
@@ -626,8 +630,8 @@ class Ansel_Storage
 
         $sql .= ' ORDER BY image_uploaded_date DESC LIMIT ' . (int)$limit;
         $query = $this->_db->prepare($sql);
-        if (is_a($query, 'PEAR_Error')) {
-            return $query;
+        if ($query instanceof PEAR_Error) {
+           throw new Horde_Exception($query->getMessage());
         }
 
         if (count($slugs)) {
@@ -636,8 +640,8 @@ class Ansel_Storage
             $images = $query->execute($galleries);
         }
         $query->free();
-        if (is_a($images, 'PEAR_Error')) {
-            return $images;
+        if ($images instanceof PEAR_Error) {
+            throw new Horde_Exception($images->getMessage());
         } elseif ($images->numRows() == 0) {
             return array();
         }
@@ -661,15 +665,22 @@ class Ansel_Storage
      * @param integer $gallery_id  The gallery id
      * @param string  $slug        The gallery slug
      *
-     * @return mixed  true | false | PEAR_Error
+     * @return boolean
+     * @throws Horde_Exception
      */
     public function galleryExists($gallery_id, $slug = null)
     {
         if (empty($slug)) {
-            return (bool)$this->_db->queryOne(
+            $results = $this->_db->queryOne(
                 'SELECT COUNT(share_id) FROM ' . $this->_shares->_table
                 . ' WHERE share_id = ' . (int)$gallery_id);
+            if ($results instanceof PEAR_Error) {
+                throw new Horde_Exception($results->getMessage());
+            }
+
+            return (bool)$results;
         } else {
+
             return (bool)$this->slugExists($slug);
         }
     }
@@ -682,15 +693,16 @@ class Ansel_Storage
     * @param integer $from   The gallery to start listing at.
     * @param integer $count  The number of galleries to return.
     *
-    * @return mixed  List of categories | PEAR_Error
+    * @return array  List of categories
+    * @throws Horde_Exception
     */
     public function listCategories($perm = PERMS_SHOW, $from = 0, $count = 0)
     {
         $sql = 'SELECT DISTINCT attribute_category FROM '
                . $this->_shares->_table;
         $results = $this->_shares->_db->query($sql);
-        if (is_a($results, 'PEAR_Error')) {
-            return $results;
+        if ($results instanceof PEAR_Error) {
+            throw new Horde_Exception($results->getMessage());
         }
         $all_categories = $results->fetchCol('attribute_category');
         $results->free();
@@ -710,6 +722,12 @@ class Ansel_Storage
         }
     }
 
+    /**
+     *
+     * @param $perms
+     *
+     * @return int  The count of categories
+     */
     public function countCategories($perms = PERMS_SHOW)
     {
         return count($this->listCategories($perms));
@@ -729,13 +747,16 @@ class Ansel_Storage
     * @param string  $parent       The parent share to start counting at.
     * @param boolean $allLevels    Return all levels, or just the direct
     *                              children of $parent? Defaults to all levels.
+    *
+    * @return int  The count
+    * @throws Horde_Exception
     */
     public function countGalleries($userid, $perm = PERMS_SHOW, $attributes = null,
                             $parent = null, $allLevels = true)
     {
         static $counts;
 
-        if (is_a($parent, 'Ansel_Gallery')) {
+        if ($parent instanceof Ansel_Gallery) {
             $parent_id = $parent->getId();
         } else {
             $parent_id = $parent;
@@ -750,6 +771,10 @@ class Ansel_Storage
         $count = $this->_shares->countShares($userid, $perm, $attributes,
                                             $parent, $allLevels);
 
+        if ($count instanceof PEAR_Error) {
+            throw new Horde_Exception($count->getMessage());
+        }
+
         $counts[$key] = $count;
 
         return $count;
@@ -775,7 +800,8 @@ class Ansel_Storage
     *                               0 - ascending
     *                               1 - descending
     *
-    * @return mixed An array of Ansel_Gallery objects | PEAR_Error
+    * @return array of Ansel_Gallery objects
+    * @throws Horde_Exception
     */
     public function listGalleries($perm = PERMS_SHOW,
                            $attributes = null,
@@ -786,9 +812,15 @@ class Ansel_Storage
                            $sort_by = null,
                            $direction = 0)
     {
-        return $this->_shares->listShares(Horde_Auth::getAuth(), $perm, $attributes,
-                                         $from, $count, $sort_by, $direction,
-                                         $parent, $allLevels);
+        $shares = $this->_shares->listShares(Horde_Auth::getAuth(), $perm, $attributes,
+                                             $from, $count, $sort_by, $direction,
+                                             $parent, $allLevels);
+
+        if ($shares instanceof PEAR_Error) {
+            throw new Horde_Exception($shares->getMessage());
+        }
+
+        return $shares;
     }
 
     /**
@@ -802,10 +834,11 @@ class Ansel_Storage
      * @param string $image_view   Which image view to use? screen, thumb etc..
      * @param boolean $view_links  Include links to the image view
      *
-     * @return string  The json data || PEAR_Error
+     * @return string  The json data
+     * @throws Horde_Exception
      */
     public function getImageJson($images, $style = null, $full = false,
-                          $image_view = 'mini', $view_links = false)
+                                 $image_view = 'mini', $view_links = false)
     {
         $galleries = array();
         if (is_null($style)) {
@@ -816,51 +849,46 @@ class Ansel_Storage
 
         foreach ($images as $id) {
             $image = $this->getImage($id);
-            if (!is_a($image, 'PEAR_Error')) {
-                $gallery_id = abs($image->gallery);
+            $gallery_id = abs($image->gallery);
+            if (empty($galleries[$gallery_id])) {
+                $galleries[$gallery_id]['gallery'] = $GLOBALS['ansel_storage']->getGallery($gallery_id);
+            }
 
-                if (empty($galleries[$gallery_id])) {
-                    $galleries[$gallery_id]['gallery'] = $GLOBALS['ansel_storage']->getGallery($gallery_id);
-                    if (is_a($galleries[$gallery_id]['gallery'], 'PEAR_Error')) {
-                        return $galleries[$gallery_id];
-                    }
-                }
+            // Any authentication that needs to take place for any of the
+            // images included here MUST have already taken place or the
+            // image will not be incldued in the output.
+            if (!isset($galleries[$gallery_id]['perm'])) {
+                $galleries[$gallery_id]['perm'] =
+                    ($galleries[$gallery_id]['gallery']->hasPermission(Horde_Auth::getAuth(), PERMS_READ) &&
+                     $galleries[$gallery_id]['gallery']->isOldEnough() &&
+                     !$galleries[$gallery_id]['gallery']->hasPasswd());
+            }
 
-                // Any authentication that needs to take place for any of the
-                // images included here MUST have already taken place or the
-                // image will not be incldued in the output.
-                if (!isset($galleries[$gallery_id]['perm'])) {
-                    $galleries[$gallery_id]['perm'] =
-                        ($galleries[$gallery_id]['gallery']->hasPermission(Horde_Auth::getAuth(), PERMS_READ) &&
-                         $galleries[$gallery_id]['gallery']->isOldEnough() &&
-                         !$galleries[$gallery_id]['gallery']->hasPasswd());
+            if ($galleries[$gallery_id]['perm']) {
+                $data = array(Ansel::getImageUrl($image->id, $image_view, $full, $style),
+                    htmlspecialchars($image->filename, ENT_COMPAT, Horde_Nls::getCharset()),
+                    Horde_Text_Filter::filter($image->caption, 'text2html', array('parselevel' => Horde_Text_Filter_Text2html::MICRO_LINKURL)),
+                    $image->id,
+                    0);
+
+                if ($view_links) {
+                    $data[] = Ansel::getUrlFor('view',
+                        array('gallery' => $image->gallery,
+                              'image' => $image->id,
+                              'view' => 'Image',
+                              'slug' => $galleries[$gallery_id]['gallery']->get('slug')),
+                        $full);
+
+                    $data[] = Ansel::getUrlFor('view',
+                        array('gallery' => $image->gallery,
+                              'slug' => $galleries[$gallery_id]['gallery']->get('slug'),
+                              'view' => 'Gallery'),
+                        $full);
                 }
 
-                if ($galleries[$gallery_id]['perm']) {
-                    $data = array(Ansel::getImageUrl($image->id, $image_view, $full, $style),
-                        htmlspecialchars($image->filename, ENT_COMPAT, Horde_Nls::getCharset()),
-                        Horde_Text_Filter::filter($image->caption, 'text2html', array('parselevel' => Horde_Text_Filter_Text2html::MICRO_LINKURL)),
-                        $image->id,
-                        0);
-
-                    if ($view_links) {
-                        $data[] = Ansel::getUrlFor('view',
-                            array('gallery' => $image->gallery,
-                                  'image' => $image->id,
-                                  'view' => 'Image',
-                                  'slug' => $galleries[$gallery_id]['gallery']->get('slug')),
-                            $full);
-
-                        $data[] = Ansel::getUrlFor('view',
-                            array('gallery' => $image->gallery,
-                                  'slug' => $galleries[$gallery_id]['gallery']->get('slug'),
-                                  'view' => 'Gallery'),
-                            $full);
-                    }
-
-                    $json[] = $data;
-                }
+                $json[] = $data;
             }
+
         }
 
         if (count($json)) {
@@ -905,7 +933,8 @@ class Ansel_Storage
      *                             ignored if this is non-empty).
      * @param mixed $sort          The field(s) to sort by.
      *
-     * @return mixed  An array of image_ids | PEAR_Error
+     * @return array  An array of image_ids
+     * @throws Horde_Exception
      */
     public function listImages($gallery_id, $from = 0, $count = 0,
                         $fields = 'image_id', $where = '', $sort = 'image_sort')
@@ -934,8 +963,8 @@ class Ansel_Storage
         Horde::logMessage('Query by Ansel_Storage::listImages: ' . $sql, __FILE__, __LINE__, PEAR_LOG_DEBUG);
         $results = $this->_db->query('SELECT ' . $fields . ' FROM ansel_images '
             . $query_where . ' ORDER BY ' . $sort);
-        if (is_a($results, 'PEAR_Error')) {
-            return $results;
+        if ($results instanceof PEAR_Error) {
+            throw new Horde_Exception($results->getMessage());
         }
         if ($field_count > 1) {
             return $results->fetchAll(MDB2_FETCHMODE_ASSOC, true, true, false);
@@ -952,7 +981,7 @@ class Ansel_Storage
      *                          all images in the gallery that have geolocation
      *                          data ($image_ids would be ignored).
      *
-     * @return mixed An array of geodata || PEAR_Error
+     * @return array of geodata
      */
     public function getImagesGeodata($image_ids = array(), $gallery = null)
     {
@@ -974,12 +1003,19 @@ class Ansel_Storage
     /**
      * Get image attribtues from ansel_image_attributes table
      *
-     * @param $image_id
-     * @return unknown_type
+     * @param int $image_id  The image id
+     *
+     * @return array
+     * @throws Horde_Exception
      */
     public function getImageAttributes($image_id)
     {
-        return $GLOBALS['ansel_db']->queryAll('SELECT attr_name, attr_value FROM ansel_image_attributes WHERE image_id = ' . (int)$image_id, null, MDB2_FETCHMODE_ASSOC, true);
+        $results = $GLOBALS['ansel_db']->queryAll('SELECT attr_name, attr_value FROM ansel_image_attributes WHERE image_id = ' . (int)$image_id, null, MDB2_FETCHMODE_ASSOC, true);
+        if ($results instanceof PEAR_Error) {
+            throw new Horde_Exception($results->getMessage());
+        }
+
+        return $results;
     }
 
     /**
@@ -994,13 +1030,20 @@ class Ansel_Storage
         return $this->listImages(0, $start, $count, array('image_id as id', 'image_id', 'gallery_id', 'image_latitude', 'image_longitude', 'image_location'), $where, 'image_geotag_date DESC');
     }
 
+    /**
+     *
+     * @param string $search  Search fragment for autocompleting location strings
+     *
+     * @return array  The results
+     * @throws Horde_Exception
+     */
     public function searchLocations($search = '')
     {
         $sql = 'SELECT DISTINCT image_location, image_latitude, image_longitude'
             . ' FROM ansel_images WHERE image_location LIKE "' . $search . '%"';
         $results = $this->_db->query($sql);
-        if (is_a($results, 'PEAR_Error')) {
-            return $results;
+        if ($results instanceof PEAR_Error) {
+            throw new Horde_Exception($results->getMessage());
         }
 
         return $results->fetchAll(MDB2_FETCHMODE_ASSOC, true, true, false);
index d7684a3..357cf6b 100644 (file)
@@ -173,7 +173,7 @@ if (empty($rss)) {
         $tag_id = array_values(Ansel_Tags::getTagIds(array($id)));
         $images = Ansel_Tags::searchTagsById($tag_id, 10, 0, 'images');
         $tag_id = array_pop($tag_id);
-        $images = $ansel_storage->getImages($images['images']);
+        $images = $ansel_storage->getImages(array('ids' => $images['images']));
         if (!is_a($images, 'PEAR_Error') && count($images)) {
             $tag_id = $tag_id[0];
             $images = array_values($images);