From f449c78dbf637ced97974b7fbe08083114638706 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Wed, 2 Sep 2015 13:27:12 +0200 Subject: [PATCH] NavigationItem: Make it having a name instead of a id.. ..and require it as first argument on construction time. refs #5600 --- library/Icinga/Web/Navigation/Navigation.php | 80 +++++-------------- .../Icinga/Web/Navigation/NavigationItem.php | 31 +++---- 2 files changed, 35 insertions(+), 76 deletions(-) diff --git a/library/Icinga/Web/Navigation/Navigation.php b/library/Icinga/Web/Navigation/Navigation.php index bad58e390..5eff8e37c 100644 --- a/library/Icinga/Web/Navigation/Navigation.php +++ b/library/Icinga/Web/Navigation/Navigation.php @@ -8,10 +8,10 @@ use ArrayIterator; use Countable; use InvalidArgumentException; use IteratorAggregate; -use Icinga\Authentication\Auth; use Icinga\Application\Config; use Icinga\Application\Icinga; use Icinga\Application\Logger; +use Icinga\Authentication\Auth; use Icinga\Data\ConfigObject; use Icinga\Exception\ConfigurationError; use Icinga\Exception\ProgrammingError; @@ -19,47 +19,6 @@ use Icinga\Util\String; /** * Container for navigation items - * - * Usage example: - * - * setLayout(Navigation::LAYOUT_TABS); - * $home = new NavigationItem(); - * $home - * ->setIcon('home') - * ->setLabel('Home'); - * ->setUrl('/home'); - * $logout = new NavigationItem(); - * $logout - * ->setIcon('logout') - * ->setLabel('Logout') - * ->setUrl('/logout'); - * $dropdown = new DropdownItem(); - * $dropdown - * ->setIcon('user') - * ->setLabel('Preferences'); - * $preferences = new NavigationItem(); - * $preferences - * ->setIcon('preferences'); - * ->setLabel('preferences') - * ->setUrl('/preferences'); - * $dropdown->addChild($preferences); - * $navigation - * ->addItem($home) - * ->addItem($logout); - * ->addItem($dropdown); - * echo $navigation - * ->getRenderer() - * ->setCssClass('example-nav') - * ->render(); */ class Navigation implements ArrayAccess, Countable, IteratorAggregate { @@ -92,14 +51,14 @@ class Navigation implements ArrayAccess, Countable, IteratorAggregate protected static $types; /** - * Navigation items + * This navigation's items * * @var NavigationItem[] */ protected $items = array(); /** - * Navigation layout + * This navigation's layout * * @var int */ @@ -240,30 +199,27 @@ class Navigation implements ArrayAccess, Countable, IteratorAggregate $item = $name; } - $this->items[] = $item; + $this->items[$item->getName()] = $item; return true; } /** - * Get the item with the given ID + * Return the item with the given name * - * @param mixed $id + * @param string $name * @param mixed $default * * @return NavigationItem|mixed */ - public function getItem($id, $default = null) + public function getItem($name, $default = null) { - if (isset($this->items[$id])) { - return $this->items[$id]; - } - return $default; + return isset($this->items[$name]) ? $this->items[$name] : $default; } /** - * Get the items + * Return this navigation's items * - * @return array + * @return array */ public function getItems() { @@ -281,9 +237,9 @@ class Navigation implements ArrayAccess, Countable, IteratorAggregate } /** - * Get whether the navigation is empty + * Return whether this navigation is empty * - * @return bool + * @return bool */ public function isEmpty() { @@ -291,9 +247,9 @@ class Navigation implements ArrayAccess, Countable, IteratorAggregate } /** - * Get the layout + * Return this navigation's layout * - * @return int + * @return int */ public function getLayout() { @@ -301,9 +257,9 @@ class Navigation implements ArrayAccess, Countable, IteratorAggregate } /** - * Set the layout + * Set this navigation's layout * - * @param int $layout + * @param int $layout * * @return $this */ @@ -314,9 +270,9 @@ class Navigation implements ArrayAccess, Countable, IteratorAggregate } /** - * Get the navigation renderer + * Create and return the renderer for this navigation * - * @return RecursiveNavigationRenderer + * @return RecursiveNavigationRenderer */ public function getRenderer() { diff --git a/library/Icinga/Web/Navigation/NavigationItem.php b/library/Icinga/Web/Navigation/NavigationItem.php index b7a57f449..51b1f5225 100644 --- a/library/Icinga/Web/Navigation/NavigationItem.php +++ b/library/Icinga/Web/Navigation/NavigationItem.php @@ -53,11 +53,11 @@ class NavigationItem implements Countable, IteratorAggregate protected $icon; /** - * Item's ID + * This item's name * - * @var mixed + * @var string */ - protected $id; + protected $name; /** * Label @@ -95,15 +95,18 @@ class NavigationItem implements Countable, IteratorAggregate protected $view; /** - * Create a new navigation item + * Create a new NavigationItem * - * @param array $properties + * @param string $name + * @param array $properties */ - public function __construct(array $properties = array()) + public function __construct($name, array $properties = null) { + $this->setName($name); if (! empty($properties)) { $this->setProperties($properties); } + $this->children = new Navigation(); $this->init(); } @@ -309,25 +312,25 @@ class NavigationItem implements Countable, IteratorAggregate } /** - * Get the item's ID + * Return this item's name * - * @return mixed + * @return string */ - public function getId() + public function getName() { - return $this->id; + return $this->name; } /** - * Set the item's ID + * Set this item's name * - * @param mixed $id ID of the item + * @param string $name * * @return $this */ - public function setId($id) + public function setName($name) { - $this->id = $id; + $this->name = $name; return $this; }