Adjust editbackend-action to suit the new backend-form interface

refs #5525
This commit is contained in:
Johannes Meyer 2014-07-25 14:55:36 +02:00
parent 488ea4633d
commit b743cf8143
2 changed files with 36 additions and 33 deletions

View File

@ -3,12 +3,12 @@
// {{{ICINGA_LICENSE_HEADER}}}
use \Exception;
use \Zend_Config;
use Icinga\Config\PreservingIniWriter;
use Icinga\Web\Controller\ModuleActionController;
use Icinga\Web\Notification;
use Icinga\Module\Monitoring\Form\Config\BackendForm;
use Icinga\Module\Monitoring\Form\Config\ConfirmRemovalForm;
use Icinga\Module\Monitoring\Form\Config\Backend\EditBackendForm;
use Icinga\Module\Monitoring\Form\Config\Backend\CreateBackendForm;
use Icinga\Module\Monitoring\Form\Config\Instance\EditInstanceForm;
use Icinga\Module\Monitoring\Form\Config\Instance\CreateInstanceForm;
use Icinga\Exception\NotReadableError;
@ -43,29 +43,40 @@ class Monitoring_ConfigController extends ModuleActionController
*/
public function editbackendAction()
{
// Fetch the backend to be edited
$backend = $this->getParam('backend');
if (!$this->isExistingBackend($backend)) {
$this->view->error = 'Unknown backend ' . $backend;
return;
$backendsConfig = $this->Config('backends')->toArray();
if (false === array_key_exists($backend, $backendsConfig)) {
// TODO: Should behave as in the app's config controller (Specific redirect to an error action)
Notification::error(sprintf($this->translate('Cannot edit "%s". Backend not found.'), $backend));
$this->redirectNow('monitoring/config');
}
$backendForm = new EditBackendForm();
$backendForm->setRequest($this->getRequest());
$backendForm->setBackendConfiguration($this->Config('backends')->get($backend));
if ($backendForm->isSubmittedAndValid()) {
$newConfig = $backendForm->getConfig();
$config = $this->Config('backends');
$config->$backend = $newConfig;
if ($this->writeConfiguration($config, 'backends')) {
Notification::success('Backend ' . $backend . ' Modified.');
$this->redirectNow('monitoring/config');
} else {
$this->render('show-configuration');
return;
$form = new BackendForm();
$request = $this->getRequest();
if ($request->isPost()) {
if ($form->isValid($request->getPost())) {
list($newName, $config) = $form->getBackendConfig();
if ($newName !== $backend) {
// Backend name has changed
unset($backendsConfig[$backend]); // We can safely use unset as all values are part of the form
}
$backendsConfig[$newName] = $config;
if ($this->writeConfiguration($backendsConfig, 'backends')) {
Notification::success(sprintf($this->translate('Backend "%s" successfully modified.'), $backend));
$this->redirectNow('monitoring/config');
} else {
$this->render('show-configuration');
return;
}
}
} else {
$form->setBackendConfig($backend, $backendsConfig[$backend]);
}
$this->view->name = $backend;
$this->view->form = $backendForm;
$this->view->form = $form;
}
/**
@ -218,6 +229,9 @@ class Monitoring_ConfigController extends ModuleActionController
*/
protected function writeConfiguration($config, $file)
{
if (is_array($config)) {
$config = new Zend_Config($config);
}
$target = $this->Config($file)->getConfigFile();
$writer = new PreservingIniWriter(array('filename' => $target, 'config' => $config));

View File

@ -1,13 +1,2 @@
<?php if ($this->name): ?>
<h4><i> <i class="icinga-icon-edit"></i> Edit Backend "<?= $this->escape($this->name) ?>"</h4>
<?php else: ?>
<h4> <i class="icinga-icon-create"></i> Create New Backend</h4>
<?php endif; ?>
<?php if ($this->error): ?>
<div class="alert alert-danger">
<?= $this->escape($this->error); ?>
</div>
<?php endif; ?>
<?= $this->form; ?>
<h4><?= $this->translate('Edit Existing Backend'); ?></h4>
<?= $form; ?>