-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEscalationCondition.php
More file actions
189 lines (171 loc) · 6.42 KB
/
Copy pathEscalationCondition.php
File metadata and controls
189 lines (171 loc) · 6.42 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
// SPDX-FileCopyrightText: 2023 Icinga GmbH <https://icinga.com>
// SPDX-License-Identifier: GPL-3.0-or-later
namespace Icinga\Module\Notifications\Forms\EventRuleConfigElements;
use ipl\Html\Attributes;
use ipl\Html\FormElement\FieldsetElement;
use ipl\Html\FormElement\SubmitButtonElement;
use ipl\Html\HtmlElement;
use ipl\Stdlib\Filter\Condition;
use ipl\Stdlib\Filter\Equal;
use ipl\Stdlib\Filter\GreaterThan;
use ipl\Stdlib\Filter\GreaterThanOrEqual;
use ipl\Stdlib\Filter\LessThan;
use ipl\Stdlib\Filter\LessThanOrEqual;
use ipl\Stdlib\Filter\Unequal;
use ipl\Web\Filter\QueryString;
use ipl\Web\Widget\Icon;
/**
* @phpstan-type ConditionData array{
* column: string,
* operator: string,
* severity?: string,
* no_of?: string,
* unit?: string
* }
*/
class EscalationCondition extends FieldsetElement
{
protected $defaultAttributes = ['class' => 'escalation-condition'];
/** @var ?SubmitButtonElement The button to remove this condition */
protected ?SubmitButtonElement $removeButton = null;
/**
* Set the button to remove this condition
*
* @param SubmitButtonElement $removeButton
*
* @return void
*/
public function setRemoveButton(SubmitButtonElement $removeButton): void
{
$this->removeButton = $removeButton;
}
/**
* Prepare the condition for display
*
* @param Condition $condition
*
* @return ConditionData
*/
public static function prepare(Condition $condition): array
{
$data = [
'column' => $condition->getColumn(),
'operator' => QueryString::getRuleSymbol($condition)
];
if ($data['column'] === 'incident_severity') {
$data['severity'] = $condition->getValue();
} else {
preg_match('/^(\d+)([hms])$/', $condition->getValue(), $matches);
$data['no_of'] = $matches[1];
$data['unit'] = $matches[2];
}
return $data;
}
/**
* Get the condition to store
*
* @return Condition
*/
public function getCondition(): Condition
{
$this->ensureAssembled();
$column = $this->getElement('column')->getValue();
$value = match ($column) {
'incident_severity' => $this->getElement('severity')->getValue(),
'incident_age' => $this->getElement('no_of')->getValue()
. $this->getElement('unit')->getValue()
};
return match ($this->getElement('operator')->getValue()) {
'=' => new Equal($column, $value),
'>' => new GreaterThan($column, $value),
'>=' => new GreaterThanOrEqual($column, $value),
'<' => new LessThan($column, $value),
'<=' => new LessThanOrEqual($column, $value),
'!=' => new Unequal($column, $value)
};
}
protected function assemble(): void
{
$this->addElement('select', 'column', [
'required' => true,
'options' => [
'' => sprintf(' - %s - ', $this->translate('Please choose')),
'incident_severity' => $this->translate('Incident Severity'),
'incident_age' => $this->translate('Incident Age')
],
'class' => 'autosubmit',
'disabledOptions' => [''],
'value' => ''
]);
$this->addHtml(new Icon('spinner', [
'class' => 'spinner',
'title' => $this->translate(
'This page will be automatically updated upon change of the value'
)
]));
$this->addElement('select', 'operator', [
'required' => true,
'options' => [
'=' => '=',
'>' => '>',
'>=' => '>=',
'<' => '<',
'<=' => '<=',
'!=' => '!='
]
]);
if ($this->getPopulatedValue('column') === 'incident_severity') {
$this->addElement('select', 'severity', [
'required' => true,
'options' => [
'ok' => $this->translate('Ok', 'notification.severity'),
'debug' => $this->translate('Debug', 'notification.severity'),
'info' => $this->translate('Information', 'notification.severity'),
'notice' => $this->translate('Notice', 'notification.severity'),
'warning' => $this->translate('Warning', 'notification.severity'),
'err' => $this->translate('Error', 'notification.severity'),
'crit' => $this->translate('Critical', 'notification.severity'),
'alert' => $this->translate('Alert', 'notification.severity'),
'emerg' => $this->translate('Emergency', 'notification.severity')
]
]);
} elseif ($this->getPopulatedValue('column') === 'incident_age') {
$noOf = $this->createElement('number', 'no_of', [
'required' => true,
'min' => 1,
'step' => 1,
'value' => 1
]);
$unit = $this->createElement('select', 'unit', [
'required' => true,
'options' => [
'h' => $this->translate('Hours'),
'm' => $this->translate('Minutes'),
's' => $this->translate('Seconds')
]
]);
$this->registerElement($noOf);
$this->registerElement($unit);
$this->addHtml(new HtmlElement('div', Attributes::create(['class' => 'age-inputs']), $noOf, $unit));
} else {
$this->addElement('text', 'noop', [
'required' => true,
'placeholder' => $this->translate('Please make a decision'),
'disabled' => true
]);
}
if ($this->removeButton !== null) {
$this->addHtml(
$this->removeButton->setLabel(new Icon('minus'))
->setAttribute('class', ['remove-button', 'animated'])
->setAttribute('title', $this->translate('Remove Condition'))
);
} else {
$this->addHtml(new HtmlElement('span', Attributes::create([
'class' => 'remove-button-disabled',
'title' => $this->translate('Only the first escalation can be immediately triggered')
]), (new Icon('minus'))));
}
}
}