NavigationItem: Make it having a name instead of a id..

..and require it as first argument on construction time.

refs #5600
This commit is contained in:
Johannes Meyer 2015-09-02 13:27:12 +02:00
parent b3159ee60d
commit f449c78dbf
2 changed files with 35 additions and 76 deletions

View File

@ -8,10 +8,10 @@ use ArrayIterator;
use Countable; use Countable;
use InvalidArgumentException; use InvalidArgumentException;
use IteratorAggregate; use IteratorAggregate;
use Icinga\Authentication\Auth;
use Icinga\Application\Config; use Icinga\Application\Config;
use Icinga\Application\Icinga; use Icinga\Application\Icinga;
use Icinga\Application\Logger; use Icinga\Application\Logger;
use Icinga\Authentication\Auth;
use Icinga\Data\ConfigObject; use Icinga\Data\ConfigObject;
use Icinga\Exception\ConfigurationError; use Icinga\Exception\ConfigurationError;
use Icinga\Exception\ProgrammingError; use Icinga\Exception\ProgrammingError;
@ -19,47 +19,6 @@ use Icinga\Util\String;
/** /**
* Container for navigation items * Container for navigation items
*
* Usage example:
* <code>
* <?php
*
* namespace Icinga\Example;
*
* use Icinga\Web\Navigation\DropdownItem;
* use Icinga\Web\Navigation\Navigation;
* use Icinga\Web\Navigation\NavigationItem;
*
* $navigation = new Navigation();
* $navigation->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 class Navigation implements ArrayAccess, Countable, IteratorAggregate
{ {
@ -92,14 +51,14 @@ class Navigation implements ArrayAccess, Countable, IteratorAggregate
protected static $types; protected static $types;
/** /**
* Navigation items * This navigation's items
* *
* @var NavigationItem[] * @var NavigationItem[]
*/ */
protected $items = array(); protected $items = array();
/** /**
* Navigation layout * This navigation's layout
* *
* @var int * @var int
*/ */
@ -240,30 +199,27 @@ class Navigation implements ArrayAccess, Countable, IteratorAggregate
$item = $name; $item = $name;
} }
$this->items[] = $item; $this->items[$item->getName()] = $item;
return true; 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 * @param mixed $default
* *
* @return NavigationItem|mixed * @return NavigationItem|mixed
*/ */
public function getItem($id, $default = null) public function getItem($name, $default = null)
{ {
if (isset($this->items[$id])) { return isset($this->items[$name]) ? $this->items[$name] : $default;
return $this->items[$id];
}
return $default;
} }
/** /**
* Get the items * Return this navigation's items
* *
* @return array * @return array
*/ */
public function getItems() 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() 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() 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 * @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() public function getRenderer()
{ {

View File

@ -53,11 +53,11 @@ class NavigationItem implements Countable, IteratorAggregate
protected $icon; protected $icon;
/** /**
* Item's ID * This item's name
* *
* @var mixed * @var string
*/ */
protected $id; protected $name;
/** /**
* Label * Label
@ -95,15 +95,18 @@ class NavigationItem implements Countable, IteratorAggregate
protected $view; 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)) { if (! empty($properties)) {
$this->setProperties($properties); $this->setProperties($properties);
} }
$this->children = new Navigation(); $this->children = new Navigation();
$this->init(); $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 * @return $this
*/ */
public function setId($id) public function setName($name)
{ {
$this->id = $id; $this->name = $name;
return $this; return $this;
} }