Use Content_Tagger::getSimilarObjects for the related images widget.
authorMichael J. Rubinsky <mrubinsk@horde.org>
Sat, 31 Jul 2010 20:42:38 +0000 (16:42 -0400)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Sat, 31 Jul 2010 20:43:40 +0000 (16:43 -0400)
ansel/lib/Tagger.php
ansel/lib/Widget/SimilarPhotos.php
kronolith/lib/Tagger.php

index e679e84..f9592d9 100644 (file)
@@ -275,7 +275,7 @@ class Ansel_Tagger
      *
      * @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
      */
@@ -346,4 +346,41 @@ class Ansel_Tagger
         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;
+    }
+
 }
index 8cdad5d..6349e00 100755 (executable)
@@ -44,9 +44,6 @@ class Ansel_Widget_SimilarPhotos extends Ansel_Widget_Base
     /**
      * 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
      */
@@ -55,40 +52,29 @@ class Ansel_Widget_SimilarPhotos extends Ansel_Widget_Base
         $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;
     }
+
 }
index dc92edf..ebe1074 100644 (file)
@@ -212,7 +212,7 @@ class Kronolith_Tagger
             $args['typeId'] = $this->_type_ids['event'];
             $event_results = $GLOBALS['injector']->getInstance('Content_Tagger')->getObjects($args);
         }
-        
+
         $results = array('calendars' => array_values($cal_results),
                          'events' => (!empty($args['calendarId']) && count($event_results))
                                      ? Kronolith::getDriver()->filterEventsByCalendar(array_values($event_results), $args['calendarId'])