summaryrefslogtreecommitdiffstats
path: root/library/Director/Web/Widget/DeployedConfigInfoHeader.php
blob: 0e841f3ee4cc9895ce961a1dfb74af3937b20b0b (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
<?php

namespace Icinga\Module\Director\Web\Widget;

use Icinga\Module\Director\Db\Branch\Branch;
use ipl\Html\HtmlDocument;
use Icinga\Module\Director\Core\DeploymentApiInterface;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Forms\DeployConfigForm;
use Icinga\Module\Director\IcingaConfig\IcingaConfig;
use ipl\Html\Html;
use gipfl\IcingaWeb2\Link;
use gipfl\Translation\TranslationHelper;
use gipfl\IcingaWeb2\Widget\NameValueTable;

class DeployedConfigInfoHeader extends HtmlDocument
{
    use TranslationHelper;

    /** @var IcingaConfig */
    protected $config;

    /** @var int */
    protected $deploymentId;

    /** @var Db */
    protected $db;

    /** @var DeploymentApiInterface */
    protected $api;

    /** @var Branch */
    protected $branch;

    public function __construct(
        IcingaConfig $config,
        Db $db,
        DeploymentApiInterface $api,
        Branch $branch,
        $deploymentId = null
    ) {
        $this->config = $config;
        $this->db     = $db;
        $this->api    = $api;
        $this->branch = $branch;
        if ($deploymentId) {
            $this->deploymentId = (int) $deploymentId;
        }
    }

    /**
     * @throws \Icinga\Exception\IcingaException
     * @throws \Zend_Form_Exception
     */
    protected function assemble()
    {
        $config = $this->config;
        if ($this->branch->isBranch()) {
            $deployForm = null;
        } else {
            $deployForm = DeployConfigForm::load()
                ->setDb($this->db)
                ->setApi($this->api)
                ->setChecksum($config->getHexChecksum())
                ->setDeploymentId($this->deploymentId)
                ->setAttrib('class', 'inline')
                ->handleRequest();
        }

        $links = new NameValueTable();
        $links->addNameValueRow(
            $this->translate('Actions'),
            [
                $deployForm,
                Html::tag('br'),
                Link::create(
                    $this->translate('Last related activity'),
                    'director/config/activity',
                    ['checksum' => $config->getLastActivityHexChecksum()],
                    ['class' => 'icon-clock', 'data-base-target' => '_next']
                ),
                Html::tag('br'),
                Link::create(
                    $this->translate('Diff with other config'),
                    'director/config/diff',
                    ['left' => $config->getHexChecksum()],
                    ['class' => 'icon-flapping', 'data-base-target' => '_self']
                )
            ]
        )->addNameValueRow(
            $this->translate('Statistics'),
            sprintf(
                $this->translate('%d files rendered in %0.2fs'),
                count($config->getFiles()),
                $config->getDuration() / 1000
            )
        );

        $this->add($links);
    }
}