Skip to content

Commit 4c41287

Browse files
committed
Fix issues in implementation
1 parent e67aa3e commit 4c41287

File tree

9 files changed

+157
-154
lines changed

9 files changed

+157
-154
lines changed

application/controllers/EventRuleController.php

+27-15
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) {
@@ -194,7 +197,7 @@ public function fromDb(int $ruleId): array
194197
$requiredValues = [];
195198

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

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

307315
$eventRuleForm = (new EventRuleForm())
@@ -312,7 +320,11 @@ public function editAction(): void
312320
$config['is_active'] = $form->getValue('is_active');
313321
$params = [];
314322
if ($ruleId === '-1') {
315-
$params = $config;
323+
$params = [
324+
'id' => -1,
325+
'name' => $config['name'],
326+
'is_active' => $config['is_active']
327+
];
316328
} else {
317329
$params['id'] = $ruleId;
318330
}

application/controllers/EventRulesController.php

+8-6
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 */
@@ -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

+27-24
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

@@ -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

+18-19
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

application/forms/EventRuleConfigElements/EventRuleConfigFilter.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class EventRuleConfigFilter extends FieldsetElement
1616
/** @var Url Url of the search editor */
1717
protected $searchEditorUrl;
1818

19-
/** @var ?string Event rule's object filter*/
19+
/** @var ?string Event rule's object filter */
2020
protected $objectFilter;
2121

2222
protected $defaultAttributes = ['class' => 'config-filter'];
@@ -34,10 +34,10 @@ protected function assemble(): void
3434
'submitButton',
3535
'add-filter',
3636
[
37-
'class' => ['add-button', 'control-button', 'spinner'],
38-
'label' => new Icon('plus'),
37+
'class' => ['add-button', 'control-button', 'spinner'],
38+
'label' => new Icon('plus'),
3939
'formnovalidate' => true,
40-
'title' => $this->translate('Add filter')
40+
'title' => $this->translate('Add filter')
4141
]
4242
);
4343

@@ -47,11 +47,13 @@ protected function assemble(): void
4747
if ($this->objectFilter !== '' || $addFilterButton->hasBeenPressed()) {
4848
$showSearchBar = '1';
4949
$this->getElement('show-searchbar')->setValue($showSearchBar);
50+
$this->removeAttribute('class', 'empty-filter');
5051
}
5152

5253
if ($showSearchBar === '0') {
5354
/** @var SubmitButtonElement $filterElement */
5455
$filterElement = $addFilterButton;
56+
$this->addAttributes(['class' => 'empty-filter']);
5557
} else {
5658
$editorOpener = new Link(
5759
new Icon('cog'),

0 commit comments

Comments
 (0)