Skip to content

Commit

Permalink
Make fields for incident age condition intuitive
Browse files Browse the repository at this point in the history
  • Loading branch information
raviks789 committed Jun 20, 2024
1 parent 437291d commit 673faa0
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 26 deletions.
59 changes: 36 additions & 23 deletions application/forms/EventRuleConfigElements/EscalationCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use ipl\Html\FormElement\FieldsetElement;
use ipl\Html\FormElement\SubmitButtonElement;
use ipl\Stdlib\Filter;
use ipl\Validator\CallbackValidator;
use ipl\Web\Filter\QueryString;
use ipl\Web\Widget\Icon;

Expand Down Expand Up @@ -111,8 +110,14 @@ protected function assemble(): void
);

$valName = 'val_' . $i;
$unitName = 'unit_' . $i;
$valUnit = null;
switch ($this->getPopulatedValue('column_' . $i)) {
case 'incident_severity':
if ($this->getPopulatedValue($unitName)) {
$this->clearPopulatedValue($valName);
}

$val = $this->createElement(
'select',
$valName,
Expand All @@ -134,35 +139,34 @@ protected function assemble(): void

break;
case 'incident_age':
if ($this->getPopulatedValue($unitName) === null) {
$this->clearPopulatedValue($valName);
}

$val = $this->createElement(
'text',
'number',
$valName,
[
'required' => true,
'class' => ['autosubmit', 'right-operand'],
'validators' => [
new CallbackValidator(function ($value, $validator) {
if (! preg_match('~^\d+(?:\.?\d*)?[hms]{1}$~', $value)) {
$validator->addMessage(
$this->translate(
'Only numbers with optional fractions (separated by a dot)'
. ' and one of these suffixes are allowed: h, m, s'
)
);

return false;
}

$validator->clearMessages();

return true;
})
]
'class' => 'right-operand',
'step' => 0.5,
'value' => 1
]
);

$valUnit = $this->createElement(
'select',
$unitName,
[
'options' => ['m' => 'm', 'h' => 'h'],
'class' => ['autosubmit', 'unit'],
'value' => 'm'
]
);

break;
default:
$this->clearPopulatedValue($valName);
$val = $this->createElement('text', $valName, [
'class' => 'right-operand',
'placeholder' => $this->translate('Please make a decision'),
Expand All @@ -174,6 +178,14 @@ protected function assemble(): void
$this->registerElement($op);
$this->registerElement($val);

if ($valUnit) {
$this->registerElement($valUnit);
$unit = $valUnit->getValue();
$value = $val->getValue();
$val->getAttributes()->set('min', $unit === 'm' ? 1 : 0.5);
$val->getAttributes()->set('value', $unit === 'm' && (float) $value === 0.5 ? 1 : $value);
}

$removeButton = null;

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

(new EventRuleDecorator())->decorate($val);
$this->conditions[$i] = new EscalationConditionListItem($i, $col, $op, $val, $removeButton);
$this->conditions[$i] = new EscalationConditionListItem($i, $col, $op, $val, $valUnit, $removeButton);
}

if ($removePosition) {
Expand Down Expand Up @@ -258,7 +270,8 @@ public function getCondition(): string

$filterStr = $chosenType
. $this->getValue('operator_' . $count)
. ($this->getValue('val_' . $count) ?? ($chosenType === 'incident_severity' ? 'ok' : ''));
. ($this->getValue('val_' . $count) ?? ($chosenType === 'incident_severity' ? 'ok' : '1'))
. $this->getValue('unit_' . $count, '');

$filter->add(QueryString::parse($filterStr));
}
Expand Down
15 changes: 12 additions & 3 deletions application/forms/EventRuleConfigForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,16 +285,25 @@ public function populate($values): self

/** @var Condition $filter */
$filter = QueryString::parse($condition);
$conditionFormValues['column_' . $count] = $filter->getColumn() === 'placeholder'
$conditionCol = $filter->getColumn();
$conditionFormValues['column_' . $count] = $conditionCol === 'placeholder'
? null
: $filter->getColumn();
: $conditionCol;

if ($conditionFormValues['column_' . $count]) {
$conditionFormValues['type_' . $count] = $conditionFormValues['column_' . $count];
}

$conditionFormValues['operator_' . $count] = QueryString::getRuleSymbol($filter);
$conditionFormValues['val_' . $count] = $filter->getValue();
$conditionValue = $filter->getValue();

if ($conditionCol === 'incident_age') {
$age = str_split($conditionValue, strlen($conditionValue) - 1);
$conditionFormValues['val_' . $count] = $age[0];
$conditionFormValues['unit_' . $count] = $age[1];
} else {
$conditionFormValues['val_' . $count] = $conditionValue;
}
}

$formValues['escalation-condition_' . bin2hex($position)] = $conditionFormValues;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,31 @@ class EscalationConditionListItem extends BaseHtmlElement
/** @var int */
protected $position;

/** @var ?FormElement Unit of the condition value */
protected $conditionUnit;

/**
* Create the condition list item of the escalation
*
* @param FormElement $conditionType
* @param FormElement $operator
* @param FormElement $conditionVal
* @param ?FormElement $conditionUnit,
* @param ?SubmitButtonElement $removeButton
*/
public function __construct(
int $position,
FormElement $conditionType,
FormElement $operator,
FormElement $conditionVal,
?FormElement $conditionUnit,
?SubmitButtonElement $removeButton
) {
$this->position = $position;
$this->conditionType = $conditionType;
$this->operator = $operator;
$this->conditionVal = $conditionVal;
$this->conditionUnit = $conditionUnit;
$this->removeButton = $removeButton;
}

Expand Down Expand Up @@ -89,6 +95,12 @@ protected function assemble(): void
$this->conditionVal->setAttribute('name', 'val_' . $this->position);

$this->addHtml($this->conditionType, $this->operator, $this->conditionVal);

if ($this->conditionUnit) {
$this->conditionUnit->setAttribute('name', 'unit_' . $this->position);
$this->addHtml($this->conditionUnit->setAttribute('name', 'unit_' . $this->position));
}

if ($this->removeButton) {
$this->removeButton->setSubmitValue((string) $this->position);
$this->addHtml($this->removeButton);
Expand Down
12 changes: 12 additions & 0 deletions public/css/event-rule-config.less
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@
margin: 0;
}

.unit {
border-radius: 0 0.4em 0.4em 0;;
min-width: 3.5em;
margin-left: 1px;
}

.errors + .remove-button {
margin: 0;
}
Expand Down Expand Up @@ -240,6 +246,12 @@
margin-right: 1px;
}

// <(needed pixel size)/(font size)>em Readjust number type input element's minimum width in escalation condition
.escalation-condition .options input[type='number'] {
border-radius: 0;
min-width: 77/12em;
}

.escalation-condition,
.escalation-recipient {
.options + .add-button,
Expand Down

0 comments on commit 673faa0

Please sign in to comment.