From d6879bfe0c188b06f7b7730bb64af469d5c6e461 Mon Sep 17 00:00:00 2001
From: Evan Prodromou <evan@controlyourself.ca>
Date: Thu, 22 Jan 2009 22:38:10 +0000
Subject: [PATCH] Debug some of the subscriptions+tags problems

---
 actions/subscribers.php   |  6 ++++-
 actions/subscriptions.php |  6 ++++-
 classes/User.php          | 54 +++++++++++++++++++++++++++++++++++++++
 lib/galleryaction.php     |  7 +++--
 lib/profilelist.php       |  4 +--
 5 files changed, 71 insertions(+), 6 deletions(-)

diff --git a/actions/subscribers.php b/actions/subscribers.php
index 408829b545..be9df2b124 100644
--- a/actions/subscribers.php
+++ b/actions/subscribers.php
@@ -75,7 +75,11 @@ class SubscribersAction extends GalleryAction
         $offset = ($this->page-1) * PROFILES_PER_PAGE;
         $limit =  PROFILES_PER_PAGE + 1;
 
-        $subscribers = $this->user->getSubscribers($offset, $limit);
+        if ($this->tag) {
+            $subscribers = $this->user->getTaggedSubscribers($this->tag, $offset, $limit);
+        } else {
+            $subscribers = $this->user->getSubscribers($offset, $limit);
+        }
 
         if ($subscribers) {
             $subscribers_list = new SubscribersList($subscribers, $this->user, $this);
diff --git a/actions/subscriptions.php b/actions/subscriptions.php
index bcc5578917..d7ba0d624b 100644
--- a/actions/subscriptions.php
+++ b/actions/subscriptions.php
@@ -84,7 +84,11 @@ class SubscriptionsAction extends GalleryAction
         $offset = ($this->page-1) * PROFILES_PER_PAGE;
         $limit =  PROFILES_PER_PAGE + 1;
 
-        $subscriptions = $this->user->getSubscriptions($offset, $limit);
+        if ($this->tag) {
+            $subscriptions = $this->user->getTaggedSubscriptions($this->tag, $offset, $limit);
+        } else {
+            $subscriptions = $this->user->getSubscriptions($offset, $limit);
+        }
 
         if ($subscriptions) {
             $subscriptions_list = new SubscriptionsList($subscriptions, $this->user, $this);
diff --git a/classes/User.php b/classes/User.php
index 5dadd6b440..5f4fb9b6fb 100644
--- a/classes/User.php
+++ b/classes/User.php
@@ -575,4 +575,58 @@ class User extends Memcached_DataObject
 
         return $profile;
     }
+
+    function getTaggedSubscribers($tag, $offset=0, $limit=null)
+    {
+        $qry =
+          'SELECT profile.* ' .
+          'FROM profile JOIN subscription ' .
+          'ON profile.id = subscription.subscriber ' .
+          'JOIN profile_tag ON (profile_tag.tagged = subscription.subscriber ' .
+          'AND profile_tag.tagger = subscription.subscribed) ' .
+          'WHERE subscription.subscribed = %d ' .
+          'AND profile_tag.tag = "%s" ' .
+          'AND subscription.subscribed != subscription.subscriber ' .
+          'ORDER BY subscription.created DESC ';
+
+        if ($offset) {
+            if (common_config('db','type') == 'pgsql') {
+                $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
+            } else {
+                $qry .= ' LIMIT ' . $offset . ', ' . $limit;
+            }
+        }
+
+        $profile = new Profile();
+
+        $cnt = $profile->query(sprintf($qry, $this->id, $tag));
+
+        return $profile;
+    }
+
+    function getTaggedSubscriptions($tag, $offset=0, $limit=null)
+    {
+        $qry =
+          'SELECT profile.* ' .
+          'FROM profile JOIN subscription ' .
+          'ON profile.id = subscription.subscribed ' .
+          'JOIN profile_tag on (profile_tag.tagged = subscription.subscribed ' .
+          'AND profile_tag.tagger = subscription.subscriber) ' .
+          'WHERE subscription.subscriber = %d ' .
+          'AND profile_tag.tag = "%s" ' .
+          'AND subscription.subscribed != subscription.subscriber ' .
+          'ORDER BY subscription.created DESC ';
+
+        if (common_config('db','type') == 'pgsql') {
+            $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
+        } else {
+            $qry .= ' LIMIT ' . $offset . ', ' . $limit;
+        }
+
+        $profile = new Profile();
+
+        $profile->query(sprintf($qry, $this->id, $tag));
+
+        return $profile;
+    }
 }
diff --git a/lib/galleryaction.php b/lib/galleryaction.php
index a277762a65..25a5e3fd59 100644
--- a/lib/galleryaction.php
+++ b/lib/galleryaction.php
@@ -32,6 +32,7 @@ class GalleryAction extends Action
     var $profile = null;
     var $user = null;
     var $page = null;
+    var $tag = null;
 
     function prepare($args)
     {
@@ -69,6 +70,8 @@ class GalleryAction extends Action
 
         $this->page = ($this->arg('page')) ? ($this->arg('page')+0) : 1;
 
+        $this->tag = $this->trimmed('tag');
+
         return true;
     }
 
@@ -84,7 +87,7 @@ class GalleryAction extends Action
 		# Post from the tag dropdown; redirect to a GET
 
 		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
-		    common_redirect($this->self_url(), 307);
+		    common_redirect($this->selfUrl(), 307);
             return;
 		}
 
@@ -124,7 +127,7 @@ class GalleryAction extends Action
                            array('href' =>
                                  common_local_url($this->trimmed('action'),
                                                   array('nickname' =>
-                                                        $profile->nickname))),
+                                                        $this->user->nickname))),
                            _('All'));
             $this->elementEnd('li');
             $this->elementStart('li', array('id'=>'filter_tags_item'));
diff --git a/lib/profilelist.php b/lib/profilelist.php
index a510c518c5..499d74f7b5 100644
--- a/lib/profilelist.php
+++ b/lib/profilelist.php
@@ -169,9 +169,9 @@ class ProfileList extends Widget
                 $this->out->elementStart('ul', 'tags xoxo');
                 foreach ($tags as $tag) {
                     $this->out->elementStart('li');
-                    $this->element('span', 'mark_hash', '#');
+                    $this->out->element('span', 'mark_hash', '#');
                     $this->out->element('a', array('rel' => 'tag',
-                                                   'href' => common_local_url($this->action,
+                                                   'href' => common_local_url($this->action->trimmed('action'),
                                                                               array('nickname' => $this->owner->nickname,
                                                                                     'tag' => $tag))),
                                         $tag);
-- 
2.39.5