2014-09-05 17:57:20 +02:00
|
|
|
<?php
|
2016-02-08 15:41:00 +01:00
|
|
|
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */
|
2014-09-05 17:57:20 +02:00
|
|
|
|
|
|
|
namespace Icinga\Web\Menu;
|
|
|
|
|
2015-04-20 17:02:42 +02:00
|
|
|
use Icinga\Application\Icinga;
|
2014-09-05 17:57:20 +02:00
|
|
|
use Icinga\Web\Menu;
|
2015-04-20 21:49:45 +02:00
|
|
|
use Icinga\Web\Url;
|
2015-04-20 17:02:42 +02:00
|
|
|
use Icinga\Web\View;
|
2015-08-20 17:38:55 +02:00
|
|
|
use Icinga\Data\ConfigObject;
|
2014-09-05 17:57:20 +02:00
|
|
|
|
|
|
|
/**
|
2015-04-20 17:02:42 +02:00
|
|
|
* Default MenuItemRenderer class
|
2014-09-05 17:57:20 +02:00
|
|
|
*/
|
2015-04-20 17:02:42 +02:00
|
|
|
class MenuItemRenderer
|
|
|
|
{
|
|
|
|
/**
|
2015-08-20 17:38:55 +02:00
|
|
|
* The view this menu item is being rendered to
|
2015-04-20 17:02:42 +02:00
|
|
|
*
|
2015-08-20 17:38:55 +02:00
|
|
|
* @var View|null
|
2015-04-20 17:02:42 +02:00
|
|
|
*/
|
2015-08-20 17:38:55 +02:00
|
|
|
protected $view = null;
|
2015-04-20 17:02:42 +02:00
|
|
|
|
|
|
|
/**
|
2015-08-20 17:38:55 +02:00
|
|
|
* The link target
|
2015-04-20 17:02:42 +02:00
|
|
|
*
|
2015-08-20 17:38:55 +02:00
|
|
|
* @var string
|
2015-04-20 17:02:42 +02:00
|
|
|
*/
|
2015-08-20 17:38:55 +02:00
|
|
|
protected $target = null;
|
2015-04-20 17:02:42 +02:00
|
|
|
|
|
|
|
/**
|
2015-08-20 17:38:55 +02:00
|
|
|
* Create a new instance of MenuItemRenderer
|
2015-04-20 17:02:42 +02:00
|
|
|
*
|
2015-08-20 17:38:55 +02:00
|
|
|
* Is is possible to configure the link target using the option 'target'
|
2015-04-20 17:02:42 +02:00
|
|
|
*
|
2015-08-20 17:38:55 +02:00
|
|
|
* @param ConfigObject|null $configuration
|
2015-04-20 17:02:42 +02:00
|
|
|
*/
|
2015-08-20 17:38:55 +02:00
|
|
|
public function __construct(ConfigObject $configuration = null)
|
2015-04-20 17:02:42 +02:00
|
|
|
{
|
2015-08-20 17:38:55 +02:00
|
|
|
if ($configuration !== null) {
|
|
|
|
$this->target = $configuration->get('target', null);
|
|
|
|
}
|
2015-04-20 17:02:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-20 17:38:55 +02:00
|
|
|
* Get the view this menu item is being rendered to
|
2015-04-20 17:02:42 +02:00
|
|
|
*
|
|
|
|
* @return View
|
|
|
|
*/
|
2015-08-20 17:38:55 +02:00
|
|
|
protected function getView()
|
2015-04-20 17:02:42 +02:00
|
|
|
{
|
|
|
|
if ($this->view === null) {
|
|
|
|
$this->view = Icinga::app()->getViewRenderer()->view;
|
|
|
|
}
|
|
|
|
return $this->view;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a menu item link element
|
|
|
|
*
|
|
|
|
* @param Menu $menu
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function createLink(Menu $menu)
|
|
|
|
{
|
2015-08-20 17:38:55 +02:00
|
|
|
$attributes = isset($this->target) ? sprintf(' target="%s"', $this->getView()->escape($this->target)) : '';
|
|
|
|
|
2015-04-20 17:02:42 +02:00
|
|
|
if ($menu->getIcon() && strpos($menu->getIcon(), '.') === false) {
|
|
|
|
return sprintf(
|
|
|
|
'<a href="%s"%s><i aria-hidden="true" class="icon-%s"></i>%s</a>',
|
|
|
|
$menu->getUrl() ? : '#',
|
2015-08-20 17:38:55 +02:00
|
|
|
$attributes,
|
2015-04-20 17:02:42 +02:00
|
|
|
$menu->getIcon(),
|
|
|
|
$this->getView()->escape($menu->getTitle())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return sprintf(
|
|
|
|
'<a href="%s"%s>%s%s<span></span></a>',
|
|
|
|
$menu->getUrl() ? : '#',
|
2015-08-20 17:38:55 +02:00
|
|
|
$attributes,
|
2017-01-27 14:48:59 +01:00
|
|
|
$menu->getIcon()
|
|
|
|
? '<img aria-hidden="true" src="' . Url::fromPath($menu->getIcon()) . '" class="icon" /> '
|
|
|
|
: '',
|
2015-04-20 17:02:42 +02:00
|
|
|
$this->getView()->escape($menu->getTitle())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-20 17:38:55 +02:00
|
|
|
* Renders the html content of a single menu item
|
|
|
|
*
|
|
|
|
* @param Menu $menu
|
2015-04-20 17:02:42 +02:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-08-20 17:38:55 +02:00
|
|
|
public function render(Menu $menu)
|
2015-04-20 17:02:42 +02:00
|
|
|
{
|
2015-08-20 17:38:55 +02:00
|
|
|
return $this->createLink($menu);
|
2015-04-20 17:02:42 +02:00
|
|
|
}
|
2014-09-05 17:57:20 +02:00
|
|
|
}
|