icingaweb2/application/controllers/NavigationController.php

265 lines
8.9 KiB
PHP
Raw Normal View History

<?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
namespace Icinga\Controllers;
use Exception;
use Icinga\Application\Config;
use Icinga\Exception\NotFoundError;
use Icinga\Forms\ConfirmRemovalForm;
use Icinga\Forms\Navigation\NavigationConfigForm;
use Icinga\Web\Controller;
use Icinga\Web\Form;
use Icinga\Web\Notification;
use Icinga\Web\Url;
/**
* Navigation configuration
*/
class NavigationController extends Controller
{
/**
* Show the current user a list of his/her navigation items
*/
public function indexAction()
{
$user = $this->Auth()->getUser();
$userConfig = $user->loadNavigationConfig();
$sharedConfig = Config::app('navigation');
$this->view->items = array_merge(
$sharedConfig->select()->where('owner', $user->getUsername())->fetchAll(),
iterator_to_array($userConfig)
);
$this->getTabs()->add(
'navigation',
array(
'title' => $this->translate('List and configure your own navigation items'),
'label' => $this->translate('Navigation'),
'url' => 'navigation'
)
)->activate('navigation');
}
/**
* List all shared navigation items
*/
public function sharedAction()
{
$this->assertPermission('config/application/navigation');
$this->view->items = Config::app('navigation');
$removeForm = new Form();
$removeForm->setUidDisabled();
$removeForm->setAction(Url::fromPath('navigation/unshare'));
$removeForm->addElement('hidden', 'name', array(
'decorators' => array('ViewHelper')
));
$removeForm->addElement('hidden', 'redirect', array(
'value' => Url::fromPath('navigation/shared'),
'decorators' => array('ViewHelper')
));
$removeForm->addElement('button', 'btn_submit', array(
'escape' => false,
'type' => 'submit',
'class' => 'link-like spinner',
'value' => 'btn_submit',
'decorators' => array('ViewHelper'),
'label' => $this->view->icon('trash'),
'title' => $this->translate('Unshare this navigation item')
));
$this->view->removeForm = $removeForm;
$this->getTabs()->add(
'navigation/shared',
array(
'title' => $this->translate('List and configure shared navigation items'),
'label' => $this->translate('Shared Navigation'),
'url' => 'navigation/shared'
)
)->activate('navigation/shared');
}
/**
* Add a navigation item
*/
public function addAction()
{
$form = new NavigationConfigForm();
$form->setRedirectUrl('navigation');
$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->setIniConfig($this->Auth()->getUser()->loadNavigationConfig());
$form->setOnSuccess(function (NavigationConfigForm $form) {
try {
$form->add(array_filter($form->getValues()));
} catch (Exception $e) {
$form->error($e->getMessage());
return false;
}
if ($form->save()) {
Notification::success(t('Navigation item successfully created'));
return true;
}
return false;
});
$form->handleRequest();
$this->view->form = $form;
$this->render('form');
}
/**
* Edit a navigation item
*/
public function editAction()
{
$itemName = $this->params->getRequired('name');
$user = $this->Auth()->getUser();
$config = $user->loadNavigationConfig();
if (! $config->hasSection($itemName)) {
$shareConfig = Config::app('navigation');
if ($shareConfig->hasSection($itemName)
&& ($shareConfig->get($itemName, 'owner') === $user->getUsername()
|| $user->can('config/application/navigation'))
) {
$config = $shareConfig;
}
}
$form = new NavigationConfigForm();
$form->setIniConfig($config);
$form->setRedirectUrl('navigation');
$form->setTitle(sprintf($this->translate('Edit Navigation Item %s'), $itemName));
$form->setOnSuccess(function (NavigationConfigForm $form) use ($itemName) {
try {
$form->edit($itemName, array_map(
function ($v) {
return $v !== '' ? $v : null;
},
$form->getValues()
));
} catch (NotFoundError $e) {
throw $e;
} catch (Exception $e) {
$form->error($e->getMessage());
return false;
}
if ($form->save()) {
Notification::success(sprintf(t('Navigation item "%s" successfully updated'), $itemName));
return true;
}
return false;
});
try {
$form->load($itemName);
$form->handleRequest();
} catch (NotFoundError $_) {
$this->httpNotFound(sprintf($this->translate('Navigation item "%s" not found'), $itemName));
}
$this->view->form = $form;
$this->render('form');
}
/**
* Remove a navigation item
*/
public function removeAction()
{
$itemName = $this->params->getRequired('name');
$user = $this->Auth()->getUser();
$config = $user->loadNavigationConfig();
if (! $config->hasSection($itemName)) {
$shareConfig = Config::app('navigation');
if ($shareConfig->hasSection($itemName) && $shareConfig->get($itemName, 'owner') === $user->getUsername()) {
$config = $shareConfig;
}
}
$navigationConfigForm = new NavigationConfigForm();
$navigationConfigForm->setIniConfig($config);
$form = new ConfirmRemovalForm();
$form->setRedirectUrl('navigation');
$form->setTitle(sprintf($this->translate('Remove Navigation Item %s'), $itemName));
$form->setOnSuccess(function (ConfirmRemovalForm $form) use ($itemName, $navigationConfigForm) {
try {
$navigationConfigForm->delete($itemName);
} catch (Exception $e) {
$form->error($e->getMessage());
return false;
}
if ($navigationConfigForm->save()) {
Notification::success(sprintf(t('Navigation Item "%s" successfully removed'), $itemName));
return true;
}
return false;
});
$form->handleRequest();
$this->view->form = $form;
$this->render('form');
}
/**
* Unshare a navigation item
*/
public function unshareAction()
{
$this->assertPermission('config/application/navigation');
$this->assertHttpMethod('POST');
$navigationConfigForm = new NavigationConfigForm();
$navigationConfigForm->setIniConfig(Config::app('navigation'));
$form = new Form(array(
'onSuccess' => function ($form) use ($navigationConfigForm) {
try {
if ($navigationConfigForm->unshare($form->getValue('name'))) {
Notification::success(sprintf(
t('Navigation item "%s" has been unshared'),
$form->getValue('name')
));
} else {
Notification::error(sprintf(
t('Failed to unshare navigation item "%s"'),
$form->getValue('name')
));
}
} catch (NotFoundError $e) {
throw $e;
} catch (Exception $e) {
Notification::error($e->getMessage());
}
$redirect = $form->getValue('redirect');
if (! empty($redirect)) {
$form->setRedirectUrl(htmlspecialchars_decode($redirect));
}
return true;
}
));
$form->setUidDisabled();
$form->setSubmitLabel('btn_submit'); // Required to ensure that isSubmitted() is called
$form->addElement('hidden', 'name', array('required' => true));
$form->addElement('hidden', 'redirect');
try {
$form->handleRequest();
} catch (NotFoundError $_) {
$this->httpNotFound(sprintf($this->translate('Navigation item "%s" not found'), $form->getValue('name')));
}
}
}