if (is_a($result = $image->createView($view, $style, false), 'PEAR_Error')) {
return Ansel::getErrorImage($view);
}
- $viewHash = $image->_getViewHash($view, $style) . '/'
+ $viewHash = $image->getViewHash($view, $style) . '/'
. $image->getVFSName($view);
}
}
/**
+ * Return a hash key for the given view and style.
+ *
+ * @param string $view The view (thumb, prettythumb etc...)
+ * @param string $style The named style.
+ *
+ * @return string A md5 hash suitable for use as a key.
+ */
+ static public function getViewHash($view, $style)
+ {
+ $style = Ansel::getStyleDefinition($style);
+
+ if ($view != 'screen' && $view != 'thumb' && $view != 'mini' &&
+ $view != 'full') {
+
+ $view = md5($style['thumbstyle'] . '.' . $style['background']);
+ }
+
+ return $view;
+ }
+
+ /**
* Add a custom stylesheet to the current page. Need our own implementation
* since we want to be able to ouput specific CSS files at specific times
* (like when rendering embedded content, or calling via the api etc...).
class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
{
/**
- * Cache the Gallery Id - to match the Ansel_Image interface
- */
- var $id;
-
- /**
* The gallery mode helper
*
* @var Ansel_Gallery_Mode object
*/
- var $_modeHelper;
-
- /**
- *
- */
- function __sleep()
- {
- $properties = get_object_vars($this);
- unset($properties['_shareOb']);
- unset($properties['_modeHelper']);
- $properties = array_keys($properties);
- return $properties;
- }
-
- function __wakeup()
- {
- $this->setShareOb($GLOBALS['ansel_storage']->shares);
- $mode = $this->get('view_mode');
- $this->_setModeHelper($mode);
- }
+ protected $_modeHelper;
/**
* The Ansel_Gallery constructor.
*
* @param string $name The name of the gallery
*/
- function Ansel_Gallery($attributes = array())
+ public function __construct($attributes = array())
{
- /* Existing gallery? */
- if (!empty($attributes['share_id'])) {
- $this->id = (int)$attributes['share_id'];
- }
-
/* Pass on up the chain */
parent::Horde_Share_Object_sql_hierarchical($attributes);
$this->setShareOb($GLOBALS['ansel_storage']->shares);
$this->_setModeHelper($mode);
}
+ public function __get($property)
+ {
+ switch ($property) {
+ case 'id':
+ return $this->_getId();
+ default:
+ return null;
+ }
+ }
+
/**
* Check for special capabilities of this gallery.
*
*/
- function hasFeature($feature)
+ public function hasFeature($feature)
{
// First check for purely Ansel_Gallery features
}
/**
- * Simple factory to retrieve the proper mode object.
+ * Simple factory to set the proper mode object.
*
* @param string $type The mode to use
*
* @return Ansel_Gallery_Mode object
*/
- function _setModeHelper($type = 'Normal')
+ protected function _setModeHelper($type = 'Normal')
{
$type = basename($type);
$class = 'Ansel_GalleryMode_' . $type;
*
* @return boolean Whether or not user can download full photos
*/
- function canDownload()
+ public function canDownload()
{
if (Horde_Auth::getAuth() == $this->data['share_owner'] || Horde_Auth::isAdmin('ansel:admin')) {
return true;
/**
* Saves any changes to this object to the backend permanently.
*
+ * @TODO: this needs to stay public b/c Horde_Share_Object (which this extends)
+ * is not yet ported to PHP5. This will be fixed when we no longer
+ * extend Horde_Share_Object here.
+ *
* @return mixed true || PEAR_Error on failure.
*/
function _save()
if (!empty($this->data['attribute_slug']) &&
preg_match('/[^a-zA-Z0-9_@]/', $this->data['attribute_slug'])) {
+ // TODO: Need to keep the pear error here since Horde_Share still
+ // uses them.
return PEAR::raiseError(
sprintf(_("Could not save gallery, the slug, \"%s\", contains invalid characters."),
$this->data['attribute_slug']));
* @param boolean $add Action to take (add or remove)
* @param integer $gallery_id Gallery id to update images for
*/
- function _updateImageCount($images, $add = true, $gallery_id = null)
+ public function updateImageCount($images, $add = true, $gallery_id = null)
{
// We do the query directly here to avoid having to instantiate a
// gallery object just to increment/decrement one value in the table.
+ // TODO: Change this - should always use the appropriate object, not
+ // direct manipulation of the share table...
$sql = 'UPDATE ' . $this->_shareOb->_table
. ' SET attribute_images = attribute_images '
. ($add ? ' + ' : ' - ') . $images . ' WHERE share_id = '
*
* @return integer The id of the new image.
*/
- function addImage($image_data, $default = false)
+ public function addImage($image_data, $default = false)
{
global $conf;
+ //@TODO: Maybe addImage() gets moved to the modeHelper delegate?
/* Normal is the only view mode that can accurately update gallery counts */
$vMode = $this->get('view_mode');
if ($vMode != 'Normal') {
/* Create the image object */
$image = new Ansel_Image($image_data);
$result = $image->save();
- if (is_a($result, 'PEAR_Error')) {
- return $result;
- }
-
if (empty($image_data['image_id'])) {
- $this->_updateImageCount(1);
+ $this->updateImageCount(1);
if ($this->countImages() < 5) {
$resetStack = true;
}
* Clear all of this gallery's default image stacks from the VFS and the
* gallery's data store.
*
+ * @return void
*/
- function clearStacks()
+ public function clearStacks()
{
$ids = @unserialize($this->data['attribute_default_prettythumb']);
if (is_array($ids)) {
* Removes all generated and cached 'prettythumb' thumbnails for this
* gallery
*
+ * @return void
*/
- function clearThumbs()
+ public function clearThumbs()
{
$images = $this->listImages();
foreach ($images as $id) {
/**
* Removes all generated and cached views for this gallery
*
+ * @return void
*/
- function clearViews()
+ public function clearViews()
{
$images = $this->listImages();
foreach ($images as $id) {
*
* @return integer | PEAR_Error The number of images moved, or an error message.
*/
- function moveImagesTo($images, $gallery)
+ public function moveImagesTo($images, $gallery)
{
return $this->_modeHelper->moveImagesTo($images, $gallery);
}
* @param array $images An array of image ids.
* @param Ansel_Gallery $gallery The gallery to copy images to.
*
- * @return integer | PEAR_Error The number of images copied or error message
+ * @return integer The number of images copied
+ * @throws Horde_Exception
*/
- function copyImagesTo($images, $gallery)
+ public function copyImagesTo($images, $gallery)
{
if (!$gallery->hasPermission(Horde_Auth::getAuth(), PERMS_EDIT)) {
- return PEAR::raiseError(
- sprintf(_("Access denied copying photos to \"%s\"."),
- $gallery->get('name')));
+ throw new Horde_Exception(sprintf(_("Access denied copying photos to \"%s\"."), $gallery->get('name')));
}
$db = $this->_shareOb->_write_db;
'image_filename' => $img->filename,
'image_type' => $img->getType(),
'image_uploaded_date' => $img->uploaded));
- if (is_a($newId, 'PEAR_Error')) {
- return $newId;
- }
/* Copy any tags */
// Since we know that the tags already exist, no need to
// go through Ansel_Tags::writeTags() - this saves us a SELECT query
$tags = $img->getTags();
$query = $this->_shareOb->_write_db->prepare('INSERT INTO ansel_images_tags (image_id, tag_id) VALUES(' . $newId . ',?);');
if (is_a($query, 'PEAR_Error')) {
- return $query;
+ throw new Horde_Exception($query->getMessage());
}
foreach ($tags as $tag_id => $tag_name) {
$result = $query->execute($tag_id);
if (is_a($result, 'PEAR_Error')) {
- return $result;
+ throw new Horde_Exception($result->getMessge());
}
}
$query->free();
if (is_array($exif) && count($exif) > 0) {
$insert = $db->prepare('INSERT INTO ansel_image_attributes (image_id, attr_name, attr_value) VALUES (?, ?, ?)');
if (is_a($insert, 'PEAR_Error')) {
- return $insert;
+ throw new Horde_Exception($insert->getMessage());
}
foreach ($exif as $attr){
$result = $insert->execute(array($newId, $attr['attr_name'], $attr['attr_value']));
if (is_a($result, 'PEAR_Error')) {
- return $result;
+ throw new Horde_Exception($result->getMessage());
}
}
$insert->free();
* @param integer $imageId The image to sort.
* @param integer $pos The sort position of the image.
*/
- function setImageOrder($imageId, $pos)
+ public function setImageOrder($imageId, $pos)
{
return $this->_shareOb->_write_db->exec('UPDATE ansel_images SET image_sort = ' . (int)$pos . ' WHERE image_id = ' . (int)$imageId);
}
*
* @return boolean True on success, false on failure.
*/
- function removeImage($image, $isStack = false)
+ public function removeImage($image, $isStack = false)
{
return $this->_modeHelper->removeImage($image, $isStack);
}
/**
* Returns this share's owner's Identity object.
*
+ * @TODO: Maybe rename this to getIdentity() to avoid confusion with
+ * the share's owner attribute?
+ *
* @return Identity object for the owner of this gallery.
*/
- function getOwner()
+ public function getOwner()
{
require_once 'Horde/Identity.php';
$identity = Identity::singleton('none', $this->data['share_owner']);
* @param array $params Any additional parameters the Ansel_Tile
* object may need.
*/
- function getTile($parent = null, $style = null, $mini = false,
+ public function getTile($parent = null, $style = null, $mini = false,
$params = array())
{
if (!is_null($parent) && is_null($style)) {
* @return A mixed array of Ansel_Gallery and Ansel_Image objects that are
* children of this gallery.
*/
- function getGalleryChildren($perm = PERMS_SHOW, $from = 0, $to = 0, $noauto = true)
+ public function getGalleryChildren($perm = PERMS_SHOW, $from = 0, $to = 0, $noauto = true)
{
return $this->_modeHelper->getGalleryChildren($perm, $from, $to, $noauto);
}
*
* @return integer The count of this gallery's children.
*/
- function countGalleryChildren($perm = PERMS_SHOW, $galleries_only = false, $noauto = true)
+ public function countGalleryChildren($perm = PERMS_SHOW, $galleries_only = false, $noauto = true)
{
return $this->_modeHelper->countGalleryChildren($perm, $galleries_only, $noauto);
}
* @param integer $from The image to start listing.
* @param integer $count The numer of images to list.
*
- * @return mixed An array of image_ids | PEAR_Error
+ * @return array An array of image_ids
*/
- function listImages($from = 0, $count = 0)
+ public function listImages($from = 0, $count = 0)
{
return $this->_modeHelper->listImages($from, $count);
}
*
* @param mixed An array of Ansel_Image objects | PEAR_Error
*/
- function getImages($from = 0, $count = 0)
+ public function getImages($from = 0, $count = 0)
{
return $this->_modeHelper->getImages($from, $count);
}
*
* @return mixed An array of Ansel_Image objects | PEAR_Error
*/
- function getRecentImages($limit = 10)
+ public function getRecentImages($limit = 10)
{
return $GLOBALS['ansel_storage']->getRecentImages(array($this->id),
$limit);
*
* @return Ansel_Image The image object corresponding to the given id.
*/
- function &getImage($id)
+ public function &getImage($id)
{
return $GLOBALS['ansel_storage']->getImage($id);
}
/**
* Checks if the gallery has any subgallery
*/
- function hasSubGalleries()
+ public function hasSubGalleries()
{
return $this->_modeHelper->hasSubGalleries();
}
*
* @return integer number of images in this gallery
*/
- function countImages($subgalleries = false)
+ public function countImages($subgalleries = false)
{
return $this->_modeHelper->countImages($subgalleries);
}
/**
* Returns the default image for this gallery.
*
+ * @TODO: Rename default images to 'key' images - they really are not
+ * 'default' in any sense.
+ *
* @param string $style Force the use of this style, if it's available
* otherwise use whatever style is choosen for this
* gallery. If prettythumbs are not available then
*
* @return mixed The image_id of the default image or false.
*/
- function getDefaultImage($style = null)
+ public function getDefaultImage($style = null)
{
// Check for explicitly requested style
if (!is_null($style)) {
$gal_style['default_galleryimage_type'] != 'plain') {
$thumbstyle = $gal_style['default_galleryimage_type'];
- $styleHash = $this->_getViewHash($thumbstyle, $style);
+ $styleHash = $this->getViewHash($thumbstyle, $gal_style['name']);
// First check for the existence of a default image in the style
// we are looking for.
// Don't already have one, must generate it.
$params = array('gallery' => $this, 'style' => $gal_style);
- $iview = Ansel_ImageView::factory(
- $gal_style['default_galleryimage_type'], $params);
-
- if (!is_a($iview, 'PEAR_Error')) {
+ try {
+ $iview = Ansel_ImageView::factory($gal_style['default_galleryimage_type'], $params);
$img = $iview->create();
- if (!is_a($img, 'PEAR_Error')) {
- // Note the gallery_id is negative for generated stacks
- $iparams = array('image_filename' => $this->get('name'),
- 'image_caption' => $this->get('name'),
- 'data' => $img->raw(),
- 'image_sort' => 0,
- 'gallery_id' => -$this->id);
- $newImg = new Ansel_Image($iparams);
- $newImg->save();
- $prettyData = serialize(
- array_merge($thumbs,
- array($styleHash => $newImg->id)));
-
- $this->set('default_prettythumb', $prettyData, true);
- return $newImg->id;
+ if ($img) {
+ // Note the gallery_id is negative for generated stacks
+ $iparams = array('image_filename' => $this->get('name'),
+ 'image_caption' => $this->get('name'),
+ 'data' => $img->raw(),
+ 'image_sort' => 0,
+ 'gallery_id' => -$this->id);
+ $newImg = new Ansel_Image($iparams);
+ $newImg->save();
+ $prettyData = serialize(
+ array_merge($thumbs,
+ array($styleHash => $newImg->id)));
+
+ $this->set('default_prettythumb', $prettyData, true);
+ return $newImg->id;
} else {
Horde::logMessage($img, __FILE__, __LINE__, PEAR_LOG_ERR);
}
- } else {
+
+ } catch (Horde_Exception $e) {
// Might not support the requested style...try ansel_default
// but protect against infinite recursion.
Horde::logMessage($iview, __FILE__, __LINE__, PEAR_LOG_DEBUG);
}
Horde::logMessage($iview, __FILE__, __LINE__, PEAR_LOG_ERR);
}
+
} else {
// We are just using an image thumbnail for the gallery default.
if ($this->countImages()) {
return $this->data['attribute_default'];
}
$keys = $this->listImages();
- if (is_a($keys, 'PEAR_Error')) {
- return $keys;
- }
$this->data['attribute_default'] = $keys[count($keys) - 1];
$this->data['attribute_default_type'] = 'auto';
$this->save();
if ($this->hasSubGalleries()) {
// Fall through to a default image of a sub gallery.
- $galleries = $GLOBALS['ansel_storage']->listGalleries(
- PERMS_SHOW, null, $this, false);
- if ($galleries && !is_a($galleries, 'PEAR_Error')) {
+ try {
+ $galleries = $GLOBALS['ansel_storage']->listGalleries(PERMS_SHOW, null, $this, false);
+ } catch (Horde_Exception $e) {
+ return false;
+ }
+ if ($galleries) {
foreach ($galleries as $galleryId => $gallery) {
if ($default_img = $gallery->getDefaultImage($style)) {
return $default_img;
}
}
}
+
return false;
}
/**
* Returns this gallery's tags.
+ *
+ * @return array of tag info
+ * @throws Horde_Exception
*/
- function getTags() {
+ public function getTags() {
if ($this->hasPermission(Horde_Auth::getAuth(), PERMS_READ)) {
return Ansel_Tags::readTags($this->id, 'gallery');
} else {
- return PEAR::raiseError(_("Access denied viewing this gallery."));
+ throw new Horde_Exception(_("Access denied viewing this gallery."));
}
}
/**
* Set/replace this gallery's tags.
*
- * @param array $tags AN array of tag names to associate with this image.
+ * @param array $tags An array of tag names to associate with this image.
+ *
+ * @return true on success
+ * @throws Horde_Exception
*/
- function setTags($tags)
+ public function setTags($tags)
{
if ($this->hasPermission(Horde_Auth::getAuth(), PERMS_EDIT)) {
return Ansel_Tags::writeTags($this->id, $tags, 'gallery');
} else {
- return PEAR::raiseError(_("Access denied adding tags to this gallery."));
+ throw new Horde_Exception(_("Access denied adding tags to this gallery."));
}
}
*
* @return array The style definition array.
*/
- function getStyle()
+ public function getStyle()
{
if (empty($this->data['attribute_style'])) {
$style = $GLOBALS['prefs']->getValue('default_gallerystyle');
*
* @return string A md5 hash suitable for use as a key.
*/
- function _getViewHash($view, $style = null)
+ public function getViewHash($view, $style = null)
{
- if (is_null($style)) {
- $style = $this->getStyle();
- } else {
- $style = Ansel::getStyleDefinition($style);
+ if (empty($style)) {
+ $style = $this->data['attribute_style'];
}
- if ($view != 'screen' && $view != 'thumb' && $view != 'mini' &&
- $view != 'full') {
- $view = md5($style['thumbstyle'] . '.' . $style['background']);
- }
- return $view;
+ return Ansel::getViewHash($view, $style);
}
+
/**
* Checks to see if a user has a given permission.
*
*
* @return boolean Whether or not $userid has $permission.
*/
- function hasPermission($userid, $permission, $creator = null)
+ public function hasPermission($userid, $permission, $creator = null)
{
if ($userid == $this->data['share_owner'] ||
Horde_Auth::isAdmin('ansel:admin')) {
*
* @return boolean
*/
- function isOldEnough()
+ public function isOldEnough()
{
if ($this->data['share_owner'] == Horde_Auth::getAuth() ||
empty($GLOBALS['conf']['ages']['limits']) ||
*
* @return boolean
*/
- function hasPasswd()
+ public function hasPasswd()
{
if (Horde_Auth::getAuth() == $this->get('owner') || Horde_Auth::isAdmin('ansel:admin')) {
return false;
/**
* Sets this gallery's parent gallery.
*
- * @TODO: Check how this interacts with date galleries - shouldn't be able
- * to remove a subgallery from a date gallery anyway, but just incase
- * @param mixed $parent An Ansel_Gallery or a gallery_id.
+ * @param mixed $parent An Ansel_Gallery or a gallery_id.
*
- * @return mixed Ture || PEAR_Error
+ * @return boolean true on sucess
+ * @throws Horde_Exception
*/
- function setParent($parent)
+ public function setParent($parent)
{
/* Make sure we have a gallery object */
if (!is_null($parent) && !is_a($parent, 'Ansel_Gallery')) {
$parent = $GLOBALS['ansel_storage']->getGallery($parent);
- if (is_a($parent, 'PEAR_Error')) {
- return $parent;
- }
}
/* Check this now since we don't know if we are updating the DB or not */
/* Call the parent class method */
$result = parent::setParent($parent);
if (is_a($result, 'PEAR_Error')) {
- return $result;
+ // Horde_Share still uses PEAR_Error
+ throw new Horde_Exception($result->getMessage());
}
/* Tell the parent the good news */
* @return mixed True if setting the attribute did succeed, a PEAR_Error
* otherwise.
*/
- function set($attribute, $value, $update = false)
+ public function set($attribute, $value, $update = false)
{
/* Translate the keys */
if ($attribute == 'owner') {
}
$result = $query->execute(array($data[$driver_key], $this->id));
$query->free();
+ if (is_a($result, 'PEAR_Error')) {
+ throw new Horde_Exception($result->getMessage());
+ }
- return $result;
}
return true;
}
- function setDate($date)
+ public function setDate($date)
{
$this->_modeHelper->setDate($date);
}
- function getDate()
+ public function getDate()
{
return $this->_modeHelper->getDate();
}
* @return An array of 'title' and 'navdata' hashes with the [0] element
* being the deepest part.
*/
- function getGalleryCrumbData()
+ public function getGalleryCrumbData()
{
return $this->_modeHelper->getGalleryCrumbData();
}
+ /**
+ *
+ */
+ public function __sleep()
+ {
+ $properties = get_object_vars($this);
+ unset($properties['_shareOb']);
+ unset($properties['_modeHelper']);
+ $properties = array_keys($properties);
+ return $properties;
+ }
+
+ public function __wakeup()
+ {
+ $this->setShareOb($GLOBALS['ansel_storage']->shares);
+ $mode = $this->get('view_mode');
+ $this->_setModeHelper($mode);
+ }
+
}
*/
if ($this->_gallery->get('has_subgalleries')) {
$gallery_ids = array();
- $images = $GLOBALS['ansel_storage']->getImages($ids);
+ $images = $GLOBALS['ansel_storage']->getImages(array('ids' => $ids));
foreach ($images as $image) {
if (empty($gallery_ids[$image->gallery])) {
$gallery_ids[$image->gallery] = 1;
/* Update the gallery counts for each affected gallery */
if ($this->_gallery->get('has_subgalleries')) {
foreach ($gallery_ids as $id => $count) {
- $this->_gallery->_updateImageCount($count, false, $id);
+ $this->_gallery->updateImageCount($count, false, $id);
}
} else {
- $this->_gallery->_updateImageCount(count($ids), false);
+ $this->_gallery->updateImageCount(count($ids), false);
}
- $this->_gallery->_updateImageCount(count($ids), true, $gallery->id);
+ $this->_gallery->updateImageCount(count($ids), true, $gallery->id);
/* Expire the cache since we have no reason to save() the gallery */
if ($GLOBALS['conf']['ansel_cache']['usecache']) {
$this->_gallery->_shareOb->_write_db->exec('DELETE FROM ansel_images_geolocation WHERE image_id = ' . (int)$image->id);
if (!$isStack) {
- $this->_gallery->_updateImageCount(1, false, $image_gallery);
+ $this->_gallery->updateImageCount(1, false, $image_gallery);
}
/* Update the modified flag if we are not a stack image */
$ids = array_merge($ids, $child->_images);
}
$ids = $this->_getArraySlice($ids, $from, $count);
- $images = $GLOBALS['ansel_storage']->getImages($ids);
+ $images = $GLOBALS['ansel_storage']->getImages(array('ids' => $ids));
}
return $images;
*
* @return string A md5 hash suitable for use as a key.
*/
- function _getViewHash($view, $style = null)
+ function getViewHash($view, $style = null)
{
- return $this->_gallery->_getViewHash($view, $style);
+ return $this->_gallery->getViewHash($view, $style);
}
/**
return $result;
}
- $this->_gallery->_updateImageCount(count($ids), false);
- $this->_gallery->_updateImageCount(count($ids), true, $gallery->id);
+ $this->_gallery->updateImageCount(count($ids), false);
+ $this->_gallery->updateImageCount(count($ids), true, $gallery->id);
/* Expire the cache since we have no reason to save() the gallery */
if ($GLOBALS['conf']['ansel_cache']['usecache']) {
$this->_gallery->_shareOb->_write_db->exec('DELETE FROM ansel_image_attributes WHERE image_id = ' . (int)$image->id);
if (!$isStack) {
- $this->_gallery->_updateImageCount(1, false);
+ $this->_gallery->updateImageCount(1, false);
}
/* Remove any geolocation data */
* @param integer $from The image to start fetching.
* @param integer $count The numer of images to return.
*
- * @param mixed An array of Ansel_Image objects | PEAR_Error
+ * @param mixed An array of Ansel_Image objects
*/
function getImages($from = 0, $count = 0)
{
$images = $GLOBALS['ansel_storage']->getImages(array('gallery_id' => $this->_gallery->id,
'count' => $count,
'from' => $from));
+
return array_values($images);
}
*/
function getVFSPath($view = 'full', $style = null)
{
- $view = $this->_getViewHash($view, $style);
+ $view = $this->getViewHash($view, $style);
return '.horde/ansel/'
. substr(str_pad($this->id, 2, 0, STR_PAD_LEFT), -2)
. '/' . $view;
return true;
}
- $viewHash = $this->_getViewHash($view, $style);
+ $viewHash = $this->getViewHash($view, $style);
/* If we've already loaded the data, just return now. */
if (!empty($this->_loaded[$viewHash])) {
return true;
}
/* Get the VFS path. */
- $view = Ansel_Gallery::_getViewHash($view, $style);
+ $view = Ansel_Gallery::getViewHash($view, $style);
/* Can't call the various vfs methods here, since this method needs
to be called statically */
return $res;
}
- $view = $this->_getViewHash($view, $style);
+ $view = $this->getViewHash($view, $style);
$this->_data[$view] = $this->_image->raw();
$this->_image->loadString($vfspath . '/' . $this->id,
if (is_a($imageFile, 'PEAR_Error')) {
return $imageFile;
}
- $exif = Horde_Image_Exif::factory($GLOBALS['conf']['exif'], $GLOBALS['conf']['exif']['params']);
+ $exif = Horde_Image_Exif::factory($GLOBALS['conf']['exif']['driver'], $GLOBALS['conf']['exif']['params']);
$exif_fields = $exif->getData($imageFile);
/* Flag to determine if we need to resave the image data */
*
* @return string A md5 hash suitable for use as a key.
*/
- function _getViewHash($view, $style = null)
+ function getViewHash($view, $style = null)
{
global $ansel_storage;
return $view;
}
+
if (is_null($style)) {
$gallery = $ansel_storage->getGallery(abs($this->gallery));
if (is_a($gallery, 'PEAR_Error')) {
} else {
$style = Ansel::getStyleDefinition($style);
}
-
$view = md5($style['thumbstyle'] . '.' . $style['background']);
+
return $view;
}
return $this->_create();
}
+ /**
+ *
+ * @param string $type The type of concrete instance to return.
+ * @param array $params Additional parameters needed for the instance.
+ *
+ * @return Ansel_ImageView
+ * @throws Horde_Exception
+ */
function factory($type, $params = array())
{
$type = basename($type);
// requested effect.
foreach ($view->need as $need) {
if (!Ansel::isAvailable($need)) {
- $err = PEAR::raiseError(_("This install does not support the %s feature. Please contact your administrator."), $need);
Horde::logMessage($err, __FILE__, __LINE__, PEAR_LOG_ERR);
- return $err;
+ throw new Horde_Exception(_("This install does not support the %s feature. Please contact your administrator."), $need);
}
}
return $view;
} else {
- $err = PEAR::raiseError(sprintf(_("Unable to load the definition of %s."), $class));
Horde::logMessage($err, __FILE__, __LINE__, PEAR_LOG_ERR);
- return $err;
+ throw new Horde_Exception(sprintf(_("Unable to load the definition of %s."), $class));
}
}
return $result;
}
}
+
return $parent;
}
$cnt = min(5, $gallery->countImages());
$default = $gallery->get('default');
if (!empty($default) && $default > 0) {
- $img = &$gallery->getImage($default);
- if (!is_a($img, 'PEAR_Error')) {
+ try {
+ $img = &$gallery->getImage($default);
$images[] = &$gallery->getImage($default);
$cnt--;
- }
+ } catch (Horde_Exception $e) {}
}
for ($i = 0; $i < $cnt; $i++) {
$rnd = mt_rand(0, $cnt);
- $temp = $gallery->getImages($rnd, 1);
- if (!is_a($temp, 'PEAR_Error') && count($temp)) {
- $images[] = array_shift($temp);
- }
+ try {
+ $temp = $gallery->getImages($rnd, 1);
+ $images[] = array_shift($temp);
+ } catch (Horde_Exception $e) {}
}
// Reverse the array to ensure the requested default image
* defaults are not desirable.
* @param mixed $parent The gallery id of the parent (if any)
*
- * @return Ansel_Gallery A new gallery object or PEAR_Error.
+ * @return Ansel_Gallery A new gallery object.
+ * @throws Horde_Exception
*/
public function createGallery($attributes = array(), $perm = null, $parent = null)
{
/* Check for slug uniqueness */
if (!empty($attributes['slug']) &&
$this->slugExists($attributes['slug'])) {
- return PEAR::raiseError(sprintf(_("The slug \"%s\" already exists."),
- $attributes['slug']));
+ throw new Horde_Exception(sprintf(_("The slug \"%s\" already exists."), $attributes['slug']));
}
/* Create the gallery */
$gallery = $this->_shares->newShare('');
- if (is_a($gallery, 'PEAR_Error')) {
+ if ($gallery instanceof PEAR_Error) {
Horde::logMessage($gallery, __FILE__, __LINE__, PEAR_LOG_ERR);
- return $gallery;
+ throw new Horde_Exception($gallery->getMessage());
}
Horde::logMessage('New Ansel_Gallery object instantiated', __FILE__, __LINE__, PEAR_LOG_DEBUG);
if ($GLOBALS['conf']['ansel_cache']['usecache']) {
$GLOBALS['cache']->expire('Ansel_Gallery' . $parent);
}
- if (is_a($result, 'PEAR_Error')) {
- Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR);
- return $result;
- }
}
/* Fill up the new gallery */
/* Save it to storage */
$result = $this->_shares->addShare($gallery);
- if (is_a($result, 'PEAR_Error')) {
+ if ($result instanceof PEAR_Error) {
$error = sprintf(_("The gallery \"%s\" could not be created: %s"),
$attributes['name'], $result->getMessage());
Horde::logMessage($error, __FILE__, __LINE__, PEAR_LOG_ERR);
- return PEAR::raiseError($error);
+ throw new Horde_Exception($error);
}
/* Convenience */
if ($perms) {
$groups = Group::singleton();
$group_list = $groups->getGroupMemberships(Horde_Auth::getAuth());
- if (!is_a($group_list, 'PEAR_Error') && count($group_list)) {
+ if (!($group_list instanceof PEAR_Error) && count($group_list)) {
foreach ($group_list as $group_id => $group_name) {
$perm->addGroupPermission($group_id, $perms, false);
}
$stmt = $this->_db->prepare('SELECT share_id FROM '
. $this->_shares->_table . ' WHERE attribute_slug = ?');
- if (is_a($stmt, 'PEAR_Error')) {
+ if ($stmt instanceof PEAR_Error) {
Horde::logMessage($stmt, __FILE__, __LINE__, PEAR_LOG_ERR);
return 0;
}
$result = $stmt->execute($slug);
- if (is_a($result, 'PEAR_Error')) {
+ if ($result instanceof PEAR_Error) {
Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR);
+ return 0;
}
if (!$result->numRows()) {
return 0;
* @param array $overrides An array of attributes that should be overridden
* when the gallery is returned.
*
- * @return mixed Ansel_Gallery object | PEAR_Error
+ * @return Ansel_Gallery object
+ * @throws Horde_Exception
*/
public function &getGalleryBySlug($slug, $overrides = array())
{
if ($id) {
return $this->getGallery($id, $overrides);
} else {
- return PEAR::raiseError(sprintf(_("Gallery %s not found."), $slug));
+ throw new Horde_Exception(sprintf(_("Gallery %s not found."), $slug));
}
}
* @param array $overrides An array of attributes that should be
* overridden when the gallery is returned.
*
- * @return mixed Ansel_Gallery | PEAR_Error
+ * @return Ansel_Gallery
+ * @throws Horde_Exception
*/
public function &getGallery($gallery_id, $overrides = array())
{
}
$result = &$this->_shares->getShareById($gallery_id);
- if (is_a($result, 'PEAR_Error')) {
- return $result;
+ if ($result instanceof PEAR_Error) {
+ throw new Horde_Exception($result->getMessage());
}
$this->_galleries[$gallery_id] = &$result;
$this->_galleries[$gallery_id]->set($key, $value, false);
}
}
+
return $this->_galleries[$gallery_id];
}
*
* @param array $slugs The gallery slugs
*
- * @return mixed Array of Ansel_Gallery objects | PEAR_Error
+ * @return array of Ansel_Gallery objects
+ * @throws Horde_Exception
*/
public function getGalleriesBySlugs($slugs)
{
. ' WHERE attribute_slug IN (' . str_repeat('?, ', count($slugs) - 1) . '?)';
$stmt = $this->_shares->_db->prepare($sql);
- if (is_a($stmt, 'PEAR_Error')) {
- return $stmt;
+ if ($stmt instanceof PEAR_Error) {
+ throw new Horde_Exception($stmt->getMessage());
}
$result = $stmt->execute($slugs);
- if (is_a($result, 'PEAR_Error')) {
- return $result;
+ if ($result instanceof PEAR_Error) {
+ throw new Horde_Exception($result->getMessage());
}
$ids = array_values($result->fetchCol());
$shares = $this->_shares->getShares($ids);
/**
* Retrieve an array of Ansel_Gallery objects for the requested ids
+ *
+ * @return array of Ansel_Gallery objects
*/
public function getGalleries($ids)
{
if ($GLOBALS['conf']['ansel_cache']['usecache']) {
$GLOBALS['cache']->expire('Ansel_OtherGalleries' . $gallery->get('owner'));
}
-
}
/**
*
* @param Ansel_Gallery $gallery The gallery to delete
*
- * @return mixed True || PEAR_Error
+ * @return boolean true on success
+ * @throws Horde_Exception
*/
public function removeGallery($gallery)
{
/* Get any children and empty them */
$children = $gallery->getChildren(null, true);
- if (is_a($children, 'PEAR_Error')) {
- return $children;
- }
foreach ($children as $child) {
$this->emptyGallery($child);
$child->setTags(array());
/* Delete the gallery from storage */
$result = $this->_shares->removeShare($gallery);
- if (is_a($result, 'PEAR_Error')) {
- return $result;
+ if ($result instanceof PEAR_Error) {
+ throw new Horde_Exception($result->getMessage());
}
/* Expire the cache */
unset($this->_galleries[$id]);
/* See if we need to clear the has_subgalleries field */
- if (is_a($parent, 'Ansel_Gallery')) {
+ if ($parent instanceof Ansel_Gallery) {
if (!$parent->countChildren(PERMS_SHOW, false)) {
$parent->set('has_subgalleries', 0, true);
-
if ($GLOBALS['conf']['ansel_cache']['usecache']) {
$GLOBALS['cache']->expire('Ansel_Gallery' . $parent->id);
}
* @param integer $id The ID of the image to retrieve.
*
* @return Ansel_Image The image object corresponding to the given name.
+ * @throws Horde_Exception
*/
public function &getImage($id)
{
}
$q = $this->_db->prepare('SELECT ' . $this->_getImageFields() . ' FROM ansel_images WHERE image_id = ?');
- if (is_a($q, 'PEAR_Error')) {
+ if ($q instanceof PEAR_Error) {
Horde::logMessage($q, __FILE__, __LINE__, PEAR_LOG_ERR);
- return $q;
+ throw new Horde_Exception($q->getMessage());
}
$result = $q->execute((int)$id);
- if (is_a($result, 'PEAR_Error')) {
+ if ($result instanceof PEAR_Error) {
Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR);
- return $result;
+ throw new Horde_Exception($result->getMessage());
}
$image = $result->fetchRow(MDB2_FETCHMODE_ASSOC);
$q->free();
$result->free();
if (is_null($image)) {
- return PEAR::raiseError(_("Photo not found"));
- } elseif (is_a($image, 'PEAR_Error')) {
+ throw new Horde_Exception(_("Photo not found"));
+ } elseif ($image instanceof PEAR_Error) {
Horde::logMessage($image, __FILE__, __LINE__, PEAR_LOG_ERR);
- return $image;
+ throw new Horde_Exception($image->getMessage());
} else {
$image['image_filename'] = Horde_String::convertCharset($image['image_filename'], $GLOBALS['conf']['sql']['charset']);
$image['image_caption'] = Horde_String::convertCharset($image['image_caption'], $GLOBALS['conf']['sql']['charset']);
* </pre>
*
* @return array of Ansel_Image objects.
+ * @throws Horde_Exception
*/
public function getImages($params = array())
{
}
$images = $this->_db->query($sql);
- if (is_a($images, 'PEAR_Error')) {
- return $images;
- } elseif ($images->numRows() == 0) {
+ if ($images instanceof PEAR_Error) {
+ throw new Horde_Exception($images->getMessage());
+ } elseif ($images->numRows() == 0 && empty($params['gallery_id'])) {
$images->free();
- return PEAR::raiseError(_("Photos not found"));
+ throw new Horde_Exception(_("Images not found"));
+ } elseif ($images->numRows() == 0) {
+ return array();
}
$return = array();
/* Need to get comment counts if comments are enabled */
$ccounts = $this->_getImageCommentCounts(array_keys($return));
- if (!is_a($ccounts, 'PEAR_Error') && count($ccounts)) {
+ if (!($ccounts instanceof PEAR_Error) && count($ccounts)) {
foreach ($return as $key => $image) {
$return[$key]->commentCount = (!empty($ccounts[$key]) ? $ccounts[$key] : 0);
}
* @param string $where Additional where clause
*
* @return array An array of Ansel_Image objects
+ * @throws Horde_Exception
*/
public function getRecentImages($galleries = array(), $limit = 10, $slugs = array())
{
$sql .= ' ORDER BY image_uploaded_date DESC LIMIT ' . (int)$limit;
$query = $this->_db->prepare($sql);
- if (is_a($query, 'PEAR_Error')) {
- return $query;
+ if ($query instanceof PEAR_Error) {
+ throw new Horde_Exception($query->getMessage());
}
if (count($slugs)) {
$images = $query->execute($galleries);
}
$query->free();
- if (is_a($images, 'PEAR_Error')) {
- return $images;
+ if ($images instanceof PEAR_Error) {
+ throw new Horde_Exception($images->getMessage());
} elseif ($images->numRows() == 0) {
return array();
}
* @param integer $gallery_id The gallery id
* @param string $slug The gallery slug
*
- * @return mixed true | false | PEAR_Error
+ * @return boolean
+ * @throws Horde_Exception
*/
public function galleryExists($gallery_id, $slug = null)
{
if (empty($slug)) {
- return (bool)$this->_db->queryOne(
+ $results = $this->_db->queryOne(
'SELECT COUNT(share_id) FROM ' . $this->_shares->_table
. ' WHERE share_id = ' . (int)$gallery_id);
+ if ($results instanceof PEAR_Error) {
+ throw new Horde_Exception($results->getMessage());
+ }
+
+ return (bool)$results;
} else {
+
return (bool)$this->slugExists($slug);
}
}
* @param integer $from The gallery to start listing at.
* @param integer $count The number of galleries to return.
*
- * @return mixed List of categories | PEAR_Error
+ * @return array List of categories
+ * @throws Horde_Exception
*/
public function listCategories($perm = PERMS_SHOW, $from = 0, $count = 0)
{
$sql = 'SELECT DISTINCT attribute_category FROM '
. $this->_shares->_table;
$results = $this->_shares->_db->query($sql);
- if (is_a($results, 'PEAR_Error')) {
- return $results;
+ if ($results instanceof PEAR_Error) {
+ throw new Horde_Exception($results->getMessage());
}
$all_categories = $results->fetchCol('attribute_category');
$results->free();
}
}
+ /**
+ *
+ * @param $perms
+ *
+ * @return int The count of categories
+ */
public function countCategories($perms = PERMS_SHOW)
{
return count($this->listCategories($perms));
* @param string $parent The parent share to start counting at.
* @param boolean $allLevels Return all levels, or just the direct
* children of $parent? Defaults to all levels.
+ *
+ * @return int The count
+ * @throws Horde_Exception
*/
public function countGalleries($userid, $perm = PERMS_SHOW, $attributes = null,
$parent = null, $allLevels = true)
{
static $counts;
- if (is_a($parent, 'Ansel_Gallery')) {
+ if ($parent instanceof Ansel_Gallery) {
$parent_id = $parent->getId();
} else {
$parent_id = $parent;
$count = $this->_shares->countShares($userid, $perm, $attributes,
$parent, $allLevels);
+ if ($count instanceof PEAR_Error) {
+ throw new Horde_Exception($count->getMessage());
+ }
+
$counts[$key] = $count;
return $count;
* 0 - ascending
* 1 - descending
*
- * @return mixed An array of Ansel_Gallery objects | PEAR_Error
+ * @return array of Ansel_Gallery objects
+ * @throws Horde_Exception
*/
public function listGalleries($perm = PERMS_SHOW,
$attributes = null,
$sort_by = null,
$direction = 0)
{
- return $this->_shares->listShares(Horde_Auth::getAuth(), $perm, $attributes,
- $from, $count, $sort_by, $direction,
- $parent, $allLevels);
+ $shares = $this->_shares->listShares(Horde_Auth::getAuth(), $perm, $attributes,
+ $from, $count, $sort_by, $direction,
+ $parent, $allLevels);
+
+ if ($shares instanceof PEAR_Error) {
+ throw new Horde_Exception($shares->getMessage());
+ }
+
+ return $shares;
}
/**
* @param string $image_view Which image view to use? screen, thumb etc..
* @param boolean $view_links Include links to the image view
*
- * @return string The json data || PEAR_Error
+ * @return string The json data
+ * @throws Horde_Exception
*/
public function getImageJson($images, $style = null, $full = false,
- $image_view = 'mini', $view_links = false)
+ $image_view = 'mini', $view_links = false)
{
$galleries = array();
if (is_null($style)) {
foreach ($images as $id) {
$image = $this->getImage($id);
- if (!is_a($image, 'PEAR_Error')) {
- $gallery_id = abs($image->gallery);
+ $gallery_id = abs($image->gallery);
+ if (empty($galleries[$gallery_id])) {
+ $galleries[$gallery_id]['gallery'] = $GLOBALS['ansel_storage']->getGallery($gallery_id);
+ }
- if (empty($galleries[$gallery_id])) {
- $galleries[$gallery_id]['gallery'] = $GLOBALS['ansel_storage']->getGallery($gallery_id);
- if (is_a($galleries[$gallery_id]['gallery'], 'PEAR_Error')) {
- return $galleries[$gallery_id];
- }
- }
+ // Any authentication that needs to take place for any of the
+ // images included here MUST have already taken place or the
+ // image will not be incldued in the output.
+ if (!isset($galleries[$gallery_id]['perm'])) {
+ $galleries[$gallery_id]['perm'] =
+ ($galleries[$gallery_id]['gallery']->hasPermission(Horde_Auth::getAuth(), PERMS_READ) &&
+ $galleries[$gallery_id]['gallery']->isOldEnough() &&
+ !$galleries[$gallery_id]['gallery']->hasPasswd());
+ }
- // Any authentication that needs to take place for any of the
- // images included here MUST have already taken place or the
- // image will not be incldued in the output.
- if (!isset($galleries[$gallery_id]['perm'])) {
- $galleries[$gallery_id]['perm'] =
- ($galleries[$gallery_id]['gallery']->hasPermission(Horde_Auth::getAuth(), PERMS_READ) &&
- $galleries[$gallery_id]['gallery']->isOldEnough() &&
- !$galleries[$gallery_id]['gallery']->hasPasswd());
+ if ($galleries[$gallery_id]['perm']) {
+ $data = array(Ansel::getImageUrl($image->id, $image_view, $full, $style),
+ htmlspecialchars($image->filename, ENT_COMPAT, Horde_Nls::getCharset()),
+ Horde_Text_Filter::filter($image->caption, 'text2html', array('parselevel' => Horde_Text_Filter_Text2html::MICRO_LINKURL)),
+ $image->id,
+ 0);
+
+ if ($view_links) {
+ $data[] = Ansel::getUrlFor('view',
+ array('gallery' => $image->gallery,
+ 'image' => $image->id,
+ 'view' => 'Image',
+ 'slug' => $galleries[$gallery_id]['gallery']->get('slug')),
+ $full);
+
+ $data[] = Ansel::getUrlFor('view',
+ array('gallery' => $image->gallery,
+ 'slug' => $galleries[$gallery_id]['gallery']->get('slug'),
+ 'view' => 'Gallery'),
+ $full);
}
- if ($galleries[$gallery_id]['perm']) {
- $data = array(Ansel::getImageUrl($image->id, $image_view, $full, $style),
- htmlspecialchars($image->filename, ENT_COMPAT, Horde_Nls::getCharset()),
- Horde_Text_Filter::filter($image->caption, 'text2html', array('parselevel' => Horde_Text_Filter_Text2html::MICRO_LINKURL)),
- $image->id,
- 0);
-
- if ($view_links) {
- $data[] = Ansel::getUrlFor('view',
- array('gallery' => $image->gallery,
- 'image' => $image->id,
- 'view' => 'Image',
- 'slug' => $galleries[$gallery_id]['gallery']->get('slug')),
- $full);
-
- $data[] = Ansel::getUrlFor('view',
- array('gallery' => $image->gallery,
- 'slug' => $galleries[$gallery_id]['gallery']->get('slug'),
- 'view' => 'Gallery'),
- $full);
- }
-
- $json[] = $data;
- }
+ $json[] = $data;
}
+
}
if (count($json)) {
* ignored if this is non-empty).
* @param mixed $sort The field(s) to sort by.
*
- * @return mixed An array of image_ids | PEAR_Error
+ * @return array An array of image_ids
+ * @throws Horde_Exception
*/
public function listImages($gallery_id, $from = 0, $count = 0,
$fields = 'image_id', $where = '', $sort = 'image_sort')
Horde::logMessage('Query by Ansel_Storage::listImages: ' . $sql, __FILE__, __LINE__, PEAR_LOG_DEBUG);
$results = $this->_db->query('SELECT ' . $fields . ' FROM ansel_images '
. $query_where . ' ORDER BY ' . $sort);
- if (is_a($results, 'PEAR_Error')) {
- return $results;
+ if ($results instanceof PEAR_Error) {
+ throw new Horde_Exception($results->getMessage());
}
if ($field_count > 1) {
return $results->fetchAll(MDB2_FETCHMODE_ASSOC, true, true, false);
* all images in the gallery that have geolocation
* data ($image_ids would be ignored).
*
- * @return mixed An array of geodata || PEAR_Error
+ * @return array of geodata
*/
public function getImagesGeodata($image_ids = array(), $gallery = null)
{
/**
* Get image attribtues from ansel_image_attributes table
*
- * @param $image_id
- * @return unknown_type
+ * @param int $image_id The image id
+ *
+ * @return array
+ * @throws Horde_Exception
*/
public function getImageAttributes($image_id)
{
- return $GLOBALS['ansel_db']->queryAll('SELECT attr_name, attr_value FROM ansel_image_attributes WHERE image_id = ' . (int)$image_id, null, MDB2_FETCHMODE_ASSOC, true);
+ $results = $GLOBALS['ansel_db']->queryAll('SELECT attr_name, attr_value FROM ansel_image_attributes WHERE image_id = ' . (int)$image_id, null, MDB2_FETCHMODE_ASSOC, true);
+ if ($results instanceof PEAR_Error) {
+ throw new Horde_Exception($results->getMessage());
+ }
+
+ return $results;
}
/**
return $this->listImages(0, $start, $count, array('image_id as id', 'image_id', 'gallery_id', 'image_latitude', 'image_longitude', 'image_location'), $where, 'image_geotag_date DESC');
}
+ /**
+ *
+ * @param string $search Search fragment for autocompleting location strings
+ *
+ * @return array The results
+ * @throws Horde_Exception
+ */
public function searchLocations($search = '')
{
$sql = 'SELECT DISTINCT image_location, image_latitude, image_longitude'
. ' FROM ansel_images WHERE image_location LIKE "' . $search . '%"';
$results = $this->_db->query($sql);
- if (is_a($results, 'PEAR_Error')) {
- return $results;
+ if ($results instanceof PEAR_Error) {
+ throw new Horde_Exception($results->getMessage());
}
return $results->fetchAll(MDB2_FETCHMODE_ASSOC, true, true, false);
$tag_id = array_values(Ansel_Tags::getTagIds(array($id)));
$images = Ansel_Tags::searchTagsById($tag_id, 10, 0, 'images');
$tag_id = array_pop($tag_id);
- $images = $ansel_storage->getImages($images['images']);
+ $images = $ansel_storage->getImages(array('ids' => $images['images']));
if (!is_a($images, 'PEAR_Error') && count($images)) {
$tag_id = $tag_id[0];
$images = array_values($images);