Skip to content

Commit d5c0c52

Browse files
committed
Make fields for incident age condition intuitive
1 parent 7d9ad13 commit d5c0c52

File tree

4 files changed

+62
-26
lines changed

4 files changed

+62
-26
lines changed

application/forms/EventRuleConfigElements/EscalationCondition.php

+26-23
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use ipl\Html\FormElement\FieldsetElement;
1111
use ipl\Html\FormElement\SubmitButtonElement;
1212
use ipl\Stdlib\Filter;
13-
use ipl\Validator\CallbackValidator;
1413
use ipl\Web\Filter\QueryString;
1514
use ipl\Web\Widget\Icon;
1615

@@ -111,6 +110,7 @@ protected function assemble(): void
111110
);
112111

113112
$valName = 'val_' . $i;
113+
$valUnit = null;
114114
switch ($this->getPopulatedValue('column_' . $i)) {
115115
case 'incident_severity':
116116
$val = $this->createElement(
@@ -135,29 +135,23 @@ protected function assemble(): void
135135
break;
136136
case 'incident_age':
137137
$val = $this->createElement(
138-
'text',
138+
'number',
139139
$valName,
140140
[
141141
'required' => true,
142-
'class' => ['autosubmit', 'right-operand'],
143-
'validators' => [
144-
new CallbackValidator(function ($value, $validator) {
145-
if (! preg_match('~^\d+(?:\.?\d*)?[hms]{1}$~', $value)) {
146-
$validator->addMessage(
147-
$this->translate(
148-
'Only numbers with optional fractions (separated by a dot)'
149-
. ' and one of these suffixes are allowed: h, m, s'
150-
)
151-
);
152-
153-
return false;
154-
}
155-
156-
$validator->clearMessages();
157-
158-
return true;
159-
})
160-
]
142+
'class' => 'right-operand',
143+
'step' => 0.5,
144+
'value' => 1
145+
]
146+
);
147+
148+
$valUnit = $this->createElement(
149+
'select',
150+
'unit_' . $i,
151+
[
152+
'options' => ['m' => 'm', 'h' => 'h'],
153+
'class' => ['autosubmit', 'unit'],
154+
'value' => 'm'
161155
]
162156
);
163157

@@ -174,6 +168,14 @@ protected function assemble(): void
174168
$this->registerElement($op);
175169
$this->registerElement($val);
176170

171+
if ($valUnit) {
172+
$this->registerElement($valUnit);
173+
$unit = $valUnit->getValue();
174+
$value = $val->getValue();
175+
$val->getAttributes()->set('min', $unit === 'm' ? 1 : 0.5);
176+
$val->getAttributes()->set('value', $unit === 'm' && (float) $value === 0.5 ? 1 : $value);
177+
}
178+
177179
$removeButton = null;
178180

179181
if (($conditionCount > 1) || ($conditionCount === 1 && ! $configHasZeroConditionEscalation)) {
@@ -184,7 +186,7 @@ protected function assemble(): void
184186
}
185187

186188
(new EventRuleDecorator())->decorate($val);
187-
$this->conditions[$i] = new EscalationConditionListItem($i, $col, $op, $val, $removeButton);
189+
$this->conditions[$i] = new EscalationConditionListItem($i, $col, $op, $val, $valUnit, $removeButton);
188190
}
189191

190192
if ($removePosition) {
@@ -257,7 +259,8 @@ public function getCondition(): string
257259

258260
$filterStr = $chosenType
259261
. $this->getValue('operator_' . $count)
260-
. ($this->getValue('val_' . $count) ?? ($chosenType === 'incident_severity' ? 'ok' : ''));
262+
. ($this->getValue('val_' . $count) ?? ($chosenType === 'incident_severity' ? 'ok' : ''))
263+
. $this->getValue('unit_' . $count, '');
261264

262265
$filter->add(QueryString::parse($filterStr));
263266
}

application/forms/EventRuleConfigForm.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -285,16 +285,25 @@ public function populate($values): self
285285

286286
/** @var Condition $filter */
287287
$filter = QueryString::parse($condition);
288-
$conditionFormValues['column_' . $count] = $filter->getColumn() === 'placeholder'
288+
$conditionCol = $filter->getColumn();
289+
$conditionFormValues['column_' . $count] = $conditionCol === 'placeholder'
289290
? null
290-
: $filter->getColumn();
291+
: $conditionCol;
291292

292293
if ($conditionFormValues['column_' . $count]) {
293294
$conditionFormValues['type_' . $count] = $conditionFormValues['column_' . $count];
294295
}
295296

296297
$conditionFormValues['operator_' . $count] = QueryString::getRuleSymbol($filter);
297-
$conditionFormValues['val_' . $count] = $filter->getValue();
298+
$conditionValue = $filter->getValue();
299+
300+
if ($conditionCol === 'incident_age') {
301+
$age = str_split($conditionValue, strlen($conditionValue) - 1);
302+
$conditionFormValues['val_' . $count] = $age[0];
303+
$conditionFormValues['unit_' . $count] = $age[1];
304+
} else {
305+
$conditionFormValues['val_' . $count] = $conditionValue;
306+
}
298307
}
299308

