From 59ec5d9b84ea7d3a7c0e0bcae506a7d9d6088835 Mon Sep 17 00:00:00 2001 From: "Michael J. Rubinsky" Date: Mon, 10 May 2010 15:50:31 -0400 Subject: [PATCH] Horde 4 changes Use exceptions, instanceof etc.. --- ansel/lib/GalleryMode/Normal.php | 30 +++++++++----- ansel/lib/Image.php | 5 --- ansel/lib/View/List.php | 3 -- ansel/lib/View/Results.php | 85 +++++++++++++++++----------------------- 4 files changed, 57 insertions(+), 66 deletions(-) diff --git a/ansel/lib/GalleryMode/Normal.php b/ansel/lib/GalleryMode/Normal.php index 0915dd673..b3f5aa2fe 100644 --- a/ansel/lib/GalleryMode/Normal.php +++ b/ansel/lib/GalleryMode/Normal.php @@ -165,13 +165,21 @@ class Ansel_GalleryMode_Normal { $count); } - + /** + * + * @param array $images The image ids to move. + * @param Ansel_Gallery $gallery The gallery to move images into. + * + * @throws Ansel_Exception + * @throws Horde_Exception_PermissionDenied + * @return boolean + */ function moveImagesTo($images, $gallery) { if (!$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::EDIT)) { - return PEAR::raiseError(sprintf(_("Access denied moving photos to \"%s\"."), $newGallery->get('name'))); + throw new Horde_Exception_PermissionDenied(sprintf(_("Access denied moving photos to \"%s\"."), $newGallery->get('name'))); } elseif (!$this->_gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::DELETE)) { - return PEAR::raiseError(sprintf(_("Access denied removing photos from \"%s\"."), $gallery->get('name'))); + throw new Horde_Exception_PermissionDenied(sprintf(_("Access denied removing photos from \"%s\"."), $gallery->get('name'))); } /* Sanitize image ids, and see if we're removing our default image. */ @@ -184,8 +192,8 @@ class Ansel_GalleryMode_Normal { } $result = $this->_gallery->_shareOb->_write_db->exec('UPDATE ansel_images SET gallery_id = ' . $gallery->id . ' WHERE image_id IN (' . implode(',', $ids) . ')'); - if (is_a($result, 'PEAR_Error')) { - return $result; + if ($result instanceof PEAR_Error) { + throw new Ansel_Exception($result->getMessage()); } $this->_gallery->updateImageCount(count($ids), false); @@ -200,14 +208,18 @@ class Ansel_GalleryMode_Normal { return true; } + /** + * + * @param integer | Ansel_Image $image The image id or object + * @param boolean $isStack This represents a stack image + * + * @return boolean + */ function removeImage($image, $isStack) { /* Make sure $image is an Ansel_Image; if not, try loading it. */ - if (!is_a($image, 'Ansel_Image')) { + if (!($image instanceof Ansel_Image)) { $img = &$this->_gallery->getImage($image); - if (is_a($img, 'PEAR_Error')) { - return $img; - } $image = $img; } else { /* Make sure the image is in this gallery. */ diff --git a/ansel/lib/Image.php b/ansel/lib/Image.php index a15daceaa..b4107825d 100644 --- a/ansel/lib/Image.php +++ b/ansel/lib/Image.php @@ -475,11 +475,6 @@ class Ansel_Image Implements Iterator */ public function updateData($data, $view = 'full') { - // TODO: Get rid of this, $data should only be valid data. - if ($data instanceof PEAR_Error) { - throw new Ansel_Exception($data); - } - /* Delete old cached data if we are replacing the full image */ if ($view == 'full') { $this->deleteCache(); diff --git a/ansel/lib/View/List.php b/ansel/lib/View/List.php index 06f881d32..02b794e8c 100644 --- a/ansel/lib/View/List.php +++ b/ansel/lib/View/List.php @@ -121,9 +121,6 @@ class Ansel_View_List extends Ansel_View_Base $this->_numGalleries = $ansel_storage->countGalleries( Horde_Auth::getAuth(), Horde_Perms::SHOW, $filter, null, false); - if (is_a($this->_numGalleries, 'PEAR_Error')) { - return $this->_numGalleries->getMessage(); - } if ($this->_numGalleries == 0 && empty($this->_params['api'])) { if ($this->_owner && $filter == $this->_owner && $this->_owner == Horde_Auth::getAuth()) { diff --git a/ansel/lib/View/Results.php b/ansel/lib/View/Results.php index 2c6439390..5fb18d680 100644 --- a/ansel/lib/View/Results.php +++ b/ansel/lib/View/Results.php @@ -26,7 +26,10 @@ class Ansel_View_Results extends Ansel_View_Base private $_perPage; /** - * Contructor - just set some instance variables. + * Contructor. + * + * Also handles any actions from the view. TODO: These should be done with + * an init() or handle() method instead. * * @return Ansel_View_Results */ @@ -39,17 +42,15 @@ class Ansel_View_Results extends Ansel_View_Base $this->_owner = Horde_Util::getFormData('owner', null); $this->_search = Ansel_Tags::getSearch(null, $this->_owner); $this->_page = Horde_Util::getFormData('page', 0); - $action = Horde_Util::getFormData('actionID', ''); $image_id = Horde_Util::getFormData('image'); $vars = Horde_Variables::getDefaultVariables(); - // Number perpage from prefs or config. - $this->_perPage = min($prefs->getValue('tilesperpage'), - $conf['thumbnail']['perpage']); + /* Number perpage from prefs or config. */ + $this->_perPage = min($prefs->getValue('tilesperpage'), $conf['thumbnail']['perpage']); switch ($action) { - // Image related actions + /* Image related actions */ case 'delete': if (is_array($image_id)) { $images = array_keys($image_id); @@ -58,27 +59,23 @@ class Ansel_View_Results extends Ansel_View_Base } foreach ($images as $image) { - // Need a gallery object to delete the image, but need - // the image object to get the gallery. $img = $ansel_storage->getImage($image); $gallery = $ansel_storage->getgallery($img->gallery); if (!$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::DELETE)) { $notification->push( - sprintf(_("Access denied deleting photos from \"%s\"."), $image), - 'horde.error'); + sprintf(_("Access denied deleting photos from \"%s\"."), $image), 'horde.error'); } else { - $result = $gallery->removeImage($image); - if (is_a($result, 'PEAR_Error')) { + try { + $result = $gallery->removeImage($image); + $notification->push(_("Deleted the photo."), 'horde.success'); + Ansel_Tags::clearCache(); + } catch (Ansel_Exception $e) { $notification->push( - sprintf(_("There was a problem deleting photos: %s"), $result->getMessage()), 'horde.error'); - } else { - $notification->push(_("Deleted the photo."), 'horde.success'); - Ansel_Tags::clearCache(); - } + sprintf(_("There was a problem deleting photos: %s"), $e->getMessage()), 'horde.error'); + } } } - // Reload the browse view again to get notifications. header('Location: ' . Ansel::getUrlFor('view', array('view' => 'Results'), true)); @@ -95,10 +92,8 @@ class Ansel_View_Results extends Ansel_View_Base // valid image ID. $newGallery = Horde_Util::getFormData('new_gallery'); if ($images && $newGallery) { - $newGallery = $ansel_storage->getGallery($newGallery); - if (is_a($newGallery, 'PEAR_Error')) { - $notification->push(_("Bad input."), 'horde.error'); - } else { + try { + $newGallery = $ansel_storage->getGallery($newGallery); // Group by gallery first, then process in bulk by gallery. $galleries = array(); foreach ($images as $image) { @@ -107,10 +102,8 @@ class Ansel_View_Results extends Ansel_View_Base } foreach ($galleries as $gallery_id => $images) { $gallery = $ansel_storage->getGallery($gallery_id); - $result = $gallery->moveImagesTo($images, $newGallery); - if (is_a($result, 'PEAR_Error')) { - $notification->push($result, 'horde.error'); - } else { + try { + $result = $gallery->moveImagesTo($images, $newGallery); $notification->push( sprintf(ngettext("Moved %d photo from \"%s\" to \"%s\"", "Moved %d photos from \"%s\" to \"%s\"", @@ -118,14 +111,14 @@ class Ansel_View_Results extends Ansel_View_Base count($images), $gallery->get('name'), $newGallery->get('name')), 'horde.success'); + } catch (Exception $e) { + $notification->push($e->getMessage(), 'horde.error'); } } + } catch (Ansel_Exception $e) { + $notification->push(_("Bad input."), 'horde.error'); } } - - // Return to the image list. - $imageurl = Horde_Util::addParameter('view.php', - array('view' => 'Results')); header('Location: ' . Ansel::getUrlFor('view', array('view' => 'Results'), true)); @@ -138,15 +131,11 @@ class Ansel_View_Results extends Ansel_View_Base $images = array($image_id); } - // Move the images if we're provided with at least one - // valid image ID. $newGallery = Horde_Util::getFormData('new_gallery'); if ($images && $newGallery) { - $newGallery = $ansel_storage->getGallery($newGallery); - if (is_a($newGallery, 'PEAR_Error')) { - $notification->push(_("Bad input."), 'horde.error'); - } else { - // Group by gallery first, then process in bulk by gallery. + try { + /* Group by gallery first, then process in bulk by gallery. */ + $newGallery = $ansel_storage->getGallery($newGallery); $galleries = array(); foreach ($images as $image) { $img = $ansel_storage->getImage($image); @@ -154,10 +143,8 @@ class Ansel_View_Results extends Ansel_View_Base } foreach ($galleries as $gallery_id => $images) { $gallery = $ansel_storage->getGallery($gallery_id); - $result = $gallery->copyImagesTo($images, $newGallery); - if (is_a($result, 'PEAR_Error')) { - $notification->push($result, 'horde.error'); - } else { + try { + $result = $gallery->copyImagesTo($images, $newGallery); $notification->push( sprintf(ngettext("Copied %d photo from %s to %s", "Copied %d photos from %s to %s", @@ -165,18 +152,18 @@ class Ansel_View_Results extends Ansel_View_Base count($images), $gallery->get('name'), $newGallery->get('name')), 'horde.success'); + } catch (Exception $e) { + $notification->push($e->getMessage(), 'horde.error'); } } + } catch (Ansel_Exception $e) { + $notification->push(_("Bad input."), 'horde.error'); } } - - // Return to the image list. - $imageurl = Horde_Util::addParameter('view.php', - array('view' => 'Results')); - header('Location: ' . Horde::applicationUrl($imageurl, true)); + header('Location: ' . Ansel::getUrlFor('view', array('view' => 'Results'), true)); exit; - // Tag related actions + /* Tag related actions */ case 'remove': $tag = Horde_Util::getFormData('tag'); if (isset($tag)) { @@ -199,7 +186,7 @@ class Ansel_View_Results extends Ansel_View_Base break; } - // Check for empty tag search and redirect if empty + /* Check for empty tag search and redirect if empty */ if ($this->_search->tagCount() < 1) { header('Location: ' . Horde::applicationUrl('browse.php', true)); exit; @@ -225,7 +212,7 @@ class Ansel_View_Results extends Ansel_View_Base */ public function html() { - global $conf, $prefs, $registry, $ansel_storage; + global $conf, $prefs, $ansel_storage; // Get the slice of galleries/images to view on this page. $results = $this->_search->getSlice($this->_page, $this->_perPage); -- 2.11.0