diff --git a/modules/monitoring/application/controllers/ConfigController.php b/modules/monitoring/application/controllers/ConfigController.php index 9e74943f3..2e01709e6 100644 --- a/modules/monitoring/application/controllers/ConfigController.php +++ b/modules/monitoring/application/controllers/ConfigController.php @@ -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)); diff --git a/modules/monitoring/application/views/scripts/config/editbackend.phtml b/modules/monitoring/application/views/scripts/config/editbackend.phtml index 6f2f19cbb..58b6c18f1 100644 --- a/modules/monitoring/application/views/scripts/config/editbackend.phtml +++ b/modules/monitoring/application/views/scripts/config/editbackend.phtml @@ -1,13 +1,2 @@ -name): ?> -

Edit Backend "escape($this->name) ?>"

- -

Create New Backend

- - -error): ?> -
- escape($this->error); ?> -
- - -form; ?> \ No newline at end of file +

translate('Edit Existing Backend'); ?>

+ \ No newline at end of file