summaryrefslogtreecommitdiffstats
path: root/library/Director/Web/Table/BranchActivityTable.php
blob: cbf940d995e0470bca3e7314f9262ee4b2c3ca60 (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
<?php

namespace Icinga\Module\Director\Web\Table;

use Icinga\Module\Director\Auth\Permission;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Db\Branch\BranchActivity;
use Icinga\Module\Director\Util;
use gipfl\IcingaWeb2\Link;
use Ramsey\Uuid\UuidInterface;

class BranchActivityTable extends IntlZfQueryBasedTable
{
    protected $extraParams = [];

    /** @var UuidInterface */
    protected $branchUuid;

    /** @var ?UuidInterface */
    protected $objectUuid;

    protected $linkToObject = true;

    public function __construct(UuidInterface $branchUuid, $db, UuidInterface $objectUuid = null)
    {
        $this->branchUuid = $branchUuid;
        $this->objectUuid = $objectUuid;
        parent::__construct($db);
    }

    public function assemble()
    {
        $this->getAttributes()->add('class', 'activity-log');
    }

    public function renderRow($row)
    {
        $ts = (int) floor(BranchActivity::fixFakeTimestamp($row->timestamp_ns) / 1000000);
        $this->splitByDay($ts);
        $activity = BranchActivity::fromDbRow($row);
        return $this::tr([
            $this::td($this->makeBranchLink($activity))->setSeparator(' '),
            $this::td($this->getTime($ts))
        ])->addAttributes(['class' => ['action-' . $activity->getAction(), 'branched']]);
    }

    public function disableObjectLink()
    {
        $this->linkToObject = false;
        return $this;
    }

    protected function linkObject(BranchActivity $activity)
    {
        if (! $this->linkToObject) {
            return $activity->getObjectName();
        }
        // $type, UuidInterface $uuid
        // Later on replacing, service_set -> serviceset
        $type = preg_replace('/^icinga_/', '', $activity->getObjectTable());
        return Link::create(
            $activity->getObjectName(),
            'director/' . str_replace('_', '', $type),
            ['uuid' => $activity->getObjectUuid()->toString()],
            ['title' => $this->translate('Jump to this object')]
        );
    }

    protected function makeBranchLink(BranchActivity $activity)
    {
        $type = preg_replace('/^icinga_/', '', $activity->getObjectTable());

        if (Util::hasPermission(Permission::SHOW_CONFIG)) {
            // Later on replacing, service_set -> serviceset
            return [
                '[' . $activity->getAuthor() . ']',
                Link::create(
                    $activity->getAction(),
                    'director/branch/activity',
                    array_merge(['ts' => $activity->getTimestampNs()], $this->extraParams),
                    ['title' => $this->translate('Show details related to this change')]
                ),
                str_replace('_', ' ', $type),
                $this->linkObject($activity)
            ];
        } else {
            return sprintf(
                '[%s] %s %s "%s"',
                $activity->getAuthor(),
                $activity->getAction(),
                $type,
                $activity->getObjectName()
            );
        }
    }

    public function prepareQuery()
    {
        /** @var Db $connection */
        $connection = $this->connection();
        $query = $this->db()->select()->from(['ba' => 'director_branch_activity'], 'ba.*')
            ->join(['b' => 'director_branch'], 'b.uuid = ba.branch_uuid', ['b.owner'])
            ->where('branch_uuid = ?', $connection->quoteBinary($this->branchUuid->getBytes()))
            ->order('timestamp_ns DESC');
        if ($this->objectUuid) {
            $query->where('ba.object_uuid = ?', $connection->quoteBinary($this->objectUuid->getBytes()));
        }

        return $query;
    }
}