Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit dd2c1a5

Browse files
committedApr 10, 2024·
WIP
1 parent 375cdd7 commit dd2c1a5

File tree

7 files changed

+88
-143
lines changed

7 files changed

+88
-143
lines changed
 

‎application/controllers/EventRuleController.php

+26-23
Original file line numberDiff line numberDiff line change
@@ -46,36 +46,40 @@ public function indexAction(): void
4646
$ruleId = $this->params->getRequired('id');
4747
$this->controls->addAttributes(['class' => 'event-rule-detail']);
4848

49-
$disableSave = false;
50-
$configValues = $this->fromDb((int) $ruleId);
51-
/** @var array<string, string> $configFilter */
52-
$configFilter = $this->getRequest()->get('config-filter');
53-
if ($this->getRequest()->has('searchbar')) {
54-
$configValues['object_filter'] = $this->getRequest()->get('searchbar');
55-
} elseif ($configFilter !== null) {
56-
if (isset($configFilter['show-searchbar']) && $configFilter['show-searchbar'] === '0') {
57-
$configValues['object_filter'] = '';
49+
$eventRuleConfigValues = $this->fromDb((int) $ruleId);
50+
51+
if ($this->getRequest()->isPost()) {
52+
if ($this->getRequest()->has('searchbar')) {
53+
$eventRuleConfigValues['object_filter'] = $this->getRequest()->get('searchbar');
54+
} else {
55+
$eventRuleConfigValues['object_filter'] = '';
5856
}
5957
}
6058

6159
$eventRuleConfig = (new EventRuleConfigForm(
62-
$configValues,
6360
Url::fromPath(
6461
'notifications/event-rule/search-editor',
65-
['id' => $ruleId, 'object_filter' => $configValues['object_filter']]
62+
['id' => $ruleId, 'object_filter' => $eventRuleConfigValues['object_filter']]
6663
)
67-
))->populate($configValues);
68-
$eventRuleConfig
69-
->on(Form::ON_SUCCESS, function (EventRuleConfigForm $form) use ($ruleId, $configValues) {
70-
$form->addOrUpdateRule($ruleId, $configValues);
71-
Notification::success((sprintf(t('Successfully saved event rule %s'), $configValues['name'])));
64+
))
65+
->populate($eventRuleConfigValues)
66+
->on(Form::ON_SUCCESS, function (EventRuleConfigForm $form) use ($ruleId, $eventRuleConfigValues) {
67+
$config = $form->getValues();
68+
$config['object_filter'] = $eventRuleConfigValues['object_filter'];
69+
$form->addOrUpdateRule($ruleId, $config);
70+
Notification::success((sprintf(t('Successfully saved event rule %s'), $eventRuleConfigValues['name'])));
7271
$this->redirectNow(Links::eventRule((int) $ruleId));
7372
})
74-
->on(EventRuleConfigForm::ON_DELETE, function (EventRuleConfigForm $form) use ($ruleId, $configValues) {
75-
$form->removeRule((int) $ruleId);
76-
Notification::success(sprintf(t('Successfully deleted event rule %s'), $configValues['name']));
77-
$this->redirectNow('__CLOSE__');
78-
})
73+
->on(
74+
EventRuleConfigForm::ON_DELETE,
75+
function (EventRuleConfigForm $form) use ($ruleId, $eventRuleConfigValues) {
76+
$form->removeRule((int) $ruleId);
77+
Notification::success(
78+
sprintf(t('Successfully deleted event rule %s'), $eventRuleConfigValues['name'])
79+
);
80+
$this->redirectNow('__CLOSE__');
81+
}
82+
)
7983
->handleRequest($this->getServerRequest());
8084

8185
$buttonsWrapper = new HtmlElement('div', Attributes::create(['class' => ['icinga-controls', 'save-config']]));
@@ -84,7 +88,6 @@ public function indexAction(): void
8488
[
8589
'label' => t('Save'),
8690
'form' => 'event-rule-config-form',
87-
'disabled' => $disableSave
8891
]
8992
));
9093
$deleteButton = (new SubmitButtonElement(
@@ -112,7 +115,7 @@ public function indexAction(): void
112115
}
113116

114117
$eventRuleForm = Html::tag('div', ['class' => 'event-rule-form', 'id' => 'event-rule-form'], [
115-
Html::tag('h2', $configValues['name'] ?? ''),
118+
Html::tag('h2', $eventRuleConfigValues['name'] ?? ''),
116119
(new Link(
117120
new Icon('edit'),
118121
Url::fromPath('notifications/event-rule/edit', [

‎application/controllers/EventRulesController.php

+17-39
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Icinga\Module\Notifications\Web\Control\SearchBar\ObjectSuggestions;
1212
use Icinga\Module\Notifications\Widget\ItemList\EventRuleList;
1313
use Icinga\Web\Notification;
14-
use Icinga\Web\Session;
1514
use ipl\Html\Attributes;
1615
use ipl\Html\Form;
1716
use ipl\Html\FormElement\SubmitButtonElement;
@@ -34,20 +33,14 @@ class EventRulesController extends CompatController
3433
/** @var Filter\Rule Filter from query string parameters */
3534
private $filter;
3635

37-
/** @var Session\SessionNamespace */
38-
private $sessionNamespace;
39-
4036
public function init()
4137
{
4238
$this->assertPermission('notifications/config/event-rules');
43-
$this->sessionNamespace = Session::getSession()->getNamespace('notifications');
4439
}
4540

4641
public function indexAction(): void
4742
{
4843
$eventRules = Rule::on(Database::get());
49-
$this->sessionNamespace->delete('-1');
50-
5144
$limitControl = $this->createLimitControl();
5245
$paginationControl = $this->createPaginationControl($eventRules);
5346
$sortControl = $this->createSortControl(
@@ -109,25 +102,15 @@ public function addAction(): void
109102
$this->addContent(Html::tag('div', ['class' => 'container', 'id' => 'dummy']));
110103

111104
$this->controls->addAttributes(['class' => 'event-rule-detail']);
112-
/** @var string $ruleId */
113-
$ruleId = $this->params->get('id') ?? '-1';
114-
115-
$params = $this->params->toArray(false);
116-
/** @var array<string, mixed>|null $config */
117-
$config = $this->sessionNamespace->get($ruleId);
105+
/** @var array<string, string> $eventRule */
106+
$eventRule = $this->params->toArray(false);
107+
$ruleId = $eventRule['id'] ?? '-1';
118108

119-
if ($config === null) {
120-
/** @var array<string, mixed> $config */
121-
$config = $params;
122-
}
123-
124-
/** @var array<string, string> $configFilter */
125-
$configFilter = $this->getRequest()->get('config-filter');
126-
if ($this->getRequest()->has('searchbar')) {
127-
$config['object_filter'] = $this->getRequest()->get('searchbar');
128-
} elseif ($configFilter !== null) {
129-
if (isset($configFilter['show-searchbar']) && $configFilter['show-searchbar'] === '0') {
130-
$config['object_filter'] = '';
109+
if ($this->getRequest()->isPost()) {
110+
if ($this->getRequest()->has('searchbar')) {
111+
$eventRule['object_filter'] = $this->getRequest()->get('searchbar');
112+
} else {
113+
$eventRule['object_filter'] = '';
131114
}
132115
}
133116

@@ -141,34 +124,29 @@ public function addAction(): void
141124
))->setWrapper(new HtmlElement('div', Attributes::create(['class' => ['icinga-controls', 'save-config']])));
142125

143126
$eventRuleConfig = (new EventRuleConfigForm(
144-
$config,
145127
Url::fromPath(
146128
'notifications/event-rules/search-editor',
147-
['id' => $ruleId, 'object_filter' => $config['object_filter'] ?? '']
129+
['id' => $ruleId, 'object_filter' => $eventRule['object_filter'] ?? '']
148130
)
149131
))
150132
->registerElement($eventRuleConfigSubmitButton)
151-
->populate($config);
133+
->populate($eventRule);
152134

153135
$eventRuleConfig
154-
->on(Form::ON_SENT, function (Form $form) use ($config) {
155-
$config = array_merge($config, $form->getValues());
156-
$this->sessionNamespace->set('-1', $config);
157-
})
158-
->on(Form::ON_SUCCESS, function (EventRuleConfigForm $form) use ($config) {
136+
->on(Form::ON_SUCCESS, function (EventRuleConfigForm $form) use ($eventRule) {
159137
/** @var string $ruleId */
160-
$ruleId = $config['id'];
138+
$ruleId = $eventRule['id'];
161139
/** @var string $ruleName */
162-
$ruleName = $config['name'];
163-
$form->addOrUpdateRule($ruleId, $config);
164-
$this->sessionNamespace->delete($ruleId);
140+
$ruleName = $eventRule['name'];
141+
$eventRuleConfig = array_merge($eventRule, $form->getValues());
142+
$form->addOrUpdateRule($ruleId, $eventRuleConfig);
165143
Notification::success(sprintf(t('Successfully add event rule %s'), $ruleName));
166144
$this->redirectNow('__CLOSE__');
167145
})
168146
->handleRequest($this->getServerRequest());
169147

170148
$eventRuleForm = Html::tag('div', ['class' => 'event-rule-form'], [
171-
Html::tag('h2', $config['name'] ?? ''),
149+
Html::tag('h2', $eventRule['name'] ?? ''),
172150
Html::tag(
173151
'div',
174152
[
@@ -233,7 +211,7 @@ public function searchEditorAction(): void
233211
$editor->setSuggestionUrl(
234212
Url::fromPath(
235213
"notifications/event-rule/complete",
236-
['_disableLayout' => true, 'showCompact' => true, 'id' => Url::fromRequest()->getParams()->get('id')]
214+
['_disableLayout' => true, 'showCompact' => true, 'id' => $ruleId]
237215
)
238216
);
239217

‎application/forms/EventRuleConfigElements/EscalationCondition.php

+3-6
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ class EscalationCondition extends FieldsetElement
2424
/** @var bool Whether zero conditions allowed */
2525
public $allowZeroConditions;
2626

27-
/** @var int Number of conditions */
28-
public $count = 0;
29-
3027
/**
3128
* Set whether the zero conditions is allowed for the escalation
3229
*
@@ -110,7 +107,7 @@ protected function assemble(): void
110107
'select',
111108
$opName,
112109
[
113-
'class' => ['class' => 'operator-input', 'autosubmit'],
110+
'class' => ['class' => 'operator-input'],
114111
'options' => array_combine($operators, $operators),
115112
'required' => true
116113
]
@@ -123,7 +120,7 @@ protected function assemble(): void
123120
'select',
124121
$valName,
125122
[
126-
'class' => ['autosubmit', 'right-operand'],
123+
'class' => ['right-operand'],
127124
'options' => [
128125
'ok' => $this->translate('Ok', 'notification.severity'),
129126
'debug' => $this->translate('Debug', 'notification.severity'),
@@ -158,7 +155,7 @@ protected function assemble(): void
158155
$valName,
159156
[
160157
'required' => true,
161-
'class' => ['autosubmit', 'right-operand'],
158+
'class' => ['right-operand'],
162159
'validators' => [
163160
new CallbackValidator(function ($value, $validator) {
164161
if (! preg_match('~^\d+(?:\.?\d*)?[hms]{1}$~', $value)) {

‎application/forms/EventRuleConfigElements/EscalationRecipient.php

+7-15
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ protected function assemble(): void
6262
'select',
6363
'column_' . $i,
6464
[
65-
'class' => ['autosubmit', 'left-operand'],
65+
'class' => ['left-operand', 'autosubmit'],
6666
'options' => [
6767
'' => sprintf(' - %s - ', $this->translate('Please choose'))
6868
] + $this->fetchOptions(),
@@ -73,6 +73,9 @@ protected function assemble(): void
7373
);
7474

7575
$this->registerElement($col);
76+
/** @var string $recipientVal */
77+
$recipientVal = $this->getValue('column_' . $i) ?? '';
78+
$recipient = explode('_', $recipientVal);
7679

7780
$options = ['' => sprintf(' - %s - ', $this->translate('Please choose'))];
7881
$options += Channel::fetchChannelNames(Database::get());
@@ -82,7 +85,7 @@ protected function assemble(): void
8285
'select',
8386
'val_' . $i,
8487
[
85-
'class' => ['autosubmit', 'right-operand'],
88+
'class' => ['right-operand'],
8689
'options' => $options,
8790
'disabledOptions' => [''],
8891
'value' => $this->getPopulatedValue('val_' . $i)
@@ -97,21 +100,10 @@ protected function assemble(): void
97100
$options[''] = $this->translate('Default User Channel');
98101

99102
$val->setOptions($options);
100-
101103
$val->setDisabledOptions([]);
102-
103-
if ($this->getPopulatedValue('val_' . $i, '') === '') {
104-
$val->addAttributes(['class' => 'default-channel']);
105-
}
104+
} else {
105+
$val->addAttributes(['required' => true]);
106106
}
107-
} else {
108-
/** @var BaseFormElement $val */
109-
$val = $this->createElement('text', 'val_' . $i, [
110-
'class' => 'right-operand',
111-
'placeholder' => $this->translate('Please make a decision'),
112-
'disabled' => true,
113-
'value' => $this->getPopulatedValue('val_' . $i)
114-
]);
115107
}
116108

117109
$this->registerElement($val);

‎application/forms/EventRuleConfigForm.php

+35-48
Original file line numberDiff line numberDiff line change
@@ -46,29 +46,17 @@ class EventRuleConfigForm extends Form
4646
'id' => 'event-rule-config-form'
4747
];
4848

49-
/** @var array<string, mixed> */
50-
protected $config;
51-
5249
/** @var Url Search editor URL for the config filter fieldset */
5350
protected $searchEditorUrl;
5451

5552
/**
5653
* Create a new EventRuleConfigForm
5754
*
58-
* @param array<string, mixed> $config
59-
* @param Url $searchEditorUrl
55+
* @param Url $searchEditorUrl
6056
*/
61-
public function __construct(array $config, Url $searchEditorUrl)
57+
public function __construct(Url $searchEditorUrl)
6258
{
63-
$this->config = $config;
6459
$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-
});
7260
}
7361

7462
public function hasBeenSubmitted()
@@ -80,8 +68,6 @@ public function hasBeenSubmitted()
8068

8169
if ($buttonName === 'delete') {
8270
$this->emit(self::ON_DELETE, [$this]);
83-
} elseif ($buttonName === 'discard_changes') {
84-
$this->emit(self::ON_DISCARD, [$this]);
8571
} elseif ($buttonName === 'save') {
8672
return true;
8773
}
@@ -126,7 +112,7 @@ protected function assemble(): void
126112
);
127113

128114
/** @var string $ruleId */
129-
$ruleId = $this->config['id'];
115+
$ruleId = $this->searchEditorUrl->getParam('id');
130116
if ($ruleId === '-1') {
131117
$initialZeroConditionEscalation = bin2hex('1');
132118
} else {
@@ -140,7 +126,7 @@ protected function assemble(): void
140126
);
141127

142128
/** @var string $objectFilter */
143-
$objectFilter = $this->config['object_filter'] ?? '';
129+
$objectFilter = $this->searchEditorUrl->getParam('object_filter', '');
144130
$configFilter = (new EventRuleConfigFilter('config-filter'))
145131
->setObjectFilter($objectFilter)
146132
->setSearchEditorUrl($this->searchEditorUrl);
@@ -393,13 +379,19 @@ public function getValues(): array
393379
$prefixesMap = explode(',', $prefixesString);
394380
$i = 1;
395381
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+
403395
$i++;
404396
}
405397

@@ -421,7 +413,7 @@ public function getValues(): array
421413
protected function createRemoveButton(string $prefix): SubmitButtonElement
422414
{
423415
/** @var array<int, array<string, mixed>> $escalations */
424-
$escalations = $this->config['rule_escalation'] ?? [];
416+
$escalations = $this->getValues()['rule_escalation'];
425417

426418
$pos = hex2bin($prefix);
427419
$disableRemoveButton = false;
@@ -484,9 +476,7 @@ protected function createRemoveButton(string $prefix): SubmitButtonElement
484476
public function addOrUpdateRule(string $id, array $config): void
485477
{
486478
$db = Database::get();
487-
488479
$db->beginTransaction();
489-
490480
if ($id < 0) {
491481
$db->insert('rule', [
492482
'name' => $config['name'],
@@ -498,44 +488,41 @@ public function addOrUpdateRule(string $id, array $config): void
498488
$id = $db->lastInsertId();
499489
} else {
500490
$db->update('rule', [
501-
'name' => $config['name'],
502-
'timeperiod_id' => $config['timeperiod_id'] ?? null,
503491
'object_filter' => $config['object_filter'] ?? null,
504-
'is_active' => $config['is_active'] ?? 'n'
505492
], ['id = ?' => $id]);
506493
}
507494

508495
$escalationsFromDb = RuleEscalation::on($db)
509496
->filter(Filter::equal('rule_id', $id));
510497

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'];
513500

514501
$escalationsToUpdate = [];
515502
$escalationsToRemove = [];
516503

517504
/** @var RuleEscalation $escalationFromDB */
518505
foreach ($escalationsFromDb as $escalationFromDB) {
519506
$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;
524511
});
525512

526-
if ($escalationInCache) {
527-
$position = array_key_first($escalationInCache);
513+
if ($escalationInForm) {
514+
$position = array_key_first($escalationInForm);
528515
// Escalations in DB to update
529-
$escalationsToUpdate[$position] = $escalationInCache[$position];
530-
unset($escalationsInCache[$position]);
516+
$escalationsToUpdate[$position] = $escalationInForm[$position];
517+
unset($escalationsInForm[$position]);
531518
} else {
532519
// Escalation in DB to remove
533520
$escalationsToRemove[] = $escalationId;
534521
}
535522
}
536523

537524
// Escalations to add
538-
$escalationsToAdd = $escalationsInCache;
525+
$escalationsToAdd = $escalationsInForm;
539526

540527
if (! empty($escalationsToRemove)) {
541528
$db->delete('rule_escalation_recipient', ['rule_escalation_id IN (?)' => $escalationsToRemove]);
@@ -598,17 +585,17 @@ private function insertOrUpdateEscalations(
598585
/** @var RuleEscalationRecipient $recipient */
599586
foreach ($recipients as $recipient) {
600587
$recipientId = $recipient->id;
601-
$recipientInCache = array_filter(
588+
$recipientInForm = array_filter(
602589
$recipientsFromConfig,
603590
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;
607594
}
608595
);
609596

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
612599
$recipientsToRemove[] = $recipientId;
613600
}
614601
}

‎public/css/detail/event-rule-detail.less

-8
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,6 @@
33
align-items: baseline;
44
}
55

6-
.cache-notice {
7-
margin: 1em;
8-
padding: 1em;
9-
background-color: @gray-lighter;
10-
text-align: center;
11-
.rounded-corners();
12-
}
13-
146
.new-event-rule {
157
margin-bottom: 1em;
168
}

‎public/css/event-rule-config.less

-4
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,6 @@
184184
}
185185
}
186186

187-
.default-channel {
188-
color: @disabled-gray;
189-
}
190-
191187
select, input {
192188
min-width: 10em;
193189
text-align: center;

0 commit comments

Comments
 (0)
Please sign in to comment.