NavigationController: Provide available item types externally

refs #5600
This commit is contained in:
Johannes Meyer 2015-09-16 15:05:43 +02:00
parent d1fa5e8fce
commit 4c1dd2ed36
2 changed files with 72 additions and 31 deletions

View File

@ -5,6 +5,7 @@ namespace Icinga\Controllers;
use Exception; use Exception;
use Icinga\Application\Config; use Icinga\Application\Config;
use Icinga\Application\Icinga;
use Icinga\Exception\NotFoundError; use Icinga\Exception\NotFoundError;
use Icinga\Forms\ConfirmRemovalForm; use Icinga\Forms\ConfirmRemovalForm;
use Icinga\Forms\Navigation\NavigationConfigForm; use Icinga\Forms\Navigation\NavigationConfigForm;
@ -18,6 +19,44 @@ use Icinga\Web\Url;
*/ */
class NavigationController extends Controller class NavigationController extends Controller
{ {
/**
* The default item types provided by Icinga Web 2
*
* @var array
*/
protected $defaultItemTypes;
/**
* {@inheritdoc}
*/
public function init()
{
parent::init();
$this->defaultItemTypes = array(
'menu-item' => $this->translate('Menu Entry'),
'dashlet' => 'Dashlet'
);
}
/**
* Return a list of available navigation item types
*
* @return array
*/
protected function listItemTypes()
{
$types = $this->defaultItemTypes;
foreach (Icinga::app()->getModuleManager()->getLoadedModules() as $module) {
$moduleTypes = $module->getNavigationItems();
if (! empty($moduleTypes)) {
$types = array_merge($types, $moduleTypes);
}
}
return $types;
}
/** /**
* Show the current user a list of his/her navigation items * Show the current user a list of his/her navigation items
*/ */
@ -88,6 +127,7 @@ 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->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());
@ -122,6 +162,7 @@ class NavigationController extends Controller
$form = new NavigationConfigForm(); $form = new NavigationConfigForm();
$form->setRedirectUrl('navigation'); $form->setRedirectUrl('navigation');
$form->setItemTypes($this->listItemTypes());
$form->setTitle(sprintf($this->translate('Edit Navigation Item %s'), $itemName)); $form->setTitle(sprintf($this->translate('Edit Navigation Item %s'), $itemName));
$form->setUser($this->Auth()->getUser()); $form->setUser($this->Auth()->getUser());
$form->setShareConfig(Config::app('navigation')); $form->setShareConfig(Config::app('navigation'));

View File

@ -5,7 +5,6 @@ namespace Icinga\Forms\Navigation;
use InvalidArgumentException; use InvalidArgumentException;
use Icinga\Application\Config; use Icinga\Application\Config;
use Icinga\Application\Icinga;
use Icinga\Exception\IcingaException; use Icinga\Exception\IcingaException;
use Icinga\Exception\NotFoundError; use Icinga\Exception\NotFoundError;
use Icinga\Forms\ConfigForm; use Icinga\Forms\ConfigForm;
@ -17,13 +16,6 @@ use Icinga\Web\Form;
*/ */
class NavigationConfigForm extends ConfigForm class NavigationConfigForm extends ConfigForm
{ {
/**
* The default item types provided by Icinga Web 2
*
* @var array
*/
protected $defaultItemTypes;
/** /**
* The secondary configuration to write * The secondary configuration to write
* *
@ -62,6 +54,13 @@ class NavigationConfigForm extends ConfigForm
*/ */
protected $shareConfig; protected $shareConfig;
/**
* The available navigation item types
*
* @var array
*/
protected $itemTypes;
/** /**
* Initialize this form * Initialize this form
*/ */
@ -69,11 +68,6 @@ class NavigationConfigForm extends ConfigForm
{ {
$this->setName('form_config_navigation'); $this->setName('form_config_navigation');
$this->setSubmitLabel($this->translate('Save Changes')); $this->setSubmitLabel($this->translate('Save Changes'));
$this->defaultItemTypes = array(
'menu-item' => $this->translate('Menu Entry'),
'dashlet' => 'Dashlet'
);
} }
/** /**
@ -149,6 +143,29 @@ class NavigationConfigForm extends ConfigForm
return $this->shareConfig; return $this->shareConfig;
} }
/**
* Set the available navigation item types
*
* @param array $itemTypes
*
* @return $this
*/
public function setItemTypes(array $itemTypes)
{
$this->itemTypes = $itemTypes;
return $this;
}
/**
* Return the available navigation item types
*
* @return array
*/
public function getItemTypes()
{
return $this->itemTypes ?: array();
}
/** /**
* Populate the form with the given navigation item's config * Populate the form with the given navigation item's config
* *
@ -329,7 +346,7 @@ class NavigationConfigForm extends ConfigForm
*/ */
public function createElements(array $formData) public function createElements(array $formData)
{ {
$itemTypes = $this->listItemTypes(); $itemTypes = $this->getItemTypes();
$itemType = isset($formData['type']) ? $formData['type'] : key($itemTypes); $itemType = isset($formData['type']) ? $formData['type'] : key($itemTypes);
$this->addElement( $this->addElement(
@ -434,23 +451,6 @@ class NavigationConfigForm extends ConfigForm
} }
} }
/**
* Return a list of available item types
*
* @return array
*/
protected function listItemTypes()
{
$types = $this->defaultItemTypes;
foreach (Icinga::app()->getModuleManager()->getLoadedModules() as $moduleName => $module) {
foreach ($module->getNavigationItems() as $type => $label) {
$types[$type] = mt($moduleName, $label);
}
}
return $types;
}
/** /**
* Return the navigation configuration the given item is a part of * Return the navigation configuration the given item is a part of
* *