From 6582c75e79c538d3529adc38e5034a9f82dfc02a Mon Sep 17 00:00:00 2001 From: "Michael J. Rubinsky" Date: Mon, 17 Jan 2011 16:10:10 -0500 Subject: [PATCH] Improvements to obtaining galleries by slug --- ansel/lib/Gallery.php | 15 ++++++++++----- ansel/lib/Storage.php | 50 +++++++------------------------------------------- 2 files changed, 17 insertions(+), 48 deletions(-) diff --git a/ansel/lib/Gallery.php b/ansel/lib/Gallery.php index 926744d13..b9fea4582 100644 --- a/ansel/lib/Gallery.php +++ b/ansel/lib/Gallery.php @@ -136,11 +136,12 @@ class Ansel_Gallery extends Horde_Share_Object_Sql_Hierarchical implements Seria } // Check for slug uniqueness - $slugGalleryId = $GLOBALS['injector']->getInstance('Ansel_Storage')->getGalleryIdFromSlug($this->data['attribute_slug']); - if ($slugGalleryId > 0 && $slugGalleryId <> $this->id) { - throw InvalidArgumentException( - sprintf(_("Could not save gallery, the slug, \"%s\", already exists."), - $this->data['attribute_slug'])); + if (!empty($this->_oldSlug)) { + if ($GLOBALS['injector']->getInstance('Ansel_Storage')->galleryExists(null, $this->_data['attribute_slug'])) { + throw InvalidArgumentException( + sprintf(_("Could not save gallery, the slug, \"%s\", already exists."), + $this->data['attribute_slug'])); + } } if ($GLOBALS['conf']['ansel_cache']['usecache']) { @@ -917,6 +918,10 @@ class Ansel_Gallery extends Horde_Share_Object_Sql_Hierarchical implements Seria $this->_setModeHelper($mode); } + if ($driver_key == 'attribute_slug') { + $this->_oldSlug = $this->data['attribute_slug']; + } + /* Need to serialize the style object */ if ($driver_key == 'attribute_style') { $value = serialize($value); diff --git a/ansel/lib/Storage.php b/ansel/lib/Storage.php index 20a87d91f..8483602f7 100644 --- a/ansel/lib/Storage.php +++ b/ansel/lib/Storage.php @@ -224,45 +224,6 @@ class Ansel_Storage } /** - * Check that a slug exists. - * - * @param string $slug The slug name - * - * @return integer The share_id the slug represents, or 0 if not found. - */ - public function getGalleryIdFromSlug($slug) - { - // An empty slug should never match. - if (!strlen($slug)) { - return 0; - } - - $stmt = $this->_db->prepare('SELECT share_id FROM ' - . $this->_shares->getTable() . ' WHERE attribute_slug = ?'); - - if ($stmt instanceof PEAR_Error) { - Horde::logMessage($stmt, 'ERR'); - return 0; - } - - $result = $stmt->execute($slug); - if ($result instanceof PEAR_Error) { - Horde::logMessage($result, 'ERR'); - return 0; - } - if (!$result->numRows()) { - return 0; - } - - $slug = $result->fetchRow(); - - $result->free(); - $stmt->free(); - - return $slug[0]; - } - - /** * Retrieve an Ansel_Gallery given the gallery's slug * * @param string $slug The gallery slug @@ -274,12 +235,15 @@ class Ansel_Storage */ public function getGalleryBySlug($slug, $overrides = array()) { - $id = $this->getGalleryIdFromSlug($slug); - if ($id) { - return $this->getGallery($id, $overrides); - } else { + $shares = $this->_shares->listShares( + $GLOBALS['registry']->getAuth(), + array('attributes' => array('slug' => $slug))); + + if (!count($shares)) { throw new Horde_Exception_NotFound(sprintf(_("Gallery %s not found."), $slug)); } + + return current($shares); } /** -- 2.11.0