diff --git a/application/controllers/EventRuleController.php b/application/controllers/EventRuleController.php index fee84617c..40975385c 100644 --- a/application/controllers/EventRuleController.php +++ b/application/controllers/EventRuleController.php @@ -7,11 +7,13 @@ use Icinga\Module\Notifications\Common\Auth; use Icinga\Module\Notifications\Common\Database; use Icinga\Module\Notifications\Common\Links; +use Icinga\Module\Notifications\Forms\EventRuleConfigElements\EventRuleConfigFilter; use Icinga\Module\Notifications\Forms\EventRuleConfigForm; use Icinga\Module\Notifications\Forms\EventRuleForm; use Icinga\Module\Notifications\Model\Incident; use Icinga\Module\Notifications\Model\Rule; use Icinga\Module\Notifications\Web\Control\SearchBar\ExtraTagSuggestions; +use Icinga\Util\Environment; use Icinga\Web\Notification; use Icinga\Web\Session; use ipl\Html\Attributes; @@ -31,36 +33,23 @@ class EventRuleController extends CompatController { use Auth; - /** @var Session\SessionNamespace */ - private $sessionNamespace; - /** @var ?string Event rule config filter */ protected $filter; - public function init() - { - $this->sessionNamespace = Session::getSession()->getNamespace('notifications'); - } - public function indexAction(): void { $this->assertPermission('notifications/config/event-rule'); - $this->sessionNamespace->delete('-1'); + $this->addContent(Html::tag('div', ['class' => 'container', 'id' => 'dummy'])); $this->addTitleTab(t('Event Rule')); $this->controls->addAttributes(['class' => 'event-rule-detail']); /** @var string $ruleId */ $ruleId = $this->params->getRequired('id'); - /** @var array|null $configValues */ - $configValues = $this->sessionNamespace->get($ruleId); $this->controls->addAttributes(['class' => 'event-rule-detail']); $disableSave = false; - if ($configValues === null) { - $configValues = $this->fromDb((int) $ruleId); - $disableSave = true; - } + $configValues = $this->fromDb((int) $ruleId); $eventRuleConfig = (new EventRuleConfigForm( $configValues, @@ -72,51 +61,16 @@ public function indexAction(): void $eventRuleConfig ->on(Form::ON_SUCCESS, function (EventRuleConfigForm $form) use ($ruleId, $configValues) { $form->addOrUpdateRule($ruleId, $configValues); - $this->sessionNamespace->delete($ruleId); Notification::success((sprintf(t('Successfully saved event rule %s'), $configValues['name']))); $this->redirectNow(Links::eventRule((int) $ruleId)); }) ->on(EventRuleConfigForm::ON_DELETE, function (EventRuleConfigForm $form) use ($ruleId, $configValues) { $form->removeRule((int) $ruleId); - $this->sessionNamespace->delete($ruleId); Notification::success(sprintf(t('Successfully deleted event rule %s'), $configValues['name'])); $this->redirectNow('__CLOSE__'); }) - ->on(EventRuleConfigForm::ON_DISCARD, function () use ($ruleId, $configValues) { - $this->sessionNamespace->delete($ruleId); - Notification::success( - sprintf( - t('Successfully discarded changes to event rule %s'), - $configValues['name'] - ) - ); - $this->redirectNow(Links::eventRule((int) $ruleId)); - }) - ->on(EventRuleConfigForm::ON_CHANGE, function (EventRuleConfigForm $form) use ($ruleId, $configValues) { - $configValues = array_merge($configValues, $form->getValues()); - $configValues['rule_escalation'] = $form->getValues()['rule_escalation']; - $this->sessionNamespace->set($ruleId, $configValues); - }) ->handleRequest($this->getServerRequest()); - /** @var array $cache */ - $cache = $this->sessionNamespace->get($ruleId); - $discardChangesButton = null; - if ($cache !== null) { - $this->addContent(Html::tag('div', ['class' => 'cache-notice'], t('There are unsaved changes.'))); - $discardChangesButton = (new SubmitButtonElement( - 'discard_changes', - [ - 'label' => t('Discard Changes'), - 'form' => 'event-rule-config-form', - 'class' => 'btn-discard-changes', - 'formnovalidate' => true, - ] - )); - $disableSave = false; - } - - $buttonsWrapper = new HtmlElement('div', Attributes::create(['class' => ['icinga-controls', 'save-config']])); $eventRuleConfigSubmitButton = (new SubmitButtonElement( 'save', @@ -137,7 +91,7 @@ public function indexAction(): void )); $buttonsWrapper->add( - [$eventRuleConfigSubmitButton, $discardChangesButton, $deleteButton] + [$eventRuleConfigSubmitButton, $deleteButton] ); if ($ruleId > 0) { @@ -150,7 +104,7 @@ public function indexAction(): void } } - $eventRuleForm = Html::tag('div', ['class' => 'event-rule-form'], [ + $eventRuleForm = Html::tag('div', ['class' => 'event-rule-form', 'id' => 'event-rule-form'], [ Html::tag('h2', $configValues['name'] ?? ''), (new Link( new Icon('edit'), @@ -166,6 +120,40 @@ public function indexAction(): void $this->addContent($eventRuleConfig); } + public function ruleAction(): void + { + $ruleId = $this->params->getRequired('id'); + $query = Rule::on(Database::get()) + ->withoutColumns('timeperiod_id') + ->filter(Filter::equal('id', $ruleId)); + + $rule = $query->first(); + + $this->getDocument()->add([ + Html::tag('h2', $rule->name ?? ''), + (new Link( + new Icon('edit'), + Url::fromPath('notifications/event-rule/edit', [ + 'id' => $ruleId + ]), + ['class' => 'control-button'] + ))->openInModal() + ]); + } + + public function configFilterAction(): void + { + $ruleId = $this->params->getRequired('id'); + $objectFilter = $this->params->getRequired('object_filter'); + $this->getDocument()->add((new EventRuleConfigFilter('config-filter')) + ->setObjectFilter($objectFilter) + ->setSearchEditorUrl( + Url::fromPath('notifications/event-rule/search-editor', ['id' => $ruleId]) + ) + ->ensureAssembled() + ->getContent()); + } + /** * Create config from db * @@ -237,12 +225,7 @@ public function searchEditorAction(): void { /** @var string $ruleId */ $ruleId = $this->params->shiftRequired('id'); - - $eventRule = $this->sessionNamespace->get($ruleId); - - if ($eventRule === null) { - $eventRule = $this->fromDb((int) $ruleId); - } + $eventRule = $this->fromDb($ruleId); $editor = new SearchEditor(); @@ -259,16 +242,20 @@ public function searchEditorAction(): void $editor->on(SearchEditor::ON_SUCCESS, function (SearchEditor $form) use ($ruleId, $eventRule) { $filter = self::createFilterString($form->getFilter()); - $eventRule['object_filter'] = $filter; - $this->sessionNamespace->set($ruleId, $eventRule); - $this->getResponse() - ->setHeader('X-Icinga-Container', '_self') - ->redirectAndExit( - Url::fromPath( - 'notifications/event-rule', - ['id' => $ruleId] + $this->sendExtraUpdates( + [ + '#config-filter' => Url::fromPath( + 'notifications/event-rule/config-filter', + [ + 'id' => $ruleId, + 'object_filter' => $filter + ] ) - ); + ] + ); + $this->getResponse() + ->setHeader('X-Icinga-Container', 'dummy') + ->redirectAndExit('__CLOSE__'); }); $editor->handleRequest($this->getServerRequest()); @@ -304,13 +291,10 @@ public function editAction(): void /** @var string $ruleId */ $ruleId = $this->params->getRequired('id'); /** @var array|null $config */ - $config = $this->sessionNamespace->get($ruleId); - if ($config === null) { - if ($ruleId === '-1') { - $config = ['id' => $ruleId]; - } else { - $config = $this->fromDb((int) $ruleId); - } + if ($ruleId === '-1') { + $config = ['id' => $ruleId]; + } else { + $config = $this->fromDb((int) $ruleId); } $eventRuleForm = (new EventRuleForm()) @@ -332,14 +316,22 @@ public function editAction(): void if ($ruleId === '-1') { $redirectUrl = Url::fromPath('notifications/event-rules/add', $params); + $this->redirectNow($redirectUrl); } else { - $redirectUrl = Url::fromPath('notifications/event-rule', $params); - $this->sendExtraUpdates(['#col1']); + Database::get()->update('rule', [ + 'name' => $config['name'], + 'is_active' => $config['is_active'] ?? 'n' + ], ['id = ?' => $ruleId]); + $this->sendExtraUpdates([ + '#event-rule-form' => Url::fromPath( + 'notifications/event-rule/rule', + ['id' => $ruleId] + )->getAbsoluteUrl() + ]); + + $this->getResponse()->setHeader('X-Icinga-Container', 'dummy') + ->redirectAndExit('__CLOSE__'); } - - $this->sessionNamespace->set($ruleId, $config); - $this->getResponse()->setHeader('X-Icinga-Container', 'col2'); - $this->redirectNow($redirectUrl); })->handleRequest($this->getServerRequest()); if ($ruleId === '-1') { diff --git a/application/controllers/EventRulesController.php b/application/controllers/EventRulesController.php index 6fecaab40..54df2770c 100644 --- a/application/controllers/EventRulesController.php +++ b/application/controllers/EventRulesController.php @@ -157,13 +157,20 @@ public function addAction(): void $eventRuleForm = Html::tag('div', ['class' => 'event-rule-form'], [ Html::tag('h2', $config['name'] ?? ''), - (new Link( - new Icon('edit'), - Url::fromPath('notifications/event-rule/edit', [ - 'id' => -1 - ]), - ['class' => 'control-button'] - ))->openInModal() + Html::tag( + 'div', + [ + 'class' => 'not-allowed', + 'title' => $this->translate('Cannot edit event rule until it is saved to database') + ], + (new Link( + new Icon('edit'), + Url::fromPath('notifications/event-rule/edit', [ + 'id' => -1 + ]), + ['class' => ['control-button', 'disabled']] + ))->openInModal() + ) ]); $this->addControl($eventRuleForm); diff --git a/application/forms/EventRuleConfigElements/EventRuleConfigFilter.php b/application/forms/EventRuleConfigElements/EventRuleConfigFilter.php index 7f43eb374..3c8a1bbb3 100644 --- a/application/forms/EventRuleConfigElements/EventRuleConfigFilter.php +++ b/application/forms/EventRuleConfigElements/EventRuleConfigFilter.php @@ -19,7 +19,7 @@ class EventRuleConfigFilter extends FieldsetElement /** @var ?string Event rule's object filter */ protected $objectFilter; - protected $defaultAttributes = ['class' => 'config-filter']; + protected $defaultAttributes = ['class' => 'config-filter', 'id' => 'config-filter']; protected function assemble(): void { diff --git a/public/css/detail/event-rule-detail.less b/public/css/detail/event-rule-detail.less index 0e3c83e1b..dcc68a8b0 100644 --- a/public/css/detail/event-rule-detail.less +++ b/public/css/detail/event-rule-detail.less @@ -38,6 +38,14 @@ width: 25em; } } + + .control-button.disabled { + pointer-events: none; + } + + .not-allowed { + cursor: not-allowed; + } } .save-config {