Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
329 changes: 148 additions & 181 deletions application/controllers/EventRuleController.php

Large diffs are not rendered by default.

143 changes: 19 additions & 124 deletions application/controllers/EventRulesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,29 @@

use Icinga\Module\Notifications\Common\Database;
use Icinga\Module\Notifications\Common\Links;
use Icinga\Module\Notifications\Forms\SaveEventRuleForm;
use Icinga\Module\Notifications\Forms\EventRuleForm;
use Icinga\Module\Notifications\Model\Rule;
use Icinga\Module\Notifications\View\EventRuleRenderer;
use Icinga\Module\Notifications\Web\Control\SearchBar\ObjectSuggestions;
use Icinga\Module\Notifications\Widget\EventRuleConfig;
use Icinga\Module\Notifications\Widget\ItemList\ObjectList;
use Icinga\Web\Notification;
use Icinga\Web\Session;
use ipl\Html\Html;
use ipl\Stdlib\Filter;
use ipl\Html\Form;
use ipl\Web\Compat\CompatController;
use ipl\Web\Compat\SearchControls;
use ipl\Web\Control\LimitControl;
use ipl\Web\Control\SearchEditor;
use ipl\Web\Control\SortControl;
use ipl\Web\Filter\QueryString;
use ipl\Web\Layout\DetailedItemLayout;
use ipl\Web\Url;
use ipl\Web\Widget\ButtonLink;
use ipl\Web\Widget\Icon;
use ipl\Web\Widget\Link;

