Skip to content

Commit 1d26d1c

Browse files
committed
WIP: Make fields for incident age condition intuitive
1 parent 9819b5f commit 1d26d1c

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
@@ -116,6 +116,7 @@ protected function assemble(): void
116116
]
117117
);
118118

119+
$valUnit = null;
119120
switch ($this->getPopulatedValue('column_' . $i)) {
120121
case 'incident_severity':
121122
/** @var BaseFormElement $val */
@@ -154,28 +155,25 @@ protected function assemble(): void
154155
case 'incident_age':
155156
/** @var BaseFormElement $val */
156157
$val = $this->createElement(
157-
'text',
158+
'number',
158159
$valName,
159160
[
160161
'required' => true,
161-
'class' => ['autosubmit', 'right-operand'],
162-
'validators' => [
163-
new CallbackValidator(function ($value, $validator) {
164-
if (! preg_match('~^\d+(?:\.?\d*)?[hms]{1}$~', $value)) {
165-
$validator->addMessage(
166-
$this->translate(
167-
'Only numbers with optional fractions (separated by a dot)'
168-
. ' and one of these suffixes are allowed: h, m, s'
169-
)
170-
);
171-
172-
return false;
173-
}
174-
175-
$validator->clearMessages();
176-
return true;
177-
})
178-
]
162+
'class' => ['right-operand'],
163+
'value' => 1
164+
]
165+
);
166+
167+
$valUnit = $this->createElement(
168+
'select',
169+
'age_unit_' . $i,
170+
[
171+
'options' => [
172+
's' => 's',
173+
'm' => 'm',
174+
'h' => 'h'
175+
],
176+
'class' => 'age-unit'
179177
]
180178
);
181179

@@ -191,6 +189,7 @@ protected function assemble(): void
191189
'value' => 'incident_age'
192190
]);
193191

192+
$this->registerElement($valUnit);
194193
break;
195194
default:
196195
/** @var BaseFormElement $val */
@@ -213,6 +212,7 @@ protected function assemble(): void
213212
$col,
214213
$op,
215214
$val,
215+
$valUnit,
216216
$removeButton
217217
);
218218
}
@@ -230,6 +230,10 @@ protected function assemble(): void
230230
$this->conditions[$nextCount]->conditionType->setName('column_' . $n);
231231
$this->conditions[$nextCount]->operator->setName('operator_' . $n);
232232
$this->conditions[$nextCount]->conditionVal->setName('val_' . $n);
233+
if ($this->conditions[$nextCount]->conditionUnit) {
234+
$this->conditions[$nextCount]->conditionUnit->setName('age_unit_' . $n);
235+
}
236+
233237
if ($conditionCount === 1) {
234238
$this->conditions[$nextCount]->removeButton = null;
235239
} elseif ($this->conditions[$nextCount]->removeButton) {
@@ -308,7 +312,8 @@ public function getCondition(): string
308312

309313
$filterStr = $chosenType
310314
. $this->getValue('operator_' . $count)
311-
. ($this->getValue('val_' . $count) ?? ($chosenType === 'incident_severity' ? 'ok' : ''));
315+
. ($this->getValue('val_' . $count) ?? ($chosenType === 'incident_severity' ? 'ok' : ''))
316+
. $this->getValue('age_unit_' . $count, '');
312317

313318
$filter->add(QueryString::parse($filterStr));
314319
}

application/forms/EventRuleConfigForm.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,14 @@ public function populate($values): self
340340
}
341341

342342
$conditionFormValues['operator_' . $count] = QueryString::getRuleSymbol($filter);
343-
$conditionFormValues['val_' . $count] = $filter->getValue();
343+
$conditionValue = $filter->getValue();
344+
$incidentAgeCondition = preg_split('~^\d+(?:\.?\d*)?[hms]$~', $filter->getValue());
345+
if ($incidentAgeCondition !== false && count($incidentAgeCondition) === 2) {
346+
$conditionFormValues['val_' . $count] = $incidentAgeCondition[0];
347+
$conditionFormValues['age_unit_' . $count] = $incidentAgeCondition[1];
348+
} else {
349+
$conditionFormValues['val_' . $count] = $conditionValue;
350+
}
344351
}
345352

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