Adjust editresource-action to suit the new resource form interface

refs #5525
This commit is contained in:
Johannes Meyer 2014-07-24 12:37:33 +02:00
parent 9c434fe8bf
commit 0964316df4
2 changed files with 34 additions and 46 deletions

View File

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

View File

@ -1,18 +1,3 @@
<h4> <h4><?= $this->translate('Edit Existing Resource'); ?></h4>
<i class="icinga-icon-edit"></i> <?= $messageBox; ?>
Edit Resource "<?= $this->escape($this->name); ?>" <?= $form; ?>
</h4>
<?php if (isset($this->messageBox)): ?>
<?= $this->messageBox->render() ?>
<?php endif ?>
<?php if ($this->form->getErrorMessages()): ?>
<div>
<?php foreach ($this->form->getErrorMessages() as $error): ?>
<?= $this->escape($error); ?><br/>
<?php endforeach; ?>
</div>
<?php endif ?>
<?= $this->form ?>