Skip to content

Commit

Permalink
Fix issues in implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
raviks789 committed Apr 10, 2024
1 parent 37f0ce5 commit 5161d6f
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 165 deletions.
42 changes: 27 additions & 15 deletions application/controllers/EventRuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function init()
public function indexAction(): void
{
$this->assertPermission('notifications/config/event-rule');
$this->sessionNamespace->delete('-1');

$this->addTitleTab(t('Event Rule'));
$this->controls->addAttributes(['class' => 'event-rule-detail']);
Expand Down Expand Up @@ -83,10 +84,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) {
Expand Down Expand Up @@ -195,7 +198,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;
Expand Down Expand Up @@ -247,10 +250,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());
Expand Down Expand Up @@ -298,11 +303,14 @@ public function editAction(): void
{
/** @var string $ruleId */
$ruleId = $this->params->getRequired('id');

if ($ruleId === '-1') {
$config = ['id' => $ruleId];
} else {
$config = $this->fromDb((int) $ruleId);
/** @var array<string, mixed>|null $config */
$config = $this->sessionNamespace->get($ruleId);
if ($config === null) {
if ($ruleId === '-1') {
$config = ['id' => $ruleId];
} else {
$config = $this->fromDb((int) $ruleId);
}
}

$eventRuleForm = (new EventRuleForm())
Expand All @@ -313,7 +321,11 @@ public function editAction(): void
$config['is_active'] = $form->getValue('is_active');
$params = [];
if ($ruleId === '-1') {
$params = $config;
$params = [
'id' => -1,
'name' => $config['name'],
'is_active' => $config['is_active']
];
} else {
$params['id'] = $ruleId;
}
Expand Down
18 changes: 10 additions & 8 deletions application/controllers/EventRulesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -46,6 +45,7 @@ public function init()
public function indexAction(): void
{
$eventRules = Rule::on(Database::get());
$this->sessionNamespace->delete('-1');

$limitControl = $this->createLimitControl();
$paginationControl = $this->createPaginationControl($eventRules);
Expand Down Expand Up @@ -108,7 +108,7 @@ public function addAction(): void

$this->controls->addAttributes(['class' => 'event-rule-detail']);
/** @var string $ruleId */
$ruleId = $this->params->getRequired('id');
$ruleId = $this->params->get('id') ?? '-1';

$params = $this->params->toArray(false);
/** @var array<string, mixed>|null $config */
Expand All @@ -122,8 +122,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']])));
Expand Down Expand Up @@ -197,10 +197,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());
Expand Down
55 changes: 29 additions & 26 deletions application/forms/EventRuleConfigElements/EscalationCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,25 @@ 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
]
);

$this->registerElement($addCondition);

/** @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);
}

Expand All @@ -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
]
);

Expand All @@ -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
]
);
Expand Down Expand Up @@ -146,7 +147,7 @@ protected function assemble(): void
}

$this->addElement('hidden', $typeName, [
'value' => 'incident_severity'
'value' => 'incident_severity'
]);

break;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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
]);
}

Expand Down Expand Up @@ -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
]
);

Expand Down
37 changes: 18 additions & 19 deletions application/forms/EventRuleConfigElements/EscalationRecipient.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class EscalationRecipient extends FieldsetElement
{
protected $defaultAttributes = ['class' => 'escalation-recipient'];

/** @var EscalationRecipientListItem[] */
/** @var EscalationRecipientListItem[] */
protected $recipients = [];

protected function assemble(): void
Expand All @@ -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
]
);
Expand All @@ -51,7 +51,6 @@ protected function assemble(): void
$this->getElement('recipient-count')->setValue($recipientCount);
}

$removePosition = null;
foreach (range(1, $recipientCount) as $i) {
$this->addElement(
'hidden',
Expand All @@ -63,13 +62,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)
]
);

Expand All @@ -83,10 +82,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)
]
);

Expand Down Expand Up @@ -200,11 +199,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
]
);

Expand Down
Loading

0 comments on commit 5161d6f

Please sign in to comment.