From b743cf814319d2db301a028b41cf9557bc93e26b Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Fri, 25 Jul 2014 14:55:36 +0200 Subject: [PATCH] Adjust editbackend-action to suit the new backend-form interface refs #5525 --- .../controllers/ConfigController.php | 54 ++++++++++++------- .../views/scripts/config/editbackend.phtml | 15 +----- 2 files changed, 36 insertions(+), 33 deletions(-) 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