diff options
-rw-r--r-- | core/libraries/ORM_MPTT.php | 30 | ||||
-rw-r--r-- | modules/media_rss/controllers/media_rss.php | 30 | ||||
-rw-r--r-- | modules/media_rss/views/feed.mrss.php | 33 |
3 files changed, 50 insertions, 43 deletions
diff --git a/core/libraries/ORM_MPTT.php b/core/libraries/ORM_MPTT.php index 2a1c257b..5cdf9c02 100644 --- a/core/libraries/ORM_MPTT.php +++ b/core/libraries/ORM_MPTT.php @@ -128,10 +128,7 @@ class ORM_MPTT_Core extends ORM { */ function children_count() { if (!isset($this->children_count)) { - $this->children_count = - $this->where("parent_id", $this->id) - ->orderby("id", "ASC") - ->count_all(); + $this->children_count = $this->where("parent_id", $this->id)->count_all(); } return $this->children_count; } @@ -144,18 +141,20 @@ class ORM_MPTT_Core extends ORM { * @param string type to return * @return object ORM_Iterator */ - function decendents($limit=NULL, $offset=0, $type="all") { + function descendants($limit=NULL, $offset=0, $type=null) { // @todo create a unit test // @todo set up caching in an array; using type=all allows us to cache as decendents[$type] // @todo needs to take into account the offset and limit as well $this->where("left >=", $this->left) ->where("right <=", $this->right); - if ($type != "all") { + if ($type) { $this->where("type", $type); } - $descendants = - $this->orderby("id", "ASC") - ->find_all($limit, $offset); + + // @todo: make the order column data driven + $this->orderby("id", "ASC"); + + $descendants = $this->find_all($limit, $offset); return $descendants; } @@ -165,21 +164,20 @@ class ORM_MPTT_Core extends ORM { * @param string type to count * @return integer child count */ - function decendents_count($type=all) { + function descendants_count($type=null) { // @todo create a unit test // @todo set up caching in an array;; using type=all allows us to cache as decendents[$type] $this->where("left >=", $this->left) - ->where("right <=", $this->right); - if ($type != "all") { + ->where("right <=", $this->right); + if ($type) { $this->where("type", $type); } - + // @todo does it make sense to order it before counting? - return $this->orderby("id", "ASC") - ->count_all(); + return $this->count_all(); } - + /** * @see ORM::reload */ diff --git a/modules/media_rss/controllers/media_rss.php b/modules/media_rss/controllers/media_rss.php index 68025259..10d9741b 100644 --- a/modules/media_rss/controllers/media_rss.php +++ b/modules/media_rss/controllers/media_rss.php @@ -18,26 +18,36 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class Media_RSS_Controller extends Controller { - public static $LIMIT = 10; + public static $page_size = 10; + public function feed($id) { $item = ORM::factory("item", $id)->find(); if (!$item->loaded) { return Kohana::show_404(); } - $view = new View("feed.mrss"); - $view->item = $item; + $page = $this->input->get("page", 1); + if ($page < 1) { + url::redirect("media_rss/feed/{$item->id}"); + } - $offset = $this->input->get("offset", 0); - $view->children = $item->decendents(Media_RSS_Controller::$LIMIT, $offset, "photo"); + $children = $item->descendants(self::$page_size, ($page - 1) * self::$page_size, "photo"); + $count = $item->descendants_count("photo"); + $max_pages = ceil($item->descendants_count("photo") / self::$page_size); - if (!empty($offset)) { - $view->prevOffset = $offset - Media_RSS_Controller::$LIMIT; - } - if ($view->children->count() >= Media_RSS_Controller::$LIMIT) { - $view->nextOffset = $offset + Media_RSS_Controller::$LIMIT; + if ($page > $max_pages) { + url::redirect("media_rss/feed/{$item->id}?page=$max_pages"); } + $view = new View("feed.mrss"); + $view->item = $item; + $view->children = $children; + $view->previous_page = $page > 1 ? $page - 1 : null; + $view->next_page = $page < $max_pages ? $page + 1 : null; + + // @todo do we want to add an upload date to the items table? + $view->pub_date = date("D, d M Y H:i:s T"); + header("Content-type: application/rss+xml"); print $view; } diff --git a/modules/media_rss/views/feed.mrss.php b/modules/media_rss/views/feed.mrss.php index 8c88b8a2..b3f44a17 100644 --- a/modules/media_rss/views/feed.mrss.php +++ b/modules/media_rss/views/feed.mrss.php @@ -1,5 +1,5 @@ <? defined("SYSPATH") or die("No direct script access."); ?> -<? echo "<?xml version=\"1.0\" ?>"; ?> +<? echo "<?xml version=\"1.0\" ?>" ?> <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/" xmlns:atom="http://www.w3.org/2005/Atom"> <channel> @@ -7,21 +7,20 @@ <link><?= url::abs_site("albums/{$item->id}") ?></link> <description><?= $item->description ?></description> <language>en-us</language> - <atom:link rel="self" href="<?= url::abs_site("media_rss/feed/{$item->id}") ?>" type="application/rss+xml" /> - <? if (isset($prevOffset)): ?> - <atom:link rel="previous" href="<?= url::abs_site("media_rss/feed/{$item->id}?offset={$prevOffset}") ?>" - type="application/rss+xml" /> - <? endif; ?> - <? if (isset($nextOffset)): ?> - <atom:link rel="next" href="<?= url::abs_site("media_rss/feed/{$item->id}?offset={$nextOffset}") ?>" - type="application/rss+xml" /> - <? endif; ?> - <? - // @todo do we want to add an upload date to the items table? - $date = date("D, d M Y H:i:s T"); - ?> - <pubDate><?= $date ?></pubDate> - <lastBuildDate><?= $date ?></lastBuildDate> + <atom:link rel="self" href="<?= url::abs_site("media_rss/feed/{$item->id}") ?>" + type="application/rss+xml" /> + <? if ($previous_page): ?> + <atom:link rel="previous" + href="<?= url::abs_site("media_rss/feed/{$item->id}?page=$previous_page") ?>" + type="application/rss+xml" /> + <? endif ?> + <? if ($next_page): ?> + <atom:link rel="next" + href="<?= url::abs_site("media_rss/feed/{$item->id}?page=$next_page") ?>" + type="application/rss+xml" /> + <? endif ?> + <pubDate><?= $pub_date ?></pubDate> + <lastBuildDate><?= $pub_date ?></lastBuildDate> <? foreach ($children as $child): ?> <item> <title><?= $child->title ?></title> @@ -46,6 +45,6 @@ /> </media:group> </item> - <? endforeach; ?> + <? endforeach ?> </channel> </rss> |