diff options
| author | Bharat Mediratta <bharat@menalto.com> | 2008-12-08 06:14:34 +0000 | 
|---|---|---|
| committer | Bharat Mediratta <bharat@menalto.com> | 2008-12-08 06:14:34 +0000 | 
| commit | b878ed174d9e8628098931156d526b3fc028d905 (patch) | |
| tree | ae355456ce164d46a36d1a01b93f9c46a758d780 /core/libraries/Menu.php | |
| parent | 655581f3c996a6404f07486ade64d30cab335e8e (diff) | |
Refactor Menu code to create allow you to create menus using a
chainable factory interface and retrieve them by ids.  Streamlined the
HTML creation code a little bit in the process, moved the basic menu
functionality into Theme_View and created the option to have different
menus other than site_navigation().
Diffstat (limited to 'core/libraries/Menu.php')
| -rw-r--r-- | core/libraries/Menu.php | 169 | 
1 files changed, 78 insertions, 91 deletions
| 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; +class Menu_Element { +  public $label; +  public $url; +  public $id; -  protected function __construct($type, $text, $url) { -    $this->text = $text; -    $this->type = $type; -    $this->url = $url; +  /** +   * 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 href=\"$this->url\">$this->label</a><li>";    } +} - +/** + * Menu element that provides a pop-up dialog + */ +class Menu_Element_Dialog 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 class=\"gDialogLink\" href=\"$this->url\" " . +           "title=\"$this->label\">$this->label</a></li>";    }  } -class Menu_Core extends Menu_Item { -  public function __construct($text="", $url="#") { -    parent::__construct("menu", $text, $url); -    $this->_data['items'] = array(); -  } +/** + * Root menu or submenu + */ +class Menu_Core extends Menu_Element { +  public $elements; +  public $is_root; -  public function append($menu_item) { -    $items = $this->items; -    $items[] = $menu_item; -    $this->items = $items; -  } +  /** +   * Return an instance of a Menu_Element +   * @chainable +   */ +  public static function factory($type) { +    switch($type) { +    case "link": +      return new Menu_Element_Link(); -  public function get($text) { -    foreach ($this->items as $item) { -      if ($item->text == $text) { -        return $item; -      } -    } -    return false; -  } +    case "dialog": +      return new Menu_Element_Dialog(); -  private function _get_index($text) { -    foreach ($this->items as $idx => $item) { -      if ($item->text == $text) { -        return (int)$idx; -      } +    case "submenu": +      return new Menu(); + +    default: +      throw Exception("@todo UNKNOWN_MENU_TYPE");      } -    return false;    } -  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); +  public function __construct($is_root=false) { +    $this->elements = array(); +    $this->is_root = $is_root;    } -  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; +  /** +   * Add a new element to this menu +   */ +  public function append($menu_element) { +    $this->elements[$menu_element->id] = $menu_element; +    return $this; +  } -    $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;    }  } | 
