Activity log: add tabs, reorganize output

This commit is contained in:
Thomas Gelf 2015-11-06 09:07:37 +01:00
parent 2706af6976
commit 2fb0e7c9c5
5 changed files with 125 additions and 75 deletions

View File

@ -9,6 +9,125 @@ use Icinga\Module\Director\Objects\IcingaObject;
class ShowController extends ActionController
{
protected $defaultTab;
protected function activityTabs($entry)
{
$tabs = $this->getTabs();
if ($entry->action_name === 'modify') {
$tabs->add('diff', array(
'label' => $this->translate('Diff'),
'url' => 'director/show/activitylog',
'urlParams' => array('id' => $entry->id)
));
$this->defaultTab = 'diff';
}
if (in_array($entry->action_name, array('create', 'modify'))) {
$tabs->add('new', array(
'label' => $this->translate('New object'),
'url' => 'director/show/activitylog',
'urlParams' => array('id' => $entry->id, 'show' => 'new')
));
if ($this->defaultTab === null) {
$this->defaultTab = 'diff';
}
}
if (in_array($entry->action_name, array('create', 'modify'))) {
$tabs->add('old', array(
'label' => $this->translate('Former object'),
'url' => 'director/show/activitylog',
'urlParams' => array('id' => $entry->id, 'show' => 'old')
));
if ($this->defaultTab === null) {
$this->defaultTab = 'diff';
}
}
return $tabs;
}
protected function showDiff($entry)
{
$this->view->title = sprintf('%s config diff', $entry->object_name);
$this->getTabs()->activate('diff');
$d = ConfigDiff::create(
$this->oldObject($entry),
$this->newObject($entry)
);
$this->view->output = $d->renderHtml();
}
protected function showOld($entry)
{
$this->view->title = sprintf('%s former config', $entry->object_name);
$this->getTabs()->activate('old');
$this->showObject($this->oldObject($entry));
}
protected function showNew($entry)
{
$this->view->title = sprintf('%s new config', $entry->object_name);
$this->getTabs()->activate('new');
$this->showObject($this->newObject($entry));
}
protected function oldObject($entry)
{
return $this->createObject($entry->object_type, $entry->old_properties);
}
protected function newObject($entry)
{
return $this->createObject(
$entry->object_type,
$entry->old_properties
)->setProperties((array) json_decode($entry->new_properties));
}
protected function showObject($object)
{
$this->view->output = '<pre>' . $this->view->escape(
(string) $object
) . '</pre>';
}
protected function showInfo($entry)
{
$typeName = $this->translate(
ucfirst(preg_replace('/^icinga_/', '', $entry->object_type)) // really?
);
switch ($entry->action_name) {
case 'create':
$this->view->title = sprintf(
$this->translate('%s "%s" has been created'),
$typeName,
$entry->object_name
);
break;
case 'delete':
$this->view->title = sprintf(
$this->translate('%s "%s" has been deleted'),
$typeName,
$entry->object_name
);
break;
case 'modify':
$this->view->title = sprintf(
$this->translate('%s "%s" has been modified'),
$typeName,
$entry->object_name
);
break;
}
}
public function activitylogAction()
{
if ($id = $this->params->get('id')) {
@ -18,32 +137,10 @@ class ShowController extends ActionController
}
$entry = $this->view->entry;
if ($entry->old_properties) {
$old = $this->createObject($entry->object_type, $entry->old_properties);
if ($entry->new_properties) {
$new = $this->createObject($entry->object_type, $entry->old_properties);
$new->setProperties((array) json_decode($entry->new_properties));
} else {
$new = null;
}
} else {
$old = null;
$new = $this->createObject($entry->object_type, $entry->new_properties);
}
if ($old && $new) {
$d = ConfigDiff::create($old, $new);
$this->view->diff = strtr(
$d->renderHtml(),
array('\\n' => "\\n\n")
);
}
$this->view->entry = $entry;
$this->view->newObject = $new;
$this->view->oldObject = $old;
$this->view->title = $this->translate('Activity');
$this->activityTabs($entry);
$this->showInfo($entry);
$func = 'show' . ucfirst($this->params->get('show', $this->defaultTab));
$this->$func($entry);
}
protected function createObject($type, $props)

View File

@ -1,8 +0,0 @@
<h2><?= sprintf(
'%s "%s" has been created',
ucfirst(preg_replace('/^icinga_/', '', $entry->object_type)),
$entry->object_name
) ?></h2>
<pre><?= $this->escape((string) $this->newObject) ?></pre>

View File

@ -1,8 +0,0 @@
<h3><?= sprintf(
'%s "%s" has been deleted',
ucfirst(preg_replace('/^icinga_/', '', $entry->object_type)),
$entry->object_name
) ?></h3>
<pre>
<?= (string) $this->oldObject ?>
</pre>

View File

@ -1,16 +0,0 @@
<h2><?= sprintf(
'%s "%s" has been modified',
ucfirst(preg_replace('/^icinga_/', '', $entry->object_type)),
$entry->object_name
) ?></h2>
<table class="log-properties">
<tr><td style="vertical-align: top; width: 50%">
<h3><?= $this->translate('Former state') ?></h3>
<span class="diff old"><?= $this->diff ?></pre>
</td><td style="vertical-align: top; width: 50%">
<h3><?= $this->translate('New object') ?></h3>
<span class="diff new"><?= $this->diff ?></pre>
</td></tr>
</table>

View File

@ -1,23 +1,8 @@
<div class="controls">
<?= $this->tabs ?>
<h1><?= $this->escape($this->title) ?></h1>
</div>
<div class="content">
<?php
switch ($entry->action_name) {
case 'create':
echo $this->render('show/activitylog-create.phtml');
break;
case 'modify':
echo $this->render('show/activitylog-modify.phtml');
break;
case 'delete':
echo $this->render('show/activitylog-delete.phtml');
break;
default:
echo 'Invalid action';
}
?>
<?= $this->output ?>
</div>