diff --git a/application/controllers/EventRuleController.php b/application/controllers/EventRuleController.php index d41958c41..3a8ea5da4 100644 --- a/application/controllers/EventRuleController.php +++ b/application/controllers/EventRuleController.php @@ -46,36 +46,40 @@ public function indexAction(): void $ruleId = $this->params->getRequired('id'); $this->controls->addAttributes(['class' => 'event-rule-detail']); - $disableSave = false; - $configValues = $this->fromDb((int) $ruleId); - /** @var array $configFilter */ - $configFilter = $this->getRequest()->get('config-filter'); - if ($this->getRequest()->has('searchbar')) { - $configValues['object_filter'] = $this->getRequest()->get('searchbar'); - } elseif ($configFilter !== null) { - if (isset($configFilter['show-searchbar']) && $configFilter['show-searchbar'] === '0') { - $configValues['object_filter'] = ''; + $eventRuleConfigValues = $this->fromDb((int) $ruleId); + + if ($this->getRequest()->isPost()) { + if ($this->getRequest()->has('searchbar')) { + $eventRuleConfigValues['object_filter'] = $this->getRequest()->get('searchbar'); + } else { + $eventRuleConfigValues['object_filter'] = ''; } } $eventRuleConfig = (new EventRuleConfigForm( - $configValues, Url::fromPath( 'notifications/event-rule/search-editor', - ['id' => $ruleId, 'object_filter' => $configValues['object_filter']] + ['id' => $ruleId, 'object_filter' => $eventRuleConfigValues['object_filter']] ) - ))->populate($configValues); - $eventRuleConfig - ->on(Form::ON_SUCCESS, function (EventRuleConfigForm $form) use ($ruleId, $configValues) { - $form->addOrUpdateRule($ruleId, $configValues); - Notification::success((sprintf(t('Successfully saved event rule %s'), $configValues['name']))); + )) + ->populate($eventRuleConfigValues) + ->on(Form::ON_SUCCESS, function (EventRuleConfigForm $form) use ($ruleId, $eventRuleConfigValues) { + $config = $form->getValues(); + $config['object_filter'] = $eventRuleConfigValues['object_filter']; + $form->addOrUpdateRule($ruleId, $config); + Notification::success((sprintf(t('Successfully saved event rule %s'), $eventRuleConfigValues['name']))); $this->redirectNow(Links::eventRule((int) $ruleId)); }) - ->on(EventRuleConfigForm::ON_DELETE, function (EventRuleConfigForm $form) use ($ruleId, $configValues) { - $form->removeRule((int) $ruleId); - Notification::success(sprintf(t('Successfully deleted event rule %s'), $configValues['name'])); - $this->redirectNow('__CLOSE__'); - }) + ->on( + EventRuleConfigForm::ON_DELETE, + function (EventRuleConfigForm $form) use ($ruleId, $eventRuleConfigValues) { + $form->removeRule((int) $ruleId); + Notification::success( + sprintf(t('Successfully deleted event rule %s'), $eventRuleConfigValues['name']) + ); + $this->redirectNow('__CLOSE__'); + } + ) ->handleRequest($this->getServerRequest()); $buttonsWrapper = new HtmlElement('div', Attributes::create(['class' => ['icinga-controls', 'save-config']])); @@ -84,7 +88,6 @@ public function indexAction(): void [ 'label' => t('Save'), 'form' => 'event-rule-config-form', - 'disabled' => $disableSave ] )); $deleteButton = (new SubmitButtonElement( @@ -112,7 +115,7 @@ public function indexAction(): void } $eventRuleForm = Html::tag('div', ['class' => 'event-rule-form', 'id' => 'event-rule-form'], [ - Html::tag('h2', $configValues['name'] ?? ''), + Html::tag('h2', $eventRuleConfigValues['name'] ?? ''), (new Link( new Icon('edit'), Url::fromPath('notifications/event-rule/edit', [ diff --git a/application/controllers/EventRulesController.php b/application/controllers/EventRulesController.php index dcb912190..75f8beb8c 100644 --- a/application/controllers/EventRulesController.php +++ b/application/controllers/EventRulesController.php @@ -11,7 +11,6 @@ use Icinga\Module\Notifications\Web\Control\SearchBar\ObjectSuggestions; use Icinga\Module\Notifications\Widget\ItemList\EventRuleList; use Icinga\Web\Notification; -use Icinga\Web\Session; use ipl\Html\Attributes; use ipl\Html\Form; use ipl\Html\FormElement\SubmitButtonElement; @@ -34,20 +33,14 @@ class EventRulesController extends CompatController /** @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 { $eventRules = Rule::on(Database::get()); - $this->sessionNamespace->delete('-1'); - $limitControl = $this->createLimitControl(); $paginationControl = $this->createPaginationControl($eventRules); $sortControl = $this->createSortControl( @@ -109,25 +102,15 @@ public function addAction(): void $this->addContent(Html::tag('div', ['class' => 'container', 'id' => 'dummy'])); $this->controls->addAttributes(['class' => 'event-rule-detail']); - /** @var string $ruleId */ - $ruleId = $this->params->get('id') ?? '-1'; - - $params = $this->params->toArray(false); - /** @var array|null $config */ - $config = $this->sessionNamespace->get($ruleId); + /** @var array $eventRule */ + $eventRule = $this->params->toArray(false); + $ruleId = $eventRule['id'] ?? '-1'; - if ($config === null) { - /** @var array $config */ - $config = $params; - } - - /** @var array $configFilter */ - $configFilter = $this->getRequest()->get('config-filter'); - if ($this->getRequest()->has('searchbar')) { - $config['object_filter'] = $this->getRequest()->get('searchbar'); - } elseif ($configFilter !== null) { - if (isset($configFilter['show-searchbar']) && $configFilter['show-searchbar'] === '0') { - $config['object_filter'] = ''; + if ($this->getRequest()->isPost()) { + if ($this->getRequest()->has('searchbar')) { + $eventRule['object_filter'] = $this->getRequest()->get('searchbar'); + } else { + $eventRule['object_filter'] = ''; } } @@ -141,34 +124,29 @@ public function addAction(): void ))->setWrapper(new HtmlElement('div', Attributes::create(['class' => ['icinga-controls', 'save-config']]))); $eventRuleConfig = (new EventRuleConfigForm( - $config, Url::fromPath( 'notifications/event-rules/search-editor', - ['id' => $ruleId, 'object_filter' => $config['object_filter'] ?? ''] + ['id' => $ruleId, 'object_filter' => $eventRule['object_filter'] ?? ''] ) )) ->registerElement($eventRuleConfigSubmitButton) - ->populate($config); + ->populate($eventRule); $eventRuleConfig - ->on(Form::ON_SENT, function (Form $form) use ($config) { - $config = array_merge($config, $form->getValues()); - $this->sessionNamespace->set('-1', $config); - }) - ->on(Form::ON_SUCCESS, function (EventRuleConfigForm $form) use ($config) { + ->on(Form::ON_SUCCESS, function (EventRuleConfigForm $form) use ($eventRule) { /** @var string $ruleId */ - $ruleId = $config['id']; + $ruleId = $eventRule['id']; /** @var string $ruleName */ - $ruleName = $config['name']; - $form->addOrUpdateRule($ruleId, $config); - $this->sessionNamespace->delete($ruleId); + $ruleName = $eventRule['name']; + $eventRuleConfig = array_merge($eventRule, $form->getValues()); + $form->addOrUpdateRule($ruleId, $eventRuleConfig); Notification::success(sprintf(t('Successfully add event rule %s'), $ruleName)); $this->redirectNow('__CLOSE__'); }) ->handleRequest($this->getServerRequest()); $eventRuleForm = Html::tag('div', ['class' => 'event-rule-form'], [ - Html::tag('h2', $config['name'] ?? ''), + Html::tag('h2', $eventRule['name'] ?? ''), Html::tag( 'div', [ @@ -233,7 +211,7 @@ public function searchEditorAction(): void $editor->setSuggestionUrl( Url::fromPath( "notifications/event-rule/complete", - ['_disableLayout' => true, 'showCompact' => true, 'id' => Url::fromRequest()->getParams()->get('id')] + ['_disableLayout' => true, 'showCompact' => true, 'id' => $ruleId] ) ); diff --git a/application/forms/EventRuleConfigElements/EscalationCondition.php b/application/forms/EventRuleConfigElements/EscalationCondition.php index 1bbbc1ff3..047409253 100644 --- a/application/forms/EventRuleConfigElements/EscalationCondition.php +++ b/application/forms/EventRuleConfigElements/EscalationCondition.php @@ -24,9 +24,6 @@ class EscalationCondition extends FieldsetElement /** @var bool Whether zero conditions allowed */ public $allowZeroConditions; - /** @var int Number of conditions */ - public $count = 0; - /** * Set whether the zero conditions is allowed for the escalation * @@ -110,7 +107,7 @@ protected function assemble(): void 'select', $opName, [ - 'class' => ['class' => 'operator-input', 'autosubmit'], + 'class' => ['class' => 'operator-input'], 'options' => array_combine($operators, $operators), 'required' => true ] @@ -123,7 +120,7 @@ protected function assemble(): void 'select', $valName, [ - 'class' => ['autosubmit', 'right-operand'], + 'class' => ['right-operand'], 'options' => [ 'ok' => $this->translate('Ok', 'notification.severity'), 'debug' => $this->translate('Debug', 'notification.severity'), @@ -158,7 +155,7 @@ protected function assemble(): void $valName, [ 'required' => true, - 'class' => ['autosubmit', 'right-operand'], + 'class' => ['right-operand'], 'validators' => [ new CallbackValidator(function ($value, $validator) { if (! preg_match('~^\d+(?:\.?\d*)?[hms]{1}$~', $value)) { diff --git a/application/forms/EventRuleConfigElements/EscalationRecipient.php b/application/forms/EventRuleConfigElements/EscalationRecipient.php index c61faf388..e18746edf 100644 --- a/application/forms/EventRuleConfigElements/EscalationRecipient.php +++ b/application/forms/EventRuleConfigElements/EscalationRecipient.php @@ -62,7 +62,7 @@ protected function assemble(): void 'select', 'column_' . $i, [ - 'class' => ['autosubmit', 'left-operand'], + 'class' => ['left-operand', 'autosubmit'], 'options' => [ '' => sprintf(' - %s - ', $this->translate('Please choose')) ] + $this->fetchOptions(), @@ -73,6 +73,9 @@ protected function assemble(): void ); $this->registerElement($col); + /** @var string $recipientVal */ + $recipientVal = $this->getValue('column_' . $i) ?? ''; + $recipient = explode('_', $recipientVal); $options = ['' => sprintf(' - %s - ', $this->translate('Please choose'))]; $options += Channel::fetchChannelNames(Database::get()); @@ -82,7 +85,7 @@ protected function assemble(): void 'select', 'val_' . $i, [ - 'class' => ['autosubmit', 'right-operand'], + 'class' => ['right-operand'], 'options' => $options, 'disabledOptions' => [''], 'value' => $this->getPopulatedValue('val_' . $i) @@ -97,21 +100,10 @@ protected function assemble(): void $options[''] = $this->translate('Default User Channel'); $val->setOptions($options); - $val->setDisabledOptions([]); - - if ($this->getPopulatedValue('val_' . $i, '') === '') { - $val->addAttributes(['class' => 'default-channel']); - } + } else { + $val->addAttributes(['required' => true]); } - } else { - /** @var BaseFormElement $val */ - $val = $this->createElement('text', 'val_' . $i, [ - 'class' => 'right-operand', - 'placeholder' => $this->translate('Please make a decision'), - 'disabled' => true, - 'value' => $this->getPopulatedValue('val_' . $i) - ]); } $this->registerElement($val); diff --git a/application/forms/EventRuleConfigForm.php b/application/forms/EventRuleConfigForm.php index 7daf43045..c06d8397b 100644 --- a/application/forms/EventRuleConfigForm.php +++ b/application/forms/EventRuleConfigForm.php @@ -46,29 +46,17 @@ class EventRuleConfigForm extends Form 'id' => 'event-rule-config-form' ]; - /** @var array */ - protected $config; - /** @var Url Search editor URL for the config filter fieldset */ protected $searchEditorUrl; /** * Create a new EventRuleConfigForm * - * @param array $config - * @param Url $searchEditorUrl + * @param Url $searchEditorUrl */ - public function __construct(array $config, Url $searchEditorUrl) + public function __construct(Url $searchEditorUrl) { - $this->config = $config; $this->searchEditorUrl = $searchEditorUrl; - $this->on(self::ON_SENT, function () { - $config = array_merge($this->config, $this->getValues()); - - if ($config !== $this->config) { - $this->emit(self::ON_CHANGE, [$this]); - } - }); } public function hasBeenSubmitted() @@ -80,8 +68,6 @@ public function hasBeenSubmitted() if ($buttonName === 'delete') { $this->emit(self::ON_DELETE, [$this]); - } elseif ($buttonName === 'discard_changes') { - $this->emit(self::ON_DISCARD, [$this]); } elseif ($buttonName === 'save') { return true; } @@ -126,7 +112,7 @@ protected function assemble(): void ); /** @var string $ruleId */ - $ruleId = $this->config['id']; + $ruleId = $this->searchEditorUrl->getParam('id'); if ($ruleId === '-1') { $initialZeroConditionEscalation = bin2hex('1'); } else { @@ -140,7 +126,7 @@ protected function assemble(): void ); /** @var string $objectFilter */ - $objectFilter = $this->config['object_filter'] ?? ''; + $objectFilter = $this->searchEditorUrl->getParam('object_filter', ''); $configFilter = (new EventRuleConfigFilter('config-filter')) ->setObjectFilter($objectFilter) ->setSearchEditorUrl($this->searchEditorUrl); @@ -393,13 +379,19 @@ public function getValues(): array $prefixesMap = explode(',', $prefixesString); $i = 1; foreach ($prefixesMap as $prefixMap) { - /** @var EscalationCondition $escalationCondition */ - $escalationCondition = $this->getElement('escalation-condition_' . $prefixMap); - /** @var EscalationRecipient $escalationRecipient */ - $escalationRecipient = $this->getElement('escalation-recipient_' . $prefixMap); - $escalations[$i]['condition'] = $escalationCondition->getCondition(); - $escalations[$i]['id'] = $escalationCondition->getValue('id'); - $escalations[$i]['recipients'] = $escalationRecipient->getRecipients(); + if ($this->hasElement('escalation-condition_' . $prefixMap)) { + /** @var EscalationCondition $escalationCondition */ + $escalationCondition = $this->getElement('escalation-condition_' . $prefixMap); + $escalations[$i]['condition'] = $escalationCondition->getCondition(); + $escalations[$i]['id'] = $escalationCondition->getValue('id'); + } + + if ($this->hasElement('escalation-condition_' . $prefixMap)) { + /** @var EscalationRecipient $escalationRecipient */ + $escalationRecipient = $this->getElement('escalation-recipient_' . $prefixMap); + $escalations[$i]['recipients'] = $escalationRecipient->getRecipients(); + } + $i++; } @@ -421,7 +413,7 @@ public function getValues(): array protected function createRemoveButton(string $prefix): SubmitButtonElement { /** @var array> $escalations */ - $escalations = $this->config['rule_escalation'] ?? []; + $escalations = $this->getValues()['rule_escalation']; $pos = hex2bin($prefix); $disableRemoveButton = false; @@ -484,9 +476,7 @@ protected function createRemoveButton(string $prefix): SubmitButtonElement public function addOrUpdateRule(string $id, array $config): void { $db = Database::get(); - $db->beginTransaction(); - if ($id < 0) { $db->insert('rule', [ 'name' => $config['name'], @@ -498,18 +488,15 @@ public function addOrUpdateRule(string $id, array $config): void $id = $db->lastInsertId(); } else { $db->update('rule', [ - 'name' => $config['name'], - 'timeperiod_id' => $config['timeperiod_id'] ?? null, 'object_filter' => $config['object_filter'] ?? null, - 'is_active' => $config['is_active'] ?? 'n' ], ['id = ?' => $id]); } $escalationsFromDb = RuleEscalation::on($db) ->filter(Filter::equal('rule_id', $id)); - /** @var array> $escalationsInCache */ - $escalationsInCache = $config['rule_escalation']; + /** @var array> $escalationsInForm */ + $escalationsInForm = $config['rule_escalation']; $escalationsToUpdate = []; $escalationsToRemove = []; @@ -517,17 +504,17 @@ public function addOrUpdateRule(string $id, array $config): void /** @var RuleEscalation $escalationFromDB */ foreach ($escalationsFromDb as $escalationFromDB) { $escalationId = $escalationFromDB->id; - $escalationInCache = array_filter($escalationsInCache, function (array $element) use ($escalationId) { - /** @var string $idInCache */ - $idInCache = $element['id'] ?? null; - return (int) $idInCache === $escalationId; + $escalationInForm = array_filter($escalationsInForm, function (array $element) use ($escalationId) { + /** @var string $idInForm */ + $idInForm = $element['id'] ?? null; + return (int) $idInForm === $escalationId; }); - if ($escalationInCache) { - $position = array_key_first($escalationInCache); + if ($escalationInForm) { + $position = array_key_first($escalationInForm); // Escalations in DB to update - $escalationsToUpdate[$position] = $escalationInCache[$position]; - unset($escalationsInCache[$position]); + $escalationsToUpdate[$position] = $escalationInForm[$position]; + unset($escalationsInForm[$position]); } else { // Escalation in DB to remove $escalationsToRemove[] = $escalationId; @@ -535,7 +522,7 @@ public function addOrUpdateRule(string $id, array $config): void } // Escalations to add - $escalationsToAdd = $escalationsInCache; + $escalationsToAdd = $escalationsInForm; if (! empty($escalationsToRemove)) { $db->delete('rule_escalation_recipient', ['rule_escalation_id IN (?)' => $escalationsToRemove]); @@ -598,17 +585,17 @@ private function insertOrUpdateEscalations( /** @var RuleEscalationRecipient $recipient */ foreach ($recipients as $recipient) { $recipientId = $recipient->id; - $recipientInCache = array_filter( + $recipientInForm = array_filter( $recipientsFromConfig, function (array $element) use ($recipientId) { - /** @var string $idFromCache */ - $idFromCache = $element['id']; - return (int) $idFromCache === $recipientId; + /** @var string $idFromForm */ + $idFromForm = $element['id']; + return (int) $idFromForm === $recipientId; } ); - if (empty($recipientInCache)) { - // Recipients to remove from Db not in cache + if (empty($recipientInForm)) { + // Recipients to remove from Db not in form $recipientsToRemove[] = $recipientId; } } diff --git a/public/css/detail/event-rule-detail.less b/public/css/detail/event-rule-detail.less index dcc68a8b0..522ee23a2 100644 --- a/public/css/detail/event-rule-detail.less +++ b/public/css/detail/event-rule-detail.less @@ -3,14 +3,6 @@ align-items: baseline; } -.cache-notice { - margin: 1em; - padding: 1em; - background-color: @gray-lighter; - text-align: center; - .rounded-corners(); -} - .new-event-rule { margin-bottom: 1em; } diff --git a/public/css/event-rule-config.less b/public/css/event-rule-config.less index 241976a90..b3c7274bb 100644 --- a/public/css/event-rule-config.less +++ b/public/css/event-rule-config.less @@ -184,10 +184,6 @@ } } - .default-channel { - color: @disabled-gray; - } - select, input { min-width: 10em; text-align: center;