Improvements to obtaining galleries by slug
authorMichael J. Rubinsky <mrubinsk@horde.org>
Mon, 17 Jan 2011 21:10:10 +0000 (16:10 -0500)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Mon, 17 Jan 2011 21:10:10 +0000 (16:10 -0500)
ansel/lib/Gallery.php
ansel/lib/Storage.php

index 926744d..b9fea45 100644 (file)
@@ -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);
index 20a87d9..8483602 100644 (file)
@@ -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);
      }
 
     /**