icingaweb2-module-director/application/controllers/BranchController.php

114 lines
3.9 KiB
PHP
Raw Normal View History

2021-08-16 11:43:09 +02:00
<?php
namespace Icinga\Module\Director\Controllers;
use gipfl\Diff\HtmlRenderer\SideBySideDiff;
use gipfl\Diff\PhpDiff;
use gipfl\IcingaWeb2\Widget\NameValueTable;
use Icinga\Module\Director\Data\Db\DbObjectStore;
use Icinga\Module\Director\Data\Db\DbObjectTypeRegistry;
use Icinga\Module\Director\Db\Branch\BranchActivity;
2021-08-16 11:43:09 +02:00
use Icinga\Module\Director\IcingaConfig\IcingaConfig;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\PlainObjectRenderer;
2021-08-16 11:43:09 +02:00
use Icinga\Module\Director\Web\Controller\ActionController;
use Icinga\Module\Director\Web\Controller\BranchHelper;
use Icinga\Module\Director\Web\Widget\IcingaConfigDiff;
2021-08-16 11:43:09 +02:00
use ipl\Html\Html;
class BranchController extends ActionController
{
use BranchHelper;
public function init()
{
parent::init();
IcingaObject::setDbObjectStore(new DbObjectStore($this->db(), $this->getBranch()));
}
2021-08-16 11:43:09 +02:00
protected function checkDirectorPermissions()
{
}
public function activityAction()
{
$this->assertPermission('director/showconfig');
$ts = $this->params->getRequired('ts');
2021-08-16 11:43:09 +02:00
$this->addSingleTab($this->translate('Activity'));
$this->addTitle($this->translate('Branch Activity'));
$activity = BranchActivity::load($ts, $this->db());
2021-08-16 11:43:09 +02:00
$this->content()->add($this->prepareActivityInfo($activity));
$this->showActivity($activity);
}
protected function prepareActivityInfo(BranchActivity $activity)
2021-08-16 11:43:09 +02:00
{
$table = new NameValueTable();
$table->addNameValuePairs([
$this->translate('Author') => $activity->getAuthor(),
$this->translate('Date') => date('Y-m-d H:i:s', $activity->getTimestamp()),
$this->translate('Action') => $activity->getAction()
. ' ' . preg_replace('/^icinga_/', '', $activity->getObjectTable())
. ' ' . $activity->getObjectName(),
2021-08-16 11:43:09 +02:00
// $this->translate('Actions') => ['Undo form'],
]);
return $table;
}
protected function leftFromActivity(BranchActivity $activity)
2021-08-16 11:43:09 +02:00
{
if ($activity->isActionCreate()) {
return null;
2021-08-16 11:43:09 +02:00
}
$object = DbObjectTypeRegistry::newObject($activity->getObjectTable(), [], $this->db());
foreach ($activity->getFormerProperties()->jsonSerialize() as $key => $value) {
$object->set($key, $value);
2021-08-16 11:43:09 +02:00
}
return $object;
2021-08-16 11:43:09 +02:00
}
protected function rightFromActivity(BranchActivity $activity)
2021-08-16 11:43:09 +02:00
{
if ($activity->isActionDelete()) {
return null;
}
$object = DbObjectTypeRegistry::newObject($activity->getObjectTable(), [], $this->db());
if (! $activity->isActionCreate()) {
foreach ($activity->getFormerProperties()->jsonSerialize() as $key => $value) {
$object->set($key, $value);
2021-08-16 11:43:09 +02:00
}
}
foreach ($activity->getModifiedProperties()->jsonSerialize() as $key => $value) {
$object->set($key, $value);
}
2021-08-16 11:43:09 +02:00
return $object;
}
2021-08-16 11:43:09 +02:00
protected function showActivity(BranchActivity $activity)
{
$left = $this->leftFromActivity($activity);
$right = $this->rightFromActivity($activity);
if ($left instanceof IcingaObject || $right instanceof IcingaObject) {
$this->content()->add(new IcingaConfigDiff(
$left ? $left->toSingleIcingaConfig() : $this->createEmptyConfig(),
$right ? $right->toSingleIcingaConfig() : $this->createEmptyConfig()
));
} else {
$this->content()->add([
Html::tag('h3', $this->translate('Modification')),
new SideBySideDiff(new PhpDiff(
PlainObjectRenderer::render($left->getProperties()),
PlainObjectRenderer::render($right->getProperties())
))
]);
2021-08-16 11:43:09 +02:00
}
}
protected function createEmptyConfig()
2021-08-16 11:43:09 +02:00
{
return new IcingaConfig($this->db());
2021-08-16 11:43:09 +02:00
}
}