NavigationController: Register navigation item types differently

refs #10246
This commit is contained in:
Johannes Meyer 2015-09-29 17:12:57 +02:00
parent 4921b1a62e
commit b4bcfa4e08
2 changed files with 45 additions and 16 deletions

View File

@ -6,6 +6,7 @@ namespace Icinga\Controllers;
use Exception; use Exception;
use Icinga\Application\Config; use Icinga\Application\Config;
use Icinga\Application\Icinga; use Icinga\Application\Icinga;
use Icinga\Exception\IcingaException;
use Icinga\Exception\NotFoundError; use Icinga\Exception\NotFoundError;
use Icinga\Data\DataArray\ArrayDatasource; use Icinga\Data\DataArray\ArrayDatasource;
use Icinga\Forms\ConfirmRemovalForm; use Icinga\Forms\ConfirmRemovalForm;
@ -27,17 +28,43 @@ class NavigationController extends Controller
*/ */
protected $defaultItemTypes; protected $defaultItemTypes;
/**
* The item types provided by Icinga Web 2's modules
*
* @var array
*/
protected $moduleItemTypes;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function init() public function init()
{ {
parent::init(); parent::init();
$this->defaultItemTypes = array( $this->defaultItemTypes = array(
'menu-item' => $this->translate('Menu Entry'), 'menu-item' => array(
'dashlet' => 'Dashlet' 'label' => $this->translate('Menu Entry'),
'config' => 'menu'
),
'dashlet' => array(
'label' => 'Dashlet',
'config' => 'dashboard'
)
); );
$moduleItemTypes = array();
$moduleManager = Icinga::app()->getModuleManager();
foreach ($moduleManager->getLoadedModules() as $module) {
if ($this->hasPermission($moduleManager::MODULE_PERMISSION_NS . $module->getName())) {
foreach ($module->getNavigationItems() as $type => $options) {
if (! isset($moduleItemTypes[$type])) {
$moduleItemTypes[$type] = $options;
}
}
}
}
$this->moduleItemTypes = $moduleItemTypes;
} }
/** /**
@ -67,16 +94,13 @@ class NavigationController extends Controller
*/ */
protected function listItemTypes() protected function listItemTypes()
{ {
$moduleManager = Icinga::app()->getModuleManager(); $types = array();
foreach ($this->defaultItemTypes as $type => $options) {
$types[$type] = isset($options['label']) ? $options['label'] : $type;
}
$types = $this->defaultItemTypes; foreach ($this->moduleItemTypes as $type => $options) {
foreach ($moduleManager->getLoadedModules() as $module) { $types[$type] = isset($options['label']) ? $options['label'] : $type;
if ($this->hasPermission($moduleManager::MODULE_PERMISSION_NS . $module->getName())) {
$moduleTypes = $module->getNavigationItems();
if (! empty($moduleTypes)) {
$types = array_merge($types, $moduleTypes);
}
}
} }
return $types; return $types;
@ -205,8 +229,8 @@ class NavigationController extends Controller
{ {
$form = new NavigationConfigForm(); $form = new NavigationConfigForm();
$form->setRedirectUrl('navigation'); $form->setRedirectUrl('navigation');
$form->setItemTypes($this->listItemTypes());
$form->setTitle($this->translate('Create New Navigation Item')); $form->setTitle($this->translate('Create New Navigation Item'));
$form->setItemTypes(array_merge($this->defaultItemTypes, $this->moduleItemTypes));
$form->addDescription($this->translate('Create a new navigation item, such as a menu entry or dashlet.')); $form->addDescription($this->translate('Create a new navigation item, such as a menu entry or dashlet.'));
$form->setUser($this->Auth()->getUser()); $form->setUser($this->Auth()->getUser());
$form->setShareConfig(Config::app('navigation')); $form->setShareConfig(Config::app('navigation'));

View File

@ -1014,16 +1014,21 @@ class Module
} }
/** /**
* Provide a new type of configurable navigation item with a optional label * Provide a new type of configurable navigation item with a optional label and config filename
* *
* @param string $type * @param string $type
* @param string $label * @param string $label
* @param string $config
* *
* @return $this * @return $this
*/ */
protected function provideNavigationItem($type, $label = null) protected function provideNavigationItem($type, $label = null, $config = null)
{ {
$this->navigationItems[$type] = $label ?: $type; $this->navigationItems[$type] = array(
'label' => $label,
'config' => $config
);
return $this; return $this;
} }