2014-07-03 15:46:46 +02:00
|
|
|
<?php
|
2016-02-08 15:41:00 +01:00
|
|
|
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */
|
2014-07-03 15:46:46 +02:00
|
|
|
|
|
|
|
namespace Icinga\Web;
|
|
|
|
|
2014-09-10 17:12:19 +02:00
|
|
|
use Exception;
|
2015-04-20 17:04:46 +02:00
|
|
|
use Icinga\Web\Menu\MenuItemRenderer;
|
2014-07-03 15:46:46 +02:00
|
|
|
use RecursiveIteratorIterator;
|
2014-10-31 10:27:17 +01:00
|
|
|
use Icinga\Application\Logger;
|
2015-03-12 15:30:10 +01:00
|
|
|
use Icinga\Web\Menu\PermittedMenuItemFilter;
|
2014-07-03 15:46:46 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A renderer to draw a menu with its sub-menus using an unordered html list
|
|
|
|
*/
|
|
|
|
class MenuRenderer extends RecursiveIteratorIterator
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The relative url of the current request
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $url;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The html tags to assemble the menu with
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $tags = array();
|
|
|
|
|
2014-09-09 15:33:42 +02:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $useCustomRenderer = false;
|
|
|
|
|
2015-04-20 17:04:46 +02:00
|
|
|
/**
|
|
|
|
* @var MenuItemRenderer
|
|
|
|
*/
|
|
|
|
protected $defaultRenderer;
|
|
|
|
|
2014-07-03 15:46:46 +02:00
|
|
|
/**
|
|
|
|
* Create a new MenuRenderer
|
|
|
|
*
|
|
|
|
* @param Menu $menu The menu to render
|
|
|
|
* @param string $url A relative url to identify "active" children with
|
|
|
|
*/
|
|
|
|
public function __construct(Menu $menu, $url = null)
|
|
|
|
{
|
2014-09-04 19:18:09 +02:00
|
|
|
if ($url instanceof Url) {
|
|
|
|
$this->url = $url;
|
|
|
|
} else {
|
|
|
|
$this->url = Url::fromPath($url);
|
|
|
|
}
|
2015-04-20 17:04:46 +02:00
|
|
|
$this->defaultRenderer = new MenuItemRenderer();
|
2015-03-12 15:30:10 +01:00
|
|
|
parent::__construct(new PermittedMenuItemFilter($menu), RecursiveIteratorIterator::CHILD_FIRST);
|
2014-07-03 15:46:46 +02:00
|
|
|
}
|
|
|
|
|
2014-09-09 15:33:42 +02:00
|
|
|
/**
|
|
|
|
* @param bool $value
|
|
|
|
*/
|
|
|
|
public function useCustomRenderer($value = true)
|
|
|
|
{
|
|
|
|
$this->useCustomRenderer = $value;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2014-07-03 15:46:46 +02:00
|
|
|
/**
|
|
|
|
* Register the outer ul opening html-tag
|
|
|
|
*/
|
|
|
|
public function beginIteration()
|
|
|
|
{
|
2015-02-12 13:13:00 +01:00
|
|
|
$this->tags[] = '<ul>';
|
2014-07-03 15:46:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register the outer ul closing html-tag
|
|
|
|
*/
|
|
|
|
public function endIteration()
|
|
|
|
{
|
|
|
|
$this->tags[] = '</ul>';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a inner ul opening html-tag
|
|
|
|
*/
|
|
|
|
public function beginChildren()
|
|
|
|
{
|
|
|
|
// The iterator runs in mode CHILD_FIRST so we need to remember
|
|
|
|
// where to insert the parent's opening html tag once its rendered
|
|
|
|
$parent = $this->getSubIterator(0)->current();
|
|
|
|
$this->tags[$parent->getId() . '_begin'] = null;
|
|
|
|
|
|
|
|
$this->tags[] = '<ul>';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a inner ul closing html-tag
|
|
|
|
*/
|
|
|
|
public function endChildren()
|
|
|
|
{
|
|
|
|
$this->tags[] = '</ul>';
|
|
|
|
|
|
|
|
// Remember the position of the parent's closing html-tag
|
|
|
|
$parent = $this->getSubIterator(0)->current();
|
|
|
|
$this->tags[$parent->getId() . '_end'] = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render the given child
|
|
|
|
*
|
|
|
|
* @param Menu $child The menu's child to render
|
|
|
|
*
|
|
|
|
* @return string The child rendered as html
|
|
|
|
*/
|
|
|
|
public function renderChild(Menu $child)
|
|
|
|
{
|
2014-09-09 15:33:42 +02:00
|
|
|
if ($child->getRenderer() !== null && $this->useCustomRenderer) {
|
2014-09-10 14:38:11 +02:00
|
|
|
try {
|
|
|
|
return $child->getRenderer()->render($child);
|
|
|
|
} catch (Exception $e) {
|
2015-07-21 16:54:23 +02:00
|
|
|
Logger::error(
|
|
|
|
'Could not invoke custom menu renderer. %s in %s:%d with message: %s',
|
|
|
|
get_class($e),
|
|
|
|
$e->getFile(),
|
|
|
|
$e->getLine(),
|
|
|
|
$e->getMessage()
|
|
|
|
);
|
2014-09-10 14:38:11 +02:00
|
|
|
}
|
2014-09-05 17:57:20 +02:00
|
|
|
}
|
2015-04-20 17:04:46 +02:00
|
|
|
|
|
|
|
return $this->defaultRenderer->render($child);
|
2014-07-03 15:46:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the menu rendered as html
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function render()
|
|
|
|
{
|
|
|
|
$passedActiveChild = false;
|
|
|
|
foreach ($this as $child) {
|
|
|
|
$childIsActive = $this->isActive($child);
|
|
|
|
if ($childIsActive && $this->getDepth() > 0) {
|
|
|
|
$passedActiveChild = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($childIsActive || ($passedActiveChild && $this->getDepth() === 0)) {
|
|
|
|
$passedActiveChild &= $this->getDepth() !== 0;
|
2014-09-05 17:24:55 +02:00
|
|
|
$openTag = '<li class="active" id="' . $child->getUniqueId() . '">';
|
2014-07-03 15:46:46 +02:00
|
|
|
} else {
|
2014-09-05 17:24:55 +02:00
|
|
|
$openTag = '<li id="' . $child->getUniqueId() . '">';
|
2014-07-03 15:46:46 +02:00
|
|
|
}
|
|
|
|
$content = $this->renderChild($child);
|
|
|
|
$closingTag = '</li>';
|
|
|
|
|
|
|
|
if (array_key_exists($child->getId() . '_begin', $this->tags)) {
|
|
|
|
$this->tags[$child->getId() . '_begin'] = $openTag . $content;
|
|
|
|
$this->tags[$child->getId() . '_end'] = $closingTag;
|
|
|
|
} else {
|
|
|
|
$this->tags[] = $openTag . $content . $closingTag;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return implode("\n", $this->tags);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see MenuRenderer::render()
|
|
|
|
*/
|
|
|
|
public function __toString()
|
|
|
|
{
|
|
|
|
return $this->render();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return whether the current request url references the child's url
|
|
|
|
*
|
|
|
|
* @param Menu $child The menu's child to check
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function isActive(Menu $child)
|
|
|
|
{
|
2014-09-04 19:18:09 +02:00
|
|
|
if (! $this->url) return false;
|
|
|
|
if (! ($childUrl = $child->getUrl())) return false;
|
|
|
|
|
|
|
|
return $this->url && $this->url->matches($childUrl);
|
2014-07-03 15:46:46 +02:00
|
|
|
}
|
|
|
|
}
|