Skip to content

Commit 3775b8c

Browse files
committed
WIP
1 parent 4ba49e7 commit 3775b8c

File tree

10 files changed

+1773
-170
lines changed

10 files changed

+1773
-170
lines changed

application/controllers/EventRuleController.php

Lines changed: 149 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,22 @@
66

77
use Icinga\Module\Notifications\Common\Auth;
88
use Icinga\Module\Notifications\Common\Database;
9-
use Icinga\Module\Notifications\Common\Links;
9+
use Icinga\Module\Notifications\Forms\EventRuleConfigForm;
1010
use Icinga\Module\Notifications\Forms\EventRuleForm;
11-
use Icinga\Module\Notifications\Forms\SaveEventRuleForm;
12-
use Icinga\Module\Notifications\Model\Incident;
13-
use Icinga\Module\Notifications\Model\ObjectExtraTag;
1411
use Icinga\Module\Notifications\Model\Rule;
1512
use Icinga\Module\Notifications\Web\Control\SearchBar\ExtraTagSuggestions;
16-
use Icinga\Module\Notifications\Widget\EventRuleConfig;
17-
use Icinga\Web\Notification;
13+
use Icinga\Module\Notifications\Web\Form\EventRuleDecorator;
1814
use Icinga\Web\Session;
15+
use ipl\Html\Attributes;
1916
use ipl\Html\Form;
17+
use ipl\Html\FormElement\ButtonElement;
18+
use ipl\Html\FormElement\SubmitButtonElement;
2019
use ipl\Html\Html;
20+
use ipl\Html\HtmlElement;
2121
use ipl\Stdlib\Filter;
2222
use ipl\Web\Compat\CompatController;
2323
use ipl\Web\Control\SearchEditor;
24+
use ipl\Web\Filter\QueryString;
2425
use ipl\Web\Url;
2526
use ipl\Web\Widget\Icon;
2627
use ipl\Web\Widget\Link;
@@ -32,6 +33,9 @@ class EventRuleController extends CompatController
3233
/** @var Session\SessionNamespace */
3334
private $sessionNamespace;
3435