class EventRulesController extends CompatController
{
use SearchControls;

/** @var Filter\Rule Filter from query string parameters */
private $filter;

/** @var Session\SessionNamespace */
private $sessionNamespace;

public function init()
{
$this->assertPermission('notifications/config/event-rules');
$this->sessionNamespace = Session::getSession()->getNamespace('notifications');
}

public function indexAction(): void
Expand All @@ -53,8 +40,8 @@ public function indexAction(): void
$sortControl = $this->createSortControl(
$eventRules,
[
'name' => t('Name'),
'changed_at' => t('Changed At')
'name' => $this->translate('Name'),
'changed_at' => $this->translate('Changed At')
]
);

Expand All @@ -68,7 +55,7 @@ public function indexAction(): void

if ($searchBar->hasBeenSent() && ! $searchBar->isValid()) {
if ($searchBar->hasBeenSubmitted()) {
$filter = $this->getFilter();
$filter = QueryString::parse((string) $this->params);
} else {
$this->addControl($searchBar);
$this->sendMultipartUpdate();
Expand All @@ -84,13 +71,14 @@ public function indexAction(): void
$this->addControl($sortControl);
$this->addControl($limitControl);
$this->addControl($searchBar);

$this->addContent(
(new ButtonLink(
t('New Event Rule'),
Url::fromPath('notifications/event-rule/edit', ['id' => -1, 'clearCache' => true]),
'plus'
$this->translate('Add Event Rule'),
Url::fromPath('notifications/event-rules/add'),
'plus',
['class' => 'add-new-component']
))->openInModal()
->addAttributes(['class' => 'add-new-component'])
);

$this->addContent(
Expand All @@ -108,69 +96,19 @@ public function indexAction(): void

public function addAction(): void
{
$this->addTitleTab(t('Add Event Rule'));
$this->getTabs()->setRefreshUrl(Url::fromPath('notifications/event-rules/add'));

$this->controls->addAttributes(['class' => 'event-rule-detail']);

if ($this->params->has('use_cache') || $this->getServerRequest()->getMethod() !== 'GET') {
$cache = $this->sessionNamespace->get(-1, []);
} else {
$this->sessionNamespace->delete(-1);

$cache = [];
}

$eventRuleConfig = new EventRuleConfig(Url::fromPath('notifications/event-rules/add-search-editor'), $cache);
$this->setTitle($this->translate('Add Event Rule'));

$eventRuleForm = Html::tag('div', ['class' => 'event-rule-form'], [
Html::tag('h2', $eventRuleConfig->getConfig()['name'] ?? ''),
(new Link(
new Icon('edit'),
Url::fromPath('notifications/event-rule/edit', [
'id' => -1
]),
['class' => 'control-button']
))->openInModal()
]);

$saveForm = (new SaveEventRuleForm())
->on(SaveEventRuleForm::ON_SUCCESS, function ($saveForm) use ($eventRuleConfig) {
if (! $eventRuleConfig->isValid()) {
$eventRuleConfig->addAttributes(['class' => 'invalid']);
return;
}

$id = $saveForm->addRule($this->sessionNamespace->get(-1));

Notification::success($this->translate('Successfully added rule.'));
$eventRuleForm = (new EventRuleForm())
->populate(['id' => -1])
->setCsrfCounterMeasureId(Session::getSession()->getId())
->setAction(Url::fromRequest()->getAbsoluteUrl())
->on(Form::ON_SUCCESS, function ($form) {
$this->sendExtraUpdates(['#col1']);
$this->redirectNow(Links::eventRule($id));
$this->getResponse()->setHeader('X-Icinga-Container', 'col2');
$this->redirectNow(Links::eventRule(-1)->addParams(['name' => $form->getValue('name')]));
})->handleRequest($this->getServerRequest());

$eventRuleConfig->on(EventRuleConfig::ON_CHANGE, function ($eventRuleConfig) {
$this->sessionNamespace->set(-1, $eventRuleConfig->getConfig());

$this->redirectNow(Url::fromPath('notifications/event-rules/add', ['use_cache' => true]));
});

foreach ($eventRuleConfig->getForms() as $f) {
$f->handleRequest($this->getServerRequest());

if (! $f->hasBeenSent()) {
// Force validation of populated values in case we display an unsaved rule
$f->validatePartial();
}
}

$eventRuleFormAndSave = Html::tag('div', ['class' => 'event-rule-and-save-forms']);
$eventRuleFormAndSave->add([
$eventRuleForm,
$saveForm
]);

$this->addControl($eventRuleFormAndSave);
$this->addContent($eventRuleConfig);
$this->addContent($eventRuleForm);
}

public function completeAction(): void
Expand All @@ -195,49 +133,6 @@ public function searchEditorAction(): void
$this->setTitle($this->translate('Adjust Filter'));
}

public function addSearchEditorAction(): void
{
$cache = $this->sessionNamespace->get(-1);

$editor = EventRuleConfig::createSearchEditor()
->setQueryString($cache['object_filter'] ?? '');

$editor->on(SearchEditor::ON_SUCCESS, function (SearchEditor $form) {
$cache = $this->sessionNamespace->get(-1);
$cache['object_filter'] = EventRuleConfig::createFilterString($form->getFilter());

$this->sessionNamespace->set(-1, $cache);

$this->getResponse()
->setHeader('X-Icinga-Container', '_self')
->redirectAndExit(
Url::fromPath(
'notifications/event-rules/add',
['use_cache' => true]
)
);
});

$editor->handleRequest($this->getServerRequest());

$this->getDocument()->addHtml($editor);
$this->setTitle($this->translate('Adjust Filter'));
}

/**
* Get the filter created from query string parameters
*
* @return Filter\Rule
*/
protected function getFilter(): Filter\Rule
{
if ($this->filter === null) {
$this->filter = QueryString::parse((string) $this->params);
}

return $this->filter;
}

public function getTabs()
{
if ($this->getRequest()->getActionName() === 'index') {
Expand Down
42 changes: 0 additions & 42 deletions application/forms/AddEscalationForm.php

This file was deleted.

42 changes: 0 additions & 42 deletions application/forms/AddFilterForm.php

This file was deleted.

89 changes: 0 additions & 89 deletions application/forms/BaseEscalationForm.php

This file was deleted.

Loading
Loading