summaryrefslogtreecommitdiffstats
path: root/application/controllers/BranchController.php
blob: 3b36e83b5caac087b40c8d5cda203e416b0b9317 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?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;
use Icinga\Module\Director\Db\Branch\BranchStore;
use Icinga\Module\Director\IcingaConfig\IcingaConfig;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Objects\SyncRule;
use Icinga\Module\Director\PlainObjectRenderer;
use Icinga\Module\Director\Web\Controller\ActionController;
use Icinga\Module\Director\Web\Controller\BranchHelper;
use Icinga\Module\Director\Web\Widget\IcingaConfigDiff;
use ipl\Html\Html;

class BranchController extends ActionController
{
    use BranchHelper;

    public function init()
    {
        parent::init();
        IcingaObject::setDbObjectStore(new DbObjectStore($this->db(), $this->getBranch()));
        SyncRule::setDbObjectStore(new DbObjectStore($this->db(), $this->getBranch()));
    }

    protected function checkDirectorPermissions()
    {
    }

    public function activityAction()
    {
        $this->assertPermission('director/showconfig');
        $ts = $this->params->getRequired('ts');
        $activity = BranchActivity::load($ts, $this->db());
        $store = new BranchStore($this->db());
        $branch = $store->fetchBranchByUuid($activity->getBranchUuid());
        if ($branch->isSyncPreview()) {
            $this->addSingleTab($this->translate('Sync Preview'));
            $this->addTitle($this->translate('Expected Modification'));
        } else {
            $this->addSingleTab($this->translate('Activity'));
            $this->addTitle($this->translate('Branch Activity'));
        }

        $this->content()->add($this->prepareActivityInfo($activity));
        $this->showActivity($activity);
    }

    protected function prepareActivityInfo(BranchActivity $activity)
    {
        $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(),
            // $this->translate('Actions') => ['Undo form'],
        ]);
        return $table;
    }

    protected function leftFromActivity(BranchActivity $activity)
    {
        if ($activity->isActionCreate()) {
            return null;
        }
        $object = DbObjectTypeRegistry::newObject($activity->getObjectTable(), [], $this->db());
        $properties = $this->objectTypeFirst($activity->getFormerProperties()->jsonSerialize());
        foreach ($properties as $key => $value) {
            $object->set($key, $value);
        }

        return $object;
    }

    protected function rightFromActivity(BranchActivity $activity)
    {
        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);
            }
        }
        $properties = $this->objectTypeFirst($activity->getModifiedProperties()->jsonSerialize());
        foreach ($properties as $key => $value) {
            $object->set($key, $value);
        }

        return $object;
    }

    protected function objectTypeFirst($properties)
    {
        $properties = (array) $properties;
        if (isset($properties['object_type'])) {
            $type = $properties['object_type'];
            unset($properties['object_type']);
            $properties = ['object_type' => $type] + $properties;
        }

        return $properties;
    }

    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())
                ))
            ]);
        }
    }

    protected function createEmptyConfig()
    {
        return new IcingaConfig($this->db());
    }
}