Skip to content

Commit 5e55ce6

Browse files
committed
Make fields for incident age condition intuitive
1 parent 06362d1 commit 5e55ce6

File tree

4 files changed

+45
-21
lines changed

4 files changed

+45
-21
lines changed

application/forms/EventRuleConfigElements/EscalationCondition.php

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ protected function assemble(): void
111111
]
112112
);
113113

114+
$valUnit = null;
114115
switch ($this->getPopulatedValue('column_' . $i)) {
115116
case 'incident_severity':
116117
$val = $this->createElement(
@@ -135,28 +136,25 @@ protected function assemble(): void
135136
break;
136137
case 'incident_age':
137138
$val = $this->createElement(
138-
'text',
139+
'number',
139140
$valName,
140141
[
141142
'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-
return true;
158-
})
159-
]
143+
'class' => ['right-operand'],
144+
'value' => 1
145+
]
146+
);
147+
148+
$valUnit = $this->createElement(
149+
'select',
150+
'age_unit_' . $i,
151+
[
152+
'options' => [
153+
's' => 's',
154+
'm' => 'm',
155+
'h' => 'h'
156+
],
157+
'class' => 'age-unit'
160158
]
161159
);
162160

@@ -172,6 +170,9 @@ protected function assemble(): void
172170
$this->registerElement($col);
173171
$this->registerElement($op);
174172
$this->registerElement($val);
173+
if ($valUnit) {
174+
$this->registerElement($valUnit);
175+
}
175176

176177
(new EventRuleDecorator())->decorate($val);
177178
$removeButton = $this->createRemoveButton($i);
@@ -180,6 +181,7 @@ protected function assemble(): void
180181
$col,
181182
$op,
182183
$val,
184+
$valUnit,
183185
$removeButton
184186
);
185187
}
@@ -280,7 +282,8 @@ public function getCondition(): string
280282

281283
$filterStr = $chosenType
282284
. $this->getValue('operator_' . $count)
283-
. ($this->getValue('val_' . $count) ?? ($chosenType === 'incident_severity' ? 'ok' : ''));
285+
. ($this->getValue('val_' . $count) ?? ($chosenType === 'incident_severity' ? 'ok' : ''))
286+
. $this->getValue('age_unit_' . $count, '');
284287

285288
$filter->add(QueryString::parse($filterStr));
286289
}

application/forms/EventRuleConfigForm.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,16 @@ public function populate($values): self
333333
}
334334

335335
$conditionFormValues['operator_' . $count] = QueryString::getRuleSymbol($filter);
336-
$conditionFormValues['val_' . $count] = $filter->getValue();
336+
$conditionValue = $filter->getValue();
337+
if (
338+
preg_match('~^(\d+(?:\.?\d*))?([hms])$~', $conditionValue, $matches)
339+
&& count($matches) === 3
340+
) {
341+
$conditionFormValues['val_' . $count] = $matches[1];
342+
$conditionFormValues['age_unit_' . $count] = $matches[2];
343+
} else {
344+
$conditionFormValues['val_' . $count] = $conditionValue;
345+
}
337346
}
338347

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

library/Notifications/Widget/ItemList/EscalationConditionListItem.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,20 @@ class EscalationConditionListItem extends BaseHtmlElement
2626
/** @var BaseFormElement Condition value */
2727
public $conditionVal;
2828

29+
/** @var ?BaseFormElement Condition value */
30+
public $conditionUnit;
31+
2932
public function __construct(
3033
BaseFormElement $conditionType,
3134
BaseFormElement $operator,
3235
BaseFormElement $conditionVal,
36+
?BaseFormElement $conditionUnit,
3337
?SubmitButtonElement $removeButton
3438
) {
3539
$this->conditionType = $conditionType;
3640
$this->operator = $operator;
3741
$this->conditionVal = $conditionVal;
42+
$this->conditionUnit = $conditionUnit;
3843
$this->removeButton = $removeButton;
3944
}
4045

@@ -44,6 +49,7 @@ protected function assemble(): void
4449
$this->conditionType,
4550
$this->operator,
4651
$this->conditionVal,
52+
$this->conditionUnit,
4753
$this->removeButton
4854
]);
4955
}

public/css/event-rule-config.less

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,12 @@
178178
margin: 0;
179179
}
180180

181+
.age-unit {
182+
border-radius: 0.4em;
183+
min-width: 3.5em;
184+
margin-left: 1px;
185+
}
186+
181187
.errors + .remove-button {
182188
margin: 0;
183189
}

0 commit comments

Comments
 (0)