summaryrefslogtreecommitdiffstats
path: root/modules/monitoring/application/forms/EventOverviewForm.php
blob: 78774f95abe310d126cfde6e81c32294d0b5e0f1 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Monitoring\Forms;

use Icinga\Web\Url;
use Icinga\Web\Form;
use Icinga\Data\Filter\Filter;

/**
 * Configure the filter for the event overview
 */
class EventOverviewForm extends Form
{
    /**
     * {@inheritdoc}
     */
    public function init()
    {
        $this->setName('form_event_overview');
        $this->setDecorators(array(
            'FormElements',
            array('HtmlTag', array('tag' => 'div', 'class' => 'hbox')),
            'Form'
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function createElements(array $formData)
    {
        $decorators = array(
            array('Label', array('class' => 'optional')),
            'ViewHelper',
            array('HtmlTag', array('tag' => 'div', 'class' => 'hbox-item optionbox')),
        );

        $url = Url::fromRequest()->getAbsoluteUrl();
        $this->addElement(
            'checkbox',
            'statechange',
            array(
                'label' => $this->translate('State Changes'),
                'class' => 'autosubmit',
                'decorators' => $decorators,
                'value' => strpos($url, $this->stateChangeFilter()->toQueryString()) === false ? 0 : 1
            )
        );
        $this->addElement(
            'checkbox',
            'downtime',
            array(
                'label' => $this->translate('Downtimes'),
                'class' => 'autosubmit',
                'decorators' => $decorators,
                'value' => strpos($url, $this->downtimeFilter()->toQueryString()) === false ? 0 : 1
            )
        );
        $this->addElement(
            'checkbox',
            'comment',
            array(
                'label' => $this->translate('Comments'),
                'class' => 'autosubmit',
                'decorators' => $decorators,
                'value' => strpos($url, $this->commentFilter()->toQueryString()) === false ? 0 : 1
            )
        );
        $this->addElement(
            'checkbox',
            'notification',
            array(
                'label' => $this->translate('Notifications'),
                'class' => 'autosubmit',
                'decorators' => $decorators,
                'value' => strpos($url, $this->notificationFilter()->toQueryString()) === false ? 0 : 1
            )
        );
        $this->addElement(
            'checkbox',
            'flapping',
            array(
                'label' => $this->translate('Flapping'),
                'class' => 'autosubmit',
                'decorators' => $decorators,
                'value' => strpos($url, $this->flappingFilter()->toQueryString()) === false ? 0 : 1
            )
        );
    }

    /**
     * Return the corresponding filter-object
     *
     * @returns Filter
     */
    public function getFilter()
    {
        $filters = array();
        if ($this->getValue('statechange')) {
            $filters[] = $this->stateChangeFilter();
        }
        if ($this->getValue('comment')) {
            $filters[] = $this->commentFilter();
        }
        if ($this->getValue('notification')) {
            $filters[] = $this->notificationFilter();
        }
        if ($this->getValue('downtime')) {
            $filters[] = $this->downtimeFilter();
        }
        if ($this->getValue('flapping')) {
            $filters[] = $this->flappingFilter();
        }
        return Filter::matchAny($filters);
    }

    public function stateChangeFilter()
    {
        return Filter::matchAny(
            Filter::expression('type', '=', 'hard_state'),
            Filter::expression('type', '=', 'soft_state')
        );
    }

    public function commentFilter()
    {
        return Filter::matchAny(
            Filter::expression('type', '=', 'comment'),
            Filter::expression('type', '=', 'comment_deleted'),
            Filter::expression('type', '=', 'dt_comment'),
            Filter::expression('type', '=', 'dt_comment_deleted'),
            Filter::expression('type', '=', 'ack')
        );
    }

    public function notificationFilter()
    {
        return Filter::expression('type', '=', 'notify');
    }

    public function downtimeFilter()
    {
        return Filter::matchAny(
            Filter::expression('type', '=', 'downtime_start'),
            Filter::expression('type', '=', 'downtime_end')
        );
    }

    public function flappingFilter()
    {
        return Filter::matchAny(
            Filter::expression('type', '=', 'flapping'),
            Filter::expression('type', '=', 'flapping_deleted')
        );
    }
}