From c88749757133bf2910b1917ce884be02b979efef Mon Sep 17 00:00:00 2001 From: "Michael J. Rubinsky" Date: Mon, 17 Jan 2011 14:16:06 -0500 Subject: [PATCH] Start to reduce coupling between ansel code and share implementation use share methods everywhere possible, do not access tables directly --- ansel/lib/Ajax/Imple/GallerySlugCheck.php | 2 +- ansel/lib/Gallery.php | 2 +- ansel/lib/Storage.php | 28 ++++++++++------------------ 3 files changed, 12 insertions(+), 20 deletions(-) diff --git a/ansel/lib/Ajax/Imple/GallerySlugCheck.php b/ansel/lib/Ajax/Imple/GallerySlugCheck.php index 135917b03..3e08b75de 100644 --- a/ansel/lib/Ajax/Imple/GallerySlugCheck.php +++ b/ansel/lib/Ajax/Imple/GallerySlugCheck.php @@ -38,7 +38,7 @@ class Ansel_Ajax_Imple_GallerySlugCheck extends Horde_Core_Ajax_Imple return array('response' => '0'); } - $exists = $GLOBALS['injector']->getInstance('Ansel_Storage')->slugExists($slug) ? 0 : 1; + $exists = $GLOBALS['injector']->getInstance('Ansel_Storage')->galleryExists(null, $slug) ? 0 : 1; return array('response' => $exists); } diff --git a/ansel/lib/Gallery.php b/ansel/lib/Gallery.php index 14ff39c7e..926744d13 100644 --- a/ansel/lib/Gallery.php +++ b/ansel/lib/Gallery.php @@ -136,7 +136,7 @@ class Ansel_Gallery extends Horde_Share_Object_Sql_Hierarchical implements Seria } // Check for slug uniqueness - $slugGalleryId = $GLOBALS['injector']->getInstance('Ansel_Storage')->slugExists($this->data['attribute_slug']); + $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."), diff --git a/ansel/lib/Storage.php b/ansel/lib/Storage.php index e4bc99355..20a87d91f 100644 --- a/ansel/lib/Storage.php +++ b/ansel/lib/Storage.php @@ -115,7 +115,7 @@ class Ansel_Storage /* Check for slug uniqueness */ if (!empty($attributes['slug']) && - $this->slugExists($attributes['slug'])) { + $this->galleryExists(null, $attributes['slug'])) { throw new Horde_Exception(sprintf(_("The slug \"%s\" already exists."), $attributes['slug'])); } @@ -230,7 +230,7 @@ class Ansel_Storage * * @return integer The share_id the slug represents, or 0 if not found. */ - public function slugExists($slug) + public function getGalleryIdFromSlug($slug) { // An empty slug should never match. if (!strlen($slug)) { @@ -274,7 +274,7 @@ class Ansel_Storage */ public function getGalleryBySlug($slug, $overrides = array()) { - $id = $this->slugExists($slug); + $id = $this->getGalleryIdFromSlug($slug); if ($id) { return $this->getGallery($id, $overrides); } else { @@ -741,10 +741,8 @@ class Ansel_Storage } /** - * Check if a gallery exists. Need to do this here instead of Horde_Share - * since Horde_Share_Base::exists() takes a share_name, not a share_id. We - * might also be checking by gallery_slug and this is more efficient than - * a listShares() call for one gallery. + * Check if a gallery exists. Need to do this here so we can also check by + * gallery slug. * * @param integer $gallery_id The gallery id * @param string $slug The gallery slug @@ -752,21 +750,15 @@ class Ansel_Storage * @return boolean * @throws Ansel_Exception */ - public function galleryExists($gallery_id, $slug = null) + public function galleryExists($gallery_id = null, $slug = null) { if (empty($slug)) { - $results = $this->_db->queryOne( - 'SELECT COUNT(share_id) FROM ' . $this->_shares->getTable() - . ' WHERE share_id = ' . (int)$gallery_id); - if ($results instanceof PEAR_Error) { - throw new Ansel_Exception($results); - } - - return (bool)$results; + $results = $this->_shares->exists($gallery_id); } else { - - return (bool)$this->slugExists($slug); + $results = $this->_shares->countShares($GLOBALS['registry']->getAuth(), Horde_Perms::READ, array('slug' => $slug)); } + + return (bool)$results; } /** -- 2.11.0