Skip to content

Commit dd2c1a5

Browse files
committed
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

Lines changed: 26 additions & 23 deletions
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

Lines changed: 17 additions & 39 deletions
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

Lines changed: 3 additions & 6 deletions
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

Lines changed: 7 additions & 15 deletions
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);

0 commit comments

Comments
 (0)