diff --git a/application/controllers/ConfigController.php b/application/controllers/ConfigController.php index f2c1b5347..4e6bc9bd7 100644 --- a/application/controllers/ConfigController.php +++ b/application/controllers/ConfigController.php @@ -406,41 +406,44 @@ class ConfigController extends BaseConfigController $this->render('resource/create'); } + /** + * Display a form to edit a existing resource + */ public function editresourceAction() { $this->view->messageBox = new AlertMessageBox(true); - $resources = ResourceFactory::getResourceConfigs(); - $name = $this->getParam('resource'); - if ($resources->get($name) === null) { - $this->addErrorMessage('Can\'t edit: Unknown Resource Provided'); - $this->render('resource/modify'); - return; - } - $form = new ResourceForm(); - if ($this->_request->isPost() === false) { - $form->setOldName($name); - $form->setName($name); - } - $form->setRequest($this->_request); - $form->setResource($resources->get($name)); - if ($form->isSubmittedAndValid()) { - $oldName = $form->getOldName(); - $name = $form->getName(); - if ($oldName !== $name) { - unset($resources->{$oldName}); - } - $resources->{$name} = $form->getConfig(); - if ($this->writeConfigFile($resources, 'resources')) { - $this->addSuccessMessage('Resource "' . $name . '" edited.'); - $this->redirectNow("config/resource"); - } - return; + // Fetch the resource to be edited + $resources = IcingaConfig::app('resources')->toArray(); + $name = $this->getParam('resource'); + if (false === array_key_exists($name, $resources)) { + $this->addErrorMessage(sprintf($this->translate('Cannot edit "%s". Resource not found.'), $name)); + $this->redirectNow('config/configurationerror'); + } + + $form = new ResourceForm(); + $request = $this->getRequest(); + if ($request->isPost()) { + if ($form->isValid($request->getPost())) { + list($newName, $config) = $form->getResourceConfig(); + + if ($newName !== $name) { + // Resource name has changed + unset($resources[$name]); // We can safely use unset as all values are part of the form + } + + $resources[$newName] = $config; + if ($this->writeConfigFile($resources, 'resources')) { + $this->addSuccessMessage(sprintf($this->translate('Resource "%s" successfully edited.'), $name)); + $this->redirectNow('config/resource'); + } + } + } else { + $form->setResourceConfig($name, $resources[$name]); } - $this->view->messageBox->addForm($form); $this->view->form = $form; - $this->view->name = $name; + $this->view->messageBox->addForm($form); $this->render('resource/modify'); } diff --git a/application/views/scripts/config/resource/modify.phtml b/application/views/scripts/config/resource/modify.phtml index 5a0871aed..7f549d537 100644 --- a/application/views/scripts/config/resource/modify.phtml +++ b/application/views/scripts/config/resource/modify.phtml @@ -1,18 +1,3 @@ -