NavigationConfigForm: Let the form decide which configuration to manage
refs #5600
This commit is contained in:
parent
bc57f9b613
commit
d4c0f10b47
|
@ -90,7 +90,8 @@ class NavigationController extends Controller
|
|||
$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->setUser($this->Auth()->getUser());
|
||||
$form->setShareConfig(Config::app('navigation'));
|
||||
$form->setOnSuccess(function (NavigationConfigForm $form) {
|
||||
try {
|
||||
$form->add(array_filter($form->getValues()));
|
||||
|
@ -118,23 +119,12 @@ class NavigationController extends Controller
|
|||
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->setUser($this->Auth()->getUser());
|
||||
$form->setShareConfig(Config::app('navigation'));
|
||||
$form->setOnSuccess(function (NavigationConfigForm $form) use ($itemName) {
|
||||
try {
|
||||
$form->edit($itemName, array_map(
|
||||
|
@ -175,24 +165,19 @@ class NavigationController extends Controller
|
|||
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);
|
||||
$navigationConfigForm->setUser($this->Auth()->getUser());
|
||||
$navigationConfigForm->setShareConfig(Config::app('navigation'));
|
||||
$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 (NotFoundError $e) {
|
||||
Notification::success(sprintf(t('Navigation Item "%s" not found. No action required'), $itemName));
|
||||
return true;
|
||||
} catch (Exception $e) {
|
||||
$form->error($e->getMessage());
|
||||
return false;
|
||||
|
@ -220,7 +205,8 @@ class NavigationController extends Controller
|
|||
$this->assertHttpMethod('POST');
|
||||
|
||||
$navigationConfigForm = new NavigationConfigForm();
|
||||
$navigationConfigForm->setIniConfig(Config::app('navigation'));
|
||||
$navigationConfigForm->setUser($this->Auth()->getUser());
|
||||
$navigationConfigForm->setShareConfig(Config::app('navigation'));
|
||||
|
||||
$form = new Form(array(
|
||||
'onSuccess' => function ($form) use ($navigationConfigForm) {
|
||||
|
|
|
@ -4,10 +4,12 @@
|
|||
namespace Icinga\Forms\Navigation;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use Icinga\Application\Config;
|
||||
use Icinga\Application\Icinga;
|
||||
use Icinga\Exception\IcingaException;
|
||||
use Icinga\Exception\NotFoundError;
|
||||
use Icinga\Forms\ConfigForm;
|
||||
use Icinga\User;
|
||||
use Icinga\Web\Form;
|
||||
|
||||
/**
|
||||
|
@ -32,6 +34,27 @@ class NavigationConfigForm extends ConfigForm
|
|||
*/
|
||||
protected $itemToLoad;
|
||||
|
||||
/**
|
||||
* The user for whom to manage navigation items
|
||||
*
|
||||
* @var User
|
||||
*/
|
||||
protected $user;
|
||||
|
||||
/**
|
||||
* The user's navigation configuration
|
||||
*
|
||||
* @var Config
|
||||
*/
|
||||
protected $userConfig;
|
||||
|
||||
/**
|
||||
* The shared navigation configuration
|
||||
*
|
||||
* @var Config
|
||||
*/
|
||||
protected $shareConfig;
|
||||
|
||||
/**
|
||||
* Initialize this form
|
||||
*/
|
||||
|
@ -41,6 +64,79 @@ class NavigationConfigForm extends ConfigForm
|
|||
$this->setSubmitLabel($this->translate('Save Changes'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the user for whom to manage navigation items
|
||||
*
|
||||
* @param User $user
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setUser(User $user)
|
||||
{
|
||||
$this->user = $user;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the user for whom to manage navigation items
|
||||
*
|
||||
* @return User
|
||||
*/
|
||||
public function getUser()
|
||||
{
|
||||
return $this->user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the user's navigation configuration
|
||||
*
|
||||
* @param Config $config
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setUserConfig(Config $config)
|
||||
{
|
||||
$this->userConfig = $config;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the user's navigation configuration
|
||||
*
|
||||
* @return Config
|
||||
*/
|
||||
public function getUserConfig()
|
||||
{
|
||||
if ($this->userConfig === null) {
|
||||
$this->userConfig = $this->getUser()->loadNavigationConfig();
|
||||
}
|
||||
|
||||
return $this->userConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the shared navigation configuration
|
||||
*
|
||||
* @param Config $config
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setShareConfig(Config $config)
|
||||
{
|
||||
$this->shareConfig = $config;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the shared navigation configuration
|
||||
*
|
||||
* @return Config
|
||||
*/
|
||||
public function getShareConfig()
|
||||
{
|
||||
return $this->shareConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* Populate the form with the given navigation item's config
|
||||
*
|
||||
|
@ -52,7 +148,7 @@ class NavigationConfigForm extends ConfigForm
|
|||
*/
|
||||
public function load($name)
|
||||
{
|
||||
if (! $this->config->hasSection($name)) {
|
||||
if ($this->getConfigForItem($name) === null) {
|
||||
throw new NotFoundError('No navigation item called "%s" found', $name);
|
||||
}
|
||||
|
||||
|
@ -79,7 +175,8 @@ class NavigationConfigForm extends ConfigForm
|
|||
}
|
||||
|
||||
$itemName = $data['name'];
|
||||
if ($this->config->hasSection($itemName)) {
|
||||
$config = $this->getUserConfig();
|
||||
if ($config->hasSection($itemName)) {
|
||||
throw new IcingaException(
|
||||
$this->translate('A navigation item with the name "%s" does already exist'),
|
||||
$itemName
|
||||
|
@ -87,7 +184,8 @@ class NavigationConfigForm extends ConfigForm
|
|||
}
|
||||
|
||||
unset($data['name']);
|
||||
$this->config->setSection($itemName, $data);
|
||||
$config->setSection($itemName, $data);
|
||||
$this->setIniConfig($config);
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
@ -103,14 +201,15 @@ class NavigationConfigForm extends ConfigForm
|
|||
*/
|
||||
public function edit($name, array $data)
|
||||
{
|
||||
if (! $this->config->hasSection($name)) {
|
||||
$config = $this->getConfigForItem($name);
|
||||
if ($config === null) {
|
||||
throw new NotFoundError('No navigation item called "%s" found', $name);
|
||||
}
|
||||
|
||||
$itemConfig = $this->config->getSection($name);
|
||||
$itemConfig = $config->getSection($name);
|
||||
if (isset($data['name'])) {
|
||||
if ($data['name'] !== $name) {
|
||||
$this->config->removeSection($name);
|
||||
$config->removeSection($name);
|
||||
$name = $data['name'];
|
||||
}
|
||||
|
||||
|
@ -124,7 +223,8 @@ class NavigationConfigForm extends ConfigForm
|
|||
}
|
||||
}
|
||||
|
||||
$this->config->setSection($name, $itemConfig);
|
||||
$config->setSection($name, $itemConfig);
|
||||
$this->setIniConfig($config);
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
@ -137,7 +237,13 @@ class NavigationConfigForm extends ConfigForm
|
|||
*/
|
||||
public function delete($name)
|
||||
{
|
||||
$this->config->removeSection($name);
|
||||
$config = $this->getConfigForItem($name);
|
||||
if ($config === null) {
|
||||
throw new NotFoundError('No navigation item called "%s" found', $name);
|
||||
}
|
||||
|
||||
$config->removeSection($name);
|
||||
$this->setIniConfig($config);
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
@ -152,7 +258,7 @@ class NavigationConfigForm extends ConfigForm
|
|||
*/
|
||||
public function unshare($name)
|
||||
{
|
||||
throw new NotFoundError($name);
|
||||
throw new NotFoundError('No navigation item called "%s" found', $name);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -210,7 +316,7 @@ class NavigationConfigForm extends ConfigForm
|
|||
public function onRequest()
|
||||
{
|
||||
if ($this->itemToLoad) {
|
||||
$data = $this->config->getSection($this->itemToLoad)->toArray();
|
||||
$data = $this->getConfigForItem($this->itemToLoad)->getSection($this->itemToLoad)->toArray();
|
||||
$data['name'] = $this->itemToLoad;
|
||||
$this->populate($data);
|
||||
}
|
||||
|
@ -245,6 +351,27 @@ class NavigationConfigForm extends ConfigForm
|
|||
return $types;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the navigation configuration the given item is a part of
|
||||
*
|
||||
* @param string $name
|
||||
*
|
||||
* @return Config|null In case the item is not part of any configuration
|
||||
*/
|
||||
protected function getConfigForItem($name)
|
||||
{
|
||||
if ($this->getUserConfig()->hasSection($name)) {
|
||||
return $this->getUserConfig();
|
||||
} elseif ($this->getShareConfig()->hasSection($name)) {
|
||||
if (
|
||||
$this->getShareConfig()->get($name, 'owner') === $this->getUser()->getUsername()
|
||||
|| $this->getUser()->can('config/application/navigation')
|
||||
) {
|
||||
return $this->getShareConfig();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the form for the given type of navigation item
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue