From 26cd108cb7909bb2e5c3af1621714774b3324111 Mon Sep 17 00:00:00 2001 From: rWatcher Date: Wed, 13 Jun 2012 18:50:14 -0400 Subject: [PATCH] Expand virtual album code to cover the rest of Latest Updates. --- .../controllers/latestupdates.php | 532 ++++++++++++------ .../helpers/latestupdates_block.php | 1 - 2 files changed, 363 insertions(+), 170 deletions(-) diff --git a/3.0/modules/latestupdates/controllers/latestupdates.php b/3.0/modules/latestupdates/controllers/latestupdates.php index 7284781a..37350a13 100644 --- a/3.0/modules/latestupdates/controllers/latestupdates.php +++ b/3.0/modules/latestupdates/controllers/latestupdates.php @@ -75,15 +75,15 @@ class latestupdates_Controller extends Controller { 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"); - } + 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; @@ -126,7 +126,7 @@ 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->page_title = t("Gallery :: Latest Updates"); $template->set_global( array("page" => $page, "max_pages" => $max_pages, @@ -140,7 +140,7 @@ class latestupdates_Controller extends Controller { url::site("latestupdates/users/{$str_display_type}/{$user_id}"))->set_last()), "children_count" => $count)); $template->content = new View("dynamic.html"); - $template->content->title = t($str_page_title); + $template->content->title = $str_page_title; // Display the page. print $template; @@ -151,9 +151,183 @@ class latestupdates_Controller extends Controller { $str_display_type, $user_id); } + public function albums($id) { + // Figure out how many items to display on each page. + $page_size = module::get_var("gallery", "page_size", 9); + + // Load the parent album. + $item = ORM::factory("item", $id); + + // Figure out which page # the visitor is on and + // don't allow the visitor to go below page 1. + $page = Input::instance()->get("page", 1); + if ($page < 1) { + url::redirect("latestupdates/albums/{$item->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, "descendants", $item->id); + if ($index) { + $page = ceil($index / $page_size); + if ($page == 1) { + url::redirect("latestupdates/albums/{$item->id}"); + } else { + url::redirect("latestupdates/albums/{$item->id}?page=$page"); + } + } + } + + // First item to display. + $offset = ($page - 1) * $page_size; + + // Determine the total number of items, + // for page numbering purposes. + $count = latestupdates_Controller::items_count("descendants", $item->id); + + // Figure out what the highest page number is. + $max_pages = ceil($count / $page_size); + + // Don't let the visitor go past the last page. + if ($max_pages && $page > $max_pages) { + url::redirect("latestupdates/albums/{$item->id}?page=$max_pages"); + } + + // Figure out which items to display on this page. + $children = latestupdates_Controller::items("descendants", $item->id, $page_size, $offset); + + // Set up the previous and next page buttons. + if ($page > 1) { + $previous_page = $page - 1; + $view->previous_page_link = url::site("latestupdates/albums/{$item->id}?page={$previous_page}"); + } + if ($page < $max_pages) { + $next_page = $page + 1; + $view->next_page_link = url::site("latestupdates/albums/{$item->id}?page={$next_page}"); + } + + // Set up breadcrumbs. + $breadcrumbs = array(); + $counter = 0; + $breadcrumbs[] = Breadcrumb::instance(t("Recent Uploads"), url::site("latestupdates/albums/{$item->id}"))->set_last(); + $parent_item = $item; + while ($parent_item->id != 1) { + $breadcrumbs[] = Breadcrumb::instance($parent_item->title, $parent_item->url()); + $parent_item = ORM::factory("item", $parent_item->parent_id); + } + $breadcrumbs[] = Breadcrumb::instance($parent_item->title, $parent_item->url())->set_first(); + $breadcrumbs = array_reverse($breadcrumbs, true); + + // 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( + array("page" => $page, + "max_pages" => $max_pages, + "page_size" => $page_size, + "children" => $children, + "breadcrumbs" => $breadcrumbs, + "children_count" => $count)); + $template->content = new View("dynamic.html"); + $template->content->title = t("Recent Uploads"); + + // Display the page. + print $template; + + // Set up the callback so links within the photo page will lead to photos within the virtual album + // instead of the actual album. + item::set_display_context_callback("latestupdates_Controller::get_display_context", + "descendants", $item->id); + } + + public function updates() { + // Figure out how many items to display on each page. + $page_size = module::get_var("gallery", "page_size", 9); + + // Figure out which page # the visitor is on and + // don't allow the visitor to go below page 1. + $page = Input::instance()->get("page", 1); + if ($page < 1) { + url::redirect("latestupdates/updates"); + } + + // 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, "recent", 0); + if ($index) { + $page = ceil($index / $page_size); + if ($page == 1) { + url::redirect("latestupdates/updates"); + } else { + url::redirect("latestupdates/updates?page=$page"); + } + } + } + + // First item to display. + $offset = ($page - 1) * $page_size; + + // Determine the total number of items, + // for page numbering purposes. + $count = latestupdates_Controller::items_count("recent", 0); + + // Figure out what the highest page number is. + $max_pages = ceil($count / $page_size); + + // Don't let the visitor go past the last page. + if ($max_pages && $page > $max_pages) { + url::redirect("latestupdates/updates?page=$max_pages"); + } + + // Figure out which items to display on this page. + $items = latestupdates_Controller::items("recent", 0, $page_size, $offset); + + // Set up the previous and next page buttons. + if ($page > 1) { + $previous_page = $page - 1; + $view->previous_page_link = url::site("latestupdates/updates?page={$previous_page}"); + } + if ($page < $max_pages) { + $next_page = $page + 1; + $view->next_page_link = url::site("latestupdates/updates?page={$next_page}"); + } + + // 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( + array("page" => $page, + "max_pages" => $max_pages, + "page_size" => $page_size, + "children" => $items, + "breadcrumbs" => array( + Breadcrumb::instance($root->title, $root->url())->set_first(), + Breadcrumb::instance(t("Recent Uploads"), + url::site("latestupdates/updates"))->set_last()), + "children_count" => $count)); + $template->content = new View("dynamic.html"); + $template->content->title = t("Recent Uploads"); + + // Display the page. + print $template; + + // Set up the callback so links within the photo page will lead to photos within the virtual album + // instead of the actual album. + item::set_display_context_callback("latestupdates_Controller::get_display_context", + "recent", 0); + } + static function get_display_context($item, $str_display_type, $user_id) { // Set up display elements on the photo page to link to the virtual album. - $current_user = ORM::factory("user", $user_id); + // Valid $str_display_type values are popular, recent, albums and descendants. + // $user_id can be set to "0" to search site wide. + // For "descendants", $user_id should be the album id #. // Figure out page title. $str_page_title = ""; @@ -161,6 +335,8 @@ class latestupdates_Controller extends Controller { $str_page_title = t("Recent Uploads"); } elseif ($str_display_type == "albums") { $str_page_title = t("Recent Albums"); + } elseif ($str_display_type == "descendants") { + $str_page_title = t("Recent Uploads"); } else { $str_page_title = t("Most Viewed"); } @@ -180,36 +356,89 @@ class latestupdates_Controller extends Controller { // Figure out total number of items (excluding albums). $count = latestupdates_Controller::items_count($str_display_type, $user_id); - // Return the display elements. + // Set up breadcrumbs. $root = item::root(); + $breadcrumbs = array(); + if ($user_id == 0) { + $breadcrumbs[0] = Breadcrumb::instance($root->title, $root->url())->set_first(); + $breadcrumbs[1] = Breadcrumb::instance($str_page_title, + url::site("latestupdates/updates?show={$item->id}")); + $breadcrumbs[2] = Breadcrumb::instance($item->title, $item->url())->set_last(); + } else { + if ($str_display_type == "descendants") { + $counter = 0; + $breadcrumbs[] = Breadcrumb::instance($item->title, $item->url())->set_last(); + $breadcrumbs[] = Breadcrumb::instance(t("Recent Uploads"), url::site("latestupdates/albums/{$user_id}?show={$item->id}")); + $parent_item = ORM::factory("item", $user_id); + while ($parent_item->id != 1) { + $breadcrumbs[] = Breadcrumb::instance($parent_item->title, $parent_item->url()); + $parent_item = ORM::factory("item", $parent_item->parent_id); + } + $breadcrumbs[] = Breadcrumb::instance($parent_item->title, $parent_item->url())->set_first(); + $breadcrumbs = array_reverse($breadcrumbs, true); + } else { + $current_user = ORM::factory("user", $user_id); + $breadcrumbs[0] = Breadcrumb::instance($root->title, $root->url())->set_first(); + $breadcrumbs[1] = Breadcrumb::instance(t("User profile: %name", array("name" => $current_user->display_name())), + url::site("user_profile/show/{$user_id}")); + $breadcrumbs[2] = Breadcrumb::instance($str_page_title, + url::site("latestupdates/users/{$str_display_type}/{$user_id}?show={$item->id}")); + $breadcrumbs[3] = Breadcrumb::instance($item->title, $item->url())->set_last(); + } + } + + // Return the display elements. 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()) + "breadcrumbs" => $breadcrumbs ); } static function items_count($str_display_type, $user_id) { // Figure out the total number of items. + // Valid $str_display_type values are popular, recent, albums and descendants. + // $user_id can be set to "0" to search site wide. + // For "descendants", $user_id should be the album id #. + + // If $str_display_type is albums, then we only want albums. + // If it's not, then we want everything except albums. if ($str_display_type == "albums") { + // This is only used for user profiles, so we always want + // results from a specific user. $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(); + + // If $user_id is not 0 we only want results from a specific user, + // Or else we want results from any user. + if ($user_id == 0) { + $count = ORM::factory("item") + ->viewable() + ->where("type", "!=", "album") + ->count_all(); + } else { + + // If type is descendants, then user_id is actually an item id#. + if ($str_display_type == "descendants") { + $item = ORM::factory("item", $user_id); + $count = $item + ->viewable() + ->where("type", "!=", "album") + ->order_by("created", "DESC") + ->descendants_count(); + } else { + $count = ORM::factory("item") + ->viewable() + ->where("type", "!=", "album") + ->where("owner_id", "=", $user_id) + ->count_all(); + } + } } return $count; @@ -217,6 +446,11 @@ class latestupdates_Controller extends Controller { static function items($str_display_type, $user_id, $limit=null, $offset=null) { // Query the database for a list of items to display in the virtual album. + // Valid $str_display_type values are popular, recent, albums and descendants. + // $user_id can be set to "0" to search site wide. + // For "descendants", $user_id should be the album id #. + + // Figure out search parameters based on $str_display_type. $str_where = array(); $str_orderby_field = ""; if ($str_display_type == "recent") { @@ -230,16 +464,45 @@ class latestupdates_Controller extends Controller { $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); + // Search the database for matching items. + + // Searching for descendants of a parent album is significantly + // different from the other query types, so we're doing this one + // seperately. + if ($str_display_type == "descendants") { + $item = ORM::factory("item", $user_id); + return $item + ->viewable() + ->where("type", "!=", "album") + ->order_by("created", "DESC") + ->descendants($limit, $offset); + } + + // If $user_id is greater then 0, limit results + // to a specific user. + if ($user_id == 0) { + return ORM::factory("item") + ->viewable() + ->merge_where($str_where) + ->order_by($str_orderby_field, "DESC") + ->find_all($limit, $offset); + } else { + 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) { // Figure out the item's position within the virtual album. + // Valid $str_display_type values are popular, recent, albums and descendants. + // $user_id can be set to "0" to search site wide. + // For "descendants", $user_id should be the album id #. + + // Figure out search conditions. $str_where = array(); $str_orderby_field = ""; if ($str_display_type == "recent") { @@ -253,149 +516,80 @@ class latestupdates_Controller extends Controller { $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; + // Count the number of records that have a higher orderby_field value then + // the item we're looking for. + $position = 0; + if ($user_id == 0) { + $position = ORM::factory("item") + ->viewable() + ->merge_where($str_where) + ->where($str_orderby_field, ">", $item->$str_orderby_field) + ->order_by($str_orderby_field, "DESC") + ->count_all(); + } else { + if ($str_display_type == "descendants") { + $album_item = ORM::factory("item", $user_id); + $position = $album_item + ->viewable() + ->where("type", "!=", "album") + ->where("created", ">", $item->created) + ->order_by("created", "DESC") + ->descendants_count(); + } else { + $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(); } } + // Set up a db query for all records with the same orderby field value + // as the item we're looking for. + $items = ORM::factory("item"); + if ($user_id == 0) { + $items->viewable() + ->merge_where($str_where) + ->where($str_orderby_field, "=", $item->$str_orderby_field) + ->order_by($str_orderby_field, "DESC"); + } else { + if ($str_display_type == "descendants") { + $item_album = ORM::factory("item", $user_id); + $items = $item_album + ->viewable() + ->where("type", "!=", "album") + ->where("created", "=", $item->created) + ->order_by("created", "DESC"); + } else { + $items->viewable() + ->where("owner_id", "=", $user_id) + ->merge_where($str_where) + ->where($str_orderby_field, "=", $item->$str_orderby_field) + ->order_by($str_orderby_field, "DESC"); + } + } + + // Loop through each remaining match, increasing position by 1 each time + // until we find a match. + if ($str_display_type == "descendants") { + foreach ($items->descendants() as $row) { + $position++; + if ($row->id == $item->id) { + break; + } + } + } else { + foreach ($items->find_all() as $row) { + $position++; + if ($row->id == $item->id) { + break; + } + } + } + + // Return the result. return $position; } - - public function albums($id) { - // Figure out how many items to display on each page. - $page_size = module::get_var("gallery", "page_size", 9); - - // Figure out which page # the visitor is on and - // don't allow the visitor to go below page 1. - $page = Input::instance()->get("page", 1); - if ($page < 1) { - url::redirect("latestupdates/albums/{$item->id}"); - } - - // First item to display. - $offset = ($page - 1) * $page_size; - - $item = ORM::factory("item", $id); - - // Determine the total number of items, - // for page numbering purposes. - $count = $item - ->viewable() - ->where("type", "!=", "album") - ->order_by("created", "DESC") - ->descendants_count(); - - // Figure out what the highest page number is. - $max_pages = ceil($count / $page_size); - - // Don't let the visitor go past the last page. - if ($max_pages && $page > $max_pages) { - url::redirect("latestupdates/albums/{$item->id}?page=$max_pages"); - } - - // Figure out which items to display on this page. - $children = $item - ->viewable() - ->where("type", "!=", "album") - ->order_by("created", "DESC") - ->descendants($page_size, $offset); - - // Set up the previous and next page buttons. - if ($page > 1) { - $previous_page = $page - 1; - $view->previous_page_link = url::site("latestupdates/albums/{$item->id}?page={$previous_page}"); - } - if ($page < $max_pages) { - $next_page = $page + 1; - $view->next_page_link = url::site("latestupdates/albums/{$item->id}?page={$next_page}"); - } - - // Set up and display the actual page. - $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->content = new View("dynamic.html"); - $template->content->title = t("Latest Updates"); - print $template; - } - - public function updates() { - // Figure out how many items to display on each page. - $page_size = module::get_var("gallery", "page_size", 9); - - // Figure out which page # the visitor is on and - // don't allow the visitor to go below page 1. - $page = Input::instance()->get("page", 1); - if ($page < 1) { - url::redirect("latestupdates/updates"); - } - - // First item to display. - $offset = ($page - 1) * $page_size; - - // Determine the total number of items, - // for page numbering purposes. - $count = ORM::factory("item") - ->viewable() - ->where("type", "!=", "album") - ->find_all() - ->count(); - - // Figure out what the highest page number is. - $max_pages = ceil($count / $page_size); - - // Don't let the visitor go past the last page. - if ($max_pages && $page > $max_pages) { - url::redirect("latestupdates/updates?page=$max_pages"); - } - - // Figure out which items to display on this page. - $items = ORM::factory("item") - ->viewable() - ->where("type", "!=", "album") - ->order_by("created", "DESC") - ->find_all($page_size, $offset); - - // Set up the previous and next page buttons. - if ($page > 1) { - $previous_page = $page - 1; - $view->previous_page_link = url::site("latestupdates/updates?page={$previous_page}"); - } - if ($page < $max_pages) { - $next_page = $page + 1; - $view->next_page_link = url::site("latestupdates/updates?page={$next_page}"); - } - - // Set up and display the actual page. - $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", $items); - $template->set_global("children_count", $count); - $template->content = new View ("dynamic.html"); - $template->content->title = t("Latest Updates"); - print $template; - } } diff --git a/3.0/modules/latestupdates/helpers/latestupdates_block.php b/3.0/modules/latestupdates/helpers/latestupdates_block.php index 550821da..158ffa96 100644 --- a/3.0/modules/latestupdates/helpers/latestupdates_block.php +++ b/3.0/modules/latestupdates/helpers/latestupdates_block.php @@ -25,7 +25,6 @@ class latestupdates_block_Core { static function get($block_id, $theme) { $block = ""; - switch ($block_id) { case "latestupdates":