_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[] = ""; } if (!empty($this->_text)) { $items_html[] = "
  • "; } return implode("\n", $items_html); } }