diff options
author | Evan Prodromou <evan@status.net> | 2010-12-11 10:24:46 -0500 |
---|---|---|
committer | Evan Prodromou <evan@status.net> | 2010-12-11 10:24:46 -0500 |
commit | 7285bbc93b5394a16731e498b62188ea847de38d (patch) | |
tree | 5eba573385415c15ffbf5fac256059ded7f218c0 | |
parent | baae319aefc8500b9d50d267937aab1022c723e5 (diff) |
Subscription stream functions
Made two new functions, Subscription::bySubscriber() and
Subscription::bySubscribed(), to get streams of Subscription objects.
Converted Profile::getSubscribers() and Profile::getSubscriptions() to
use these functions.
-rw-r--r-- | classes/Profile.php | 54 | ||||
-rw-r--r-- | classes/Subscription.php | 64 |
2 files changed, 80 insertions, 38 deletions
diff --git a/classes/Profile.php b/classes/Profile.php index 8dbdcbd97..239c368ca 100644 --- a/classes/Profile.php +++ b/classes/Profile.php @@ -380,54 +380,32 @@ class Profile extends Memcached_DataObject function getSubscriptions($offset=0, $limit=null) { - $qry = - 'SELECT profile.* ' . - 'FROM profile JOIN subscription ' . - 'ON profile.id = subscription.subscribed ' . - 'WHERE subscription.subscriber = %d ' . - 'AND subscription.subscribed != subscription.subscriber ' . - 'ORDER BY subscription.created DESC '; - - if ($offset>0 && !is_null($limit)){ - if (common_config('db','type') == 'pgsql') { - $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset; - } else { - $qry .= ' LIMIT ' . $offset . ', ' . $limit; - } - } + $subs = Subscription::bySubscriber($this->id, + $offset, + $limit); - $profile = new Profile(); + $profiles = array(); - $profile->query(sprintf($qry, $this->id)); + while ($subs->fetch()) { + $profiles[] = Profile::staticGet($subs->subscribed); + } - return $profile; + return new ArrayWrapper($profiles); } function getSubscribers($offset=0, $limit=null) { - $qry = - 'SELECT profile.* ' . - 'FROM profile JOIN subscription ' . - 'ON profile.id = subscription.subscriber ' . - 'WHERE subscription.subscribed = %d ' . - 'AND subscription.subscribed != subscription.subscriber ' . - 'ORDER BY subscription.created DESC '; - - if ($offset>0 && !is_null($limit)){ - if ($offset) { - if (common_config('db','type') == 'pgsql') { - $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset; - } else { - $qry .= ' LIMIT ' . $offset . ', ' . $limit; - } - } - } + $subs = Subscription::bySubscribed($this->id, + $offset, + $limit); - $profile = new Profile(); + $profiles = array(); - $cnt = $profile->query(sprintf($qry, $this->id)); + while ($subs->fetch()) { + $profiles[] = Profile::staticGet($subs->subscriber); + } - return $profile; + return new ArrayWrapper($profiles); } function getConnectedApps($offset = 0, $limit = null) diff --git a/classes/Subscription.php b/classes/Subscription.php index e9ad2a5a2..a4764e9f1 100644 --- a/classes/Subscription.php +++ b/classes/Subscription.php @@ -264,4 +264,68 @@ class Subscription extends Memcached_DataObject return $act; } + + /** + * Stream of subscriptions with the same subscriber + * + * Useful for showing pages that list subscriptions in reverse + * chronological order. Has offset & limit to make paging + * easy. + * + * @param integer $subscriberId Profile ID of the subscriber + * @param integer $offset Offset from latest + * @param integer $limit Maximum number to fetch + * + * @return Subscription stream of subscriptions; use fetch() to iterate + */ + + static function bySubscriber($subscriberId, + $offset = 0, + $limit = PROFILES_PER_PAGE) + { + $sub = new Subscription(); + + $sub->subscriber = $subscriberId; + + $sub->whereAdd('subscribed != ' . $subscriberId); + + $sub->orderBy('created DESC'); + $sub->limit($offset, $limit); + + $sub->find(); + + return $sub; + } + + /** + * Stream of subscriptions with the same subscribed profile + * + * Useful for showing pages that list subscribers in reverse + * chronological order. Has offset & limit to make paging + * easy. + * + * @param integer $subscribedId Profile ID of the subscribed + * @param integer $offset Offset from latest + * @param integer $limit Maximum number to fetch + * + * @return Subscription stream of subscriptions; use fetch() to iterate + */ + + static function bySubscribed($subscribedId, + $offset = 0, + $limit = PROFILES_PER_PAGE) + { + $sub = new Subscription(); + + $sub->subscribed = $subscribedId; + + $sub->whereAdd('subscriber != ' . $subscribedId); + + $sub->orderBy('created DESC'); + $sub->limit($offset, $limit); + + $sub->find(); + + return $sub; + } } |