Skip to content

Commit c1364de

Browse files
committed
Fix issues in implementation
1 parent af10941 commit c1364de

File tree

9 files changed

+168
-165
lines changed

9 files changed

+168
-165
lines changed

application/controllers/EventRuleController.php

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public function init()
4545
public function indexAction(): void
4646
{
4747
$this->assertPermission('notifications/config/event-rule');
48+
$this->sessionNamespace->delete('-1');
4849

4950
$this->addTitleTab(t('Event Rule'));
5051
$this->controls->addAttributes(['class' => 'event-rule-detail']);
@@ -83,10 +84,12 @@ public function indexAction(): void
8384
})
8485
->on(EventRuleConfigForm::ON_DISCARD, function () use ($ruleId, $configValues) {
8586
$this->sessionNamespace->delete($ruleId);
86-
Notification::success(sprintf(
87-
t('Successfully discarded changes to event rule %s'),
88-
$configValues['name']
89-
));
87+
Notification::success(
88+
sprintf(
89+
t('Successfully discarded changes to event rule %s'),
90+
$configValues['name']
91+
)
92+
);
9093
$this->redirectNow(Links::eventRule((int) $ruleId));
9194
})
9295
->on(EventRuleConfigForm::ON_CHANGE, function (EventRuleConfigForm $form) use ($ruleId, $configValues) {
@@ -195,7 +198,7 @@ public function fromDb(int $ruleId): array
195198
$requiredValues = [];
196199

197200
foreach ($recipient as $k => $v) {
198-
if (in_array($k, ['contact_id', 'contactgroup_id', 'schedule_id']) && $v !== null) {
201+
if (in_array($k, ['contact_id', 'contactgroup_id', 'schedule_id']) && $v !== null) {
199202
$requiredValues[$k] = (string) $v;
200203
} elseif (in_array($k, ['id', 'channel_id'])) {
201204
$requiredValues[$k] = $v ? (string) $v : null;
@@ -247,10 +250,12 @@ public function searchEditorAction(): void
247250
$objectFilter = $eventRule['object_filter'] ?? '';
248251
$editor->setQueryString($objectFilter);
249252
$editor->setAction(Url::fromRequest()->getAbsoluteUrl());
250-
$editor->setSuggestionUrl(Url::fromPath(
251-
"notifications/event-rule/complete",
252-
['_disableLayout' => true, 'showCompact' => true, 'id' => Url::fromRequest()->getParams()->get('id')]
253-
));
253+
$editor->setSuggestionUrl(
254+
Url::fromPath(
255+
"notifications/event-rule/complete",
256+
['_disableLayout' => true, 'showCompact' => true, 'id' => Url::fromRequest()->getParams()->get('id')]
257+
)
258+
);
254259

255260
$editor->on(SearchEditor::ON_SUCCESS, function (SearchEditor $form) use ($ruleId, $eventRule) {
256261
$filter = self::createFilterString($form->getFilter());
@@ -298,11 +303,14 @@ public function editAction(): void
298303
{
299304
/** @var string $ruleId */
300305
$ruleId = $this->params->getRequired('id');
301-
302-
if ($ruleId === '-1') {
303-
$config = ['id' => $ruleId];
304-
} else {
305-
$config = $this->fromDb((int) $ruleId);
306+
/** @var array<string, mixed>|null $config */
307+
$config = $this->sessionNamespace->get($ruleId);
308+
if ($config === null) {
309+
if ($ruleId === '-1') {
310+
$config = ['id' => $ruleId];
311+
} else {
312+
$config = $this->fromDb((int) $ruleId);
313+
}
306314
}
307315

308316
$eventRuleForm = (new EventRuleForm())
@@ -313,7 +321,11 @@ public function editAction(): void
313321
$config['is_active'] = $form->getValue('is_active');
314322
$params = [];
315323
if ($ruleId === '-1') {
316-
$params = $config;
324+
$params = [
325+
'id' => -1,
326+
'name' => $config['name'],
327+
'is_active' => $config['is_active']
328+
];
317329
} else {
318330
$params['id'] = $ruleId;
319331
}

application/controllers/EventRulesController.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Icinga\Module\Notifications\Forms\EventRuleConfigForm;
99
use Icinga\Module\Notifications\Model\Rule;
1010
use Icinga\Module\Notifications\Web\Control\SearchBar\ObjectSuggestions;
11-
use Icinga\Module\Notifications\Widget\EventRuleConfig;
1211
use Icinga\Module\Notifications\Widget\ItemList\EventRuleList;
1312
use Icinga\Web\Notification;
1413
use Icinga\Web\Session;
@@ -46,6 +45,7 @@ public function init()
4645
public function indexAction(): void
4746
{
4847
$eventRules = Rule::on(Database::get());
48+
$this->sessionNamespace->delete('-1');
4949

5050
$limitControl = $this->createLimitControl();
5151
$paginationControl = $this->createPaginationControl($eventRules);
@@ -108,7 +108,7 @@ public function addAction(): void
108108

109109
$this->controls->addAttributes(['class' => 'event-rule-detail']);
110110
/** @var string $ruleId */
111-
$ruleId = $this->params->getRequired('id');
111+
$ruleId = $this->params->get('id') ?? '-1';
112112

113113
$params = $this->params->toArray(false);
114114
/** @var array<string, mixed>|null $config */
@@ -122,8 +122,8 @@ public function addAction(): void
122122
$eventRuleConfigSubmitButton = (new SubmitButtonElement(
123123
'save',
124124
[
125-
'label' => t('Add Event Rule'),
126-
'form' => 'event-rule-config-form',
125+
'label' => t('Add Event Rule'),
126+
'form' => 'event-rule-config-form',
127127
'formnovalidate' => true
128128
]
129129
))->setWrapper(new HtmlElement('div', Attributes::create(['class' => ['icinga-controls', 'save-config']])));
@@ -197,10 +197,12 @@ public function searchEditorAction(): void
197197
$objectFilter = $eventRule['object_filter'] ?? '';
198198
$editor->setQueryString($objectFilter);
199199
$editor->setAction(Url::fromRequest()->getAbsoluteUrl());
200-
$editor->setSuggestionUrl(Url::fromPath(
201-
"notifications/event-rule/complete",
202-
['_disableLayout' => true, 'showCompact' => true, 'id' => Url::fromRequest()->getParams()->get('id')]
203-
));
200+
$editor->setSuggestionUrl(
201+
Url::fromPath(
202+
"notifications/event-rule/complete",
203+
['_disableLayout' => true, 'showCompact' => true, 'id' => Url::fromRequest()->getParams()->get('id')]
204+
)
205+
);
204206

205207
$editor->on(SearchEditor::ON_SUCCESS, function (SearchEditor $form) use ($ruleId, $eventRule) {
206208
$filter = self::createFilterString($form->getFilter());

application/forms/EventRuleConfigElements/EscalationCondition.php

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -60,24 +60,25 @@ protected function assemble(): void
6060
'submitButton',
6161
'add-condition',
6262
[
63-
'class' => ['add-button', 'control-button', 'spinner'],
64-
'label' => new Icon('plus'),
65-
'title' => $this->translate('Add Condition'),
66-
'formnovalidate' => true
63+
'class' => ['add-button', 'control-button', 'spinner'],
64+
'label' => new Icon('plus'),
65+
'title' => $this->translate('Add Condition'),
66+
'formnovalidate' => true
6767
]
6868
);
6969

7070
$this->registerElement($addCondition);
7171

7272
/** @var string|int $conditionCount */
7373
$conditionCount = $this->getValue('condition-count');
74+
$conditionCount = (int) $conditionCount;
7475
$this->addElement(
7576
'hidden',
7677
'id'
7778
);
7879

7980
if ($addCondition->hasBeenPressed()) {
80-
$conditionCount += 1;
81+
$conditionCount = $conditionCount + 1;
8182
$this->getElement('condition-count')->setValue($conditionCount);
8283
}
8384

@@ -92,14 +93,14 @@ protected function assemble(): void
9293
'select',
9394
$colName,
9495
[
95-
'class' => ['autosubmit', 'left-operand'],
96-
'options' => [
97-
'' => sprintf(' - %s - ', $this->translate('Please choose')),
96+
'class' => ['autosubmit', 'left-operand'],
97+
'options' => [
98+
'' => sprintf(' - %s - ', $this->translate('Please choose')),
9899
'incident_severity' => $this->translate('Incident Severity'),
99-
'incident_age' => $this->translate('Incident Age')
100+
'incident_age' => $this->translate('Incident Age')
100101
],
101-
'disabledOptions' => [''],
102-
'required' => true
102+
'disabledOptions' => [''],
103+
'required' => true
103104
]
104105
);
105106

@@ -109,8 +110,8 @@ protected function assemble(): void
109110
'select',
110111
$opName,
111112
[
112-
'class' => ['class' => 'operator-input', 'autosubmit'],
113-
'options' => array_combine($operators, $operators),
113+
'class' => ['class' => 'operator-input', 'autosubmit'],
114+
'options' => array_combine($operators, $operators),
114115
'required' => true
115116
]
116117
);
@@ -146,7 +147,7 @@ protected function assemble(): void
146147
}
147148

148149
$this->addElement('hidden', $typeName, [
149-
'value' => 'incident_severity'
150+
'value' => 'incident_severity'
150151
]);
151152

152153
break;
@@ -156,15 +157,17 @@ protected function assemble(): void
156157
'text',
157158
$valName,
158159
[
159-
'required' => true,
160-
'class' => ['autosubmit', 'right-operand'],
160+
'required' => true,
161+
'class' => ['autosubmit', 'right-operand'],
161162
'validators' => [
162163
new CallbackValidator(function ($value, $validator) {
163164
if (! preg_match('~^\d+(?:\.?\d*)?[hms]{1}$~', $value)) {
164-
$validator->addMessage($this->translate(
165-
'Only numbers with optional fractions (separated by a dot)'
166-
. ' and one of these suffixes are allowed: h, m, s'
167-
));
165+
$validator->addMessage(
166+
$this->translate(
167+
'Only numbers with optional fractions (separated by a dot)'
168+
. ' and one of these suffixes are allowed: h, m, s'
169+
)
170+
);
168171

169172
return false;
170173
}
@@ -194,7 +197,7 @@ protected function assemble(): void
194197
$val = $this->createElement('text', $valName, [
195198
'class' => 'right-operand',
196199
'placeholder' => $this->translate('Please make a decision'),
197-
'disabled' => true
200+
'disabled' => true
198201
]);
199202
}
200203

@@ -269,11 +272,11 @@ protected function createRemoveButton(int $count): ?SubmitButtonElement
269272
'submitButton',
270273
'remove',
271274
[
272-
'class' => ['remove-button', 'control-button', 'spinner'],
273-
'label' => new Icon('minus'),
274-
'title' => $this->translate('Remove'),
275-
'formnovalidate' => true,
276-
'value' => (string) $count
275+
'class' => ['remove-button', 'control-button', 'spinner'],
276+
'label' => new Icon('minus'),
277+
'title' => $this->translate('Remove'),
278+
'formnovalidate' => true,
279+
'value' => (string) $count
277280
]
278281
);
279282

application/forms/EventRuleConfigElements/EscalationRecipient.php

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class EscalationRecipient extends FieldsetElement
2020
{
2121
protected $defaultAttributes = ['class' => 'escalation-recipient'];
2222

23-
/** @var EscalationRecipientListItem[] */
23+
/** @var EscalationRecipientListItem[] */
2424
protected $recipients = [];
2525

2626
protected function assemble(): void
@@ -36,9 +36,9 @@ protected function assemble(): void
3636
'submitButton',
3737
'add-recipient',
3838
[
39-
'class' => ['add-button', 'control-button', 'spinner'],
40-
'label' => new Icon('plus'),
41-
'title' => $this->translate('Add Recipient'),
39+
'class' => ['add-button', 'control-button', 'spinner'],
40+
'label' => new Icon('plus'),
41+
'title' => $this->translate('Add Recipient'),
4242
'formnovalidate' => true
4343
]
4444
);
@@ -51,7 +51,6 @@ protected function assemble(): void
5151
$this->getElement('recipient-count')->setValue($recipientCount);
5252
}
5353

54-
$removePosition = null;
5554
foreach (range(1, $recipientCount) as $i) {
5655
$this->addElement(
5756
'hidden',
@@ -63,13 +62,13 @@ protected function assemble(): void
6362
'select',
6463
'column_' . $i,
6564
[
66-
'class' => ['autosubmit', 'left-operand'],
67-
'options' => [
65+
'class' => ['autosubmit', 'left-operand'],
66+
'options' => [
6867
'' => sprintf(' - %s - ', $this->translate('Please choose'))
6968
] + $this->fetchOptions(),
70-
'disabledOptions' => [''],
71-
'required' => true,
72-
'value' => $this->getPopulatedValue('column_' . $i)
69+
'disabledOptions' => [''],
70+
'required' => true,
71+
'value' => $this->getPopulatedValue('column_' . $i)
7372
]
7473
);
7574

@@ -83,10 +82,10 @@ protected function assemble(): void
8382
'select',
8483
'val_' . $i,
8584
[
86-
'class' => ['autosubmit', 'right-operand'],
87-
'options' => $options,
88-
'disabledOptions' => [''],
89-
'value' => $this->getPopulatedValue('val_' . $i)
85+
'class' => ['autosubmit', 'right-operand'],
86+
'options' => $options,
87+
'disabledOptions' => [''],
88+
'value' => $this->getPopulatedValue('val_' . $i)
9089
]
9190
);
9291

@@ -200,11 +199,11 @@ protected function createRemoveButton(int $pos): ?FormElement
200199
'submitButton',
201200
'remove',
202201
[
203-
'class' => ['remove-button', 'control-button', 'spinner'],
204-
'label' => new Icon('minus'),
205-
'title' => $this->translate('Remove'),
206-
'formnovalidate' => true,
207-
'value' => (string) $pos
202+
'class' => ['remove-button', 'control-button', 'spinner'],
203+
'label' => new Icon('minus'),
204+
'title' => $this->translate('Remove'),
205+
'formnovalidate' => true,
206+
'value' => (string) $pos
208207
]
209208
);
210209

0 commit comments

Comments
 (0)