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

namespace Icinga\Module\Director\Web\Table;

use Icinga\Module\Director\Db;
use Icinga\Module\Director\IcingaConfig\IcingaConfig;
use gipfl\IcingaWeb2\Link;
use gipfl\IcingaWeb2\Table\ZfQueryBasedTable;

class GeneratedConfigFileTable extends ZfQueryBasedTable
{
    use DbHelper;

    protected $searchColumns = ['file_path'];

    protected $deploymentId;

    protected $activeFile;

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

    public static function load(IcingaConfig $config, Db $db)
    {
        $table = new static($db);
        $table->config = $config;
        $table->getAttributes()->set('data-base-target', '_self');
        return $table;
    }

    public function renderRow($row)
    {
        $counts = implode(' / ', [
            $row->cnt_object,
            $row->cnt_template,
            $row->cnt_apply
        ]);

        $tr = $this::row([
            $this->getFileLink($row),
            $counts,
            $row->size
        ]);

        if ($row->file_path === $this->activeFile) {
            $tr->getAttributes()->add('class', 'active');
        }

        return $tr;
    }

    public function setActiveFilename($filename)
    {
        $this->activeFile = $filename;
        return $this;
    }

    protected function getFileLink($row)
    {
        $params = [
            'config_checksum' => $row->config_checksum,
            'file_path'       => $row->file_path
        ];

        if ($this->deploymentId) {
            $params['deployment_id'] = $this->deploymentId;
        }

        return Link::create($row->file_path, 'director/config/file', $params);
    }

    public function setDeploymentId($id)
    {
        if ($id) {
            $this->deploymentId = (int) $id;
        }

        return $this;
    }

    public function getColumnsToBeRendered()
    {
        return [
            $this->translate('File'),
            $this->translate('Object/Tpl/Apply'),
            $this->translate('Size'),
        ];
    }

    public function prepareQuery()
    {
        $columns = [
            'file_path'       => 'cf.file_path',
            'size'            => 'LENGTH(f.content)',
            'cnt_object'      => 'f.cnt_object',
            'cnt_template'    => 'f.cnt_template',
            'cnt_apply'       => 'f.cnt_apply',
            'cnt_all'         => "f.cnt_object || ' / ' || f.cnt_template || ' / ' || f.cnt_apply",
            'checksum'        => 'LOWER(HEX(f.checksum))',
            'config_checksum' => 'LOWER(HEX(cf.config_checksum))',
        ];

        if ($this->isPgsql()) {
            $columns['checksum'] = "LOWER(ENCODE(f.checksum, 'hex'))";
            $columns['config_checksum'] = "LOWER(ENCODE(cf.config_checksum, 'hex'))";
        }

        return $this->db()->select()->from(
            ['cf' => 'director_generated_config_file'],
            $columns
        )->join(
            ['f' => 'director_generated_file'],
            'cf.file_checksum = f.checksum',
            []
        )->where(
            'config_checksum = ?',
            $this->quoteBinary($this->config->getChecksum())
        )->order('cf.file_path ASC');
    }
}