Skip to content

Commit

Permalink
Merge branch 'bugfix/separate-configuration-files-for-navigation-item…
Browse files Browse the repository at this point in the history
…s-10246'

fixes #10246
  • Loading branch information
Johannes Meyer committed Sep 30, 2015
2 parents 64783fd + 8b0c820 commit ff25c24
Show file tree
Hide file tree
Showing 9 changed files with 299 additions and 107 deletions.
134 changes: 97 additions & 37 deletions application/controllers/NavigationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

use Exception;
use Icinga\Application\Config;
use Icinga\Application\Icinga;
use Icinga\Exception\NotFoundError;
use Icinga\Data\DataArray\ArrayDatasource;
use Icinga\Forms\ConfirmRemovalForm;
use Icinga\Forms\Navigation\NavigationConfigForm;
use Icinga\Web\Controller;
use Icinga\Web\Form;
use Icinga\Web\Navigation\Navigation;
use Icinga\Web\Notification;
use Icinga\Web\Url;

Expand All @@ -21,23 +21,31 @@
class NavigationController extends Controller
{
/**
* The default item types provided by Icinga Web 2
* The global navigation item type configuration
*
* @var array
*/
protected $defaultItemTypes;
protected $itemTypeConfig;

/**
* {@inheritdoc}
*/
public function init()
{
parent::init();
$this->itemTypeConfig = Navigation::getItemTypeConfiguration();
}

$this->defaultItemTypes = array(
'menu-item' => $this->translate('Menu Entry'),
'dashlet' => 'Dashlet'
);
/**
* Return the label for the given navigation item type
*
* @param string $type
*
* @return string $type if no label can be found
*/
protected function getItemLabel($type)
{
return isset($this->itemTypeConfig[$type]['label']) ? $this->itemTypeConfig[$type]['label'] : $type;
}

/**
Expand All @@ -47,33 +55,71 @@ public function init()
*/
protected function listItemTypes()
{
$moduleManager = Icinga::app()->getModuleManager();

$types = $this->defaultItemTypes;
foreach ($moduleManager->getLoadedModules() as $module) {
if ($this->hasPermission($moduleManager::MODULE_PERMISSION_NS . $module->getName())) {
$moduleTypes = $module->getNavigationItems();
if (! empty($moduleTypes)) {
$types = array_merge($types, $moduleTypes);
}
}
$types = array();
foreach ($this->itemTypeConfig as $type => $options) {
$types[$type] = isset($options['label']) ? $options['label'] : $type;
}

return $types;
}

/**
* Return all shared navigation item configurations
*
* @param string $owner A username if only items shared by a specific user are desired
*
* @return array
*/
protected function fetchSharedNavigationItemConfigs($owner = null)
{
$configs = array();
foreach ($this->itemTypeConfig as $type => $_) {
$config = Config::navigation($type);
$config->getConfigObject()->setKeyColumn('name');
$query = $config->select();
if ($owner !== null) {
$query->where('owner', $owner);
}

foreach ($query as $itemConfig) {
$configs[] = $itemConfig;
}
}

return $configs;
}

/**
* Return all user navigation item configurations
*
* @param string $username
*
* @return array
*/
protected function fetchUserNavigationItemConfigs($username)
{
$configs = array();
foreach ($this->itemTypeConfig as $type => $_) {
$config = Config::navigation($type, $username);
$config->getConfigObject()->setKeyColumn('name');
foreach ($config->select() as $itemConfig) {
$configs[] = $itemConfig;
}
}

return $configs;
}

/**
* Show the current user a list of his/her navigation items
*/
public function indexAction()
{
$user = $this->Auth()->getUser();

$ds = new ArrayDatasource(array_merge(
Config::app('navigation')->select()->where('owner', $user->getUsername())->fetchAll(),
iterator_to_array($user->loadNavigationConfig())
$this->fetchSharedNavigationItemConfigs($user->getUsername()),
$this->fetchUserNavigationItemConfigs($user->getUsername())
));
$ds->setKeyColumn('name');
$query = $ds->select();

$this->view->types = $this->listItemTypes();
Expand All @@ -91,7 +137,7 @@ public function indexAction()
array(
'type' => $this->translate('Type'),
'owner' => $this->translate('Shared'),
'name' => $this->translate('Shared Navigation')
'name' => $this->translate('Navigation')
),
$query
);
Expand All @@ -103,13 +149,11 @@ public function indexAction()
public function sharedAction()
{
$this->assertPermission('config/application/navigation');
$config = Config::app('navigation');
$config->getConfigObject()->setKeyColumn('name');
$query = $config->select();
$ds = new ArrayDatasource($this->fetchSharedNavigationItemConfigs());
$query = $ds->select();

$removeForm = new Form();
$removeForm->setUidDisabled();
$removeForm->setAction(Url::fromPath('navigation/unshare'));
$removeForm->addElement('hidden', 'name', array(
'decorators' => array('ViewHelper')
));
Expand Down Expand Up @@ -156,11 +200,10 @@ public function addAction()
{
$form = new NavigationConfigForm();
$form->setRedirectUrl('navigation');
$form->setUser($this->Auth()->getUser());
$form->setItemTypes($this->listItemTypes());
$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->setUser($this->Auth()->getUser());
$form->setShareConfig(Config::app('navigation'));
$form->setOnSuccess(function (NavigationConfigForm $form) {
$data = array_filter($form->getValues());

Expand All @@ -172,7 +215,7 @@ public function addAction()
}

if ($form->save()) {
if (isset($data['type']) && $data['type'] === 'menu-item') {
if ($data['type'] === 'menu-item') {
$form->getResponse()->setRerenderLayout();
}

Expand All @@ -194,14 +237,22 @@ public function addAction()
public function editAction()
{
$itemName = $this->params->getRequired('name');
$itemType = $this->params->getRequired('type');
$referrer = $this->params->get('referrer', 'index');

$user = $this->Auth()->getUser();
if ($user->can('config/application/navigation')) {
$itemOwner = $this->params->get('owner', $user->getUsername());
} else {
$itemOwner = $user->getUsername();
}

$form = new NavigationConfigForm();
$form->setUser($user);
$form->setShareConfig(Config::navigation($itemType));
$form->setUserConfig(Config::navigation($itemType, $itemOwner));
$form->setRedirectUrl($referrer === 'shared' ? 'navigation/shared' : 'navigation');
$form->setItemTypes($this->listItemTypes());
$form->setTitle(sprintf($this->translate('Edit Navigation Item %s'), $itemName));
$form->setUser($this->Auth()->getUser());
$form->setShareConfig(Config::app('navigation'));
$form->setTitle(sprintf($this->translate('Edit %s %s'), $this->getItemLabel($itemType), $itemName));
$form->setOnSuccess(function (NavigationConfigForm $form) use ($itemName) {
$data = array_map(
function ($v) {
Expand Down Expand Up @@ -248,13 +299,17 @@ function ($v) {
public function removeAction()
{
$itemName = $this->params->getRequired('name');
$itemType = $this->params->getRequired('type');
$user = $this->Auth()->getUser();

$navigationConfigForm = new NavigationConfigForm();
$navigationConfigForm->setUser($this->Auth()->getUser());
$navigationConfigForm->setShareConfig(Config::app('navigation'));
$navigationConfigForm->setUser($user);
$navigationConfigForm->setShareConfig(Config::navigation($itemType));
$navigationConfigForm->setUserConfig(Config::navigation($itemType, $user->getUsername()));

$form = new ConfirmRemovalForm();
$form->setRedirectUrl('navigation');
$form->setTitle(sprintf($this->translate('Remove Navigation Item %s'), $itemName));
$form->setTitle(sprintf($this->translate('Remove %s %s'), $this->getItemLabel($itemType), $itemName));
$form->setOnSuccess(function (ConfirmRemovalForm $form) use ($itemName, $navigationConfigForm) {
try {
$itemConfig = $navigationConfigForm->delete($itemName);
Expand Down Expand Up @@ -291,9 +346,14 @@ public function unshareAction()
$this->assertPermission('config/application/navigation');
$this->assertHttpMethod('POST');

// TODO: I'd like these being form fields
$itemType = $this->params->getRequired('type');
$itemOwner = $this->params->getRequired('owner');

$navigationConfigForm = new NavigationConfigForm();
$navigationConfigForm->setUser($this->Auth()->getUser());
$navigationConfigForm->setShareConfig(Config::app('navigation'));
$navigationConfigForm->setShareConfig(Config::navigation($itemType));
$navigationConfigForm->setUserConfig(Config::navigation($itemType, $itemOwner));

$form = new Form(array(
'onSuccess' => function ($form) use ($navigationConfigForm) {
Expand Down

0 comments on commit ff25c24

Please sign in to comment.