2015-09-01 12:48:45 +02:00
|
|
|
<?php
|
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
|
|
|
|
|
|
|
namespace Icinga\Web\Navigation;
|
|
|
|
|
|
|
|
use Countable;
|
2015-09-02 15:16:05 +02:00
|
|
|
use Exception;
|
2015-09-01 12:48:45 +02:00
|
|
|
use InvalidArgumentException;
|
|
|
|
use IteratorAggregate;
|
|
|
|
use Icinga\Application\Icinga;
|
2015-09-02 15:16:05 +02:00
|
|
|
use Icinga\Exception\IcingaException;
|
2015-09-01 12:48:45 +02:00
|
|
|
use Icinga\Web\View;
|
|
|
|
use Icinga\Web\Url;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A navigation item
|
|
|
|
*
|
|
|
|
* @see \Icinga\Web\Navigation\Navigation For a usage example.
|
|
|
|
*/
|
|
|
|
class NavigationItem implements Countable, IteratorAggregate
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Alternative markup element if the navigation item has no URL
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
const LINK_ALTERNATIVE = 'span';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the item is active
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $active = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attributes of the item's element
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $attributes = array();
|
|
|
|
|
|
|
|
/**
|
2015-09-02 15:20:26 +02:00
|
|
|
* This item's children
|
2015-09-01 12:48:45 +02:00
|
|
|
*
|
|
|
|
* @var Navigation
|
|
|
|
*/
|
|
|
|
protected $children;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Icon
|
|
|
|
*
|
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
protected $icon;
|
|
|
|
|
|
|
|
/**
|
2015-09-02 13:27:12 +02:00
|
|
|
* This item's name
|
2015-09-01 12:48:45 +02:00
|
|
|
*
|
2015-09-02 13:27:12 +02:00
|
|
|
* @var string
|
2015-09-01 12:48:45 +02:00
|
|
|
*/
|
2015-09-02 13:27:12 +02:00
|
|
|
protected $name;
|
2015-09-01 12:48:45 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Label
|
|
|
|
*
|
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
protected $label;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parent
|
|
|
|
*
|
|
|
|
* @var NavigationItem|null
|
|
|
|
*/
|
|
|
|
private $parent;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* URL
|
|
|
|
*
|
|
|
|
* @var Url|null
|
|
|
|
*/
|
|
|
|
protected $url;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* URL parameters
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $urlParameters = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* View
|
|
|
|
*
|
|
|
|
* @var View|null
|
|
|
|
*/
|
|
|
|
protected $view;
|
|
|
|
|
|
|
|
/**
|
2015-09-02 13:27:12 +02:00
|
|
|
* Create a new NavigationItem
|
2015-09-01 12:48:45 +02:00
|
|
|
*
|
2015-09-02 13:27:12 +02:00
|
|
|
* @param string $name
|
|
|
|
* @param array $properties
|
2015-09-01 12:48:45 +02:00
|
|
|
*/
|
2015-09-02 13:27:12 +02:00
|
|
|
public function __construct($name, array $properties = null)
|
2015-09-01 12:48:45 +02:00
|
|
|
{
|
2015-09-02 13:27:12 +02:00
|
|
|
$this->setName($name);
|
2015-09-02 15:20:26 +02:00
|
|
|
$this->children = new Navigation();
|
|
|
|
|
2015-09-01 12:48:45 +02:00
|
|
|
if (! empty($properties)) {
|
|
|
|
$this->setProperties($properties);
|
|
|
|
}
|
2015-09-02 13:27:12 +02:00
|
|
|
|
2015-09-01 12:48:45 +02:00
|
|
|
$this->init();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-09-02 15:20:26 +02:00
|
|
|
* Initialize this NavigationItem
|
2015-09-01 12:48:45 +02:00
|
|
|
*/
|
|
|
|
public function init()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function count()
|
|
|
|
{
|
2015-09-02 15:20:26 +02:00
|
|
|
return $this->getChildren()->count();
|
2015-09-01 12:48:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getIterator()
|
|
|
|
{
|
2015-09-02 15:20:26 +02:00
|
|
|
return $this->getChildren();
|
2015-09-01 12:48:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get whether the item is active
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function getActive()
|
|
|
|
{
|
|
|
|
return $this->active;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set whether the item is active
|
|
|
|
*
|
|
|
|
* Bubbles active state.
|
|
|
|
*
|
|
|
|
* @param bool $active
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setActive($active = true)
|
|
|
|
{
|
|
|
|
$this->active = (bool) $active;
|
|
|
|
$parent = $this;
|
|
|
|
while (($parent = $parent->parent) !== null) {
|
|
|
|
$parent->setActive($active);
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an attribute's value of the item's element
|
|
|
|
*
|
|
|
|
* @param string $name Name of the attribute
|
|
|
|
* @param mixed $default Default value
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getAttribute($name, $default = null)
|
|
|
|
{
|
|
|
|
$name = (string) $name;
|
|
|
|
if (array_key_exists($name, $this->attributes)) {
|
|
|
|
return $this->attributes[$name];
|
|
|
|
}
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set an attribute of the item's element
|
|
|
|
*
|
|
|
|
* @param string $name Name of the attribute
|
|
|
|
* @param mixed $value Value of the attribute
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setAttribute($name, $value)
|
|
|
|
{
|
|
|
|
$name = (string) $name;
|
|
|
|
$this->attributes[$name] = $value;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the item's attributes
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getAttributes()
|
|
|
|
{
|
|
|
|
return $this->attributes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the item's attributes
|
|
|
|
*
|
|
|
|
* @param array $attributes
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setAttributes(array $attributes)
|
|
|
|
{
|
|
|
|
foreach ($attributes as $name => $value) {
|
|
|
|
$this->setAttribute($name, $value);
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-09-02 15:17:26 +02:00
|
|
|
* Add a child to this item
|
2015-09-01 12:48:45 +02:00
|
|
|
*
|
2015-09-02 15:17:26 +02:00
|
|
|
* If the child is active this item gets activated as well.
|
2015-09-01 12:48:45 +02:00
|
|
|
*
|
2015-09-02 15:17:26 +02:00
|
|
|
* @param NavigationItem $child
|
2015-09-01 12:48:45 +02:00
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
2015-09-02 15:17:26 +02:00
|
|
|
public function addChild(NavigationItem $child)
|
2015-09-01 12:48:45 +02:00
|
|
|
{
|
2015-09-02 15:17:26 +02:00
|
|
|
$this->getChildren()->addItem($child->setParent($this));
|
2015-09-01 12:48:45 +02:00
|
|
|
if ($child->getActive()) {
|
|
|
|
$this->setActive();
|
|
|
|
}
|
2015-09-02 15:17:26 +02:00
|
|
|
|
2015-09-01 12:48:45 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the item's children
|
|
|
|
*
|
|
|
|
* @return Navigation
|
|
|
|
*/
|
|
|
|
public function getChildren()
|
|
|
|
{
|
|
|
|
return $this->children;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-09-02 15:20:26 +02:00
|
|
|
* Return whether this item has any children
|
2015-09-01 12:48:45 +02:00
|
|
|
*
|
2015-09-02 15:20:26 +02:00
|
|
|
* @return bool
|
2015-09-01 12:48:45 +02:00
|
|
|
*/
|
|
|
|
public function hasChildren()
|
|
|
|
{
|
2015-09-02 15:20:26 +02:00
|
|
|
return ! $this->getChildren()->isEmpty();
|
2015-09-01 12:48:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set children
|
|
|
|
*
|
2015-09-02 12:59:05 +02:00
|
|
|
* @param array|Navigation $children
|
2015-09-01 12:48:45 +02:00
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
2015-09-02 12:59:05 +02:00
|
|
|
public function setChildren($children)
|
2015-09-01 12:48:45 +02:00
|
|
|
{
|
2015-09-02 12:59:05 +02:00
|
|
|
if (is_array($children)) {
|
|
|
|
$children = Navigation::fromArray($children);
|
|
|
|
} elseif (! $children instanceof Navigation) {
|
|
|
|
throw new InvalidArgumentException('Argument $children must be of type array or Navigation');
|
|
|
|
}
|
|
|
|
|
2015-09-01 12:48:45 +02:00
|
|
|
$this->children = $children;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the icon
|
|
|
|
*
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public function getIcon()
|
|
|
|
{
|
|
|
|
return $this->icon;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the icon
|
|
|
|
*
|
|
|
|
* @param string $icon
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setIcon($icon)
|
|
|
|
{
|
|
|
|
$this->icon = (string) $icon;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-09-02 13:27:12 +02:00
|
|
|
* Return this item's name
|
2015-09-01 12:48:45 +02:00
|
|
|
*
|
2015-09-02 13:27:12 +02:00
|
|
|
* @return string
|
2015-09-01 12:48:45 +02:00
|
|
|
*/
|
2015-09-02 13:27:12 +02:00
|
|
|
public function getName()
|
2015-09-01 12:48:45 +02:00
|
|
|
{
|
2015-09-02 13:27:12 +02:00
|
|
|
return $this->name;
|
2015-09-01 12:48:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-09-02 13:27:12 +02:00
|
|
|
* Set this item's name
|
2015-09-01 12:48:45 +02:00
|
|
|
*
|
2015-09-02 13:27:12 +02:00
|
|
|
* @param string $name
|
2015-09-01 12:48:45 +02:00
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
2015-09-02 13:27:12 +02:00
|
|
|
public function setName($name)
|
2015-09-01 12:48:45 +02:00
|
|
|
{
|
2015-09-02 13:27:12 +02:00
|
|
|
$this->name = $name;
|
2015-09-01 12:48:45 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the label
|
|
|
|
*
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public function getLabel()
|
|
|
|
{
|
|
|
|
return $this->label;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the label
|
|
|
|
*
|
|
|
|
* @param string $label
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setLabel($label)
|
|
|
|
{
|
|
|
|
$this->label = (string) $label;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the URL
|
|
|
|
*
|
|
|
|
* @return Url|null
|
|
|
|
*/
|
|
|
|
public function getUrl()
|
|
|
|
{
|
|
|
|
if ($this->url !== null && ! $this->url instanceof Url) {
|
|
|
|
$this->url = Url::fromPath((string) $this->url);
|
|
|
|
}
|
|
|
|
return $this->url;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the URL
|
|
|
|
*
|
|
|
|
* @param Url|string $url
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setUrl($url)
|
|
|
|
{
|
|
|
|
$this->url = $url;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the URL parameters
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getUrlParameters()
|
|
|
|
{
|
|
|
|
return $this->urlParameters;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the URL parameters
|
|
|
|
*
|
|
|
|
* @param array $urlParameters
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setUrlParameters(array $urlParameters)
|
|
|
|
{
|
|
|
|
$this->urlParameters = $urlParameters;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the view
|
|
|
|
*
|
|
|
|
* @return View
|
|
|
|
*/
|
|
|
|
public function getView()
|
|
|
|
{
|
|
|
|
if ($this->view === null) {
|
|
|
|
$this->view = Icinga::app()->getViewRenderer()->view;
|
|
|
|
}
|
|
|
|
return $this->view;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the view
|
|
|
|
*
|
|
|
|
* @param View $view
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setView(View $view)
|
|
|
|
{
|
|
|
|
$this->view = $view;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set properties for the item
|
|
|
|
*
|
|
|
|
* @param array $properties
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setProperties(array $properties = array())
|
|
|
|
{
|
|
|
|
foreach ($properties as $name => $value) {
|
|
|
|
$setter = 'set' . ucfirst($name);
|
|
|
|
if (method_exists($this, $setter)) {
|
|
|
|
$this->$setter($value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-09-02 15:16:05 +02:00
|
|
|
* Return this item rendered to HTML
|
2015-09-01 12:48:45 +02:00
|
|
|
*
|
2015-09-02 15:16:05 +02:00
|
|
|
* @return string
|
2015-09-01 12:48:45 +02:00
|
|
|
*/
|
|
|
|
public function render()
|
|
|
|
{
|
|
|
|
$view = $this->getView();
|
2015-09-02 15:16:05 +02:00
|
|
|
|
2015-09-01 12:48:45 +02:00
|
|
|
$label = $view->escape($this->getLabel());
|
2015-09-02 15:16:05 +02:00
|
|
|
if (($icon = $this->getIcon()) !== null) {
|
2015-09-01 12:48:45 +02:00
|
|
|
$label = $view->icon($icon) . $label;
|
|
|
|
}
|
2015-09-02 15:16:05 +02:00
|
|
|
|
|
|
|
if (($url = $this->getUrl()) !== null) {
|
2015-09-01 12:48:45 +02:00
|
|
|
$content = sprintf(
|
|
|
|
'<a%s href="%s">%s</a>',
|
|
|
|
$view->propertiesToString($this->getAttributes()),
|
|
|
|
$view->url($url, $this->getUrlParameters()),
|
|
|
|
$label
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$content = sprintf(
|
|
|
|
'<%1$s%2$s>%3$s</%1$s>',
|
|
|
|
static::LINK_ALTERNATIVE,
|
|
|
|
$view->propertiesToString($this->getAttributes()),
|
|
|
|
$label
|
|
|
|
);
|
|
|
|
}
|
2015-09-02 15:16:05 +02:00
|
|
|
|
2015-09-01 12:48:45 +02:00
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-09-02 15:16:05 +02:00
|
|
|
* Return this item rendered to HTML
|
2015-09-01 12:48:45 +02:00
|
|
|
*
|
2015-09-02 15:16:05 +02:00
|
|
|
* @return string
|
2015-09-01 12:48:45 +02:00
|
|
|
*/
|
|
|
|
public function __toString()
|
|
|
|
{
|
2015-09-02 15:16:05 +02:00
|
|
|
try {
|
|
|
|
return $this->render();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
return IcingaException::describe($e);
|
|
|
|
}
|
2015-09-01 12:48:45 +02:00
|
|
|
}
|
|
|
|
}
|