Skip to content

Commit

Permalink
BackendConfigForm: Adjust how to process requests
Browse files Browse the repository at this point in the history
refs #9516
  • Loading branch information
Johannes Meyer committed Jun 29, 2015
1 parent 766ff8e commit db20f2d
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 165 deletions.
123 changes: 89 additions & 34 deletions modules/monitoring/application/controllers/ConfigController.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */

use Icinga\Web\Notification;
use Icinga\Data\ResourceFactory;
use Icinga\Exception\ConfigurationError;
use Icinga\Forms\ConfirmRemovalForm;
use Icinga\Web\Controller;
use Icinga\Web\Notification;
use Icinga\Module\Monitoring\Forms\Config\BackendConfigForm;
use Icinga\Module\Monitoring\Forms\Config\InstanceConfigForm;
use Icinga\Module\Monitoring\Forms\Config\SecurityConfigForm;
Expand All @@ -25,69 +26,122 @@ public function indexAction()
}

/**
* Display a form to modify the backend identified by the 'backend' parameter of the request
* Edit a monitoring backend
*/
public function editbackendAction()
{
$backendName = $this->params->getRequired('backend');

$form = new BackendConfigForm();
$form->setTitle($this->translate('Edit Existing Backend'));
$form->setRedirectUrl('monitoring/config');
$form->setTitle(sprintf($this->translate('Edit Monitoring Backend %s'), $backendName));
$form->setIniConfig($this->Config('backends'));
$form->setResourceConfig(ResourceFactory::getResourceConfigs());
$form->setRedirectUrl('monitoring/config');
$form->handleRequest();
$form->setOnSuccess(function (BackendConfigForm $form) use ($backendName) {
try {
$form->edit($backendName, array_map(
function ($v) {
return $v !== '' ? $v : null;
},
$form->getValues()
));
} catch (Exception $e) {
$form->error($e->getMessage());
return false;
}

if ($form->save()) {
Notification::success(sprintf(t('Monitoring backend "%s" successfully updated'), $backendName));
return true;
}

return false;
});

try {
$form->load($backendName);
$form->handleRequest();
} catch (NotFoundError $_) {
$this->httpNotFound(sprintf($this->translate('Monitoring backend "%s" not found'), $backendName));
}

$this->view->form = $form;
$this->render('form');
}

/**
* Display a form to create a new backend
* Create a new monitoring backend
*/
public function createbackendAction()
{
$form = new BackendConfigForm();
$form->setTitle($this->translate('Add New Backend'));
$form->setIniConfig($this->Config('backends'));
$form->setResourceConfig(ResourceFactory::getResourceConfigs());
$form->setRedirectUrl('monitoring/config');
$form->setTitle($this->translate('Create New Monitoring Backend'));
$form->setIniConfig($this->Config('backends'));

try {
$form->setResourceConfig(ResourceFactory::getResourceConfigs());
} catch (ConfigurationError $e) {
if ($this->hasPermission('config/application/resources')) {
Notification::error($e->getMessage());
$this->redirectNow('config/createresource');
}

throw $e; // No permission for resource configuration, show the error
}

$form->setOnSuccess(function (BackendConfigForm $form) {
try {
$form->add(array_filter($form->getValues()));
} catch (Exception $e) {
$form->error($e->getMessage());
return false;
}

if ($form->save()) {
Notification::success(t('Monitoring backend successfully created'));
return true;
}

return false;
});
$form->handleRequest();

$this->view->form = $form;
$this->render('form');
}

/**
* Display a confirmation form to remove the backend identified by the 'backend' parameter
*/
public function removebackendAction()
{
$config = $this->Config('backends');
$form = new ConfirmRemovalForm(array(
'onSuccess' => function ($form) use ($config) {
$backendName = $form->getRequest()->getQuery('backend');
$configForm = new BackendConfigForm();
$configForm->setIniConfig($config);

try {
$configForm->remove($backendName);
} catch (InvalidArgumentException $e) {
Notification::error($e->getMessage());
return;
}

if ($configForm->save()) {
Notification::success(sprintf(
$this->translate('Backend "%s" successfully removed.'),
$backendName
));
} else {
return false;
}
}
));
$form->setTitle($this->translate('Remove Existing Backend'));
$backendName = $this->params->getRequired('backend');

$backendForm = new BackendConfigForm();
$backendForm->setIniConfig($this->Config('backends'));
$form = new ConfirmRemovalForm();
$form->setRedirectUrl('monitoring/config');
$form->setTitle(sprintf($this->translate('Remove Monitoring Backend %s'), $backendName));
$form->setOnSuccess(function (ConfirmRemovalForm $form) use ($backendName, $backendForm) {
try {
$backendForm->delete($backendName);
} catch (Exception $e) {
$form->error($e->getMessage());
return false;
}

if ($backendForm->save()) {
Notification::success(sprintf(t('Monitoring backend "%s" successfully removed'), $backendName));
return true;
}

return false;
});
$form->handleRequest();

$this->view->form = $form;
$this->render('form');
}

/**
Expand Down Expand Up @@ -211,5 +265,6 @@ public function securityAction()

$this->view->form = $form;
$this->view->tabs = $this->Module()->getConfigTabs()->activate('security');
$this->render('form');
}
}

0 comments on commit db20f2d

Please sign in to comment.