From 4b1380cbf388cc99e68f513e34d4f37a617bffb5 Mon Sep 17 00:00:00 2001 From: raviks789 Date: Wed, 13 Mar 2024 12:55:50 +0100 Subject: [PATCH] WIP: Recheck implementation --- .../controllers/EventRuleController.php | 22 ++-- .../controllers/EventRulesController.php | 15 ++- .../EscalationCondition.php | 55 ++++---- .../EscalationRecipient.php | 36 +++--- .../EventRuleConfigFilter.php | 22 ++-- application/forms/EventRuleConfigForm.php | 120 +++++++----------- .../Widget/ItemList/Escalations.php | 6 +- public/css/detail/event-rule-detail.less | 2 +- 8 files changed, 132 insertions(+), 146 deletions(-) diff --git a/application/controllers/EventRuleController.php b/application/controllers/EventRuleController.php index 1345518bc..bcb769b2b 100644 --- a/application/controllers/EventRuleController.php +++ b/application/controllers/EventRuleController.php @@ -83,10 +83,12 @@ public function indexAction(): void }) ->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'] - )); + 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) { @@ -195,7 +197,7 @@ public function fromDb(int $ruleId): array $requiredValues = []; foreach ($recipient as $k => $v) { - if (in_array($k, ['contact_id', 'contactgroup_id', 'schedule_id']) && $v !== null) { + if (in_array($k, ['contact_id', 'contactgroup_id', 'schedule_id']) && $v !== null) { $requiredValues[$k] = (string) $v; } elseif (in_array($k, ['id', 'channel_id'])) { $requiredValues[$k] = $v ? (string) $v : null; @@ -247,10 +249,12 @@ public function searchEditorAction(): void $objectFilter = $eventRule['object_filter'] ?? ''; $editor->setQueryString($objectFilter); $editor->setAction(Url::fromRequest()->getAbsoluteUrl()); - $editor->setSuggestionUrl(Url::fromPath( - "notifications/event-rule/complete", - ['_disableLayout' => true, 'showCompact' => true, 'id' => Url::fromRequest()->getParams()->get('id')] - )); + $editor->setSuggestionUrl( + Url::fromPath( + "notifications/event-rule/complete", + ['_disableLayout' => true, 'showCompact' => true, 'id' => Url::fromRequest()->getParams()->get('id')] + ) + ); $editor->on(SearchEditor::ON_SUCCESS, function (SearchEditor $form) use ($ruleId, $eventRule) { $filter = self::createFilterString($form->getFilter()); diff --git a/application/controllers/EventRulesController.php b/application/controllers/EventRulesController.php index 140e7040f..f19185fef 100644 --- a/application/controllers/EventRulesController.php +++ b/application/controllers/EventRulesController.php @@ -8,7 +8,6 @@ use Icinga\Module\Notifications\Forms\EventRuleConfigForm; use Icinga\Module\Notifications\Model\Rule; use Icinga\Module\Notifications\Web\Control\SearchBar\ObjectSuggestions; -use Icinga\Module\Notifications\Widget\EventRuleConfig; use Icinga\Module\Notifications\Widget\ItemList\EventRuleList; use Icinga\Web\Notification; use Icinga\Web\Session; @@ -122,8 +121,8 @@ public function addAction(): void $eventRuleConfigSubmitButton = (new SubmitButtonElement( 'save', [ - 'label' => t('Add Event Rule'), - 'form' => 'event-rule-config-form', + 'label' => t('Add Event Rule'), + 'form' => 'event-rule-config-form', 'formnovalidate' => true ] ))->setWrapper(new HtmlElement('div', Attributes::create(['class' => ['icinga-controls', 'save-config']]))); @@ -197,10 +196,12 @@ public function searchEditorAction(): void $objectFilter = $eventRule['object_filter'] ?? ''; $editor->setQueryString($objectFilter); $editor->setAction(Url::fromRequest()->getAbsoluteUrl()); - $editor->setSuggestionUrl(Url::fromPath( - "notifications/event-rule/complete", - ['_disableLayout' => true, 'showCompact' => true, 'id' => Url::fromRequest()->getParams()->get('id')] - )); + $editor->setSuggestionUrl( + Url::fromPath( + "notifications/event-rule/complete", + ['_disableLayout' => true, 'showCompact' => true, 'id' => Url::fromRequest()->getParams()->get('id')] + ) + ); $editor->on(SearchEditor::ON_SUCCESS, function (SearchEditor $form) use ($ruleId, $eventRule) { $filter = self::createFilterString($form->getFilter()); diff --git a/application/forms/EventRuleConfigElements/EscalationCondition.php b/application/forms/EventRuleConfigElements/EscalationCondition.php index 8b2405bc4..1bbbc1ff3 100644 --- a/application/forms/EventRuleConfigElements/EscalationCondition.php +++ b/application/forms/EventRuleConfigElements/EscalationCondition.php @@ -60,10 +60,10 @@ protected function assemble(): void 'submitButton', 'add-condition', [ - 'class' => ['add-button', 'control-button', 'spinner'], - 'label' => new Icon('plus'), - 'title' => $this->translate('Add Condition'), - 'formnovalidate' => true + 'class' => ['add-button', 'control-button', 'spinner'], + 'label' => new Icon('plus'), + 'title' => $this->translate('Add Condition'), + 'formnovalidate' => true ] ); @@ -71,13 +71,14 @@ protected function assemble(): void /** @var string|int $conditionCount */ $conditionCount = $this->getValue('condition-count'); + $conditionCount = (int) $conditionCount; $this->addElement( 'hidden', 'id' ); if ($addCondition->hasBeenPressed()) { - $conditionCount += 1; + $conditionCount = $conditionCount + 1; $this->getElement('condition-count')->setValue($conditionCount); } @@ -92,14 +93,14 @@ protected function assemble(): void 'select', $colName, [ - 'class' => ['autosubmit', 'left-operand'], - 'options' => [ - '' => sprintf(' - %s - ', $this->translate('Please choose')), + 'class' => ['autosubmit', 'left-operand'], + 'options' => [ + '' => sprintf(' - %s - ', $this->translate('Please choose')), 'incident_severity' => $this->translate('Incident Severity'), - 'incident_age' => $this->translate('Incident Age') + 'incident_age' => $this->translate('Incident Age') ], - 'disabledOptions' => [''], - 'required' => true + 'disabledOptions' => [''], + 'required' => true ] ); @@ -109,8 +110,8 @@ protected function assemble(): void 'select', $opName, [ - 'class' => ['class' => 'operator-input', 'autosubmit'], - 'options' => array_combine($operators, $operators), + 'class' => ['class' => 'operator-input', 'autosubmit'], + 'options' => array_combine($operators, $operators), 'required' => true ] ); @@ -146,7 +147,7 @@ protected function assemble(): void } $this->addElement('hidden', $typeName, [ - 'value' => 'incident_severity' + 'value' => 'incident_severity' ]); break; @@ -156,15 +157,17 @@ protected function assemble(): void 'text', $valName, [ - 'required' => true, - 'class' => ['autosubmit', 'right-operand'], + 'required' => true, + 'class' => ['autosubmit', 'right-operand'], 'validators' => [ new CallbackValidator(function ($value, $validator) { if (! preg_match('~^\d+(?:\.?\d*)?[hms]{1}$~', $value)) { - $validator->addMessage($this->translate( - 'Only numbers with optional fractions (separated by a dot)' - . ' and one of these suffixes are allowed: h, m, s' - )); + $validator->addMessage( + $this->translate( + 'Only numbers with optional fractions (separated by a dot)' + . ' and one of these suffixes are allowed: h, m, s' + ) + ); return false; } @@ -194,7 +197,7 @@ protected function assemble(): void $val = $this->createElement('text', $valName, [ 'class' => 'right-operand', 'placeholder' => $this->translate('Please make a decision'), - 'disabled' => true + 'disabled' => true ]); } @@ -269,11 +272,11 @@ protected function createRemoveButton(int $count): ?SubmitButtonElement 'submitButton', 'remove', [ - 'class' => ['remove-button', 'control-button', 'spinner'], - 'label' => new Icon('minus'), - 'title' => $this->translate('Remove'), - 'formnovalidate' => true, - 'value' => (string) $count + 'class' => ['remove-button', 'control-button', 'spinner'], + 'label' => new Icon('minus'), + 'title' => $this->translate('Remove'), + 'formnovalidate' => true, + 'value' => (string) $count ] ); diff --git a/application/forms/EventRuleConfigElements/EscalationRecipient.php b/application/forms/EventRuleConfigElements/EscalationRecipient.php index e37d5c5e6..694e5d14d 100644 --- a/application/forms/EventRuleConfigElements/EscalationRecipient.php +++ b/application/forms/EventRuleConfigElements/EscalationRecipient.php @@ -20,7 +20,7 @@ class EscalationRecipient extends FieldsetElement { protected $defaultAttributes = ['class' => 'escalation-recipient']; - /** @var EscalationRecipientListItem[] */ + /** @var EscalationRecipientListItem[] */ protected $recipients = []; protected function assemble(): void @@ -36,9 +36,9 @@ protected function assemble(): void 'submitButton', 'add-recipient', [ - 'class' => ['add-button', 'control-button', 'spinner'], - 'label' => new Icon('plus'), - 'title' => $this->translate('Add Recipient'), + 'class' => ['add-button', 'control-button', 'spinner'], + 'label' => new Icon('plus'), + 'title' => $this->translate('Add Recipient'), 'formnovalidate' => true ] ); @@ -63,13 +63,13 @@ protected function assemble(): void 'select', 'column_' . $i, [ - 'class' => ['autosubmit', 'left-operand'], - 'options' => [ + 'class' => ['autosubmit', 'left-operand'], + 'options' => [ '' => sprintf(' - %s - ', $this->translate('Please choose')) ] + $this->fetchOptions(), - 'disabledOptions' => [''], - 'required' => true, - 'value' => $this->getPopulatedValue('column_' . $i) + 'disabledOptions' => [''], + 'required' => true, + 'value' => $this->getPopulatedValue('column_' . $i) ] ); @@ -83,10 +83,10 @@ protected function assemble(): void 'select', 'val_' . $i, [ - 'class' => ['autosubmit', 'right-operand'], - 'options' => $options, - 'disabledOptions' => [''], - 'value' => $this->getPopulatedValue('val_' . $i) + 'class' => ['autosubmit', 'right-operand'], + 'options' => $options, + 'disabledOptions' => [''], + 'value' => $this->getPopulatedValue('val_' . $i) ] ); @@ -200,11 +200,11 @@ protected function createRemoveButton(int $pos): ?FormElement 'submitButton', 'remove', [ - 'class' => ['remove-button', 'control-button', 'spinner'], - 'label' => new Icon('minus'), - 'title' => $this->translate('Remove'), - 'formnovalidate' => true, - 'value' => (string) $pos + 'class' => ['remove-button', 'control-button', 'spinner'], + 'label' => new Icon('minus'), + 'title' => $this->translate('Remove'), + 'formnovalidate' => true, + 'value' => (string) $pos ] ); diff --git a/application/forms/EventRuleConfigElements/EventRuleConfigFilter.php b/application/forms/EventRuleConfigElements/EventRuleConfigFilter.php index 3f0c3b142..ae575f9c0 100644 --- a/application/forms/EventRuleConfigElements/EventRuleConfigFilter.php +++ b/application/forms/EventRuleConfigElements/EventRuleConfigFilter.php @@ -16,7 +16,7 @@ class EventRuleConfigFilter extends FieldsetElement /** @var Url Url of the search editor */ protected $searchEditorUrl; - /** @var ?string Event rule's object filter*/ + /** @var ?string Event rule's object filter */ protected $objectFilter; protected $defaultAttributes = ['class' => 'config-filter']; @@ -34,10 +34,10 @@ protected function assemble(): void 'submitButton', 'add-filter', [ - 'class' => ['add-button', 'control-button', 'spinner'], - 'label' => new Icon('plus'), + 'class' => ['add-button', 'control-button', 'spinner'], + 'label' => new Icon('plus'), 'formnovalidate' => true, - 'title' => $this->translate('Add filter') + 'title' => $this->translate('Add filter') ] ); @@ -57,19 +57,19 @@ protected function assemble(): void new Icon('cog'), $this->getSearchEditorUrl(), Attributes::create([ - 'class' => 'search-editor-opener control-button', - 'title' => t('Adjust Filter'), - 'data-icinga-modal' => true, - 'data-no-icinga-ajax' => true, + 'class' => 'search-editor-opener control-button', + 'title' => t('Adjust Filter'), + 'data-icinga-modal' => true, + 'data-no-icinga-ajax' => true, ]) ); $searchBar = new TextElement( 'searchbar', [ - 'class' => 'filter-input control-button', - 'readonly' => true, - 'value' => $this->objectFilter + 'class' => 'filter-input control-button', + 'readonly' => true, + 'value' => $this->objectFilter ] ); diff --git a/application/forms/EventRuleConfigForm.php b/application/forms/EventRuleConfigForm.php index b3b9700b9..84cf98dc8 100644 --- a/application/forms/EventRuleConfigForm.php +++ b/application/forms/EventRuleConfigForm.php @@ -49,14 +49,14 @@ class EventRuleConfigForm extends Form /** @var array */ protected $config; - /** @var Url */ + /** @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) { @@ -90,11 +90,6 @@ public function hasBeenSubmitted() return false; } - protected function generateFakeEscalationId(): string - { - return bin2hex(random_bytes(4)); - } - protected function assemble(): void { $this->addElement($this->createCsrfCounterMeasure(Session::getSession()->getId())); @@ -155,9 +150,9 @@ protected function assemble(): void 'submitButton', 'add-escalation', [ - 'class' => ['add-button', 'control-button', 'spinner'], - 'label' => new Icon('plus'), - 'title' => $this->translate('Add Escalation'), + 'class' => ['add-button', 'control-button', 'spinner'], + 'label' => new Icon('plus'), + 'title' => $this->translate('Add Escalation'), 'formnovalidate' => true ] ); @@ -199,19 +194,12 @@ protected function assemble(): void /** @var ?string $zeroConditionEscalation */ $zeroConditionEscalation = $this->getValue('zero-condition-escalation'); - if ($zeroConditionEscalation === null) { - $noZeroEscalationConditions = true; - } else { - $noZeroEscalationConditions = false; - } - $escalations = []; - $i = 1; foreach ($prefixesMap as $key => $prefixMap) { if ($removePosition === $prefixMap) { - if ($this->getValue('zero-condition-escalation') === $prefixMap) { - $this->getElement('zero-condition-escalation')->setValue(null); + if ($zeroConditionEscalation === $prefixMap) { + $zeroConditionEscalation = null; } unset($prefixesMap[$key]); @@ -237,37 +225,34 @@ protected function assemble(): void ); if ( - $this->getValue('zero-condition-escalation') === $prefixMap + $zeroConditionEscalation === $prefixMap && $escalation->addConditionHasBeenPressed() ) { - $noZeroEscalationConditions = true; - $this->clearPopulatedValue('zero-condition-escalation'); + $zeroConditionEscalation = null; } elseif ( $zeroConditionEscalation === null && $escalation->lastConditionHasBeenRemoved() ) { - $noZeroEscalationConditions = false; - $this->clearPopulatedValue('zero-condition-escalation'); - $this->getElement('zero-condition-escalation')->setValue($prefixMap); + $zeroConditionEscalation = $prefixMap; } $escalations[$prefixMap] = $escalation; - $i++; } - if ($noZeroEscalationConditions === false) { - $noZeroEscalationConditions = $this->getValue('zero-condition-escalation') === null; - } - - $this->add(Html::tag( - 'ul', - ['class' => 'filter-wrapper'], - [ - Html::tag('li', (new FlowLine())->getRightArrow()), - Html::tag('li', $configFilter), - Html::tag('li', (new FlowLine())->getHorizontalLine()) - ] - )); + $this->getElement('zero-condition-escalation')->setValue($zeroConditionEscalation); + $noZeroEscalationConditions = $zeroConditionEscalation === null; + + $this->add( + Html::tag( + 'ul', + ['class' => 'filter-wrapper'], + [ + Html::tag('li', (new FlowLine())->getRightArrow()), + Html::tag('li', $configFilter), + Html::tag('li', (new FlowLine())->getHorizontalLine()) + ] + ) + ); if ($noZeroEscalationConditions === true) { foreach ($escalations as $escalation) { @@ -277,14 +262,10 @@ protected function assemble(): void $this->getElement('zero-condition-escalation') ->setValue(null); - } else { - /** @var string $zeroConditionPosition */ - $zeroConditionPosition = $this->getValue('zero-condition-escalation'); - if ($zeroConditionPosition) { - $escalations[$zeroConditionPosition] - ->getCondition() - ->setAllowZeroConditions(true); - } + } elseif ($zeroConditionEscalation) { + $escalations[$zeroConditionEscalation] + ->getCondition() + ->setAllowZeroConditions(true); } $this->add(new Escalations($escalations, $addEscalationButton)); @@ -362,7 +343,7 @@ public function populate($values): self $conditionFormValues['val_' . $count] = $filter->getValue(); } - $formValues['escalation-condition_' . bin2hex($position)] = $conditionFormValues; + $formValues['escalation-condition_' . bin2hex($position)] = $conditionFormValues; $recipientFormValues = []; if (isset($escalation['recipients'])) { $recipientFormValues['recipient-count'] = count($escalation['recipients']); @@ -390,7 +371,7 @@ public function populate($values): self } } - $formValues['escalation-recipient_' . bin2hex($position)] = $recipientFormValues; + $formValues['escalation-recipient_' . bin2hex($position)] = $recipientFormValues; } return parent::populate($formValues); @@ -462,15 +443,15 @@ protected function createRemoveButton(string $prefix): SubmitButtonElement 'submitButton', 'remove-escalation', [ - 'class' => [ + 'class' => [ 'remove-escalation', 'remove-button', 'control-button', 'spinner' ], - 'label' => new Icon('minus'), + 'label' => new Icon('minus'), 'formnovalidate' => true, - 'value' => $prefix + 'value' => $prefix ] ); @@ -497,7 +478,7 @@ protected function createRemoveButton(string $prefix): SubmitButtonElement /** * Insert to or update event rule in the database * - * @param string $id The id of the event rule + * @param string $id The id of the event rule * @param array $config The new configuration */ public function addOrUpdateRule(string $id, array $config): void @@ -508,19 +489,19 @@ public function addOrUpdateRule(string $id, array $config): void if ($id < 0) { $db->insert('rule', [ - 'name' => $config['name'], + 'name' => $config['name'], 'timeperiod_id' => $config['timeperiod_id'] ?? null, 'object_filter' => $config['object_filter'] ?? null, - 'is_active' => $config['is_active'] ?? 'n' + 'is_active' => $config['is_active'] ?? 'n' ]); $id = $db->lastInsertId(); } else { $db->update('rule', [ - 'name' => $config['name'], + 'name' => $config['name'], 'timeperiod_id' => $config['timeperiod_id'] ?? null, 'object_filter' => $config['object_filter'] ?? null, - 'is_active' => $config['is_active'] ?? 'n' + 'is_active' => $config['is_active'] ?? 'n' ], ['id = ?' => $id]); } @@ -576,8 +557,8 @@ public function addOrUpdateRule(string $id, array $config): void * Insert to or update escalations in Db * * @param array> $escalations - * @param Connection $db - * @param bool $insert + * @param Connection $db + * @param bool $insert */ private function insertOrUpdateEscalations( int $ruleId, @@ -590,10 +571,10 @@ private function insertOrUpdateEscalations( $recipientsFromConfig = $escalationConfig['recipients'] ?? []; if ($insert) { $db->insert('rule_escalation', [ - 'rule_id' => $ruleId, - 'position' => $position, - 'condition' => $escalationConfig['condition'] ?? null, - 'name' => $escalationConfig['name'] ?? null, + 'rule_id' => $ruleId, + 'position' => $position, + 'condition' => $escalationConfig['condition'] ?? null, + 'name' => $escalationConfig['name'] ?? null, 'fallback_for' => $escalationConfig['fallback_for'] ?? null ]); @@ -602,15 +583,14 @@ private function insertOrUpdateEscalations( } else { /** @var string $escalationId */ $escalationId = $escalationConfig['id']; - $db->update('rule_escalation', [ - 'position' => $position, - 'condition' => $escalationConfig['condition'] ?? null, - 'name' => $escalationConfig['name'] ?? null, + 'position' => $position, + 'condition' => $escalationConfig['condition'] ?? null, + 'name' => $escalationConfig['name'] ?? null, 'fallback_for' => $escalationConfig['fallback_for'] ?? null ], ['id = ?' => $escalationId, 'rule_id = ?' => $ruleId]); - $recipientsToRemove = []; + $recipientsToRemove = []; $recipients = RuleEscalationRecipient::on($db) ->columns('id') ->filter(Filter::equal('rule_escalation_id', $escalationId)); @@ -642,7 +622,7 @@ function (array $element) use ($recipientId) { foreach ($recipientsFromConfig as $recipientConfig) { $data = [ 'rule_escalation_id' => $escalationId, - 'channel_id' => $recipientConfig['channel_id'] + 'channel_id' => $recipientConfig['channel_id'] ]; switch (true) { @@ -689,9 +669,7 @@ public function isValidEvent($event) public function removeRule(int $id): void { $db = Database::get(); - $db->beginTransaction(); - $escalations = RuleEscalation::on($db) ->columns('id') ->filter(Filter::equal('rule_id', $id)); diff --git a/library/Notifications/Widget/ItemList/Escalations.php b/library/Notifications/Widget/ItemList/Escalations.php index fddbe369b..a670b0b77 100644 --- a/library/Notifications/Widget/ItemList/Escalations.php +++ b/library/Notifications/Widget/ItemList/Escalations.php @@ -14,17 +14,17 @@ class Escalations extends BaseHtmlElement protected $tag = 'ul'; - /** @var Escalation[] */ + /** @var Escalation[] Array of Escalation list items */ private $escalations; - /** @var FormElement */ + /** @var FormElement Escalation add button */ private $addButton; /** * Create Escalations for an event rule * * @param Escalation[] $escalations - * @param FormElement $addButton + * @param FormElement $addButton */ public function __construct(array $escalations, FormElement $addButton) { diff --git a/public/css/detail/event-rule-detail.less b/public/css/detail/event-rule-detail.less index c04c1ff80..0e3c83e1b 100644 --- a/public/css/detail/event-rule-detail.less +++ b/public/css/detail/event-rule-detail.less @@ -49,7 +49,7 @@ button[type="submit"] { margin-right: 1em; - &.btn-remove { + &.btn-remove:not([disabled]) { .button(@body-bg-color, @color-critical, @color-critical-accentuated); border: none; }