From dd0b622ae9e269e828699928fa8bddcd17d66225 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Sun, 7 Aug 2011 13:27:27 -0700 Subject: Initial commit of a patch for Ticket #1764. as discussed here: https://github.com/gallery/gallery3/pull/58/files#r72949. Create a Breadcrumb library which has two static methods for_item (which takes a an item and builds the entire breadcrumb for the item) or build (which takes a variable number of Breadcrumb elements and creates a breadcrumb based on the specified elements). Used tag->url() to build the tag album url. Escaped the query string for the search. Tightened up the breadcrumb code in page.html.php. When adding the show query parameter, we can't blindly concatenate using the ? separator. We have to check that we use a & if a query parameter already exists. --- modules/gallery/controllers/albums.php | 1 + modules/gallery/controllers/movies.php | 1 + modules/gallery/controllers/photos.php | 1 + modules/gallery/libraries/Breadcrumb.php | 74 +++++++++++++++++++++++++++++++ modules/gallery/tests/Breadcrumb_Test.php | 51 +++++++++++++++++++++ modules/search/controllers/search.php | 5 +++ modules/tag/controllers/tag.php | 3 ++ themes/wind/views/page.html.php | 27 +++-------- 8 files changed, 143 insertions(+), 20 deletions(-) create mode 100644 modules/gallery/libraries/Breadcrumb.php create mode 100644 modules/gallery/tests/Breadcrumb_Test.php diff --git a/modules/gallery/controllers/albums.php b/modules/gallery/controllers/albums.php index ccf6c1cb..90071fb5 100644 --- a/modules/gallery/controllers/albums.php +++ b/modules/gallery/controllers/albums.php @@ -69,6 +69,7 @@ class Albums_Controller extends Items_Controller { "item" => $album, "children" => $album->viewable()->children($page_size, $offset), "parents" => $album->parents()->as_array(), // view calls empty() on this + "breadcrumbs" => Breadcrumb::build_from_item($album), "children_count" => $children_count)); $template->content = new View("album.html"); diff --git a/modules/gallery/controllers/movies.php b/modules/gallery/controllers/movies.php index 8e81c594..77d92e13 100644 --- a/modules/gallery/controllers/movies.php +++ b/modules/gallery/controllers/movies.php @@ -43,6 +43,7 @@ class Movies_Controller extends Items_Controller { "children" => array(), "children_count" => 0, "parents" => $movie->parents()->as_array(), + "breadcrumbs" => Breadcrumb::build_from_item($movie), "next_item" => $next_item, "previous_item" => $previous_item, "sibling_count" => $movie->parent()->viewable()->children_count($where), diff --git a/modules/gallery/controllers/photos.php b/modules/gallery/controllers/photos.php index 054300a1..5c8c7b34 100644 --- a/modules/gallery/controllers/photos.php +++ b/modules/gallery/controllers/photos.php @@ -43,6 +43,7 @@ class Photos_Controller extends Items_Controller { "children" => array(), "children_count" => 0, "parents" => $photo->parents()->as_array(), + "breadcrumbs" => Breadcrumb::build_from_item($photo), "next_item" => $next_item, "previous_item" => $previous_item, "sibling_count" => $photo->parent()->viewable()->children_count($where), diff --git a/modules/gallery/libraries/Breadcrumb.php b/modules/gallery/libraries/Breadcrumb.php new file mode 100644 index 00000000..3a2499ba --- /dev/null +++ b/modules/gallery/libraries/Breadcrumb.php @@ -0,0 +1,74 @@ +parents() as $element) { + $breadcrumbs[] = new Breadcrumb($element->title, $element->url(), $element->id); + } + + if (!empty($breadcrumbs)) { + $breadcrumbs[] = new Breadcrumb($item->title, $item->url(), $item->id); + } + + return self::generate_show_query_strings($breadcrumbs); + } + + /** + * This static function takes a list (variable arguments) of Breadcrumbs and builds a dynamic + * breadcrumb list. Used to create a breadcrumb for dynamic albums. Will really be useful + * for the display context change. + */ + static function build_from_list() { + return self::generate_show_query_strings(func_get_args()); + } + + private static function generate_show_query_strings($breadcrumbs) { + if (!empty($breadcrumbs)) { + + end($breadcrumbs)->last = true;; + while ($breadcrumb = current($breadcrumbs)) { + if (isset($last_id) && $last_id > 0) { + $query = parse_url($breadcrumb->url, PHP_URL_QUERY); + $breadcrumb->url = $breadcrumb->url . ($query ? "&" : "?") . "show={$last_id}"; + } + $last_id = $breadcrumb->id; + $breadcrumb = prev($breadcrumbs); + } + $breadcrumbs[0]->first = true; + } + + return $breadcrumbs; + } + + public function __construct($title, $url, $id=0) { + $this->title = $title; + $this->url = $url; + $this->id = $id; + $this->first = false; + $this->last = false; + } +} diff --git a/modules/gallery/tests/Breadcrumb_Test.php b/modules/gallery/tests/Breadcrumb_Test.php new file mode 100644 index 00000000..9f9aeddc --- /dev/null +++ b/modules/gallery/tests/Breadcrumb_Test.php @@ -0,0 +1,51 @@ +album = test::random_album(); + $this->item = test::random_photo($this->album); + $this->album->reload(); + } + + public function teardown() { + $this->album = null; + $this->item = null; + } + + public function build_breadcrumbs_for_item_test() { + $breadcrumbs = Breadcrumb::build_from_item($this->item); + $this->assert_equal("Gallery", $breadcrumbs[0]->title); + $this->assert_equal($this->album->title, $breadcrumbs[1]->title); + $this->assert_equal($this->item->title, $breadcrumbs[2]->title); + } + + public function build_breadcrumbs_from_items_test() { + $breadcrumbs = Breadcrumb::build_from_list( + new Breadcrumb(item::root()->title, "/", item::root()->id), + new Breadcrumb($this->album->title, $this->album->relative_path(), $this->album->id), + new Breadcrumb($this->item->title, $this->item->relative_path(), $this->item->id)); + $this->assert_equal("Gallery", $breadcrumbs[0]->title); + $this->assert_equal($this->album->title, $breadcrumbs[1]->title); + $this->assert_equal($this->item->title, $breadcrumbs[2]->title); + } +} \ No newline at end of file diff --git a/modules/search/controllers/search.php b/modules/search/controllers/search.php index 261d67ee..d30ffa67 100644 --- a/modules/search/controllers/search.php +++ b/modules/search/controllers/search.php @@ -36,9 +36,14 @@ class Search_Controller extends Controller { $max_pages = max(ceil($count / $page_size), 1); $template = new Theme_View("page.html", "collection", "search"); + $root = item::root(); + $search_url = url::abs_site("search?q=" . urlencode($q)); $template->set_global(array("page" => $page, "max_pages" => $max_pages, "page_size" => $page_size, + "breadcrumbs" => Breadcrumb::build_from_list( + new Breadcrumb(item::root()->title, "/", item::root()->id), + new Breadcrumb($q, $search_url)), "children_count" => $count)); $template->content = new View("search.html"); diff --git a/modules/tag/controllers/tag.php b/modules/tag/controllers/tag.php index 8f885dea..44a171fd 100644 --- a/modules/tag/controllers/tag.php +++ b/modules/tag/controllers/tag.php @@ -40,6 +40,9 @@ class Tag_Controller extends Controller { "page_size" => $page_size, "tag" => $tag, "children" => $tag->items($page_size, $offset), + "breadcrumbs" => Breadcrumb::build_from_list( + new Breadcrumb(item::root()->title, "/", item::root()->id), + new Breadcrumb($tag->name, $tag->url())), "children_count" => $children_count)); $template->content = new View("dynamic.html"); $template->content->title = t("Tag: %tag_name", array("tag_name" => $tag->name)); diff --git a/themes/wind/views/page.html.php b/themes/wind/views/page.html.php index 045e3c45..534b7de4 100644 --- a/themes/wind/views/page.html.php +++ b/themes/wind/views/page.html.php @@ -107,28 +107,15 @@ header_bottom() ?> - item() && !empty($parents)): ?> + -- cgit v1.2.3