36+
/** @var ?string Event rule config filter */
37+
protected $filter;
38+
3539
public function init()
3640
{
3741
$this->sessionNamespace = Session::getSession()->getNamespace('notifications');
@@ -42,71 +46,99 @@ public function indexAction(): void
4246
$this->assertPermission('notifications/config/event-rules');
4347

4448
$this->addTitleTab(t('Event Rule'));
45-
4649
$this->controls->addAttributes(['class' => 'event-rule-detail']);
4750

48-
$ruleId = $this->params->getRequired('id');
49-
50-
$cache = $this->sessionNamespace->get($ruleId);
51+
$ruleId = (int) $this->params->getRequired('id');
5152

52-
if ($cache) {
53-
$this->addContent(Html::tag('div', ['class' => 'cache-notice'], t('There are unsaved changes.')));
54-
$eventRuleConfig = new EventRuleConfig(
55-
Url::fromPath('notifications/event-rule/search-editor', ['id' => $ruleId]),
56-
$cache
57-
);
58-
} else {
59-
$eventRuleConfig = new EventRuleConfig(
60-
Url::fromPath('notifications/event-rule/search-editor', ['id' => $ruleId]),
61-
$this->fromDb($ruleId)
62-
);
63-
}
64-
65-
$disableRemoveButton = false;
66-
if (ctype_digit($ruleId)) {
67-
$incidents = Incident::on(Database::get())
68-
->with('rule')
69-
->filter(Filter::equal('rule.id', $ruleId));
53+
$this->controls->addAttributes(['class' => 'event-rule-detail']);
54+
$config = $this->sessionNamespace->get($ruleId);
7055

71-
if ($incidents->count() > 0) {
72-
$disableRemoveButton = true;
73-
}
56+
if ($config === null) {
57+
$config = $this->fromDb($ruleId);
58+
$this->sessionNamespace->set($ruleId, $config);
7459
}
7560

76-
$saveForm = (new SaveEventRuleForm())
77-
->setShowRemoveButton()
78-
->setShowDismissChangesButton($cache !== null)
79-
->setRemoveButtonDisabled($disableRemoveButton)
80-
->setSubmitButtonDisabled($cache === null)
81-
->setSubmitLabel($this->translate('Save Changes'))
82-
->on(SaveEventRuleForm::ON_SUCCESS, function ($form) use ($ruleId, $eventRuleConfig) {
83-
if ($form->getPressedSubmitElement()->getName() === 'discard_changes') {
84-
$this->sessionNamespace->delete($ruleId);
85-
Notification::success($this->translate('Successfully discarded the pending changes.'));
86-
$this->redirectNow(Links::eventRule($ruleId));
87-
}
88-
89-
if (! $eventRuleConfig->isValid()) {
90-
$eventRuleConfig->addAttributes(['class' => 'invalid']);
91-
return;
61+
// $config = $this->fromDb($ruleId);
62+
//
63+
// if ($filter === '' && isset($config['object_filter'])) {
64+
// $filter = $config['object_filter'];
65+
// }
66+
67+
$eventRuleConfigSubmitButton = (new SubmitButtonElement(
68+
'save',
69+
[
70+
'label' => t('Save'),
71+
'form' => 'event-rule-config-form'
72+
]
73+
))->setWrapper(new HtmlElement('div', Attributes::create(['class' => ['icinga-controls', 'save-config']])));
74+
75+
$discardChangesButton = (new SubmitButtonElement(
76+
'discard_changes',
77+
[
78+
'label' => t('Discard Changes'),
79+
'form' => 'event-rule-config-form',
80+
'formnovalidate' => true,
81+
]
82+
))
83+
->setWrapper(new HtmlElement('div', Attributes::create(['class' => ['icinga-controls', 'save-config']])));
84+
85+
$deleteButton = (new SubmitButtonElement(
86+
'delete',
87+
[
88+
'label' => t('Delete'),
89+
'form' => 'event-rule-config-form',
90+
'class' => 'btn-remove',
91+
'formnovalidate' => true
92+
]
93+
))
94+
->setWrapper(new HtmlElement('div', Attributes::create(['class' => ['icinga-controls', 'save-config']])));
95+
96+
// $eventRuleDecorator = new EventRuleDecorator();
97+
// $eventRuleDecorator->decorate($eventRuleConfigSubmitButton);
98+
// $eventRuleDecorator->decorate($discardChangesButton);
99+
100+
$eventRuleConfig = (new EventRuleConfigForm(
101+
$config['object_filter'] ?? '',
102+
Url::fromPath('notifications/event-rule/search-editor', ['id' => $ruleId])
103+
))
104+
->registerElement($eventRuleConfigSubmitButton)
105+
->registerElement($discardChangesButton)
106+
->registerElement($deleteButton);
107+
108+
$eventRuleConfig->populate($config);
109+
$eventRuleConfig
110+
->on(Form::ON_SUCCESS, function (EventRuleConfigForm $form) use ($deleteButton, $eventRuleConfigSubmitButton, $config) {
111+
$pressedButton = $form->getPressedSubmitElement();
112+
if ($pressedButton) {
113+
var_dump($pressedButton->getName());die;
114+
if ($pressedButton->getName() === 'delete') {
115+
$this->sessionNamespace->delete($config['id']);
116+
} else {
117+
$db = Database::get();
118+
$ruleId = (int) $config['id'];
119+
if ($ruleId !== -1) {
120+
// var_dump($config);die;
121+
$db->update('rule', [
122+
'name' => $config['name'],
123+
'timeperiod_id' => $config['timeperiod_id'] ?? null,
124+
'object_filter' => $config['object_filter'] ?? null,
125+
'is_active' => $config['is_active'] ?? 'n'
126+
], ['id = ?' => $ruleId]);
127+
128+
$form->insertOrAddRule($ruleId, $config);
129+
}
130+
}
92131
}
132+
})
133+
->on(Form::ON_SENT, function (Form $form) use ($config, $ruleId) {
134+
$config = array_merge($config, $form->getValues());
135+
$this->sessionNamespace->set($ruleId, $config);
136+
});
93137

94-
$form->editRule($ruleId, $this->sessionNamespace->get($ruleId));
95-
$this->sessionNamespace->delete($ruleId);
96-
97-
Notification::success($this->translate('Successfully updated rule.'));
98-
$this->sendExtraUpdates(['#col1']);
99-
$this->redirectNow(Links::eventRule($ruleId));
100-
})->on(SaveEventRuleForm::ON_REMOVE, function ($form) use ($ruleId) {
101-
$form->removeRule($ruleId);
102-
$this->sessionNamespace->delete($ruleId);
103-
104-
Notification::success($this->translate('Successfully removed rule.'));
105-
$this->redirectNow('__CLOSE__');
106-
})->handleRequest($this->getServerRequest());
138+
$eventRuleConfig->handleRequest($this->getServerRequest());
107139

108140
$eventRuleForm = Html::tag('div', ['class' => 'event-rule-form'], [
109-
Html::tag('h2', $eventRuleConfig->getConfig()['name'] ?? ''),
141+
Html::tag('h2', $config['name'] ?? ''),
110142
(new Link(
111143
new Icon('edit'),
112144
Url::fromPath('notifications/event-rule/edit', [
@@ -115,30 +147,11 @@ public function indexAction(): void
115147
['class' => 'control-button']
116148
))->openInModal()
117149
]);
150+
$this->addControl($eventRuleForm);
151+
$this->addControl($eventRuleConfigSubmitButton);
152+
$this->addControl($discardChangesButton);
153+
$this->addControl($deleteButton);
118154

119-
$eventRuleFormAndSave = Html::tag('div', ['class' => 'event-rule-and-save-forms']);
120-
$eventRuleFormAndSave->add([
121-
$eventRuleForm,
122-
$saveForm
123-
]);
124-
125-
$eventRuleConfig
126-
->on(EventRuleConfig::ON_CHANGE, function ($eventRuleConfig) use ($ruleId, $saveForm) {
127-
$this->sessionNamespace->set($ruleId, $eventRuleConfig->getConfig());
128-
$saveForm->setSubmitButtonDisabled(false);
129-
$this->redirectNow(Links::eventRule($ruleId));
130-
});
131-
132-
foreach ($eventRuleConfig->getForms() as $form) {
133-
$form->handleRequest($this->getServerRequest());
134-
135-
if (! $form->hasBeenSent()) {
136-
// Force validation of populated values in case we display an unsaved rule
137-
$form->validatePartial();
138-
}
139-
}
140-
141-
$this->addControl($eventRuleFormAndSave);
142155
$this->addContent($eventRuleConfig);
143156
}
144157

@@ -167,7 +180,7 @@ public function fromDb(int $ruleId): array
167180
}
168181

169182
foreach ($re->rule_escalation_recipient as $recipient) {
170-
$config[$re->getTableName()][$re->position]['recipient'][] = iterator_to_array($recipient);
183+
$config[$re->getTableName()][$re->position]['recipients'][] = iterator_to_array($recipient);
171184
}
172185
}
173186

@@ -188,7 +201,6 @@ public function completeAction(): void
188201
$this->getDocument()->add($suggestions);
189202
}
190203

191-
192204
/**
193205
* searchEditorAction for Object Extra Tags
194206
*
@@ -200,13 +212,24 @@ public function searchEditorAction(): void
200212
{
201213
$ruleId = $this->params->shiftRequired('id');
202214

203-
$eventRule = $this->sessionNamespace->get($ruleId) ?? $this->fromDb($ruleId);
215+
$eventRule = $this->sessionNamespace->get($ruleId);
204216

205-
$editor = EventRuleConfig::createSearchEditor()
206-
->setQueryString($eventRule['object_filter'] ?? '');
217+
if ($eventRule === null) {
218+
$eventRule = $this->fromDb($ruleId);
219+
}
220+
221+
$editor = new SearchEditor();
222+
223+
$editor->setQueryString($eventRule['object_filter'] ?? '');
224+
$editor->setAction(Url::fromRequest()->getAbsoluteUrl());
225+
$editor->setSuggestionUrl(Url::fromPath(
226+
"notifications/event-rule/complete",
227+
['_disableLayout' => true, 'showCompact' => true, 'id' => Url::fromRequest()->getParams()->get('id')]
228+
));
207229

208230
$editor->on(SearchEditor::ON_SUCCESS, function (SearchEditor $form) use ($ruleId, $eventRule) {
209-
$eventRule['object_filter'] = EventRuleConfig::createFilterString($form->getFilter());
231+
$filter = self::createFilterString($form->getFilter());
232+
$eventRule['object_filter'] = $filter;
210233

211234
$this->sessionNamespace->set($ruleId, $eventRule);
212235
$this->getResponse()
@@ -225,45 +248,62 @@ public function searchEditorAction(): void
225248
$this->setTitle($this->translate('Adjust Filter'));
226249
}
227250

251+
public static function createFilterString($filters): string
252+
{
253+
foreach ($filters as $filter) {
254+
if ($filter instanceof Filter\Chain) {
255+
self::createFilterString($filter);
256+
} elseif (empty($filter->getValue())) {
257+
$filter->setValue(true);
258+
}
259+
}
260+
261+
if ($filters instanceof Filter\Condition && empty($filters->getValue())) {
262+
$filters->setValue(true);
263+
}
264+
265+
$filterStr = QueryString::render($filters);
266+
267+
return ! empty($filterStr) ? $filterStr : '';
268+
}
269+
228270
public function editAction(): void
229271
{
230272
/** @var string $ruleId */
231273
$ruleId = $this->params->getRequired('id');
232-
/** @var ?array<string, mixed> $cache */
233-
$cache = $this->sessionNamespace->get($ruleId);
234-
235-
if ($this->params->has('clearCache')) {
236-
$this->sessionNamespace->delete($ruleId);
237-
$cache = [];
238-
}
239274

240-
if (isset($cache) || $ruleId === '-1') {
241-
$config = $cache ?? [];
275+
if ($ruleId === '-1') {
276+
$config = ['id' => $ruleId];
242277
} else {
243278
$config = $this->fromDb((int) $ruleId);
244279
}
245280

246281
$eventRuleForm = (new EventRuleForm())
247282
->populate($config)
248283
->setAction(Url::fromRequest()->getAbsoluteUrl())
249-
->on(Form::ON_SUCCESS, function ($form) use ($ruleId, $cache, $config) {
284+
->on(Form::ON_SUCCESS, function ($form) use ($ruleId, $config) {
250285
$config['name'] = $form->getValue('name');
251286
$config['is_active'] = $form->getValue('is_active');
252287

253-
if ($cache || $ruleId === '-1') {
254-
$this->sessionNamespace->set($ruleId, $config);
288+
$db = Database::get();
289+
$params = [];
290+
if ($ruleId === '-1') {
291+
$params = $config;
255292
} else {
256-
(new SaveEventRuleForm())->editRule((int) $ruleId, $config);
293+
$db->update('rule', [
294+
'name' => $config['name'],
295+
'timeperiod_id' => $config['timeperiod_id'] ?? null,
296+
'object_filter' => $config['object_filter'] ?? null,
297+
'is_active' => $config['is_active'] ?? 'n'
298+
], ['id = ?' => $ruleId]);
299+
300+
$params['id'] = $ruleId;
257301
}
258302

259303
if ($ruleId === '-1') {
260-
$redirectUrl = Url::fromPath('notifications/event-rules/add', [
261-
'use_cache' => true
262-
]);
304+
$redirectUrl = Url::fromPath('notifications/event-rules/add', $params);
263305
} else {
264-
$redirectUrl = Url::fromPath('notifications/event-rule', [
265-
'id' => $ruleId
266-
]);
306+
$redirectUrl = Url::fromPath('notifications/event-rule', $params);
267307
$this->sendExtraUpdates(['#col1']);
268308
}
269309

0 commit comments

Comments
 (0)