mirror of
https://github.com/Icinga/icingaweb2-module-director.git
synced 2025-09-26 03:09:11 +02:00
ActivityLog: refactor code, fix HTML, use ipl
This commit is contained in:
parent
3f03fe8555
commit
f44afef214
@ -2,249 +2,33 @@
|
||||
|
||||
namespace Icinga\Module\Director\Controllers;
|
||||
|
||||
use Icinga\Module\Director\ConfigDiff;
|
||||
use Icinga\Module\Director\IcingaConfig\IcingaConfig;
|
||||
use Icinga\Module\Director\Web\Controller\ActionController;
|
||||
use Icinga\Module\Director\Util;
|
||||
use Icinga\Module\Director\Objects\IcingaObject;
|
||||
use Exception;
|
||||
use Icinga\Module\Director\Web\Widget\ActivityLogInfo;
|
||||
|
||||
class ShowController extends ActionController
|
||||
{
|
||||
protected $defaultTab;
|
||||
|
||||
protected $oldObject;
|
||||
|
||||
protected function checkDirectorPermissions()
|
||||
{
|
||||
$this->assertPermission('director/showconfig');
|
||||
}
|
||||
|
||||
protected function objectKey($entry)
|
||||
{
|
||||
if ($entry->object_type === 'icinga_service' || $entry->object_type === 'icinga_service_set') {
|
||||
// TODO: this is not correct. Activity needs to get (multi) key support
|
||||
return array('name' => $entry->object_name);
|
||||
}
|
||||
|
||||
return $entry->object_name;
|
||||
}
|
||||
|
||||
protected function activityTabs($entry)
|
||||
{
|
||||
$db = $this->db();
|
||||
|
||||
if (IcingaObject::existsByType($entry->object_type, $this->objectKey($entry), $db)) {
|
||||
$this->view->currentObject = IcingaObject::loadByType(
|
||||
$entry->object_type,
|
||||
$entry->object_name,
|
||||
$db
|
||||
);
|
||||
}
|
||||
|
||||
$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 = 'new';
|
||||
}
|
||||
}
|
||||
|
||||
if (in_array($entry->action_name, array('delete', '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 = 'old';
|
||||
}
|
||||
}
|
||||
|
||||
return $tabs;
|
||||
}
|
||||
|
||||
protected function newConfig($entry)
|
||||
{
|
||||
return $this->newObject($entry)->toSingleIcingaConfig();
|
||||
}
|
||||
|
||||
protected function oldConfig($entry)
|
||||
{
|
||||
return $this->oldObject($entry)->toSingleIcingaConfig();
|
||||
}
|
||||
|
||||
protected function showDiff($entry)
|
||||
{
|
||||
$this->view->title = sprintf('%s config diff', $entry->object_name);
|
||||
$this->getTabs()->activate('diff');
|
||||
|
||||
$oldConfig = $this->oldConfig($entry);
|
||||
$newConfig = $this->newConfig($entry);
|
||||
$this->showConfigDiff($oldConfig, $newConfig);
|
||||
}
|
||||
|
||||
protected function showConfigDiff(IcingaConfig $oldConfig, IcingaConfig $newConfig)
|
||||
{
|
||||
$oldFilenames = $oldConfig->getFileNames();
|
||||
$newFilenames = $newConfig->getFileNames();
|
||||
|
||||
$fileNames = array_merge($oldFilenames, $newFilenames);
|
||||
|
||||
$this->view->diffs = array();
|
||||
foreach ($fileNames as $filename) {
|
||||
if (in_array($filename, $oldFilenames)) {
|
||||
$left = $oldConfig->getFile($filename)->getContent();
|
||||
} else {
|
||||
$left = '';
|
||||
}
|
||||
|
||||
if (in_array($filename, $newFilenames)) {
|
||||
$right = $newConfig->getFile($filename)->getContent();
|
||||
} else {
|
||||
$right = '';
|
||||
}
|
||||
if ($left === $right) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$d = ConfigDiff::create($left, $right);
|
||||
|
||||
$this->view->diffs[$filename] = $d->renderHtml();
|
||||
}
|
||||
}
|
||||
|
||||
protected function showOld($entry)
|
||||
{
|
||||
$this->view->title = sprintf('%s former config', $entry->object_name);
|
||||
$this->getTabs()->activate('old');
|
||||
$this->showConfigDiff($this->oldConfig($entry), new IcingaConfig($this->db()));
|
||||
}
|
||||
|
||||
protected function showNew($entry)
|
||||
{
|
||||
$this->view->title = sprintf('%s new config', $entry->object_name);
|
||||
$this->getTabs()->activate('new');
|
||||
$this->showConfigDiff(new IcingaConfig($this->db()), $this->newConfig($entry));
|
||||
}
|
||||
|
||||
protected function oldObject($entry)
|
||||
{
|
||||
if ($this->oldObject === null) {
|
||||
$this->oldObject = $this->createObject(
|
||||
$entry->object_type,
|
||||
$entry->old_properties
|
||||
);
|
||||
}
|
||||
|
||||
return $this->oldObject;
|
||||
}
|
||||
|
||||
protected function newObject($entry)
|
||||
{
|
||||
return $this->createObject(
|
||||
$entry->object_type,
|
||||
$entry->new_properties
|
||||
);
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
$v = $this->view;
|
||||
|
||||
$v->object_type = $this->params->get('type');
|
||||
$v->object_name = $this->params->get('name');
|
||||
|
||||
if ($id = $this->params->get('id')) {
|
||||
$v->entry = $this->db()->fetchActivityLogEntryById($id);
|
||||
} elseif ($checksum = $this->params->get('checksum')) {
|
||||
$v->entry = $this->db()->fetchActivityLogEntry($checksum);
|
||||
$id = $v->entry->id;
|
||||
}
|
||||
|
||||
$v->neighbors = $this->db()->getActivitylogNeighbors(
|
||||
$id,
|
||||
$v->object_type,
|
||||
$v->object_name
|
||||
$p = $this->params;
|
||||
$info = new ActivityLogInfo(
|
||||
$this->db(),
|
||||
$p->get('type'),
|
||||
$p->get('name')
|
||||
);
|
||||
|
||||
$entry = $v->entry;
|
||||
$info->setChecksum($p->get('checksum'))
|
||||
->setId($p->get('id'));
|
||||
|
||||
if ($entry->old_properties) {
|
||||
$this->view->form = $this
|
||||
->loadForm('restoreObject')
|
||||
->setDb($this->db())
|
||||
->setObject($this->oldObject($entry))
|
||||
->handleRequest();
|
||||
}
|
||||
$this->tabs($info->getTabs());
|
||||
$info->showTab($this->params->get('show'));
|
||||
|
||||
$this->activityTabs($entry);
|
||||
$this->showInfo($entry);
|
||||
$func = 'show' . ucfirst($this->params->get('show', $this->defaultTab));
|
||||
$this->$func($entry);
|
||||
}
|
||||
|
||||
protected function createObject($type, $props)
|
||||
{
|
||||
$props = json_decode($props);
|
||||
$newProps = array(
|
||||
'object_name' => $props->object_name
|
||||
);
|
||||
if (property_exists($props, 'object_type')) {
|
||||
$newProps['object_type'] = $props->object_type;
|
||||
}
|
||||
|
||||
return IcingaObject::createByType(
|
||||
$type,
|
||||
$newProps,
|
||||
$this->db()
|
||||
)->setProperties((array) $props);
|
||||
$this->addTitle($info->getTitle());
|
||||
$this->controls()->prepend($info->getPagination($this->url()));
|
||||
$this->content()->add($info);
|
||||
}
|
||||
}
|
||||
|
@ -1,30 +1,18 @@
|
||||
<?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;
|
||||
use Icinga\Module\Director\Web\Form\DirectorForm;
|
||||
|
||||
class RestoreObjectForm extends QuickForm
|
||||
class RestoreObjectForm extends DirectorForm
|
||||
{
|
||||
/** @var Db */
|
||||
protected $db;
|
||||
|
||||
/** @var IcingaObject */
|
||||
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;
|
||||
$this->addSubmitButton($this->translate('Restore former object'));
|
||||
}
|
||||
|
||||
public function onSuccess()
|
||||
@ -32,24 +20,27 @@ class RestoreObjectForm extends QuickForm
|
||||
$object = $this->object;
|
||||
$name = $object->getObjectName();
|
||||
$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();
|
||||
$existing = $object::load($name, $db)->replaceWith($object);
|
||||
|
||||
if ($existing->hasBeenModified()) {
|
||||
$msg = $this->translate('Object has been restored');
|
||||
$existing->store();
|
||||
} else {
|
||||
$msg = $this->translate(
|
||||
'Nothing to do, restore would not modify the current object'
|
||||
);
|
||||
}
|
||||
} else {
|
||||
$msg = $this->translate('Object has been re-created');
|
||||
$object->store($db);
|
||||
}
|
||||
|
||||
$this->redirectOnSuccess($msg);
|
||||
}
|
||||
|
||||
public function setDb($db)
|
||||
{
|
||||
$this->db = $db;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setObject(IcingaObject $object)
|
||||
{
|
||||
$this->object = $object;
|
||||
|
@ -1,90 +0,0 @@
|
||||
<div class="controls">
|
||||
<?= $this->tabs ?>
|
||||
<?php
|
||||
$url = $this->url()->without('checksum')->without('show');
|
||||
?>
|
||||
|
||||
<div class="pagination-control" style="float: right; width: 5em;">
|
||||
<ul class="nav tab-nav">
|
||||
<li class="nav-item"><?php if ($this->neighbors->prev): ?>
|
||||
<a href="<?= $url->with('id', $this->neighbors->prev) ?>"><?=
|
||||
$this->icon('angle-double-left')
|
||||
?></a>
|
||||
<?php else: ?>
|
||||
<span class="disabled"><?= $this->icon('angle-double-left') ?></span>
|
||||
<?php endif ?>
|
||||
</li><li>
|
||||
<?php if ($this->neighbors->next): ?>
|
||||
<a href="<?= $url->with('id', $this->neighbors->next) ?>"><?=
|
||||
$this->icon('angle-double-right')
|
||||
?></a>
|
||||
<?php else: ?>
|
||||
<span class="disabled"><?= $this->icon('angle-double-right') ?></span>
|
||||
<?php endif ?>
|
||||
</li>
|
||||
</div>
|
||||
|
||||
<h1><?= $this->translate('Activity log entry') ?></h1>
|
||||
</div>
|
||||
|
||||
<div class="content">
|
||||
<table class="name-value-table">
|
||||
<tr>
|
||||
<th><?= $this->translate('Author') ?></th>
|
||||
<td><?= $this->escape($this->entry->author) ?></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th><?= $this->translate('Date') ?></th>
|
||||
<td><?= $this->entry->change_time ?></td>
|
||||
</tr>
|
||||
<?php if ($this->object_name): ?>
|
||||
<tr>
|
||||
<th><?= $this->translate('Action') ?></th>
|
||||
<td><?= $this->entry->action_name ?></td>
|
||||
</tr>
|
||||
<?php else: ?>
|
||||
<tr>
|
||||
<th><?= $this->translate('Action') ?></th>
|
||||
<td data-base-target="_next"><?= sprintf(
|
||||
'%s %s "%s"',
|
||||
$this->entry->action_name,
|
||||
$this->entry->object_type,
|
||||
$this->qlink(
|
||||
$this->entry->object_name,
|
||||
'director/' . preg_replace('/^icinga_/', '', $this->entry->object_type),
|
||||
array('name' => $this->entry->object_name)
|
||||
)
|
||||
)
|
||||
?></td>
|
||||
</tr>
|
||||
<?php endif ?>
|
||||
<?php if ($this->hasBeenEnabled): ?>
|
||||
<tr>
|
||||
<th><?= $this->translate('Rendering') ?></th>
|
||||
<td><?= $this->translate('This object has been enabled') ?></td>
|
||||
</tr>
|
||||
<?php endif ?>
|
||||
<?php if ($this->hasBeenDisabled): ?>
|
||||
<tr>
|
||||
<th><?= $this->translate('Rendering') ?></th>
|
||||
<td><?= $this->translate('This object has been disabled') ?></td>
|
||||
</tr>
|
||||
<?php endif ?>
|
||||
<tr>
|
||||
<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->diffs)): ?>
|
||||
<?php foreach ($this->diffs as $file => $diff): ?>
|
||||
<h3><?= $this->escape($file) ?></h3>
|
||||
<?= $diff ?>
|
||||
<?php endforeach ?>
|
||||
<?php endif ?>
|
||||
</div>
|
@ -7,9 +7,9 @@ use Diff_Renderer_Html_Inline;
|
||||
use Diff_Renderer_Html_SideBySide;
|
||||
use Diff_Renderer_Text_Context;
|
||||
use Diff_Renderer_Text_Unified;
|
||||
use Icinga\Application\Benchmark;
|
||||
use ipl\Html\ValidHtml;
|
||||
|
||||
class ConfigDiff
|
||||
class ConfigDiff implements ValidHtml
|
||||
{
|
||||
protected $a;
|
||||
|
||||
@ -42,6 +42,14 @@ class ConfigDiff
|
||||
$this->diff = new Diff($this->a, $this->b, $options);
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return $this->renderHtml();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function renderHtml()
|
||||
{
|
||||
return $this->renderHtmlSideBySide();
|
||||
@ -71,7 +79,7 @@ class ConfigDiff
|
||||
public function renderTextUnified()
|
||||
{
|
||||
require_once dirname(__DIR__) . '/vendor/php-diff/lib/Diff/Renderer/Text/Context.php';
|
||||
$renderer = new Diff_Renderer_Text_Context;
|
||||
$renderer = new Diff_Renderer_Text_Unified;
|
||||
return $this->diff->Render($renderer);
|
||||
}
|
||||
|
||||
|
@ -161,7 +161,7 @@ class IcingaConfig
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @return array
|
||||
*/
|
||||
public function getFileNames()
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user