*
* @param array $tags An array of either tag names or ids.
* @param integer $limit Limit results to this many.
- *
+ *
* @return array An array of hashes, tag_id, tag_name, and count.
* @throws Ansel_Exception
*/
return $results;
}
+ /**
+ * List image ids of images related (via similar tags) to the specified
+ * image
+ *
+ * @param Ansel_Image $image The image to get related images for.
+ * @param bolean $ownerOnly Only return images owned by the specified
+ * image's owner.
+ *
+ * @return array An array of 'image' and 'rank' keys..
+ */
+ public function listRelatedImages(Ansel_Image $image, $ownerOnly = true)
+ {
+ $args = array('typeId' => 'image', 'limit' => 1);
+ if ($ownerOnly) {
+ $gallery = $GLOBALS['injector']->getInstance('Ansel_Storage')->getScope()->getGallery($image->gallery);
+ $args['userId'] = $gallery->get('owner');
+ }
+
+ try {
+ $ids = $GLOBALS['injector']->getInstance('Content_Tagger')->getSimilarObjects(array('object' => (string)$image->id, 'type' => 'image'), $args);
+ } catch (Content_Exception $e) {
+ throw new Ansel_Exception($e);
+ }
+
+ try {
+ $images = $GLOBALS['injector']->getInstance('Ansel_Storage')->getScope()->getImages(array('ids' => array_keys($ids)));
+ } catch (Horde_Exception_NotFound $e) {
+ $images = array();
+ }
+ $results = array();
+ foreach ($images as $key => $image) {
+ $results[] = array('image' => $image, 'rank' => $ids[$key]);
+ }
+
+ return $results;
+ }
+
}
/**
* Helper function for generating a widget of images related to this one.
*
- * @TODO Rethink the way we determine if an image is related. This one is
- * not ideal, as it just pops tags off the tag list until all the tags
- * match. This could miss many related images. Maybe make this random?
*
* @return string The HTML
*/
$ansel_storage = $GLOBALS['injector']->getInstance('Ansel_Storage')->getScope();
$html = '';
- $tags = array_values($this->_view->resource->getTags());
- $imgs = $GLOBALS['injector']->getInstance('Ansel_Tagger')->search($tags);
- while (count($imgs['images']) <= 5 && count($tags)) {
- array_pop($tags);
- $newImgs =$GLOBALS['injector']->getInstance('Ansel_Tagger')->search($tags);
- $imgs['images'] = array_merge($imgs['images'], $newImgs['images']);
- }
- if (count($imgs['images'])) {
- $i = 0;
- foreach ($imgs['images'] as $imgId) {
- if ($i >= min(count($imgs['images']), 5)) {
- break;
- }
- if ($imgId != $this->_view->resource->id) {
- try {
- $rImg = $ansel_storage->getImage($imgId);
- $rGal = $ansel_storage->getGallery($rImg->gallery);
- } catch (Ansel_Exception $e) {
- continue;
- }
+ $args = array('typeId' => 'image',
+ 'userId' => $this->_view->gallery->get('owner'));
- $html .= Ansel::getUrlFor(
- 'view',
- array('image' => $imgId,
- 'view' => 'Image',
- 'gallery' => $rImg->gallery,
- 'slug' => $rGal->get('slug')),
- true)->link(array('title' => sprintf(_("%s from %s"), $rImg->filename, $rGal->get('name'))))
- . '<img src="'. Ansel::getImageUrl($imgId, 'mini', true) . '" alt="' . htmlspecialchars($rImg->filename) . '" /></a>';
- $i++;
- }
+ $results = $GLOBALS['injector']->getInstance('Ansel_Tagger')->listRelatedImages($this->_view->resource);
+ if (count($results)) {
+ $i = 0;
+ foreach ($results as $result) {
+ $img = $result['image'];
+ $rGal = $GLOBALS['injector']->getInstance('Ansel_Storage')->getScope()->getGallery($img->gallery);
+ if ($rGal->hasPermission())
+ $html .= Ansel::getUrlFor(
+ 'view',
+ array('image' => $img->id,
+ 'view' => 'Image',
+ 'gallery' => $img->gallery,
+ 'slug' => $rGal->get('slug')),
+ true)->link(array('title' => sprintf(_("%s from %s"), $img->filename, $rGal->get('name'))))
+ . '<img src="'. Ansel::getImageUrl($img->id, 'mini', true) . '" alt="' . htmlspecialchars($img->filename) . '" /></a>';
+ $i++;
}
}
return $html;
}
+
}