show/activitylog: add restore action

This commit is contained in:
Thomas Gelf 2016-03-16 16:02:17 +01:00
parent d605bd7b06
commit b94c6bf481
3 changed files with 81 additions and 1 deletions

View File

@ -11,6 +11,8 @@ class ShowController extends ActionController
{
protected $defaultTab;
protected $oldObject;
protected function activityTabs($entry)
{
$tabs = $this->getTabs();
@ -87,7 +89,14 @@ class ShowController extends ActionController
protected function oldObject($entry)
{
return $this->createObject($entry->object_type, $entry->old_properties);
if ($this->oldObject === null) {
$this->oldObject = $this->createObject(
$entry->object_type,
$entry->old_properties
);
}
return $this->oldObject;
}
protected function newObject($entry)
@ -167,6 +176,15 @@ class ShowController extends ActionController
);
$entry = $v->entry;
if ($entry->old_properties) {
$this->view->form = $this
->loadForm('restoreObject')
->setDb($this->db())
->setObject($this->oldObject($entry))
->handleRequest();
}
$this->activityTabs($entry);
$this->showInfo($entry);
$func = 'show' . ucfirst($this->params->get('show', $this->defaultTab));

View File

@ -0,0 +1,56 @@
<?php
// TODO: Check whether this can be removed
namespace Icinga\Module\Director\Forms;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Web\Form\QuickForm;
class RestoreObjectForm extends QuickForm
{
protected $db;
protected $object;
public function setup()
{
$this->submitLabel = $this->translate('Restore former object');
}
protected function addSubmitButtonIfSet()
{
$res = parent::addSubmitButtonIfSet();
$this->getDisplayGroup('buttons')->setDecorators(array('FormElements'));
return $res;
}
public function onSuccess()
{
$object = $this->object;
$name = $object->object_name;
$db = $this->db;
$msg = $this->translate('Object has been restored');
// TODO: service -> multi-key
if ($object::exists($name, $db)) {
$object::load($name, $db)->replaceWith($object)->store();
} else {
$object->store($db);
}
$this->redirectOnSuccess($msg);
}
public function setDb($db)
{
$this->db = $db;
return $this;
}
public function setObject(IcingaObject $object)
{
$this->object = $object;
return $this;
}
}

View File

@ -62,6 +62,12 @@ $url = $this->url()->without('checksum')->without('show');
<th><?= $this->translate('Checksum') ?></th>
<td><?= $this->entry->checksum ?></td>
</tr>
<?php if ($this->form): ?>
<tr>
<th><?= $this->translate('Actions') ?></th>
<td><?= $this->form ?></td>
</tr>
<?php endif ?>
</table>
<?php if (! empty($this->output)): ?>
<h1><?= $this->escape($this->title) ?></h1>