Skip to content

Commit

Permalink
NavigationItem: Make it having a name instead of a id..
Browse files Browse the repository at this point in the history
..and require it as first argument on construction time.

refs #5600
  • Loading branch information
Johannes Meyer committed Sep 2, 2015
1 parent b3159ee commit f449c78
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 76 deletions.
80 changes: 18 additions & 62 deletions library/Icinga/Web/Navigation/Navigation.php
Expand Up @@ -8,58 +8,17 @@
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;
use Icinga\Util\String;

/**
* 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
{
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -240,30 +199,27 @@ public function addItem($name, array $properties = array())
$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()
{
Expand All @@ -281,29 +237,29 @@ public function hasItems()
}

/**
* Get whether the navigation is empty
* Return whether this navigation is empty
*
* @return bool
* @return bool
*/
public function isEmpty()
{
return empty($this->items);
}

/**
* Get the layout
* Return this navigation's layout
*
* @return int
* @return int
*/
public function getLayout()
{
return $this->layout;
}

/**
* Set the layout
* Set this navigation's layout
*
* @param int $layout
* @param int $layout
*
* @return $this
*/
Expand All @@ -314,9 +270,9 @@ public function setLayout($layout)
}

/**
* Get the navigation renderer
* Create and return the renderer for this navigation
*
* @return RecursiveNavigationRenderer
* @return RecursiveNavigationRenderer
*/
public function getRenderer()
{
Expand Down
31 changes: 17 additions & 14 deletions library/Icinga/Web/Navigation/NavigationItem.php
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -309,25 +312,25 @@ public function setIcon($icon)
}

/**
* 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;
}

Expand Down

0 comments on commit f449c78

Please sign in to comment.