300309
$formValues['escalation-condition_' . bin2hex($position)] = $conditionFormValues;

library/Notifications/Widget/ItemList/EscalationConditionListItem.php

+12
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,31 @@ class EscalationConditionListItem extends BaseHtmlElement
2929
/** @var int */
3030
protected $position;
3131

32+
/** @var ?FormElement Unit of the condition value */
33+
protected $conditionUnit;
34+
3235
/**
3336
* Create the condition list item of the escalation
3437
*
3538
* @param FormElement $conditionType
3639
* @param FormElement $operator
3740
* @param FormElement $conditionVal
41+
* @param ?FormElement $conditionUnit,
3842
* @param ?SubmitButtonElement $removeButton
3943
*/
4044
public function __construct(
4145
int $position,
4246
FormElement $conditionType,
4347
FormElement $operator,
4448
FormElement $conditionVal,
49+
?FormElement $conditionUnit,
4550
?SubmitButtonElement $removeButton
4651
) {
4752
$this->position = $position;
4853
$this->conditionType = $conditionType;
4954
$this->operator = $operator;
5055
$this->conditionVal = $conditionVal;
56+
$this->conditionUnit = $conditionUnit;
5157
$this->removeButton = $removeButton;
5258
}
5359

@@ -89,6 +95,12 @@ protected function assemble(): void
8995
$this->conditionVal->setAttribute('name', 'val_' . $this->position);
9096

9197
$this->addHtml($this->conditionType, $this->operator, $this->conditionVal);
98+
99+
if ($this->conditionUnit) {
100+
$this->conditionUnit->setAttribute('name', 'unit_' . $this->position);
101+
$this->addHtml($this->conditionUnit->setAttribute('name', 'unit_' . $this->position));
102+
}
103+
92104
if ($this->removeButton) {
93105
$this->removeButton->setSubmitValue((string) $this->position);
94106
$this->addHtml($this->removeButton);

public/css/event-rule-config.less

+12
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,12 @@
176176
margin: 0;
177177
}
178178

179+
.unit {
180+
border-radius: 0 0.4em 0.4em 0;;
181+
min-width: 3.5em;
182+
margin-left: 1px;
183+
}
184+
179185
.errors + .remove-button {
180186
margin: 0;
181187
}
@@ -239,6 +245,12 @@
239245
margin-right: 1px;
240246
}
241247

248+
// <(needed pixel size)/(font size)>em Readjust number type input element's minimum width in escalation condition
249+
.escalation-condition .options input[type='number'] {
250+
border-radius: 0;
251+
min-width: 77/12em;
252+
}
253+
242254
.escalation-condition,
243255
.escalation-recipient {
244256
.options + .add-button,

0 commit comments

Comments
 (0)