From 954fcf034238458f292c9f5ec72c6d4b101aac0d Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Sun, 7 Dec 2008 19:45:46 +0000 Subject: Merge gallery3/branches/menus back into gallery3/trunk --- core/libraries/Menu.php | 111 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 core/libraries/Menu.php (limited to 'core/libraries/Menu.php') diff --git a/core/libraries/Menu.php b/core/libraries/Menu.php new file mode 100644 index 00000000..4f4530a7 --- /dev/null +++ b/core/libraries/Menu.php @@ -0,0 +1,111 @@ +_text = $text; + $this->_url = $url; + } + + function __toString() { + return "
  • _url\">$this->_text
  • "; + } +} + +class Menu_Core { + protected $_text; + protected $_url; + protected $_items = array(); + + public function __construct($text="", $url="#") { + $this->_text = $text; + $this->_url = $url; + } + + public function append($menu_item) { + $this->_items[] = $menu_item; + } + + public function get($text) { + foreach ($this->_items as $item) { + if ($item->_text == $text) { + return $item; + } + } + return false; + } + + private function _get_index($text) { + foreach ($this->_items as $idx => $item) { + if ($item->_text == $text) { + return (int)$idx; + } + } + return false; + } + + public function insert_before($text, $menu_item) { + $offset = $this->_get_index($text); + + $front_part = ($offset == 0) ? array() : array_splice($this->_items, 0, $offset); + $back_part = ($offset == 0) ? $this->_items : array_splice($this->_items, $offset - 1); + $this->_items = array_merge($front_part, array($menu_item), $back_part); + } + + 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; + } + + $front_part = ($offset == $last_offset) ? $this->_items : array_splice($this->_items, 0, $offset + 1); + Kohana::log("debug", print_r($front_part, 1)); + $back_part = ($offset == $last_offset) ? array() : array_splice($this->_items, $offset - 1); + Kohana::log("debug", print_r($back_part, 1)); + $this->_items = array_merge($front_part, array($menu_item), $back_part); + } + + public function __toString() { + $items_html = array(); + if (!empty($this->_text)) { + $items_html[] = "
  • _url\">$this->_text"; + } + + if (!empty($this->_items)) { +// $items_html[] = "