diff --git a/3.0/modules/latestupdates/controllers/latestupdates.php b/3.0/modules/latestupdates/controllers/latestupdates.php index f26af105..1997c8d8 100644 --- a/3.0/modules/latestupdates/controllers/latestupdates.php +++ b/3.0/modules/latestupdates/controllers/latestupdates.php @@ -57,12 +57,21 @@ class latestupdates_Controller extends Controller { // Display the page. print $template; + + item::set_display_context_callback("latestupdates_Controller::get_display_context", + $str_display_type, $user_id); return ; } public function users($str_display_type, $user_id) { // Generate a dynamic page with items uploaded by a specific user ($user_id). + // Make sure user_id is valid. + $current_user = ORM::factory("user", $user_id); + if (!$current_user->loaded()) { + throw new Kohana_404_Exception(); + } + // Figure out how many items to display on each page. $page_size = module::get_var("gallery", "page_size", 9); @@ -73,6 +82,21 @@ class latestupdates_Controller extends Controller { url::redirect("latestupdates/users/{$str_display_type}/{$user_id}"); } + // If this page was reached from a breadcrumb, figure out what page to load from the show id. + $show = Input::instance()->get("show"); + if ($show) { + $child = ORM::factory("item", $show); + $index = latestupdates_Controller::_get_position($child, $str_display_type, $user_id); + if ($index) { + $page = ceil($index / $page_size); + if ($page == 1) { + url::redirect("latestupdates/users/{$str_display_type}/{$user_id}"); + } else { + url::redirect("latestupdates/users/{$str_display_type}/{$user_id}?page=$page"); + } + } + } + // First item to display. $offset = ($page - 1) * $page_size; @@ -103,6 +127,7 @@ class latestupdates_Controller extends Controller { // Figure out which items to display on this page. $children = ""; + $str_page_title = ""; if ($str_display_type == "recent") { $children = ORM::factory("item") ->viewable() @@ -110,6 +135,7 @@ class latestupdates_Controller extends Controller { ->where("owner_id", "=", $user_id) ->order_by("created", "DESC") ->find_all($page_size, $offset); + $str_page_title = "Recent Uploads"; } elseif ($str_display_type == "albums") { $children = ORM::factory("item") ->viewable() @@ -117,6 +143,7 @@ class latestupdates_Controller extends Controller { ->where("owner_id", "=", $user_id) ->order_by("created", "DESC") ->find_all($page_size, $offset); + $str_page_title = "Recent Albums"; } else { $children = ORM::factory("item") ->viewable() @@ -124,6 +151,7 @@ class latestupdates_Controller extends Controller { ->where("owner_id", "=", $user_id) ->order_by("view_count", "DESC") ->find_all($page_size, $offset); + $str_page_title = "Most Viewed"; } // Set up the previous and next page buttons. @@ -137,16 +165,140 @@ class latestupdates_Controller extends Controller { } // Set up and display the actual page. + $root = item::root(); $template = new Theme_View("page.html", "collection", "LatestUpdates"); - $template->page_title = t("Gallery :: Latest Updates"); - $template->set_global("page", $page); - $template->set_global("page_size", $page_size); - $template->set_global("max_pages", $max_pages); - $template->set_global("children", $children); - $template->set_global("children_count", $count); + $template->page_title = t("Gallery :: Latest Updates"); + $template->set_global( + array("page" => $page, + "max_pages" => $max_pages, + "page_size" => $page_size, + "children" => $children, + "breadcrumbs" => array( + Breadcrumb::instance($root->title, $root->url())->set_first(), + Breadcrumb::instance(t("User profile: %name", array("name" => $current_user->display_name())), + url::site("user_profile/show/{$user_id}")), + Breadcrumb::instance($str_page_title, + url::site("latestupdates/users/{$str_display_type}/{$user_id}"))->set_last()), + "children_count" => $count)); $template->content = new View("dynamic.html"); - $template->content->title = t("Latest Updates"); + $template->content->title = t($str_page_title); + print $template; + + item::set_display_context_callback("latestupdates_Controller::get_display_context", + $str_display_type, $user_id); + + } + + static function get_display_context($item, $str_display_type, $user_id) { + $current_user = ORM::factory("user", $user_id); + + $str_page_title = ""; + if ($str_display_type == "recent") { + $str_page_title = "Recent Uploads"; + } elseif ($str_display_type == "albums") { + $str_page_title = "Recent Albums"; + } else { + $str_page_title = "Most Viewed"; + } + + $position = latestupdates_Controller::_get_position($item, $str_display_type, $user_id); + if ($position > 1) { + list ($previous_item, $ignore, $next_item) = + latestupdates_Controller::items($str_display_type, $user_id, 3, $position - 2); + } else { + $previous_item = null; + list ($next_item) = latestupdates_Controller::items($str_display_type, $user_id, 1, $position); + } + + $count = 0; + if ($str_display_type == "albums") { + $count = ORM::factory("item") + ->viewable() + ->where("type", "=", "album") + ->where("owner_id", "=", $user_id) + ->count_all(); + } else { + $count = ORM::factory("item") + ->viewable() + ->where("type", "!=", "album") + ->where("owner_id", "=", $user_id) + ->count_all(); + } + + $root = item::root(); + return array("position" => $position, + "previous_item" => $previous_item, + "next_item" => $next_item, + "sibling_count" => $count, + "breadcrumbs" => array( + Breadcrumb::instance($root->title, $root->url())->set_first(), + Breadcrumb::instance(t("User profile: %name", array("name" => $current_user->display_name())), + url::site("user_profile/show/{$user_id}")), + Breadcrumb::instance($str_page_title, + url::site("latestupdates/users/{$str_display_type}/{$user_id}?show={$item->id}")), + Breadcrumb::instance($item->title, $item->url())->set_last()) + ); + } + + static function items($str_display_type, $user_id, $limit=null, $offset=null) { + $str_where = array(); + $str_orderby_field = ""; + if ($str_display_type == "recent") { + $str_where = array(array("type", "!=", "album")); + $str_orderby_field = "created"; + } elseif ($str_display_type == "albums") { + $str_where = array(array("type", "=", "album")); + $str_orderby_field = "created"; + } else { + $str_where = array(array("type", "!=", "album")); + $str_orderby_field = "view_count"; + } + + return ORM::factory("item") + ->viewable() + ->merge_where($str_where) + ->where("owner_id", "=", $user_id) + ->order_by($str_orderby_field, "DESC") + ->find_all($limit, $offset); + } + + private function _get_position($item, $str_display_type, $user_id) { + $str_where = array(); + $str_orderby_field = ""; + if ($str_display_type == "recent") { + $str_where = array(array("type", "!=", "album")); + $str_orderby_field = "created"; + } elseif ($str_display_type == "albums") { + $str_where = array(array("type", "=", "album")); + $str_orderby_field = "created"; + } else { + $str_where = array(array("type", "!=", "album")); + $str_orderby_field = "view_count"; + } + + $position = ORM::factory("item") + ->viewable() + ->where("owner_id", "=", $user_id) + ->merge_where($str_where) + ->where($str_orderby_field, ">", $item->$str_orderby_field) + ->order_by($str_orderby_field, "DESC") + ->count_all(); + + foreach (ORM::factory("item") + ->viewable() + ->where("owner_id", "=", $user_id) + ->merge_where($str_where) + ->where($str_orderby_field, "=", $item->$str_orderby_field) + ->order_by($str_orderby_field, "DESC") + ->find_all() as $row) { + $position++; + if ($row->id == $item->id) { + break; + } + } + + return $position; } public function albums($id) {