Skip to content

Commit 673faa0

Browse files
committed
Make fields for incident age condition intuitive
1 parent 437291d commit 673faa0

File tree

4 files changed

+72
-26
lines changed

4 files changed

+72
-26
lines changed

application/forms/EventRuleConfigElements/EscalationCondition.php

+36-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,8 +110,14 @@ protected function assemble(): void
111110
);
112111

113112
$valName = 'val_' . $i;
113+
$unitName = 'unit_' . $i;
114+
$valUnit = null;
114115
switch ($this->getPopulatedValue('column_' . $i)) {
115116
case 'incident_severity':
117+
if ($this->getPopulatedValue($unitName)) {
118+
$this->clearPopulatedValue($valName);
119+
}
120+
116121
$val = $this->createElement(
117122
'select',
118123
$valName,
@@ -134,35 +139,34 @@ protected function assemble(): void
134139

135140
break;
136141
case 'incident_age':
142+
if ($this->getPopulatedValue($unitName) === null) {
143+
$this->clearPopulatedValue($valName);
144+
}
145+
137146
$val = $this->createElement(
138-
'text',
147+
'number',
139148
$valName,
140149
[
141150
'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-
]
151+
'class' => 'right-operand',
152+
'step' => 0.5,
153+
'value' => 1
154+
]
155+
);
156+
157+
$valUnit = $this->createElement(
158+
'select',
159+
$unitName,
160+
[
161+
'options' => ['m' => 'm', 'h' => 'h'],
162+
'class' => ['autosubmit', 'unit'],
163+
'value' => 'm'
161164
]
162165
);
163166

164167
break;
165168
default:
169+
$this->clearPopulatedValue($valName);
166170
$val = $this->createElement('text', $valName, [
167171
'class' => 'right-operand',
168172
'placeholder' => $this->translate('Please make a decision'),
@@ -174,6 +178,14 @@ protected function assemble(): void
174178
$this->registerElement($op);
175179
$this->registerElement($val);
176180

181+
if ($valUnit) {
182+
$this->registerElement($valUnit);
183+
$unit = $valUnit->getValue();
184+
$value = $val->getValue();
185+
$val->getAttributes()->set('min', $unit === 'm' ? 1 : 0.5);
186+
$val->getAttributes()->set('value', $unit === 'm' && (float) $value === 0.5 ? 1 : $value);
187+
}
188+
177189
$removeButton = null;
178190

179191
if (($conditionCount > 1) || ($conditionCount === 1 && ! $configHasZeroConditionEscalation)) {
@@ -184,7 +196,7 @@ protected function assemble(): void
184196
}
185197

186198
(new EventRuleDecorator())->decorate($val);
187-
$this->conditions[$i] = new EscalationConditionListItem($i, $col, $op, $val, $removeButton);
199+
$this->conditions[$i] = new EscalationConditionListItem($i, $col, $op, $val, $valUnit, $removeButton);
188200
}
189201

190202
if ($removePosition) {
@@ -258,7 +270,8 @@ public function getCondition(): string
258270

259271
$filterStr = $chosenType
260272
. $this->getValue('operator_' . $count)
261-
. ($this->getValue('val_' . $count) ?? ($chosenType === 'incident_severity' ? 'ok' : ''));
273+
. ($this->getValue('val_' . $count) ?? ($chosenType === 'incident_severity' ? 'ok' : '1'))
274+
. $this->getValue('unit_' . $count, '');
262275

263276
$filter->add(QueryString::parse($filterStr));
264277
}

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
@@ -177,6 +177,12 @@
177177
margin: 0;
178178
}
179179

180+
.unit {
181+
border-radius: 0 0.4em 0.4em 0;;
182+
min-width: 3.5em;
183+
margin-left: 1px;
184+
}
185+
180186
.errors + .remove-button {
181187
margin: 0;
182188
}
@@ -240,6 +246,12 @@
240246
margin-right: 1px;
241247
}
242248

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

0 commit comments

Comments
 (0)