Skip to content

Commit cdddd29

Browse files
EventRuleConfigForm::ON_SUCCESS: Only save data if changes have been made
1 parent 913e324 commit cdddd29

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

application/controllers/EventRuleController.php

+14-4
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,20 @@ public function indexAction(): void
6464
->populate($eventRuleConfigValues)
6565
->on(Form::ON_SUCCESS, function (EventRuleConfigForm $form) use ($ruleId, $eventRuleConfigValues) {
6666
$config = $form->getValues();
67-
$config['object_filter'] = $eventRuleConfigValues['object_filter'];
68-
$form->addOrUpdateRule($ruleId, $config);
69-
Notification::success((sprintf(t('Successfully saved event rule %s'), $eventRuleConfigValues['name'])));
70-
$this->redirectNow(Links::eventRule((int) $ruleId));
67+
$storedConfig = array_intersect_key($eventRuleConfigValues, $config);
68+
$redirectLink = Links::eventRules();
69+
70+
if ($storedConfig !== $config) {
71+
$config['object_filter'] = $eventRuleConfigValues['object_filter'];
72+
$form->addOrUpdateRule($ruleId, $config);
73+
$redirectLink = Links::eventRule((int) $ruleId);
74+
Notification::success(sprintf(
75+
t('Successfully saved event rule %s'),
76+
$eventRuleConfigValues['name']
77+
));
78+
}
79+
80+
$this->redirectNow($redirectLink);
7181
})
7282
->on(
7383
EventRuleConfigForm::ON_DELETE,

application/forms/EventRuleConfigElements/EscalationRecipient.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -213,23 +213,23 @@ public function getRecipients(): array
213213
/** @var int $count */
214214
$count = $this->getValue('recipient-count');
215215

216+
// Order of keys in $values maters, When comparing stored values with newly submitted values,
217+
// to get the difference. The order of keys should be same as the columns order in the db,
218+
// to compute the diff correctly when ON_SUCCESS is emitted.
216219
$values = [];
217220
for ($i = 1; $i <= $count; $i++) {
218221
$value = [];
219-
$value['channel_id'] = $this->getValue('val_' . $i);
220222
$value['id'] = $this->getValue('id_' . $i);
221223

222224
/** @var ?string $columnName */
223225
$columnName = $this->getValue('column_' . $i);
224226

225-
if ($columnName === null) {
226-
$values[] = $value;
227-
continue;
227+
if ($columnName !== null) {
228+
[$columnName, $id] = explode('_', $columnName, 2);
229+
$value[$columnName . '_id'] = $id;
228230
}
229231

230-
[$columnName, $id] = explode('_', $columnName, 2);
231-
232-
$value[$columnName . '_id'] = $id;
232+
$value['channel_id'] = $this->getValue('val_' . $i);
233233

234234
$values[] = $value;
235235
}

application/forms/EventRuleConfigForm.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,9 @@ public function populate($values): self
363363
*/
364364
public function getValues(): array
365365
{
366+
// Order of keys in $values maters, When comparing stored values with newly submitted values,
367+
// to get the difference. The order of keys should be same as the columns order in the db,
368+
// to compute the diff correctly when ON_SUCCESS is emitted.
366369
$values = [];
367370
$escalations = [];
368371
/** @var string $prefixesString */
@@ -375,8 +378,8 @@ public function getValues(): array
375378
if ($this->hasElement('escalation-condition_' . $prefixMap)) {
376379
/** @var EscalationCondition $escalationCondition */
377380
$escalationCondition = $this->getElement('escalation-condition_' . $prefixMap);
378-
$escalations[$i]['condition'] = $escalationCondition->getCondition();
379381
$escalations[$i]['id'] = $escalationCondition->getValue('id');
382+
$escalations[$i]['condition'] = $escalationCondition->getCondition();
380383
}
381384

382385
if ($this->hasElement('escalation-condition_' . $prefixMap)) {

0 commit comments

Comments
 (0)