2015-09-01 12:48:45 +02:00
|
|
|
<?php
|
2016-02-08 15:41:00 +01:00
|
|
|
/* Icinga Web 2 | (c) 2015 Icinga Development Team | GPLv2+ */
|
2015-09-01 12:48:45 +02:00
|
|
|
|
2015-09-04 09:08:20 +02:00
|
|
|
namespace Icinga\Web\Navigation\Renderer;
|
2015-09-01 12:48:45 +02:00
|
|
|
|
2015-09-04 09:08:20 +02:00
|
|
|
use Exception;
|
2015-09-01 12:48:45 +02:00
|
|
|
use RecursiveIteratorIterator;
|
2015-09-04 09:08:20 +02:00
|
|
|
use Icinga\Exception\IcingaException;
|
|
|
|
use Icinga\Web\Navigation\Navigation;
|
|
|
|
use Icinga\Web\Navigation\NavigationItem;
|
2015-10-01 17:43:48 +02:00
|
|
|
use Icinga\Web\Navigation\Renderer\NavigationItemRenderer;
|
2015-09-01 12:48:45 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Renderer for multi level navigation
|
|
|
|
*
|
|
|
|
* @method NavigationRenderer getInnerIterator() {
|
|
|
|
* {@inheritdoc}
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
class RecursiveNavigationRenderer extends RecursiveIteratorIterator implements NavigationRendererInterface
|
|
|
|
{
|
|
|
|
/**
|
2015-09-04 09:08:20 +02:00
|
|
|
* The content rendered so far
|
2015-09-01 12:48:45 +02:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2015-09-04 09:08:20 +02:00
|
|
|
protected $content;
|
2015-09-01 12:48:45 +02:00
|
|
|
|
2015-10-01 17:43:48 +02:00
|
|
|
/**
|
|
|
|
* Whether to use the standard item renderer
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $useStandardRenderer;
|
|
|
|
|
2015-09-01 12:48:45 +02:00
|
|
|
/**
|
2015-09-04 09:08:20 +02:00
|
|
|
* Create a new RecursiveNavigationRenderer
|
2015-09-01 12:48:45 +02:00
|
|
|
*
|
|
|
|
* @param Navigation $navigation
|
|
|
|
*/
|
2015-09-04 09:08:20 +02:00
|
|
|
public function __construct(Navigation $navigation)
|
2015-09-01 12:48:45 +02:00
|
|
|
{
|
2015-09-04 09:08:20 +02:00
|
|
|
$this->content = array();
|
|
|
|
parent::__construct(
|
|
|
|
new NavigationRenderer($navigation, true),
|
|
|
|
RecursiveIteratorIterator::SELF_FIRST
|
|
|
|
);
|
2015-09-01 12:48:45 +02:00
|
|
|
}
|
|
|
|
|
2015-10-01 17:43:48 +02:00
|
|
|
/**
|
|
|
|
* Set whether to use the standard navigation item renderer
|
|
|
|
*
|
|
|
|
* @param bool $state
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setUseStandardItemRenderer($state = true)
|
|
|
|
{
|
|
|
|
$this->useStandardRenderer = (bool) $state;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return whether to use the standard navigation item renderer
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function getUseStandardItemRenderer()
|
|
|
|
{
|
|
|
|
return $this->useStandardRenderer;
|
|
|
|
}
|
|
|
|
|
2015-09-01 12:48:45 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2015-09-04 09:08:20 +02:00
|
|
|
public function setElementTag($tag)
|
2015-09-01 12:48:45 +02:00
|
|
|
{
|
2015-09-04 09:08:20 +02:00
|
|
|
$this->getInnerIterator()->setElementTag($tag);
|
|
|
|
return $this;
|
2015-09-01 12:48:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2015-09-04 09:08:20 +02:00
|
|
|
public function getElementTag()
|
2015-09-01 12:48:45 +02:00
|
|
|
{
|
2015-09-04 09:08:20 +02:00
|
|
|
return $this->getInnerIterator()->getElementTag();
|
2015-09-01 12:48:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2015-09-04 09:08:20 +02:00
|
|
|
public function setCssClass($class)
|
2015-09-01 12:48:45 +02:00
|
|
|
{
|
2015-09-04 09:08:20 +02:00
|
|
|
$this->getInnerIterator()->setCssClass($class);
|
|
|
|
return $this;
|
2015-09-01 12:48:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2015-09-04 09:08:20 +02:00
|
|
|
public function getCssClass()
|
2015-09-01 12:48:45 +02:00
|
|
|
{
|
2015-09-04 09:08:20 +02:00
|
|
|
return $this->getInnerIterator()->getCssClass();
|
2015-09-01 12:48:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2015-09-04 09:08:20 +02:00
|
|
|
public function setHeading($heading)
|
2015-09-01 12:48:45 +02:00
|
|
|
{
|
2015-09-04 09:08:20 +02:00
|
|
|
$this->getInnerIterator()->setHeading($heading);
|
|
|
|
return $this;
|
2015-09-01 12:48:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2015-09-04 09:08:20 +02:00
|
|
|
public function getHeading()
|
2015-09-01 12:48:45 +02:00
|
|
|
{
|
2015-09-04 09:08:20 +02:00
|
|
|
return $this->getInnerIterator()->getHeading();
|
2015-09-01 12:48:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2015-09-04 09:08:20 +02:00
|
|
|
public function beginIteration()
|
2015-09-01 12:48:45 +02:00
|
|
|
{
|
2015-09-04 09:08:20 +02:00
|
|
|
$this->content[] = $this->getInnerIterator()->beginMarkup();
|
2015-09-01 12:48:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2015-09-04 09:08:20 +02:00
|
|
|
public function endIteration()
|
2015-09-01 12:48:45 +02:00
|
|
|
{
|
2015-09-04 09:08:20 +02:00
|
|
|
$this->content[] = $this->getInnerIterator()->endMarkup();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function beginChildren()
|
|
|
|
{
|
2015-10-27 13:52:18 +01:00
|
|
|
$this->content[] = $this->getInnerIterator()->beginChildrenMarkup($this->getDepth() + 1);
|
2015-09-04 09:08:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function endChildren()
|
|
|
|
{
|
|
|
|
$this->content[] = $this->getInnerIterator()->endChildrenMarkup();
|
2015-09-27 15:43:49 +02:00
|
|
|
$this->content[] = $this->getInnerIterator()->endItemMarkup();
|
2015-09-01 12:48:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function render()
|
|
|
|
{
|
2015-09-04 09:08:20 +02:00
|
|
|
foreach ($this as $item) {
|
|
|
|
/** @var NavigationItem $item */
|
2015-09-17 09:11:17 +02:00
|
|
|
if ($item->shouldRender()) {
|
2017-11-20 13:43:54 +01:00
|
|
|
if ($this->getDepth() > 0) {
|
|
|
|
$item->setIcon(null);
|
|
|
|
}
|
2015-10-01 17:43:48 +02:00
|
|
|
if ($this->getUseStandardItemRenderer()) {
|
|
|
|
$renderer = new NavigationItemRenderer();
|
2015-10-26 12:32:49 +01:00
|
|
|
$content = $renderer->render($item);
|
2015-10-01 17:43:48 +02:00
|
|
|
} else {
|
2015-10-26 12:32:49 +01:00
|
|
|
$content = $item->render();
|
2015-10-01 17:43:48 +02:00
|
|
|
}
|
2015-10-26 12:32:49 +01:00
|
|
|
$this->content[] = $this->getInnerIterator()->beginItemMarkup($item);
|
|
|
|
|
|
|
|
$this->content[] = $content;
|
2015-10-01 17:43:48 +02:00
|
|
|
|
2015-09-17 09:11:17 +02:00
|
|
|
if (! $item->hasChildren()) {
|
|
|
|
$this->content[] = $this->getInnerIterator()->endItemMarkup();
|
|
|
|
}
|
2015-09-01 12:48:45 +02:00
|
|
|
}
|
|
|
|
}
|
2015-09-04 09:08:20 +02:00
|
|
|
|
|
|
|
return join("\n", $this->content);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function __toString()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
return $this->render();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
return IcingaException::describe($e);
|
|
|
|
}
|
2015-09-01 12:48:45 +02:00
|
|
|
}
|
|
|
|
}
|