Merge pull request #3445 from Icinga/fix/remove-superseded-menu-classes-2338
Remove superseded menu classes
This commit is contained in:
commit
b534cf38d4
|
@ -3,8 +3,8 @@
|
|||
|
||||
namespace Icinga\Controllers;
|
||||
|
||||
use Icinga\Application\Icinga;
|
||||
use Icinga\Web\Controller\ActionController;
|
||||
use Icinga\Web\Menu;
|
||||
|
||||
/**
|
||||
* Create complex layout parts
|
||||
|
@ -18,7 +18,7 @@ class LayoutController extends ActionController
|
|||
{
|
||||
$this->setAutorefreshInterval(15);
|
||||
$this->_helper->layout()->disableLayout();
|
||||
$this->view->menuRenderer = Icinga::app()->getMenu()->getRenderer();
|
||||
$this->view->menuRenderer = (new Menu())->getRenderer();
|
||||
}
|
||||
|
||||
public function announcementsAction()
|
||||
|
|
|
@ -5,7 +5,6 @@ namespace Icinga\Controllers;
|
|||
|
||||
use Exception;
|
||||
use Icinga\Application\Config;
|
||||
use Icinga\Application\Icinga;
|
||||
use Icinga\Exception\NotFoundError;
|
||||
use Icinga\Data\DataArray\ArrayDatasource;
|
||||
use Icinga\Data\Filter\FilterMatchCaseInsensitive;
|
||||
|
@ -13,6 +12,7 @@ use Icinga\Forms\ConfirmRemovalForm;
|
|||
use Icinga\Forms\Navigation\NavigationConfigForm;
|
||||
use Icinga\Web\Controller;
|
||||
use Icinga\Web\Form;
|
||||
use Icinga\Web\Menu;
|
||||
use Icinga\Web\Navigation\Navigation;
|
||||
use Icinga\Web\Notification;
|
||||
use Icinga\Web\Url;
|
||||
|
@ -419,7 +419,7 @@ class NavigationController extends Controller
|
|||
'url' => Url::fromRequest()
|
||||
));
|
||||
|
||||
$menu = Icinga::app()->getMenu();
|
||||
$menu = new Menu();
|
||||
|
||||
$navigation = $menu->findItem($name);
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
use Icinga\Application\Icinga;
|
||||
use Icinga\Web\Menu;
|
||||
|
||||
// Don't render a menu for unauthenticated users unless menu is auth aware
|
||||
if (! $this->auth()->isAuthenticated()) {
|
||||
|
@ -26,7 +26,7 @@ if (! $this->auth()->isAuthenticated()) {
|
|||
'layout/menu.phtml',
|
||||
'default',
|
||||
array(
|
||||
'menuRenderer' => Icinga::app()->getMenu()->getRenderer()->setUseStandardItemRenderer()
|
||||
'menuRenderer' => (new Menu())->getRenderer()->setUseStandardItemRenderer()
|
||||
)
|
||||
) ?>
|
||||
</div>
|
||||
|
|
|
@ -18,6 +18,7 @@ use Icinga\Util\DirectoryIterator;
|
|||
use Icinga\Util\TimezoneDetect;
|
||||
use Icinga\Util\Translator;
|
||||
use Icinga\Web\Controller\Dispatcher;
|
||||
use Icinga\Web\Menu;
|
||||
use Icinga\Web\Navigation\Navigation;
|
||||
use Icinga\Web\Notification;
|
||||
use Icinga\Web\Session;
|
||||
|
@ -282,123 +283,13 @@ class Web extends EmbeddedWeb
|
|||
/**
|
||||
* Return the app's menu
|
||||
*
|
||||
* @return Navigation
|
||||
* @deprecated Instantiate the returned class directly instead of using this method.
|
||||
*
|
||||
* @return Menu
|
||||
*/
|
||||
public function getMenu()
|
||||
{
|
||||
if ($this->user !== null) {
|
||||
$menu = array(
|
||||
'dashboard' => array(
|
||||
'label' => t('Dashboard'),
|
||||
'url' => 'dashboard',
|
||||
'icon' => 'dashboard',
|
||||
'priority' => 10
|
||||
),
|
||||
'system' => array(
|
||||
'label' => t('System'),
|
||||
'icon' => 'services',
|
||||
'priority' => 700,
|
||||
'renderer' => array(
|
||||
'SummaryNavigationItemRenderer',
|
||||
'state' => 'critical'
|
||||
),
|
||||
'children' => array(
|
||||
'about' => array(
|
||||
'icon' => 'info',
|
||||
'description' => t('Open about page'),
|
||||
'label' => t('About'),
|
||||
'url' => 'about',
|
||||
'priority' => 700
|
||||
),
|
||||
'announcements' => array(
|
||||
'icon' => 'megaphone',
|
||||
'description' => t('List announcements'),
|
||||
'label' => t('Announcements'),
|
||||
'url' => 'announcements',
|
||||
'priority' => 710
|
||||
)
|
||||
)
|
||||
),
|
||||
'configuration' => array(
|
||||
'label' => t('Configuration'),
|
||||
'icon' => 'wrench',
|
||||
'permission' => 'config/*',
|
||||
'priority' => 800,
|
||||
'children' => array(
|
||||
'application' => array(
|
||||
'icon' => 'wrench',
|
||||
'description' => t('Open application configuration'),
|
||||
'label' => t('Application'),
|
||||
'url' => 'config/general',
|
||||
'permission' => 'config/application/*',
|
||||
'priority' => 810
|
||||
),
|
||||
'authentication' => array(
|
||||
'icon' => 'users',
|
||||
'description' => t('Open authentication configuration'),
|
||||
'label' => t('Authentication'),
|
||||
'permission' => 'config/authentication/*',
|
||||
'priority' => 830,
|
||||
'url' => 'role/list'
|
||||
),
|
||||
'navigation' => array(
|
||||
'icon' => 'sitemap',
|
||||
'description' => t('Open shared navigation configuration'),
|
||||
'label' => t('Shared Navigation'),
|
||||
'url' => 'navigation/shared',
|
||||
'permission' => 'config/application/navigation',
|
||||
'priority' => 840,
|
||||
),
|
||||
'modules' => array(
|
||||
'icon' => 'cubes',
|
||||
'description' => t('Open module configuration'),
|
||||
'label' => t('Modules'),
|
||||
'url' => 'config/modules',
|
||||
'permission' => 'config/modules',
|
||||
'priority' => 890
|
||||
)
|
||||
)
|
||||
),
|
||||
'user' => array(
|
||||
'cssClass' => 'user-nav-item',
|
||||
'label' => $this->user->getUsername(),
|
||||
'icon' => 'user',
|
||||
'priority' => 900,
|
||||
'children' => array(
|
||||
'account' => array(
|
||||
'icon' => 'sliders',
|
||||
'description' => t('Open your account preferences'),
|
||||
'label' => t('My Account'),
|
||||
'priority' => 100,
|
||||
'url' => 'account'
|
||||
),
|
||||
'logout' => array(
|
||||
'icon' => 'off',
|
||||
'description' => t('Log out'),
|
||||
'label' => t('Logout'),
|
||||
'priority' => 200,
|
||||
'attributes' => array('target' => '_self'),
|
||||
'url' => 'authentication/logout'
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
if (Logger::writesToFile()) {
|
||||
$menu['system']['children']['application_log'] = array(
|
||||
'icon' => 'doc-text',
|
||||
'description' => t(''),
|
||||
'label' => t('Application Log'),
|
||||
'url' => 'list/applicationlog',
|
||||
'permission' => 'application/log',
|
||||
'priority' => 900
|
||||
);
|
||||
}
|
||||
} else {
|
||||
$menu = array();
|
||||
}
|
||||
|
||||
return Navigation::fromArray($menu)->load('menu-item');
|
||||
return new Menu();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,806 +1,135 @@
|
|||
<?php
|
||||
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */
|
||||
/* Icinga Web 2 | (c) 2018 Icinga Development Team | GPLv2+ */
|
||||
|
||||
namespace Icinga\Web;
|
||||
|
||||
use RecursiveIterator;
|
||||
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\Web\Menu\MenuItemRenderer;
|
||||
use Icinga\Web\Navigation\Navigation;
|
||||
|
||||
class Menu implements RecursiveIterator
|
||||
/**
|
||||
* Main menu for Icinga Web 2
|
||||
*/
|
||||
class Menu extends Navigation
|
||||
{
|
||||
/**
|
||||
* The id of this menu
|
||||
*
|
||||
* @var string
|
||||
* Create the main menu
|
||||
*/
|
||||
protected $id;
|
||||
|
||||
/**
|
||||
* The title of this menu
|
||||
*
|
||||
* Used for sorting when priority is unset or equal to other items
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $title;
|
||||
|
||||
/**
|
||||
* The priority of this menu
|
||||
*
|
||||
* Used for sorting
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $priority = 100;
|
||||
|
||||
/**
|
||||
* The url of this menu
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
protected $url;
|
||||
|
||||
/**
|
||||
* The path to the icon of this menu
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $icon;
|
||||
|
||||
/**
|
||||
* The sub menus of this menu
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $subMenus = array();
|
||||
|
||||
/**
|
||||
* A custom item renderer used instead of the default rendering logic
|
||||
*
|
||||
* @var MenuItemRenderer
|
||||
*/
|
||||
protected $itemRenderer = null;
|
||||
|
||||
/*
|
||||
* Parent menu
|
||||
*
|
||||
* @var Menu
|
||||
*/
|
||||
protected $parent;
|
||||
|
||||
/**
|
||||
* Permission a user is required to have granted to display the menu item
|
||||
*
|
||||
* If a permission is set, authentication is of course required.
|
||||
*
|
||||
* Note that only one required permission can be set yet.
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
protected $permission;
|
||||
|
||||
/**
|
||||
* Create a new menu
|
||||
*
|
||||
* @param int $id The id of this menu
|
||||
* @param ConfigObject $config The configuration for this menu
|
||||
* @param Menu $parent Parent menu
|
||||
*/
|
||||
public function __construct($id, ConfigObject $config = null, Menu $parent = null)
|
||||
public function __construct()
|
||||
{
|
||||
$this->id = $id;
|
||||
if ($parent !== null) {
|
||||
$this->parent = $parent;
|
||||
$this->init();
|
||||
$this->load('menu-item');
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup the main menu
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$this->addItem('dashboard', [
|
||||
'label' => t('Dashboard'),
|
||||
'url' => 'dashboard',
|
||||
'icon' => 'dashboard',
|
||||
'priority' => 10
|
||||
]);
|
||||
$this->addItem('system', [
|
||||
'label' => t('System'),
|
||||
'icon' => 'services',
|
||||
'priority' => 700,
|
||||
'renderer' => [
|
||||
'SummaryNavigationItemRenderer',
|
||||
'state' => 'critical'
|
||||
],
|
||||
'children' => [
|
||||
'about' => [
|
||||
'icon' => 'info',
|
||||
'description' => t('Open about page'),
|
||||
'label' => t('About'),
|
||||
'url' => 'about',
|
||||
'priority' => 700
|
||||
],
|
||||
'announcements' => [
|
||||
'icon' => 'megaphone',
|
||||
'description' => t('List announcements'),
|
||||
'label' => t('Announcements'),
|
||||
'url' => 'announcements',
|
||||
'priority' => 710
|
||||
]
|
||||
]
|
||||
]);
|
||||
$this->addItem('configuration', [
|
||||
'label' => t('Configuration'),
|
||||
'icon' => 'wrench',
|
||||
'permission' => 'config/*',
|
||||
'priority' => 800,
|
||||
'children' => [
|
||||
'application' => [
|
||||
'icon' => 'wrench',
|
||||
'description' => t('Open application configuration'),
|
||||
'label' => t('Application'),
|
||||
'url' => 'config/general',
|
||||
'permission' => 'config/application/*',
|
||||
'priority' => 810
|
||||
],
|
||||
'authentication' => [
|
||||
'icon' => 'users',
|
||||
'description' => t('Open authentication configuration'),
|
||||
'label' => t('Authentication'),
|
||||
'permission' => 'config/authentication/*',
|
||||
'priority' => 830,
|
||||
'url' => 'role/list'
|
||||
],
|
||||
'navigation' => [
|
||||
'icon' => 'sitemap',
|
||||
'description' => t('Open shared navigation configuration'),
|
||||
'label' => t('Shared Navigation'),
|
||||
'url' => 'navigation/shared',
|
||||
'permission' => 'config/application/navigation',
|
||||
'priority' => 840,
|
||||
],
|
||||
'modules' => [
|
||||
'icon' => 'cubes',
|
||||
'description' => t('Open module configuration'),
|
||||
'label' => t('Modules'),
|
||||
'url' => 'config/modules',
|
||||
'permission' => 'config/modules',
|
||||
'priority' => 890
|
||||
]
|
||||
]
|
||||
]);
|
||||
$this->addItem('user', [
|
||||
'cssClass' => 'user-nav-item',
|
||||
'label' => Auth::getInstance()->getUser()->getUsername(),
|
||||
'icon' => 'user',
|
||||
'priority' => 900,
|
||||
'children' => [
|
||||
'account' => [
|
||||
'icon' => 'sliders',
|
||||
'description' => t('Open your account preferences'),
|
||||
'label' => t('My Account'),
|
||||
'priority' => 100,
|
||||
'url' => 'account'
|
||||
],
|
||||
'logout' => [
|
||||
'icon' => 'off',
|
||||
'description' => t('Log out'),
|
||||
'label' => t('Logout'),
|
||||
'priority' => 200,
|
||||
'attributes' => ['target' => '_self'],
|
||||
'url' => 'authentication/logout'
|
||||
]
|
||||
]
|
||||
]);
|
||||
|
||||
if (Logger::writesToFile()) {
|
||||
$this->getItem('system')->addChild($this->createItem('application_log', [
|
||||
'icon' => 'doc-text',
|
||||
'description' => t('Open Application Log'),
|
||||
'label' => t('Application Log'),
|
||||
'url' => 'list/applicationlog',
|
||||
'permission' => 'application/log',
|
||||
'priority' => 900
|
||||
]));
|
||||
}
|
||||
$this->setProperties($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set all given properties
|
||||
*
|
||||
* @param array|ConfigObject $props Property list
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @throws ConfigurationError If a property is invalid
|
||||
*/
|
||||
public function setProperties($props = null)
|
||||
{
|
||||
if ($props !== null) {
|
||||
foreach ($props as $key => $value) {
|
||||
$method = 'set' . implode('', array_map('ucfirst', explode('_', strtolower($key))));
|
||||
if ($key === 'renderer') {
|
||||
// nested configuration is used to pass multiple arguments to the item renderer
|
||||
if ($value instanceof ConfigObject) {
|
||||
$args = $value;
|
||||
$value = $value->get('0');
|
||||
}
|
||||
|
||||
$value = '\\' . ltrim($value, '\\');
|
||||
if (class_exists($value)) {
|
||||
if (isset($args)) {
|
||||
$value = new $value($args);
|
||||
} else {
|
||||
$value = new $value;
|
||||
}
|
||||
} else {
|
||||
$class = '\Icinga\Web\Menu' . $value;
|
||||
if (!class_exists($class)) {
|
||||
throw new ConfigurationError(
|
||||
sprintf('ItemRenderer with class "%s" does not exist', $class)
|
||||
);
|
||||
}
|
||||
if (isset($args)) {
|
||||
$value = new $class($args);
|
||||
} else {
|
||||
$value = new $class;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (method_exists($this, $method)) {
|
||||
$this->{$method}($value);
|
||||
} else {
|
||||
throw new ConfigurationError(
|
||||
sprintf('Menu got invalid property "%s"', $key)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Properties
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getProperties()
|
||||
{
|
||||
$props = array();
|
||||
$keys = array('url', 'icon', 'priority', 'title');
|
||||
foreach ($keys as $key) {
|
||||
$func = 'get' . ucfirst($key);
|
||||
if (null !== ($val = $this->{$func}())) {
|
||||
$props[$key] = $val;
|
||||
}
|
||||
}
|
||||
return $props;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this Menu conflicts with the given Menu object
|
||||
*
|
||||
* @param Menu $menu
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function conflictsWith(Menu $menu)
|
||||
{
|
||||
if ($menu->getUrl() === null || $this->getUrl() === null) {
|
||||
return false;
|
||||
}
|
||||
return $menu->getUrl() !== $this->getUrl();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create menu from the application's menu config file plus the config files from all enabled modules
|
||||
*
|
||||
* @return static
|
||||
*
|
||||
* @deprecated THIS IS OBSOLETE. LEFT HERE FOR FUTURE USE WITH USER-SPECIFIC MODULES
|
||||
*/
|
||||
public static function fromConfig()
|
||||
{
|
||||
$menu = new static('menu');
|
||||
$manager = Icinga::app()->getModuleManager();
|
||||
$modules = $manager->listEnabledModules();
|
||||
$menuConfigs = array(Config::app('menu'));
|
||||
|
||||
foreach ($modules as $moduleName) {
|
||||
$moduleMenuConfig = Config::module($moduleName, 'menu');
|
||||
if (! $moduleMenuConfig->isEmpty()) {
|
||||
$menuConfigs[] = $moduleMenuConfig;
|
||||
}
|
||||
}
|
||||
|
||||
return $menu->loadSubMenus($menu->flattenConfigs($menuConfigs));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create menu from the application's menu config plus menu entries provided by all enabled modules
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public static function load()
|
||||
{
|
||||
$menu = new static('menu');
|
||||
$menu->addMainMenuItems();
|
||||
$auth = Auth::getInstance();
|
||||
$manager = Icinga::app()->getModuleManager();
|
||||
foreach ($manager->getLoadedModules() as $module) {
|
||||
if ($auth->hasPermission($manager::MODULE_PERMISSION_NS . $module->getName())) {
|
||||
$menu->mergeSubMenus($module->getMenuItems());
|
||||
}
|
||||
}
|
||||
return $menu->order();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add Applications Main Menu Items
|
||||
*/
|
||||
protected function addMainMenuItems()
|
||||
{
|
||||
$auth = Auth::getInstance();
|
||||
|
||||
if ($auth->isAuthenticated()) {
|
||||
$this->add(t('Dashboard'), array(
|
||||
'url' => 'dashboard',
|
||||
'icon' => 'dashboard',
|
||||
'priority' => 10
|
||||
));
|
||||
|
||||
$section = $this->add(t('System'), array(
|
||||
'icon' => 'services',
|
||||
'priority' => 700,
|
||||
'renderer' => array(
|
||||
'SummaryMenuItemRenderer',
|
||||
'state' => 'critical'
|
||||
)
|
||||
));
|
||||
$section->add(t('About'), array(
|
||||
'url' => 'about',
|
||||
'priority' => 701
|
||||
));
|
||||
if (Logger::writesToFile()) {
|
||||
$section->add(t('Application Log'), array(
|
||||
'url' => 'list/applicationlog',
|
||||
'permission' => 'application/log',
|
||||
'priority' => 710
|
||||
));
|
||||
}
|
||||
|
||||
$section = $this->add(t('Configuration'), array(
|
||||
'icon' => 'wrench',
|
||||
'permission' => 'config/*',
|
||||
'priority' => 800
|
||||
));
|
||||
$section->add(t('Application'), array(
|
||||
'url' => 'config/general',
|
||||
'permission' => 'config/application/*',
|
||||
'priority' => 810
|
||||
));
|
||||
$section->add(t('Authentication'), array(
|
||||
'url' => 'config/userbackend',
|
||||
'permission' => 'config/authentication/*',
|
||||
'priority' => 820
|
||||
));
|
||||
$section->add(t('Roles'), array(
|
||||
'url' => 'role/list',
|
||||
'permission' => 'config/authentication/roles/show',
|
||||
'priority' => 830
|
||||
));
|
||||
$section->add(t('Users'), array(
|
||||
'url' => 'user/list',
|
||||
'permission' => 'config/authentication/users/show',
|
||||
'priority' => 840
|
||||
));
|
||||
$section->add(t('Usergroups'), array(
|
||||
'url' => 'group/list',
|
||||
'permission' => 'config/authentication/groups/show',
|
||||
'priority' => 850
|
||||
));
|
||||
$section->add(t('Modules'), array(
|
||||
'url' => 'config/modules',
|
||||
'permission' => 'config/modules',
|
||||
'priority' => 890
|
||||
));
|
||||
|
||||
$section = $this->add($auth->getUser()->getUsername(), array(
|
||||
'icon' => 'user',
|
||||
'priority' => 900
|
||||
));
|
||||
$section->add(t('Preferences'), array(
|
||||
'url' => 'preference',
|
||||
'priority' => 910
|
||||
));
|
||||
|
||||
$section->add(t('Logout'), array(
|
||||
'url' => 'authentication/logout',
|
||||
'priority' => 990,
|
||||
'renderer' => array(
|
||||
'MenuItemRenderer',
|
||||
'target' => '_self'
|
||||
)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the id of this menu
|
||||
*
|
||||
* @param string $id The id to set for this menu
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setId($id)
|
||||
{
|
||||
$this->id = $id;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the id of this menu
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get our ID without invalid characters
|
||||
*
|
||||
* @return string the ID
|
||||
*/
|
||||
protected function getSafeHtmlId()
|
||||
{
|
||||
return preg_replace('/[^a-zA-Z0-9]/', '_', $this->getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a unique menu item id
|
||||
*
|
||||
* @return string the ID
|
||||
*/
|
||||
public function getUniqueId()
|
||||
{
|
||||
if ($this->parent === null) {
|
||||
return 'menuitem-' . $this->getSafeHtmlId();
|
||||
} else {
|
||||
return $this->parent->getUniqueId() . '-' . $this->getSafeHtmlId();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the title of this menu
|
||||
*
|
||||
* @param string $title The title to set for this menu
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setTitle($title)
|
||||
{
|
||||
$this->title = $title;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the title of this menu if set, otherwise its id
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getTitle()
|
||||
{
|
||||
return $this->title ? $this->title : $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the priority of this menu
|
||||
*
|
||||
* @param int $priority The priority to set for this menu
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setPriority($priority)
|
||||
{
|
||||
$this->priority = (int) $priority;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the priority of this menu
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getPriority()
|
||||
{
|
||||
return $this->priority;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the url of this menu
|
||||
*
|
||||
* @param Url|string $url The url to set for this menu
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setUrl($url)
|
||||
{
|
||||
$this->url = $url;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the url of this menu
|
||||
*
|
||||
* @return Url|null
|
||||
*/
|
||||
public function getUrl()
|
||||
{
|
||||
if ($this->url !== null && ! $this->url instanceof Url) {
|
||||
$this->url = Url::fromPath($this->url);
|
||||
}
|
||||
return $this->url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the path to the icon of this menu
|
||||
*
|
||||
* @param string $path The path to the icon for this menu
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setIcon($path)
|
||||
{
|
||||
$this->icon = $path;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the path to the icon of this menu
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getIcon()
|
||||
{
|
||||
return $this->icon;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the class that renders the current menu item
|
||||
*
|
||||
* @return MenuItemRenderer
|
||||
*/
|
||||
public function getRenderer()
|
||||
{
|
||||
return $this->itemRenderer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the class that renders the current menu item
|
||||
*
|
||||
* @param MenuItemRenderer $renderer
|
||||
*/
|
||||
public function setRenderer(MenuItemRenderer $renderer)
|
||||
{
|
||||
$this->itemRenderer = $renderer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return whether this menu has any sub menus
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasSubMenus()
|
||||
{
|
||||
return false === empty($this->subMenus);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a sub menu to this menu
|
||||
*
|
||||
* @param string $id The id of the menu to add
|
||||
* @param ConfigObject $menuConfig The config with which to initialize the menu
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function addSubMenu($id, ConfigObject $menuConfig = null)
|
||||
{
|
||||
$subMenu = new static($id, $menuConfig, $this);
|
||||
$this->subMenus[$id] = $subMenu;
|
||||
return $subMenu;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the permission a user is required to have granted to display the menu item
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getPermission()
|
||||
{
|
||||
return $this->permission;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get parent menu
|
||||
*
|
||||
* @return \Icinga\Web\Menu
|
||||
*/
|
||||
public function getParent()
|
||||
{
|
||||
return $this->parent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get submenus
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getSubMenus()
|
||||
{
|
||||
return $this->subMenus;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set permission a user is required to have granted to display the menu item
|
||||
*
|
||||
* If a permission is set, authentication is of course required.
|
||||
*
|
||||
* @param string $permission
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setPermission($permission)
|
||||
{
|
||||
$this->permission = (string) $permission;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge Sub Menus
|
||||
*
|
||||
* @param array $submenus
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function mergeSubMenus(array $submenus)
|
||||
{
|
||||
foreach ($submenus as $menu) {
|
||||
$this->mergeSubMenu($menu);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge Sub Menu
|
||||
*
|
||||
* @param Menu $menu
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function mergeSubMenu(Menu $menu)
|
||||
{
|
||||
$name = $menu->getId();
|
||||
if (array_key_exists($name, $this->subMenus)) {
|
||||
/** @var $current Menu */
|
||||
$current = $this->subMenus[$name];
|
||||
if ($current->conflictsWith($menu)) {
|
||||
while (array_key_exists($name, $this->subMenus)) {
|
||||
if (preg_match('/_(\d+)$/', $name, $m)) {
|
||||
$name = preg_replace('/_\d+$/', $m[1]++, $name);
|
||||
} else {
|
||||
$name .= '_2';
|
||||
}
|
||||
}
|
||||
$menu->setId($name);
|
||||
$this->subMenus[$name] = $menu;
|
||||
} else {
|
||||
$current->setProperties($menu->getProperties());
|
||||
foreach ($menu->subMenus as $child) {
|
||||
$current->mergeSubMenu($child);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$this->subMenus[$name] = $menu;
|
||||
}
|
||||
|
||||
return $this->subMenus[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a Menu
|
||||
*
|
||||
* @param $name
|
||||
* @param array $config
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function add($name, $config = array())
|
||||
{
|
||||
return $this->addSubMenu($name, new ConfigObject($config));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return whether a sub menu with the given id exists
|
||||
*
|
||||
* @param string $id The id of the sub menu
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasSubMenu($id)
|
||||
{
|
||||
return array_key_exists($id, $this->subMenus);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get sub menu by its id
|
||||
*
|
||||
* @param string $id The id of the sub menu
|
||||
*
|
||||
* @return static The found sub menu
|
||||
*
|
||||
* @throws ProgrammingError In case there is no sub menu with the given id to be found
|
||||
*/
|
||||
public function getSubMenu($id)
|
||||
{
|
||||
if (false === $this->hasSubMenu($id)) {
|
||||
throw new ProgrammingError(
|
||||
'Tried to get invalid sub menu "%s"',
|
||||
$id
|
||||
);
|
||||
}
|
||||
|
||||
return $this->subMenus[$id];
|
||||
}
|
||||
|
||||
/**
|
||||
* Order this menu's sub menus based on their priority
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function order()
|
||||
{
|
||||
uasort($this->subMenus, array($this, 'cmpSubMenus'));
|
||||
foreach ($this->subMenus as $subMenu) {
|
||||
if ($subMenu->hasSubMenus()) {
|
||||
$subMenu->order();
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare sub menus based on priority and title
|
||||
*
|
||||
* @param Menu $a
|
||||
* @param Menu $b
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
protected function cmpSubMenus($a, $b)
|
||||
{
|
||||
if ($a->priority == $b->priority) {
|
||||
return $a->getTitle() > $b->getTitle() ? 1 : (
|
||||
$a->getTitle() < $b->getTitle() ? -1 : 0
|
||||
);
|
||||
}
|
||||
|
||||
return $a->priority > $b->priority ? 1 : -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Flatten configs
|
||||
*
|
||||
* @param array $configs An two dimensional array of menu configurations
|
||||
*
|
||||
* @return array The flattened config, as key-value array
|
||||
*/
|
||||
protected function flattenConfigs(array $configs)
|
||||
{
|
||||
$flattened = array();
|
||||
foreach ($configs as $menuConfig) {
|
||||
foreach ($menuConfig as $section => $itemConfig) {
|
||||
while (array_key_exists($section, $flattened)) {
|
||||
$section .= '_dup';
|
||||
}
|
||||
$flattened[$section] = $itemConfig;
|
||||
}
|
||||
}
|
||||
|
||||
return $flattened;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the sub menus
|
||||
*
|
||||
* @param array $menus The menus to load, as key-value array
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
protected function loadSubMenus(array $menus)
|
||||
{
|
||||
foreach ($menus as $menuId => $menuConfig) {
|
||||
$this->addSubMenu($menuId, $menuConfig);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the current menu node has any sub menus
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasChildren()
|
||||
{
|
||||
$current = $this->current();
|
||||
if (false !== $current) {
|
||||
return $current->hasSubMenus();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a iterator for the current menu node
|
||||
*
|
||||
* @return RecursiveIterator
|
||||
*/
|
||||
public function getChildren()
|
||||
{
|
||||
return $this->current();
|
||||
}
|
||||
|
||||
/**
|
||||
* Rewind the iterator to its first menu node
|
||||
*/
|
||||
public function rewind()
|
||||
{
|
||||
reset($this->subMenus);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return whether the iterator position is valid
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function valid()
|
||||
{
|
||||
return $this->key() !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the current menu node
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function current()
|
||||
{
|
||||
return current($this->subMenus);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the id of the current menu node
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function key()
|
||||
{
|
||||
return key($this->subMenus);
|
||||
}
|
||||
|
||||
/**
|
||||
* Move the iterator to the next menu node
|
||||
*/
|
||||
public function next()
|
||||
{
|
||||
next($this->subMenus);
|
||||
}
|
||||
|
||||
/**
|
||||
* PHP 5.3 GC should not leak, but just to be on the safe side...
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
$this->parent = null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,67 +0,0 @@
|
|||
<?php
|
||||
/* Icinga Web 2 | (c) 2015 Icinga Development Team | GPLv2+ */
|
||||
|
||||
namespace Icinga\Web\Menu;
|
||||
|
||||
use Icinga\Web\Menu;
|
||||
|
||||
abstract class BadgeMenuItemRenderer extends MenuItemRenderer
|
||||
{
|
||||
const STATE_OK = 'ok';
|
||||
const STATE_CRITICAL = 'critical';
|
||||
const STATE_WARNING = 'warning';
|
||||
const STATE_PENDING = 'pending';
|
||||
const STATE_UNKNOWN = 'unknown';
|
||||
|
||||
/**
|
||||
* Defines the color of the badge
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract public function getState();
|
||||
|
||||
/**
|
||||
* The amount of items to display in the badge
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
abstract public function getCount();
|
||||
|
||||
/**
|
||||
* The tooltip title
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract public function getTitle();
|
||||
|
||||
/**
|
||||
* Renders the html content of a single menu item
|
||||
*
|
||||
* @param Menu $menu
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function render(Menu $menu)
|
||||
{
|
||||
return '<div class="clearfix">' . $this->renderBadge() . $this->createLink($menu) . '</div>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the badge
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function renderBadge()
|
||||
{
|
||||
if ($count = $this->getCount()) {
|
||||
$view = $this->getView();
|
||||
return sprintf(
|
||||
'<span title="%s" class="badge pull-right state-%s">%s</span>',
|
||||
$view->escape($this->getTitle()),
|
||||
$view->escape($this->getState()),
|
||||
$count
|
||||
);
|
||||
}
|
||||
return '';
|
||||
}
|
||||
}
|
|
@ -1,101 +0,0 @@
|
|||
<?php
|
||||
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */
|
||||
|
||||
namespace Icinga\Web\Menu;
|
||||
|
||||
use Icinga\Application\Icinga;
|
||||
use Icinga\Web\Menu;
|
||||
use Icinga\Web\Url;
|
||||
use Icinga\Web\View;
|
||||
use Icinga\Data\ConfigObject;
|
||||
|
||||
/**
|
||||
* Default MenuItemRenderer class
|
||||
*/
|
||||
class MenuItemRenderer
|
||||
{
|
||||
/**
|
||||
* The view this menu item is being rendered to
|
||||
*
|
||||
* @var View|null
|
||||
*/
|
||||
protected $view = null;
|
||||
|
||||
/**
|
||||
* The link target
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $target = null;
|
||||
|
||||
/**
|
||||
* Create a new instance of MenuItemRenderer
|
||||
*
|
||||
* Is is possible to configure the link target using the option 'target'
|
||||
*
|
||||
* @param ConfigObject|null $configuration
|
||||
*/
|
||||
public function __construct(ConfigObject $configuration = null)
|
||||
{
|
||||
if ($configuration !== null) {
|
||||
$this->target = $configuration->get('target', null);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the view this menu item is being rendered to
|
||||
*
|
||||
* @return View
|
||||
*/
|
||||
protected function getView()
|
||||
{
|
||||
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)
|
||||
{
|
||||
$attributes = isset($this->target) ? sprintf(' target="%s"', $this->getView()->escape($this->target)) : '';
|
||||
|
||||
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() ? : '#',
|
||||
$attributes,
|
||||
$menu->getIcon(),
|
||||
$this->getView()->escape($menu->getTitle())
|
||||
);
|
||||
}
|
||||
|
||||
return sprintf(
|
||||
'<a href="%s"%s>%s%s<span></span></a>',
|
||||
$menu->getUrl() ? : '#',
|
||||
$attributes,
|
||||
$menu->getIcon()
|
||||
? '<img aria-hidden="true" src="' . Url::fromPath($menu->getIcon()) . '" class="icon" /> '
|
||||
: '',
|
||||
$this->getView()->escape($menu->getTitle())
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the html content of a single menu item
|
||||
*
|
||||
* @param Menu $menu
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function render(Menu $menu)
|
||||
{
|
||||
return $this->createLink($menu);
|
||||
}
|
||||
}
|
|
@ -1,34 +0,0 @@
|
|||
<?php
|
||||
/* Icinga Web 2 | (c) 2015 Icinga Development Team | GPLv2+ */
|
||||
|
||||
namespace Icinga\Web\Menu;
|
||||
|
||||
use RecursiveFilterIterator;
|
||||
use Icinga\Authentication\Auth;
|
||||
use Icinga\Web\Menu;
|
||||
|
||||
class PermittedMenuItemFilter extends RecursiveFilterIterator
|
||||
{
|
||||
/**
|
||||
* Accept menu items that are permitted to the user
|
||||
*
|
||||
* @return bool Whether the user has the required permission granted to display the menu item
|
||||
*/
|
||||
public function accept()
|
||||
{
|
||||
$item = $this->current();
|
||||
/** @var Menu $item */
|
||||
if (($permission = $item->getPermission()) !== null) {
|
||||
$auth = Auth::getInstance();
|
||||
if (! $auth->isAuthenticated()) {
|
||||
// Don't accept menu item because user is not authenticated and the menu item requires a permission
|
||||
return false;
|
||||
}
|
||||
if (! $auth->getUser()->can($permission)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// Accept menu item if it does not require a permission
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -1,94 +0,0 @@
|
|||
<?php
|
||||
/* Icinga Web 2 | (c) 2015 Icinga Development Team | GPLv2+ */
|
||||
|
||||
namespace Icinga\Web\Menu;
|
||||
|
||||
use Icinga\Web\Menu;
|
||||
use Icinga\Data\ConfigObject;
|
||||
|
||||
/**
|
||||
* Summary badge adding up all badges in the sub-menus that have the same state
|
||||
*/
|
||||
class SummaryMenuItemRenderer extends BadgeMenuItemRenderer
|
||||
{
|
||||
/**
|
||||
* Set of summarized problems from submenus
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $titles = array();
|
||||
|
||||
/**
|
||||
* The amount of problems
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $count = 0;
|
||||
|
||||
/**
|
||||
* The state that should be summarized
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $state;
|
||||
|
||||
/**
|
||||
* The amount of problems
|
||||
*/
|
||||
public function __construct(ConfigObject $configuration)
|
||||
{
|
||||
$this->state = $configuration->get('state', self::STATE_CRITICAL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the html content of a single menu item and summarized sub-menus
|
||||
*
|
||||
* @param Menu $menu
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function render(Menu $menu)
|
||||
{
|
||||
/** @var $submenu Menu */
|
||||
foreach ($menu->getSubMenus() as $submenu) {
|
||||
$renderer = $submenu->getRenderer();
|
||||
if ($renderer instanceof BadgeMenuItemRenderer) {
|
||||
if ($renderer->getState() === $this->state) {
|
||||
$this->titles[] = $renderer->getTitle();
|
||||
$this->count += $renderer->getCount();
|
||||
}
|
||||
}
|
||||
}
|
||||
return $this->renderBadge() . $this->createLink($menu);
|
||||
}
|
||||
|
||||
/**
|
||||
* The amount of items to display in the badge
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getCount()
|
||||
{
|
||||
return $this->count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines the color of the badge
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getState()
|
||||
{
|
||||
return $this->state;
|
||||
}
|
||||
|
||||
/**
|
||||
* The tooltip title
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getTitle()
|
||||
{
|
||||
return implode(', ', $this->titles);
|
||||
}
|
||||
}
|
|
@ -1,194 +0,0 @@
|
|||
<?php
|
||||
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */
|
||||
|
||||
namespace Icinga\Web;
|
||||
|
||||
use Exception;
|
||||
use Icinga\Web\Menu\MenuItemRenderer;
|
||||
use RecursiveIteratorIterator;
|
||||
use Icinga\Application\Logger;
|
||||
use Icinga\Web\Menu\PermittedMenuItemFilter;
|
||||
|
||||
/**
|
||||
* 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();
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $useCustomRenderer = false;
|
||||
|
||||
/**
|
||||
* @var MenuItemRenderer
|
||||
*/
|
||||
protected $defaultRenderer;
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
if ($url instanceof Url) {
|
||||
$this->url = $url;
|
||||
} else {
|
||||
$this->url = Url::fromPath($url);
|
||||
}
|
||||
$this->defaultRenderer = new MenuItemRenderer();
|
||||
parent::__construct(new PermittedMenuItemFilter($menu), RecursiveIteratorIterator::CHILD_FIRST);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $value
|
||||
*/
|
||||
public function useCustomRenderer($value = true)
|
||||
{
|
||||
$this->useCustomRenderer = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the outer ul opening html-tag
|
||||
*/
|
||||
public function beginIteration()
|
||||
{
|
||||
$this->tags[] = '<ul>';
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
if ($child->getRenderer() !== null && $this->useCustomRenderer) {
|
||||
try {
|
||||
return $child->getRenderer()->render($child);
|
||||
} catch (Exception $e) {
|
||||
Logger::error(
|
||||
'Could not invoke custom menu renderer. %s in %s:%d with message: %s',
|
||||
get_class($e),
|
||||
$e->getFile(),
|
||||
$e->getLine(),
|
||||
$e->getMessage()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->defaultRenderer->render($child);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
$openTag = '<li class="active" id="' . $child->getUniqueId() . '">';
|
||||
} else {
|
||||
$openTag = '<li id="' . $child->getUniqueId() . '">';
|
||||
}
|
||||
$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)
|
||||
{
|
||||
if (! $this->url) {
|
||||
return false;
|
||||
}
|
||||
if (! ($childUrl = $child->getUrl())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->url && $this->url->matches($childUrl);
|
||||
}
|
||||
}
|
|
@ -1,30 +0,0 @@
|
|||
<?php
|
||||
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */
|
||||
|
||||
namespace Tests\Icinga\Web;
|
||||
|
||||
use Icinga\Web\Menu;
|
||||
use Icinga\Test\BaseTestCase;
|
||||
use Icinga\Data\ConfigObject;
|
||||
|
||||
class MenuTest extends BaseTestCase
|
||||
{
|
||||
public function testWhetherMenusAreNaturallySorted()
|
||||
{
|
||||
$menu = new Menu('test');
|
||||
$menu->addSubMenu(5, new ConfigObject(array('title' => 'ccc5')));
|
||||
$menu->addSubMenu(0, new ConfigObject(array('title' => 'aaa')));
|
||||
$menu->addSubMenu(3, new ConfigObject(array('title' => 'ccc')));
|
||||
$menu->addSubMenu(2, new ConfigObject(array('title' => 'bbb')));
|
||||
$menu->addSubMenu(4, new ConfigObject(array('title' => 'ccc2')));
|
||||
$menu->addSubMenu(1, new ConfigObject(array('title' => 'bb')));
|
||||
|
||||
$this->assertEquals(
|
||||
array('aaa', 'bb', 'bbb', 'ccc', 'ccc2', 'ccc5'),
|
||||
array_map(function ($m) {
|
||||
return $m->getTitle();
|
||||
}, iterator_to_array($menu->order())),
|
||||
'Menu::order() does not return its elements in natural order'
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue