@@ -46,29 +46,17 @@ class EventRuleConfigForm extends Form
46
46
'id ' => 'event-rule-config-form '
47
47
];
48
48
49
- /** @var array<string, mixed> */
50
- protected $ config ;
51
-
52
49
/** @var Url Search editor URL for the config filter fieldset */
53
50
protected $ searchEditorUrl ;
54
51
55
52
/**
56
53
* Create a new EventRuleConfigForm
57
54
*
58
- * @param array<string, mixed> $config
59
- * @param Url $searchEditorUrl
55
+ * @param Url $searchEditorUrl
60
56
*/
61
- public function __construct (array $ config , Url $ searchEditorUrl )
57
+ public function __construct (Url $ searchEditorUrl )
62
58
{
63
- $ this ->config = $ config ;
64
59
$ this ->searchEditorUrl = $ searchEditorUrl ;
65
- $ this ->on (self ::ON_SENT , function () {
66
- $ config = array_merge ($ this ->config , $ this ->getValues ());
67
-
68
- if ($ config !== $ this ->config ) {
69
- $ this ->emit (self ::ON_CHANGE , [$ this ]);
70
- }
71
- });
72
60
}
73
61
74
62
public function hasBeenSubmitted ()
@@ -80,8 +68,6 @@ public function hasBeenSubmitted()
80
68
81
69
if ($ buttonName === 'delete ' ) {
82
70
$ this ->emit (self ::ON_DELETE , [$ this ]);
83
- } elseif ($ buttonName === 'discard_changes ' ) {
84
- $ this ->emit (self ::ON_DISCARD , [$ this ]);
85
71
} elseif ($ buttonName === 'save ' ) {
86
72
return true ;
87
73
}
@@ -126,7 +112,7 @@ protected function assemble(): void
126
112
);
127
113
128
114
/** @var string $ruleId */
129
- $ ruleId = $ this ->config [ 'id ' ] ;
115
+ $ ruleId = $ this ->searchEditorUrl -> getParam ( 'id ' ) ;
130
116
if ($ ruleId === '-1 ' ) {
131
117
$ initialZeroConditionEscalation = bin2hex ('1 ' );
132
118
} else {
@@ -140,7 +126,7 @@ protected function assemble(): void
140
126
);
141
127
142
128
/** @var string $objectFilter */
143
- $ objectFilter = $ this ->config [ 'object_filter ' ] ?? '' ;
129
+ $ objectFilter = $ this ->searchEditorUrl -> getParam ( 'object_filter ' , '' ) ;
144
130
$ configFilter = (new EventRuleConfigFilter ('config-filter ' ))
145
131
->setObjectFilter ($ objectFilter )
146
132
->setSearchEditorUrl ($ this ->searchEditorUrl );
@@ -393,13 +379,19 @@ public function getValues(): array
393
379
$ prefixesMap = explode (', ' , $ prefixesString );
394
380
$ i = 1 ;
395
381
foreach ($ prefixesMap as $ prefixMap ) {
396
- /** @var EscalationCondition $escalationCondition */
397
- $ escalationCondition = $ this ->getElement ('escalation-condition_ ' . $ prefixMap );
398
- /** @var EscalationRecipient $escalationRecipient */
399
- $ escalationRecipient = $ this ->getElement ('escalation-recipient_ ' . $ prefixMap );
400
- $ escalations [$ i ]['condition ' ] = $ escalationCondition ->getCondition ();
401
- $ escalations [$ i ]['id ' ] = $ escalationCondition ->getValue ('id ' );
402
- $ escalations [$ i ]['recipients ' ] = $ escalationRecipient ->getRecipients ();
382
+ if ($ this ->hasElement ('escalation-condition_ ' . $ prefixMap )) {
383
+ /** @var EscalationCondition $escalationCondition */
384
+ $ escalationCondition = $ this ->getElement ('escalation-condition_ ' . $ prefixMap );
385
+ $ escalations [$ i ]['condition ' ] = $ escalationCondition ->getCondition ();
386
+ $ escalations [$ i ]['id ' ] = $ escalationCondition ->getValue ('id ' );
387
+ }
388
+
389
+ if ($ this ->hasElement ('escalation-condition_ ' . $ prefixMap )) {
390
+ /** @var EscalationRecipient $escalationRecipient */
391
+ $ escalationRecipient = $ this ->getElement ('escalation-recipient_ ' . $ prefixMap );
392
+ $ escalations [$ i ]['recipients ' ] = $ escalationRecipient ->getRecipients ();
393
+ }
394
+
403
395
$ i ++;
404
396
}
405
397
@@ -421,7 +413,7 @@ public function getValues(): array
421
413
protected function createRemoveButton (string $ prefix ): SubmitButtonElement
422
414
{
423
415
/** @var array<int, array<string, mixed>> $escalations */
424
- $ escalations = $ this ->config ['rule_escalation ' ] ?? [ ];
416
+ $ escalations = $ this ->getValues () ['rule_escalation ' ];
425
417
426
418
$ pos = hex2bin ($ prefix );
427
419
$ disableRemoveButton = false ;
@@ -484,9 +476,7 @@ protected function createRemoveButton(string $prefix): SubmitButtonElement
484
476
public function addOrUpdateRule (string $ id , array $ config ): void
485
477
{
486
478
$ db = Database::get ();
487
-
488
479
$ db ->beginTransaction ();
489
-
490
480
if ($ id < 0 ) {
491
481
$ db ->insert ('rule ' , [
492
482
'name ' => $ config ['name ' ],
@@ -498,44 +488,41 @@ public function addOrUpdateRule(string $id, array $config): void
498
488
$ id = $ db ->lastInsertId ();
499
489
} else {
500
490
$ db ->update ('rule ' , [
501
- 'name ' => $ config ['name ' ],
502
- 'timeperiod_id ' => $ config ['timeperiod_id ' ] ?? null ,
503
491
'object_filter ' => $ config ['object_filter ' ] ?? null ,
504
- 'is_active ' => $ config ['is_active ' ] ?? 'n '
505
492
], ['id = ? ' => $ id ]);
506
493
}
507
494
508
495
$ escalationsFromDb = RuleEscalation::on ($ db )
509
496
->filter (Filter::equal ('rule_id ' , $ id ));
510
497
511
- /** @var array<int, array<string, mixed>> $escalationsInCache */
512
- $ escalationsInCache = $ config ['rule_escalation ' ];
498
+ /** @var array<int, array<string, mixed>> $escalationsInForm */
499
+ $ escalationsInForm = $ config ['rule_escalation ' ];
513
500
514
501
$ escalationsToUpdate = [];
515
502
$ escalationsToRemove = [];
516
503
517
504
/** @var RuleEscalation $escalationFromDB */
518
505
foreach ($ escalationsFromDb as $ escalationFromDB ) {
519
506
$ escalationId = $ escalationFromDB ->id ;
520
- $ escalationInCache = array_filter ($ escalationsInCache , function (array $ element ) use ($ escalationId ) {
521
- /** @var string $idInCache */
522
- $ idInCache = $ element ['id ' ] ?? null ;
523
- return (int ) $ idInCache === $ escalationId ;
507
+ $ escalationInForm = array_filter ($ escalationsInForm , function (array $ element ) use ($ escalationId ) {
508
+ /** @var string $idInForm */
509
+ $ idInForm = $ element ['id ' ] ?? null ;
510
+ return (int ) $ idInForm === $ escalationId ;
524
511
});
525
512
526
- if ($ escalationInCache ) {
527
- $ position = array_key_first ($ escalationInCache );
513
+ if ($ escalationInForm ) {
514
+ $ position = array_key_first ($ escalationInForm );
528
515
// Escalations in DB to update
529
- $ escalationsToUpdate [$ position ] = $ escalationInCache [$ position ];
530
- unset($ escalationsInCache [$ position ]);
516
+ $ escalationsToUpdate [$ position ] = $ escalationInForm [$ position ];
517
+ unset($ escalationsInForm [$ position ]);
531
518
} else {
532
519
// Escalation in DB to remove
533
520
$ escalationsToRemove [] = $ escalationId ;
534
521
}
535
522
}
536
523
537
524
// Escalations to add
538
- $ escalationsToAdd = $ escalationsInCache ;
525
+ $ escalationsToAdd = $ escalationsInForm ;
539
526
540
527
if (! empty ($ escalationsToRemove )) {
541
528
$ db ->delete ('rule_escalation_recipient ' , ['rule_escalation_id IN (?) ' => $ escalationsToRemove ]);
@@ -598,17 +585,17 @@ private function insertOrUpdateEscalations(
598
585
/** @var RuleEscalationRecipient $recipient */
599
586
foreach ($ recipients as $ recipient ) {
600
587
$ recipientId = $ recipient ->id ;
601
- $ recipientInCache = array_filter (
588
+ $ recipientInForm = array_filter (
602
589
$ recipientsFromConfig ,
603
590
function (array $ element ) use ($ recipientId ) {
604
- /** @var string $idFromCache */
605
- $ idFromCache = $ element ['id ' ];
606
- return (int ) $ idFromCache === $ recipientId ;
591
+ /** @var string $idFromForm */
592
+ $ idFromForm = $ element ['id ' ];
593
+ return (int ) $ idFromForm === $ recipientId ;
607
594
}
608
595
);
609
596
610
- if (empty ($ recipientInCache )) {
611
- // Recipients to remove from Db not in cache
597
+ if (empty ($ recipientInForm )) {
598
+ // Recipients to remove from Db not in form
612
599
$ recipientsToRemove [] = $ recipientId ;
613
600
}
614
601
}
0 commit comments