summaryrefslogtreecommitdiffstats
path: root/modules/monitoring/library/Monitoring/Backend/Ido/Query/ContactQuery.php
blob: ca10323f1031e3ae82b9c693255a7519e0fc9fb6 (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
139
<?php
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Monitoring\Backend\Ido\Query;

use Icinga\Data\Filter\FilterExpression;
use Zend_Db_Select;
use Icinga\Data\Filter\Filter;

/**
 * Query for contacts
 */
class ContactQuery extends IdoQuery
{
    protected $columnMap = [
        'contacts' => [
            'contact_id'                        => 'c.contact_id',
            'contact'                           => 'c.contact',
            'contact_name'                      => 'c.contact_name',
            'contact_alias'                     => 'c.contact_alias',
            'contact_email'                     => 'c.contact_email',
            'contact_pager'                     => 'c.contact_pager',
            'contact_object_id'                 => 'c.contact_object_id',
            'contact_has_host_notfications'     => 'c.contact_has_host_notfications',
            'contact_has_service_notfications'  => 'c.contact_has_service_notfications',
            'contact_can_submit_commands'       => 'c.contact_can_submit_commands',
            'contact_notify_service_recovery'   => 'c.contact_notify_service_recovery',
            'contact_notify_service_warning'    => 'c.contact_notify_service_warning',
            'contact_notify_service_critical'   => 'c.contact_notify_service_critical',
            'contact_notify_service_unknown'    => 'c.contact_notify_service_unknown',
            'contact_notify_service_flapping'   => 'c.contact_notify_service_flapping',
            'contact_notify_service_downtime'   => 'c.contact_notify_service_downtime',
            'contact_notify_host_recovery'      => 'c.contact_notify_host_recovery',
            'contact_notify_host_down'          => 'c.contact_notify_host_down',
            'contact_notify_host_unreachable'   => 'c.contact_notify_host_unreachable',
            'contact_notify_host_flapping'      => 'c.contact_notify_host_flapping',
            'contact_notify_host_downtime'      => 'c.contact_notify_host_downtime',
            'contact_notify_host_timeperiod'    => 'c.contact_notify_host_timeperiod',
            'contact_notify_service_timeperiod' => 'c.contact_notify_service_timeperiod'
        ]
    ];

    /** @var Zend_Db_Select The union */
    protected $contactQuery;

    /** @var IdoQuery[] Subqueries used for the contact query */
    protected $subQueries = [];

    public function allowsCustomVars()
    {
        foreach ($this->subQueries as $query) {
            if (! $query->allowsCustomVars()) {
                return false;
            }
        }

        return true;
    }

    public function addFilter(Filter $filter)
    {
        $strangers = array_diff(
            $filter->listFilteredColumns(),
            array_keys($this->columnMap['contacts'])
        );
        if (! empty($strangers)) {
            $this->transformToUnion();
        }

        foreach ($this->subQueries as $sub) {
            $sub->applyFilter(clone $filter);
        }

        return $this;
    }

    protected function joinBaseTables()
    {
        $this->contactQuery = $this->createSubQuery('Hostcontact', array_keys($this->columnMap['contacts']));
        $this->contactQuery->setIsSubQuery();
        $this->subQueries[] = $this->contactQuery;

        $this->select->from(
            ['c' => $this->contactQuery],
            []
        );

        $this->joinedVirtualTables['contacts'] = true;
    }

    public function order($columnOrAlias, $dir = null)
    {
        foreach ($this->subQueries as $sub) {
            $sub->requireColumn($columnOrAlias);
        }

        return parent::order($columnOrAlias, $dir);
    }

    public function where($condition, $value = null)
    {
        $this->requireColumn($condition);
        foreach ($this->subQueries as $sub) {
            $sub->where($condition, $value);
        }

        return $this;
    }

    public function whereEx(FilterExpression $ex)
    {
        $this->requireColumn($ex->getColumn());
        foreach ($this->subQueries as $sub) {
            $sub->whereEx($ex);
        }

        return $this;
    }

    public function transformToUnion()
    {
        $this->contactQuery = $this->db->select();
        $this->select->reset();
        $this->subQueries = [];

        $this->select->distinct()->from(
            ['c' => $this->contactQuery],
            []
        );

        $hosts = $this->createSubQuery('Hostcontact', array_keys($this->columnMap['contacts']));
        $this->subQueries[] = $hosts;
        $this->contactQuery->union([$hosts], Zend_Db_Select::SQL_UNION_ALL);

        $services = $this->createSubQuery('Servicecontact', array_keys($this->columnMap['contacts']));
        $this->subQueries[] = $services;
        $this->contactQuery->union([$services], Zend_Db_Select::SQL_UNION_ALL);
    }
}