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 24, 2024
1 parent 14b9b50 commit 5a2e08a
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 167 deletions.
41 changes: 21 additions & 20 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 @@ -82,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) {
Expand Down Expand Up @@ -193,7 +196,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 @@ -243,14 +246,6 @@ public function searchEditorAction(): void

/** @var string $objectFilter */
$objectFilter = $eventRule['object_filter'] ?? '';
<<<<<<< Updated upstream
$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->setQueryString($objectFilter)
->setAction(Url::fromRequest()->getAbsoluteUrl())
->setSuggestionUrl(
Expand All @@ -259,7 +254,6 @@ public function searchEditorAction(): void
['_disableLayout' => true, 'showCompact' => true, 'id' => $ruleId]
)
);
>>>>>>> Stashed changes

$editor->on(SearchEditor::ON_SUCCESS, function (SearchEditor $form) use ($ruleId, $eventRule) {
$eventRule['object_filter'] = self::createFilterString($form->getFilter());
Expand Down Expand Up @@ -306,11 +300,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 @@ -321,7 +318,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
13 changes: 2 additions & 11 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 Down Expand Up @@ -195,14 +195,6 @@ public function searchEditorAction(): void

/** @var string $objectFilter */
$objectFilter = $eventRule['object_filter'] ?? '';
<<<<<<< Updated upstream
$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->setQueryString($objectFilter)
->setAction(Url::fromRequest()->getAbsoluteUrl())
->setSuggestionUrl(
Expand All @@ -211,7 +203,6 @@ public function searchEditorAction(): void
['_disableLayout' => true, 'showCompact' => true, 'id' => $ruleId]
)
);
>>>>>>> Stashed changes

$editor->on(SearchEditor::ON_SUCCESS, function (SearchEditor $form) use ($ruleId, $eventRule) {
$eventRule['object_filter'] = self::createFilterString($form->getFilter());
Expand Down
55 changes: 26 additions & 29 deletions application/forms/EventRuleConfigElements/EscalationCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,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
]
);

Expand All @@ -71,7 +71,7 @@ protected function assemble(): void
$this->addElement('hidden', 'id');

if ($addCondition->hasBeenPressed()) {
$conditionCount += 1;
$conditionCount = $conditionCount + 1;
$this->getElement('condition-count')->setValue($conditionCount);
}

Expand All @@ -81,24 +81,22 @@ protected function assemble(): void
$typeName = 'type_' . $i;
$valName = 'val_' . $i;

/** @var BaseFormElement $col */
$col = $this->createElement(
'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
]
);

$operators = ['=', '>', '>=', '<', '<=', '!='];
/** @var BaseFormElement $op */
$op = $this->createElement(
'select',
$opName,
Expand All @@ -111,7 +109,6 @@ protected function assemble(): void

switch ($this->getPopulatedValue('column_' . $i)) {
case 'incident_severity':
/** @var BaseFormElement $val */
$val = $this->createElement(
'select',
$valName,
Expand Down Expand Up @@ -140,25 +137,26 @@ protected function assemble(): void
}

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

break;
case 'incident_age':
/** @var BaseFormElement $val */
$val = $this->createElement(
'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 All @@ -184,11 +182,10 @@ protected function assemble(): void

break;
default:
/** @var BaseFormElement $val */
$val = $this->createElement('text', $valName, [
'class' => 'right-operand',
'placeholder' => $this->translate('Please make a decision'),
'disabled' => true
'disabled' => true
]);
}

Expand Down Expand Up @@ -252,11 +249,11 @@ protected function createRemoveButton(int $count): ?SubmitButtonElement
$removeButton = new SubmitButtonElement(
'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
41 changes: 18 additions & 23 deletions application/forms/EventRuleConfigElements/EscalationRecipient.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
use Icinga\Module\Notifications\Model\Schedule;
use Icinga\Module\Notifications\Widget\ItemList\EscalationRecipientList;
use Icinga\Module\Notifications\Widget\ItemList\EscalationRecipientListItem;
use ipl\Html\Contract\FormElement;
use ipl\Html\FormElement\BaseFormElement;
use ipl\Html\FormElement\FieldsetElement;
use ipl\Html\FormElement\SubmitButtonElement;
use ipl\Web\Widget\Icon;
Expand All @@ -19,7 +17,7 @@ class EscalationRecipient extends FieldsetElement
{
protected $defaultAttributes = ['class' => 'escalation-recipient'];

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

protected function assemble(): void
Expand All @@ -31,9 +29,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 @@ -46,22 +44,20 @@ protected function assemble(): void
$this->getElement('recipient-count')->setValue($recipientCount);
}

$removePosition = null;
foreach (range(1, $recipientCount) as $i) {
$this->addElement('hidden', 'id_' . $i);

/** @var BaseFormElement $col */
$col = $this->createElement(
'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 @@ -75,10 +71,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 All @@ -98,7 +94,6 @@ protected function assemble(): void
}
}
} else {
/** @var BaseFormElement $val */
$val = $this->createElement('text', 'val_' . $i, [
'class' => 'right-operand',
'placeholder' => $this->translate('Please make a decision'),
Expand Down Expand Up @@ -183,11 +178,11 @@ protected function createRemoveButton(int $pos): ?SubmitButtonElement
$removeButton = new SubmitButtonElement(
'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 5a2e08a

Please sign in to comment.