Skip to content

Commit

Permalink
Fix escalation condition and recipient removal
Browse files Browse the repository at this point in the history
  • Loading branch information
raviks789 committed Apr 25, 2024
1 parent 6f807e1 commit babd436
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 34 deletions.
47 changes: 18 additions & 29 deletions application/forms/EventRuleConfigElements/EscalationCondition.php
Expand Up @@ -73,10 +73,10 @@ protected function assemble(): void
$this->getElement('condition-count')->setValue($conditionCount);
}

$removePosition = null;
for ($i = 1; $i <= $conditionCount; $i++) {
$colName = 'column_' . $i;
$opName = 'operator_' . $i;
$typeName = 'type_' . $i;
$valName = 'val_' . $i;

$col = $this->createElement(
Expand Down Expand Up @@ -126,18 +126,6 @@ protected function assemble(): void
]
);

if (
$this->getPopulatedValue($typeName) !== 'incident_severity'
&& $this->getPopulatedValue($valName) !== null
) {
$this->clearPopulatedValue($typeName);
$this->clearPopulatedValue($valName);
}

$this->addElement('hidden', $typeName, [
'value' => 'incident_severity'
]);

break;
case 'incident_age':
$val = $this->createElement(
Expand Down Expand Up @@ -166,18 +154,6 @@ protected function assemble(): void
]
);

if (
$this->getPopulatedValue($typeName) !== 'incident_age'
&& $this->getPopulatedValue($valName) !== null
) {
$this->clearPopulatedValue($typeName);
$this->clearPopulatedValue($valName);
}

$this->addElement('hidden', $typeName, [
'value' => 'incident_age'
]);

break;
default:
$val = $this->createElement('text', $valName, [
Expand All @@ -190,12 +166,16 @@ protected function assemble(): void
$this->registerElement($col);
$this->registerElement($op);
$this->registerElement($val);
$removeButton = $this->createRemoveButton($i);

if ($removeButton && $removeButton->hasBeenPressed()) {
$removePosition = $i;
}

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

$removePosition = (int) $this->getValue('remove');
if ($removePosition) {
unset($this->conditions[$removePosition]);
$conditionCount -= 1;
Expand All @@ -207,13 +187,14 @@ protected function assemble(): void
$this->conditions[$nextCount]->conditionType->setName('column_' . $n);
$this->conditions[$nextCount]->operator->setName('operator_' . $n);
$this->conditions[$nextCount]->conditionVal->setName('val_' . $n);
if ($conditionCount === 1) {
if ($conditionCount === 1 && ! $this->allowZeroConditions) {
$this->conditions[$nextCount]->removeButton = null;
} elseif ($this->conditions[$nextCount]->removeButton) {
$this->conditions[$nextCount]->removeButton->setValue((string) $n);
$this->conditions[$nextCount]->removeButton->setSubmitValue((string) $n);
}
}
}

$this->getElement('condition-count')->setValue($conditionCount);
}

Expand Down Expand Up @@ -275,9 +256,17 @@ public function getCondition(): string
$filter = Filter::any();
/** @var int $count */
$count = $this->getValue('condition-count');
$removePosition = $this->getValue('remove');
if ($removePosition) {
$count += 1;
}

if ($count > 0) { // if count is 0, loop runs in reverse direction
foreach (range(1, $count) as $count) {
if ($count === (int) $removePosition) {
continue;
}

$chosenType = $this->getValue('column_' . $count, 'placeholder');

$filterStr = $chosenType
Expand Down
18 changes: 15 additions & 3 deletions application/forms/EventRuleConfigElements/EscalationRecipient.php
Expand Up @@ -42,6 +42,7 @@ protected function assemble(): void
$this->getElement('recipient-count')->setValue($recipientCount);
}

$removePosition = null;
foreach (range(1, $recipientCount) as $i) {
$this->addElement('hidden', 'id_' . $i);

Expand Down Expand Up @@ -99,11 +100,14 @@ protected function assemble(): void
}

$this->registerElement($val);
$removeButton = $this->createRemoveButton($i);
if ($removeButton && $removeButton->hasBeenPressed()) {
$removePosition = $i;
}

$this->recipients[$i] = new EscalationRecipientListItem($col, $val, $this->createRemoveButton($i));
$this->recipients[$i] = new EscalationRecipientListItem($col, $val, $removeButton);
}

$removePosition = (int) $this->getValue('remove');
if ($removePosition) {
unset($this->recipients[$removePosition]);
$recipientCount -= 1;
Expand All @@ -117,7 +121,7 @@ protected function assemble(): void
if ($recipientCount === 1) {
$this->recipients[$nextCount]->removeButton = null;
} elseif ($this->recipients[$nextCount]->removeButton) {
$this->recipients[$nextCount]->removeButton->setValue((string) $n);
$this->recipients[$nextCount]->removeButton->setSubmitValue((string) $n);
}
}
}
Expand Down Expand Up @@ -201,9 +205,17 @@ public function getRecipients(): array
{
/** @var int $count */
$count = $this->getValue('recipient-count');
$removePosition = $this->getValue('remove');
if ($removePosition) {
$count += 1;
}

$values = [];
for ($i = 1; $i <= $count; $i++) {
if ($i === (int) $removePosition) {
continue;
}

$value = [];
$value['channel_id'] = $this->getValue('val_' . $i);
$value['id'] = $this->getValue('id_' . $i);
Expand Down
4 changes: 2 additions & 2 deletions application/forms/EventRuleConfigForm.php
Expand Up @@ -245,15 +245,15 @@ protected function assemble(): void

if ($noZeroEscalationConditions === true) {
foreach ($escalations as $escalation) {
$escalation->getCondition()
$escalation->condition
->setAllowZeroConditions(true);
}

$this->getElement('zero-condition-escalation')
->setValue(null);
} elseif ($zeroConditionEscalation) {
$escalations[$zeroConditionEscalation]
->getCondition()
->condition
->setAllowZeroConditions(true);
}

Expand Down

0 comments on commit babd436

Please sign in to comment.