--- Ansel.php	2008-02-18 21:20:46.000000000 +0100
+++ /var/www/localhost/htdocs/horde/ansel/lib/Ansel.php	2008-02-19 17:51:08.000000000 +0100
@@ -1193,6 +1193,37 @@
     }
 
     /**
+     * Returns the number of all gallery_ids that $userid has access to.
+     *
+     * @param string  $userid       The userid of the user to check access for.
+     * @param integer $perm         The level of permissions required.
+     * @param mixed   $attributes   Restrict the shares counted to those
+     *                              matching $attributes. An array of
+     *                              attribute/values pairs or a share owner
+     *                              username.
+     * @param string  $parent       The parent share to start searching at.
+     * @param boolean $allLevels    Return all levels, or just the direct
+     *                              children of $parent? Defaults to all
+     *                              levels.
+     * @param integer $from         The share to start listing at.
+     * @param integer $count        The number of shares to return.
+     * @param string  $sortby_name  Attribute name to use for sorting.
+     * @param string  $sortby_key   Attribute key to use for sorting.
+     * @param integer $direction    Sort direction:
+     *                                0 - ascending
+     *                                1 - descending
+     *
+     * @return mixed  An array of Ansel_Gallery ids | PEAR_Error
+     */
+    function countGalleryIds($userid, $perm = PERMS_SHOW, $attributes = null,
+                           $parent = DATATREE_ROOT, $allLevels = true)
+    {
+        $criteria = $this->_getShareCriteria($userid, $perm, $attributes);
+        return $this->_datatree->countValuesByAttributes(
+                                    $criteria, $parent, $allLevels, 'id', null, 'gallery-id');
+    }
+
+    /**
      * Returns an array of all gallery_ids that $userid has access to.
      *
      * @param string  $userid       The userid of the user to check access for.
@@ -3583,32 +3614,38 @@
         } else {
             $owner = null;
         }
-        $galleryIds = $this->shares->getGalleryIds(Auth::getAuth(), $perm, $owner, $parent, $allLevels);
 
-        if (is_a($galleryIds, 'PEAR_Error')) {
-            $notification->push(
-                sprintf(_("An error occurred counting galleries: %s"),
-                        $galleryIds->getMessage()), 'horde.error');
+        if (is_array($attributes) && count($attributes)) {
 
-            Horde::logMessage($galleryIds->getMessage(), __FILE__, __LINE__,
-                              PEAR_LOG_ERR);
-            return 0;
-        } elseif (empty($galleryIds)) {
-            return 0;
-        }
+            $galleryIds = $this->shares->getGalleryIds(Auth::getAuth(), $perm, $owner, $parent, $allLevels);
 
-        if (is_array($attributes) && count($attributes)) {
-            $sql = 'SELECT count(*) FROM ansel_galleries WHERE gallery_id IN (' . str_repeat('?, ', count($galleryIds) - 1) . '?) ';
+            if (is_a($galleryIds, 'PEAR_Error')) {
+                $notification->push(
+                    sprintf(_("An error occurred counting galleries: %s"),
+                            $galleryIds->getMessage()), 'horde.error');
+
+                Horde::logMessage($galleryIds->getMessage(), __FILE__, __LINE__,
+                                PEAR_LOG_ERR);
+                return 0;
+            } elseif (empty($galleryIds)) {
+                return 0;
+            }
+
+            $sql = 'SELECT COUNT(*) FROM ansel_galleries WHERE gallery_id IN (' . str_repeat('?, ', count($galleryIds) - 1) . '?) ';
             $values = array_merge($galleryIds, array_values($attributes));
             $keys = array_keys($attributes);
             foreach ($keys as $key) {
                 $sql .= 'AND ' . $key . '= ? ';
             }
-        $q = $this->_db->prepare($sql);
-        $result = $q->execute($values);
-        $count = $result->fetchOne();
+
+            $q = $this->_db->prepare($sql);
+            $result = $q->execute($values);
+            $count = $result->fetchOne();
+
         } else {
-            $count = count($galleryIds);
+
+            $count = $this->shares->countGalleryIds(Auth::getAuth(), $perm, $owner, $parent, $allLevels);
+
         }
 
         return $count;