Skip to content

Commit aab2015

Browse files
committed
Make fields for incident age condition intuitive
1 parent d35dd96 commit aab2015

File tree

4 files changed

+45
-21
lines changed

4 files changed

+45
-21
lines changed

application/forms/EventRuleConfigElements/EscalationCondition.php

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

115+
$valUnit = null;
115116
switch ($this->getPopulatedValue('column_' . $i)) {
116117
case 'incident_severity':
117118
$val = $this->createElement(
@@ -148,28 +149,25 @@ protected function assemble(): void
148149
break;
149150
case 'incident_age':
150151
$val = $this->createElement(
151-
'text',
152+
'number',
152153
$valName,
153154
[
154155
'required' => true,
155-
'class' => ['autosubmit', 'right-operand'],
156-
'validators' => [
157-
new CallbackValidator(function ($value, $validator) {
158-
if (! preg_match('~^\d+(?:\.?\d*)?[hms]{1}$~', $value)) {
159-
$validator->addMessage(
160-
$this->translate(
161-
'Only numbers with optional fractions (separated by a dot)'
162-
. ' and one of these suffixes are allowed: h, m, s'
163-
)
164-
);
165-
166-
return false;
167-
}
168-
169-
$validator->clearMessages();
170-
return true;
171-
})
172-
]
156+
'class' => ['right-operand'],
157+
'value' => 1
158+
]
159+
);
160+
161+
$valUnit = $this->createElement(
162+
'select',
163+
'age_unit_' . $i,
164+
[
165+
'options' => [
166+
's' => 's',
167+
'm' => 'm',
168+
'h' => 'h'
169+
],
170+
'class' => 'age-unit'
173171
]
174172
);
175173

@@ -185,6 +183,7 @@ protected function assemble(): void
185183
'value' => 'incident_age'
186184
]);
187185

186+
$this->registerElement($valUnit);
188187
break;
189188
default:
190189
$val = $this->createElement('text', $valName, [
@@ -205,6 +204,7 @@ protected function assemble(): void
205204
$col,
206205
$op,
207206
$val,
207+
$valUnit,
208208
$removeButton
209209
);
210210
}
@@ -221,6 +221,10 @@ protected function assemble(): void
221221
$this->conditions[$nextCount]->conditionType->setName('column_' . $n);
222222
$this->conditions[$nextCount]->operator->setName('operator_' . $n);
223223
$this->conditions[$nextCount]->conditionVal->setName('val_' . $n);
224+
if ($this->conditions[$nextCount]->conditionUnit) {
225+
$this->conditions[$nextCount]->conditionUnit->setName('age_unit_' . $n);
226+
}
227+
224228
if ($conditionCount === 1) {
225229
$this->conditions[$nextCount]->removeButton = null;
226230
} elseif ($this->conditions[$nextCount]->removeButton) {
@@ -297,7 +301,8 @@ public function getCondition(): string
297301

298302
$filterStr = $chosenType
299303
. $this->getValue('operator_' . $count)
300-
. ($this->getValue('val_' . $count) ?? ($chosenType === 'incident_severity' ? 'ok' : ''));
304+
. ($this->getValue('val_' . $count) ?? ($chosenType === 'incident_severity' ? 'ok' : ''))
305+
. $this->getValue('age_unit_' . $count, '');
301306

302307
$filter->add(QueryString::parse($filterStr));
303308
}

application/forms/EventRuleConfigForm.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,14 @@ 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+
$incidentAgeCondition = preg_split('~^\d+(?:\.?\d*)?[hms]$~', $filter->getValue());
338+
if ($incidentAgeCondition !== false && count($incidentAgeCondition) === 2) {
339+
$conditionFormValues['val_' . $count] = $incidentAgeCondition[0];
340+
$conditionFormValues['age_unit_' . $count] = $incidentAgeCondition[1];
341+
} else {
342+
$conditionFormValues['val_' . $count] = $conditionValue;
343+
}
337344
}
338345

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