diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/helpers/core_menu.php | 49 | ||||
-rw-r--r-- | core/helpers/menus.php | 42 | ||||
-rw-r--r-- | core/libraries/Menu.php | 175 | ||||
-rw-r--r-- | core/libraries/Theme_View.php | 17 | ||||
-rw-r--r-- | core/tests/Menu_Test.php | 56 |
5 files changed, 138 insertions, 201 deletions
diff --git a/core/helpers/core_menu.php b/core/helpers/core_menu.php index a20f96a2..3f0c5024 100644 --- a/core/helpers/core_menu.php +++ b/core/helpers/core_menu.php @@ -18,31 +18,52 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class core_menu_Core { - public static function items($menus, $theme) { - $menus->append(new Menu_Link(_("HOME"), url::base())); - $menus->append(new Menu_Link(_("BROWSE"), url::site("albums/1"))); + public static function site_navigation($menu, $theme) { + $menu->append( + Menu::factory("link") + ->id("home") + ->label(_("Home")) + ->url(url::base())); + + $menu->append( + Menu::factory("link") + ->id("browse") + ->label(_("Browse")) + ->url(url::site("albums/1"))); $item = $theme->item(); $user = Session::instance()->get("user", null); if ($user) { - // @todo guard with permissions - $upload_menu = new Menu(_("UPLOAD")); - $upload_menu->append( - new Menu_Dialog(_("Add Photos"), url::site("form/add/photos/$item->id"))); - $menus->append($upload_menu); + // @todo need to do a permission check here + $menu->append( + Menu::factory("submenu") + ->id("upload_menu") + ->label(_("Upload")) + ->append( + Menu::factory("dialog") + ->id("add_photos") + ->label(_("Add Photos")) + ->url(url::site("form/add/photos/$item->id")))); - $admin_menu = new Menu(_("ADMIN")); + $admin_menu = Menu::factory("submenu") + ->id("admin_menu") + ->label(_("Admin")); + $menu->append($admin_menu); // @todo need to do a permission check here $admin_menu->append( - new Menu_Dialog(_("Edit Item"), url::site("form/edit/{$item->type}s/$item->id"))); + Menu::factory("dialog") + ->id("edit") + ->label(_("Edit")) + ->url(url::site("form/edit/{$item->type}s/$item->id"))); if ($user->admin) { - $admin_menu->append(new Menu_Link(_("Site Admin"), url::site("admin"))); + $admin_menu->append( + Menu::factory("link") + ->id("site_admin") + ->label(_("Site Admin")) + ->url(url::site("admin"))); } - - $menus->append($admin_menu); } } - } diff --git a/core/helpers/menus.php b/core/helpers/menus.php deleted file mode 100644 index 9076b3bb..00000000 --- a/core/helpers/menus.php +++ /dev/null @@ -1,42 +0,0 @@ -<?php defined("SYSPATH") or die("No direct script access."); -/** - * Gallery - a web based photo album viewer and editor - * Copyright (C) 2000-2008 Bharat Mediratta - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or (at - * your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. - */ -class menus_Core { - public static function get_menu_items($theme) { - $menu = new Menu(); - - // Call core menus first to establish the basic menu - self::_get_module_menu_items("core", $menu, $theme); - foreach (module::installed() as $module) { - if ($module->name == "core") { - continue; - } - self::_get_module_menu_items($module->name, $menu, $theme); - } - - return $menu; - } - - private static function _get_module_menu_items($module_name, $menu, $theme) { - $class = "{$module_name}_menu"; - if (method_exists($class, "items")) { - call_user_func_array(array($class, "items"), array(&$menu, $theme)); - } - } -}
\ No newline at end of file diff --git a/core/libraries/Menu.php b/core/libraries/Menu.php index 4695004b..9fd51415 100644 --- a/core/libraries/Menu.php +++ b/core/libraries/Menu.php @@ -17,122 +17,109 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ -abstract class Menu_Item { - protected $text; - protected $type; - protected $url; - - protected function __construct($type, $text, $url) { - $this->text = $text; - $this->type = $type; - $this->url = $url; +class Menu_Element { + public $label; + public $url; + public $id; + + /** + * Set the id + * @chainable + */ + public function id($id) { + $this->id = $id; + return $this; } -} -class Menu_Link extends Menu_Item { - public function __construct($text="", $url="#") { - parent::__construct("link", $text, $url); + /** + * Set the label + * @chainable + */ + public function label($label) { + $this->label = $label; + return $this; } - public function __toString() { - return "<li><a href=\"$this->url\">$this->text</a><li>"; + /** + * Set the url + * @chainable + */ + public function url($url) { + $this->url = $url; + return $this; } } -class Menu_Dialog extends Menu_Item { - public function __construct($text="", $url="#", $class="gDialogLink") { - parent::__construct("dialog", $text, $url); - $this->dialog_class = $class; - $this->title = $text; - } - - +/** + * Menu element that provides a link to a new page. + */ +class Menu_Element_Link extends Menu_Element { public function __toString() { - return "<li><a class=\"$this->dialog_class\" href=\"$this->url\" " . - "title=\"$this->title\">$this->text</a></li>"; + return "<li><a href=\"$this->url\">$this->label</a><li>"; } } -class Menu_Core extends Menu_Item { - public function __construct($text="", $url="#") { - parent::__construct("menu", $text, $url); - $this->_data['items'] = array(); - } - - public function append($menu_item) { - $items = $this->items; - $items[] = $menu_item; - $this->items = $items; +/** + * Menu element that provides a pop-up dialog + */ +class Menu_Element_Dialog extends Menu_Element { + public function __toString() { + return "<li><a class=\"gDialogLink\" href=\"$this->url\" " . + "title=\"$this->label\">$this->label</a></li>"; } +} - public function get($text) { - foreach ($this->items as $item) { - if ($item->text == $text) { - return $item; - } +/** + * Root menu or submenu + */ +class Menu_Core extends Menu_Element { + public $elements; + public $is_root; + + /** + * Return an instance of a Menu_Element + * @chainable + */ + public static function factory($type) { + switch($type) { + case "link": + return new Menu_Element_Link(); + + case "dialog": + return new Menu_Element_Dialog(); + + case "submenu": + return new Menu(); + + default: + throw Exception("@todo UNKNOWN_MENU_TYPE"); } - return false; } - private function _get_index($text) { - foreach ($this->items as $idx => $item) { - if ($item->text == $text) { - return (int)$idx; - } - } - return false; + public function __construct($is_root=false) { + $this->elements = array(); + $this->is_root = $is_root; } - public function insert_before($text, $menu_item) { - $offset = $this->_get_index($text); - Kohana::log("debug", "$offset: $offset"); - - $items = $this->items; - - $front_part = ($offset == 0) ? array() : array_splice($items, 0, $offset); - $back_part = ($offset == 0) ? $this->items : array_splice($items, $offset - 1); - Kohana::log("debug", print_r($front_part, 1)); - Kohana::log("debug", print_r($front_part, 1)); - $this->items = array_merge($front_part, array($menu_item), $back_part); + /** + * Add a new element to this menu + */ + public function append($menu_element) { + $this->elements[$menu_element->id] = $menu_element; + return $this; } - public function insert_after($text, $menu_item) { - $offset = $this->_get_index($text); - $last_offset = count($this->items) - 1; - // If not found, then append to the end - if ($offset == false) { - $offset = $last_offset; - } - - $items = $this->items; - - $front_part = ($offset == $last_offset) ? $this->items : array_splice($items, 0, $offset + 1); - $back_part = ($offset == $last_offset) ? array() : array_splice($items, $offset - 1); - $this->items = array_merge($front_part, array($menu_item), $back_part); + /** + * Retrieve a Menu_Element by id + */ + public function get($id) { + return $this->elements[$id]; } public function __toString() { - Kohana::log("debug", print_r($this, 1)); - $items_html = array(); - $item_text = $this->text; - if (!empty($item_text)) { - $items_html[] = "<li><a href=\"#\">$item_text</a>"; - } - - $items = $this->items; - if (!empty($items)) { - $items_html[] = "<ul>"; - - foreach ($items as $item) { - $items_html[] = $item->__toString(); - } - - $items_html[] = "</ul>"; - } - - if (!empty($item_text)) { - $items_html[] = "</li>"; - } - return implode("\n", $items_html); + $html = $this->is_root ? "<ul>" : "<li><a href=#>$this->label</a><ul>"; + $html .= implode("\n", $this->elements); + $html .= $this->is_root ? "</ul>" : "</ul></li>"; + return $html; } } diff --git a/core/libraries/Theme_View.php b/core/libraries/Theme_View.php index 1c388596..1ab57f4c 100644 --- a/core/libraries/Theme_View.php +++ b/core/libraries/Theme_View.php @@ -58,9 +58,20 @@ class Theme_View_Core extends View { } public function site_navigation() { - $menu = menus::get_menu_items($this)->__toString(); - Kohana::log("debug", sprintf("[%s%s] site_navigation: %s", __FILE__, __LINE__, $menu)); - return $menu; + $menu = new Menu(true); + core_menu::site_navigation($menu, $this); + + foreach (module::installed() as $module) { + if ($module->name == "core") { + continue; + } + $class = "{$module->name}_menu"; + if (method_exists($class, "site_navigation")) { + call_user_func_array(array($class, "site_navigation"), array(&$menu, $this)); + } + } + + print $menu; } public function pager() { diff --git a/core/tests/Menu_Test.php b/core/tests/Menu_Test.php index c2d68c03..82e8301d 100644 --- a/core/tests/Menu_Test.php +++ b/core/tests/Menu_Test.php @@ -19,54 +19,14 @@ */ class Menu_Test extends Unit_Test_Case { public function find_menu_item_test() { - $test_menu = new Menu(); - $test_menu->append(new Menu_Link("test1")); - $test_menu->append(new Menu_Link("test2")); - $expected = new Menu_Link("test3"); - $test_menu->append($expected); - $test_menu->append(new Menu_Link("test4")); + $menu = new Menu(true); + $menu + ->append(Menu::factory("link")->id("element_1")) + ->append(Menu::factory("dialog")->id("element_2")) + ->append(Menu::factory("submenu")->id("element_3") + ->append(Menu::factory("link")->id("element_3_1"))); - $menu_item = $test_menu->get("test3"); - $this->assert_equal($expected, $menu_item); - } - - public function insert_before_test() { - $expected = new Menu(); - $expected->append(new Menu_Link("test-2")); - $expected->append(new Menu_Link("test0")); - $expected->append(new Menu_Link("test1")); - $expected->append(new Menu_Link("test1b")); - $expected->append(new Menu_Link("test2")); - $expected->append(new Menu_Link("test4")); - - $test_menu = new Menu(); - $test_menu->append(new Menu_Link("test1")); - $test_menu->append(new Menu_Link("test2")); - $test_menu->append(new Menu_Link("test4")); - $test_menu->insert_before("test2", new Menu_Link("test1b")); - $test_menu->insert_before("test1", new Menu_Link("test0")); - $test_menu->insert_before("test-1", new Menu_Link("test-2")); - - $this->assert_equal($expected, $test_menu); - } - - public function insert_after_test() { - $expected = new Menu(); - $expected->append(new Menu_Link("test1")); - $expected->append(new Menu_Link("test2")); - $expected->append(new Menu_Link("test3")); - $expected->append(new Menu_Link("test4")); - $expected->append(new Menu_Link("test5")); - $expected->append(new Menu_Link("test7")); - - $test_menu = new Menu(); - $test_menu->append(new Menu_Link("test1")); - $test_menu->append(new Menu_Link("test2")); - $test_menu->append(new Menu_Link("test4")); - $test_menu->insert_after("test2", new Menu_Link("test3")); - $test_menu->insert_after("test4", new Menu_Link("test5")); - $test_menu->insert_after("test6", new Menu_Link("test7")); - - $this->assert_equal($expected, $test_menu); + $this->assert_equal("element_2", $menu->get("element_2")->id); + $this->assert_equal("element_3_1", $menu->get("element_3")->get("element_3_1")->id); } }
\ No newline at end of file |