Skip to content

Commit 9a51106

Browse files
committed
Make fields for incident age condition intuitive
1 parent 4bbe6f0 commit 9a51106

File tree

4 files changed

+59
-23
lines changed

4 files changed

+59
-23
lines changed

application/forms/EventRuleConfigElements/EscalationCondition.php

+25-22
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

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

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

@@ -173,6 +167,14 @@ protected function assemble(): void
173167
$this->registerElement($col);
174168
$this->registerElement($op);
175169
$this->registerElement($val);
170+
if ($valUnit) {
171+
$this->registerElement($valUnit);
172+
$unit = $valUnit->getValue();
173+
$value = $val->getValue();
174+
$val->getAttributes()->set('min', $unit === 'm' ? 1 : 0.5);
175+
$val->getAttributes()->set('value', $unit === 'm' && (float) $value === 0.5 ? 1 : $value);
176+
}
177+
176178
$removeButton = null;
177179

178180
if (($conditionCount > 1) || ($conditionCount === 1 && ! $configHasZeroConditionEscalation)) {
@@ -183,7 +185,7 @@ protected function assemble(): void
183185
}
184186

185187
(new EventRuleDecorator())->decorate($val);
186-
$this->conditions[$i] = new EscalationConditionListItem($i, $col, $op, $val, $removeButton);
188+
$this->conditions[$i] = new EscalationConditionListItem($i, $col, $op, $val, $valUnit, $removeButton);
187189
}
188190

189191
if ($removePosition) {
@@ -256,7 +258,8 @@ public function getCondition(): string
256258

257259
$filterStr = $chosenType
258260
. $this->getValue('operator_' . $count)
259-
. ($this->getValue('val_' . $count) ?? ($chosenType === 'incident_severity' ? 'ok' : ''));
261+
. ($this->getValue('val_' . $count) ?? ($chosenType === 'incident_severity' ? 'ok' : ''))
262+
. $this->getValue('unit_' . $count, '');
260263

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

application/forms/EventRuleConfigForm.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,16 @@ public function populate($values): self
296296
}
297297

298298
$conditionFormValues['operator_' . $count] = QueryString::getRuleSymbol($filter);
299-
$conditionFormValues['val_' . $count] = $filter->getValue();
299+
$conditionValue = $filter->getValue();
300+
if (
301+
preg_match('~^(\d+(?:\.?\d*))?([hms])$~', $conditionValue, $matches)
302+
&& count($matches) === 3
303+
) {
304+
$conditionFormValues['val_' . $count] = $matches[1];
305+
$conditionFormValues['unit_' . $count] = $matches[2];
306+
} else {
307+
$conditionFormValues['val_' . $count] = $conditionValue;
308+
}
300309
}
301310

302311
$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)