From 0e07124a1ee4e3248790169e554a380ea2ce2d5c Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Wed, 6 Jul 2011 11:10:08 -0400 Subject: [PATCH] make Menu class more functional --- lib/menu.php | 71 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 67 insertions(+), 4 deletions(-) diff --git a/lib/menu.php b/lib/menu.php index 2713b44d50..4f671380e6 100644 --- a/lib/menu.php +++ b/lib/menu.php @@ -49,6 +49,8 @@ class Menu extends Widget { var $action = null; var $actionName = null; + var $actionArgs = null; + /** * Construction * @@ -60,12 +62,52 @@ class Menu extends Widget $this->action = $action; $this->actionName = $action->trimmed('action'); + + $rtargs = $action->returnToArgs(); + + $this->actionArgs = $rtargs[1]; + } + + function getItems() + { + return array(); + } + + function tag() + { + return null; } + + function show() + { + $items = $this->getItems(); + $tag = $this->tag(); + + $attrs = array('class' => 'nav'); + + if (!is_null($tag)) { + $attrs['id'] = 'nav_' . $tag; + } + if (Event::handle('StartNav', array($this, &$tag, &$items))) { + + $this->out->elementStart('ul', $attrs); + + foreach ($items as $item) { + list($actionName, $args, $label, $description, $id) = $item; + $this->item($actionName, $args, $label, $description, $id); + } + + $this->out->elementEnd('ul'); + + Event::handle('EndNav', array($this, $tag, $items)); + } + } + function item($actionName, $args, $label, $description, $id=null) { if (empty($id)) { - $id = $this->menuItemID($actionName); + $id = $this->menuItemID($actionName, $args); } $url = common_local_url($actionName, $args); @@ -73,13 +115,34 @@ class Menu extends Widget $this->out->menuItem($url, $label, $description, - $actionName == $this->actionName, + $this->isCurrent($actionName, $args), $id); } - function menuItemID($actionName) + function isCurrent($actionName, $args) { - return sprintf('nav_%s', $actionName); + if ($actionName != $this->actionName) { + return false; + } + + foreach ($this->actionArgs as $k => $v) { + if (!array_key_exists($k, $args) || $args[$k] != $v) { + return false; + } + } + + return true; + } + + function menuItemID($actionName, $args = null) + { + $id = sprintf('nav_%s', $actionName); + + if (!is_null($args)) { + foreach ($args as $key => $value) { + $id .= '_' . $key . '_' . $value; + } + } } function submenu($label, $menu) -- 2.39.5