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

namespace Icinga\Module\Director\Web\Table;

use gipfl\IcingaWeb2\Link;
use gipfl\IcingaWeb2\Table\ZfQueryBasedTable;
use Zend_Db_Adapter_Abstract as ZfDbAdapter;
use Zend_Db_Select as ZfDbSelect;

class DatafieldTable extends ZfQueryBasedTable
{
    protected $searchColumns = [
        'df.varname',
        'df.caption',
    ];

    public function getColumns()
    {
        return [
            'id'              => 'df.id',
            'varname'         => 'df.varname',
            'caption'         => 'df.caption',
            'description'     => 'df.description',
            'datatype'        => 'df.datatype',
            'category'        => 'dfc.category_name',
            'assigned_fields' => 'SUM(used_fields.cnt)',
            'assigned_vars'   => 'SUM(used_vars.cnt)',
        ];
    }

    public function renderRow($row)
    {
        return $this::tr([
            $this::td(Link::create(
                $row->caption,
                'director/datafield/edit',
                ['id' => $row->id]
            )),
            $this::td($row->varname),
            $this::td($row->category),
            $this::td($row->assigned_fields),
            $this::td($row->assigned_vars)
        ]);
    }

    public function getColumnsToBeRendered()
    {
        return [
            $this->translate('Label'),
            $this->translate('Field name'),
            $this->translate('Category'),
            $this->translate('# Used'),
            $this->translate('# Vars'),
        ];
    }

    public function prepareQuery()
    {
        $db = $this->db();
        $fieldTypes = ['command', 'host', 'notification', 'service', 'user'];
        $varsTypes  = ['command', 'host', 'notification', 'service', 'service_set', 'user'];

        $fieldsQueries = [];
        foreach ($fieldTypes as $type) {
            $fieldsQueries[] = $this->makeDatafieldSub($type, $db);
        }

        $varsQueries = [];
        foreach ($varsTypes as $type) {
            $varsQueries[] = $this->makeVarSub($type, $db);
        }

        return $db->select()->from(
            ['df' => 'director_datafield'],
            $this->getColumns()
        )->joinLeft(
            ['dfc' => 'director_datafield_category'],
            'df.category_id = dfc.id',
            []
        )->joinLeft(
            ['used_fields' => $db->select()->union($fieldsQueries, ZfDbSelect::SQL_UNION_ALL)],
            'used_fields.datafield_id = df.id',
            []
        )->joinLeft(
            ['used_vars' => $db->select()->union($varsQueries, ZfDbSelect::SQL_UNION_ALL)],
            'used_vars.varname = df.varname',
            []
        )->group('df.id')->group('df.varname')->group('dfc.category_name')->order('caption ASC');
    }

    /**
     * @param $type
     * @param ZfDbAdapter $db
     *
     * @return ZfDbSelect
     */
    protected function makeDatafieldSub($type, ZfDbAdapter $db)
    {
        return $db->select()->from("icinga_${type}_field", [
            'cnt' => 'COUNT(*)',
            'datafield_id'
        ])->group('datafield_id');
    }

    /**
     * @param $type
     * @param ZfDbAdapter $db
     *
     * @return ZfDbSelect
     */
    protected function makeVarSub($type, ZfDbAdapter $db)
    {
        return $db->select()->from("icinga_${type}_var", [
            'cnt' => 'COUNT(*)',
            'varname'
        ])->group('varname');
    }
}