Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
raviks789 committed Mar 5, 2024
1 parent 6347d9e commit 486ef4b
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 84 deletions.
26 changes: 15 additions & 11 deletions application/controllers/EventRuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,15 @@ public function indexAction(): void
$this->addTitleTab(t('Event Rule'));
$this->controls->addAttributes(['class' => 'event-rule-detail']);

/** @var int $ruleId */
$ruleId = $this->params->getRequired('id');
/** @var array<string, mixed> $config */
$config = $this->sessionNamespace->get($ruleId);
/** @var array<string, mixed>|null $config */
$config = $this->sessionNamespace->get((string) $ruleId);
$this->controls->addAttributes(['class' => 'event-rule-detail']);

$discardChangesButton = null;
if ($config === null) {
$config = $this->fromDb((int) $ruleId);
$config = $this->fromDb($ruleId);
}

$eventRuleConfig = (new EventRuleConfigForm(
Expand All @@ -70,13 +71,14 @@ public function indexAction(): void
))->populate($config);
$eventRuleConfig
->on(Form::ON_SUCCESS, function (EventRuleConfigForm $form) use ($config) {
$ruleId = (int) $config['id'];
/** @var string $ruleId */
$ruleId = $config['id'];
$form->insertOrAddRule($ruleId, $config);
$this->sessionNamespace->delete($config['id']);
$this->sessionNamespace->delete($ruleId);
Notification::success((sprintf(t('Successfully saved event rule %s'), $config['name'])));

$this->sendExtraUpdates(['#col1']);
$this->redirectNow(Links::eventRule($ruleId));
$this->redirectNow(Links::eventRule((int) $ruleId));
})
->on(EventRuleConfigForm::ON_DELETE, function (EventRuleConfigForm $form) use ($config) {
$ruleId = $config['id'];
Expand All @@ -97,7 +99,7 @@ public function indexAction(): void
})
->handleRequest($this->getServerRequest());

$cache = $this->sessionNamespace->get($ruleId);
$cache = $this->sessionNamespace->get((string) $ruleId);
if ($cache !== null) {
$this->addContent(Html::tag('div', ['class' => 'cache-notice'], t('There are unsaved changes.')));
$discardChangesButton = (new SubmitButtonElement(
Expand Down Expand Up @@ -215,17 +217,20 @@ public function completeAction(): void
*/
public function searchEditorAction(): void
{
/** @var string $ruleId */
$ruleId = $this->params->shiftRequired('id');

$eventRule = $this->sessionNamespace->get($ruleId);

if ($eventRule === null) {
$eventRule = $this->fromDb($ruleId);
$eventRule = $this->fromDb((int) $ruleId);
}

$editor = new SearchEditor();

$editor->setQueryString($eventRule['object_filter'] ?? '');
/** @var string $objectFilter */
$objectFilter = $eventRule['object_filter'] ?? '';
$editor->setQueryString($objectFilter);
$editor->setAction(Url::fromRequest()->getAbsoluteUrl());
$editor->setSuggestionUrl(Url::fromPath(
"notifications/event-rule/complete",
Expand All @@ -235,7 +240,6 @@ 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')
Expand Down Expand Up @@ -266,7 +270,7 @@ public static function createFilterString(Filter\Rule $filters): string
foreach ($filters as $filter) {
self::createFilterString($filter);
}
} elseif (empty($filters->getValue())) {
} elseif ($filters instanceof Filter\Condition && empty($filters->getValue())) {
$filters->setValue(true);
}

Expand Down
69 changes: 18 additions & 51 deletions application/controllers/EventRulesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,15 @@ public function addAction(): void
$this->getTabs()->setRefreshUrl(Url::fromPath('notifications/event-rules/add'));

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

$params = $this->params->toArray(false);
/** @var array<string, mixed>|null $config */
$config = $this->sessionNamespace->get($ruleId);

if ($config === null) {
/** @var array<string, mixed> $config */
$config = $params;
}

Expand All @@ -138,12 +141,16 @@ public function addAction(): void
$eventRuleConfig
->on(Form::ON_SENT, function (Form $form) use ($config) {
$config = array_merge($config, $form->getValues());
$this->sessionNamespace->set(-1, $config);
$this->sessionNamespace->set('-1', $config);
})
->on(Form::ON_SUCCESS, function (EventRuleConfigForm $form) use ($config) {
$form->insertOrAddRule((int) $config['id'], $config);
Notification::success(sprintf(t('Successfully add event rule %s'), $config['name']));
$this->sessionNamespace->delete($config['id']);
/** @var string $ruleId */
$ruleId = $config['id'];
/** @var string $ruleName */
$ruleName = $config['name'];
$form->insertOrAddRule($ruleId, $config);
$this->sessionNamespace->delete($ruleId);
Notification::success(sprintf(t('Successfully add event rule %s'), $ruleName));
$this->redirectNow('__CLOSE__');
})
->handleRequest($this->getServerRequest());
Expand Down Expand Up @@ -174,17 +181,21 @@ public function completeAction(): void

public function searchEditorAction(): void
{
/** @var string $ruleId */
$ruleId = $this->params->shiftRequired('id');

/** @var array<string, mixed>|null $eventRule */
$eventRule = $this->sessionNamespace->get($ruleId);

if ($eventRule === null) {
$eventRule = $this->fromDb((int) $ruleId);
$eventRule = ['id' => '-1'];
}

$editor = new SearchEditor();

$editor->setQueryString($eventRule['object_filter'] ?? '');
/** @var string $objectFilter */
$objectFilter = $eventRule['object_filter'] ?? '';
$editor->setQueryString($objectFilter);
$editor->setAction(Url::fromRequest()->getAbsoluteUrl());
$editor->setSuggestionUrl(Url::fromPath(
"notifications/event-rule/complete",
Expand Down Expand Up @@ -212,47 +223,6 @@ public function searchEditorAction(): void
$this->setTitle($this->translate('Adjust Filter'));
}

public function addSearchEditorAction(): void
{
$ruleId = $this->params->shiftRequired('id');

$eventRule = $this->sessionNamespace->get($ruleId);

if ($eventRule === null) {
$eventRule = $this->fromDb($ruleId);
}

$editor = new SearchEditor();

$editor->setQueryString($eventRule['object_filter'] ?? '');
$editor->setAction(Url::fromRequest()->getAbsoluteUrl());
$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());
$eventRule['object_filter'] = $filter;

$this->sessionNamespace->set($ruleId, $eventRule);

$this->getResponse()
->setHeader('X-Icinga-Container', '_self')
->redirectAndExit(
Url::fromPath(
'notifications/event-rules/add',
['id' => $ruleId]
)
);
});

$editor->handleRequest($this->getServerRequest());

$this->getDocument()->addHtml($editor);
$this->setTitle($this->translate('Adjust Filter'));
}

/**
* Create filter string from the given filter rule
*
Expand All @@ -264,15 +234,12 @@ public static function createFilterString(Filter\Rule $filters): string
{
if ($filters instanceof Filter\Chain) {
foreach ($filters as $filter) {
var_dump($filter);
self::createFilterString($filter);
}
} elseif (empty($filters->getValue())) {
} elseif ($filters instanceof Filter\Condition && empty($filters->getValue())) {
$filters->setValue(true);
}

die;

$filterStr = QueryString::render($filters);

return ! empty($filterStr) ? $filterStr : '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ class EscalationCondition extends FieldsetElement
*
* @return $this
*/
public function isZeroConditionsAllowed(bool $allowZeroConditions): self
public function setAllowZeroConditions(bool $allowZeroConditions): self
{
$this->allowZeroConditions = $allowZeroConditions;

return $this;
}

protected function assemble()
protected function assemble(): void
{
if ($this->allowZeroConditions) {
$defaultCount = 0;
Expand Down Expand Up @@ -247,7 +247,7 @@ protected function assemble()
protected function createRemoveButton(int $count): ?SubmitButtonElement
{
// check for count and if allow zero conditions
if ((int) $this->getValue('condition-count') === 1 && ! $this->allowZeroConditions) {
if ($this->getValue('condition-count') === '1' && ! $this->allowZeroConditions) {
return null;
}

Expand Down Expand Up @@ -277,7 +277,8 @@ public function hasValue(): bool
public function getCondition(): string
{
$filter = Filter::any();
$count = (int) $this->getValue('condition-count');
/** @var int $count */
$count = $this->getValue('condition-count');

if ($count > 0) { // if count is 0, loop runs in reverse direction
foreach (range(1, $count) as $count) {
Expand Down
27 changes: 21 additions & 6 deletions application/forms/EventRuleConfigElements/EscalationRecipient.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use ipl\Html\FormElement\SubmitButtonElement;
use ipl\Html\Html;
use ipl\Html\HtmlElement;
use ipl\Orm\Query;
use ipl\Web\Widget\Icon;

class EscalationRecipient extends FieldsetElement
Expand Down Expand Up @@ -46,7 +47,8 @@ protected function assemble(): void
);

$this->registerElement($addRecipientButton);
$recipientCount = (int) $this->getValue('recipient-count');
/** @var int $recipientCount */
$recipientCount = $this->getValue('recipient-count');
if ($addRecipientButton->hasBeenPressed()) {
$recipientCount += 1;
$this->getElement('recipient-count')->setValue($recipientCount);
Expand Down Expand Up @@ -91,8 +93,10 @@ protected function assemble(): void
]
);

if ($this->getValue('column_' . $i) !== null) {
$recipient = explode('_', $this->getValue('column_' . $i));
/** @var string $recipientVal */
$recipientVal = $this->getValue('column_' . $i);;
if ($recipientVal !== null) {
$recipient = explode('_', $recipientVal);
if ($recipient[0] === 'contact') {
$options[''] = $this->translate('Default User Channel');

Expand Down Expand Up @@ -156,17 +160,25 @@ protected function assemble(): void
$this->addElement($addRecipientButton);
}

/**
* Fetch recipient options
*
* @return array<string, array<string, string>>
*/
protected function fetchOptions(): array
{
$options = [];
/** @var Contact $contact */
foreach (Contact::on(Database::get()) as $contact) {
$options['Contacts']['contact_' . $contact->id] = $contact->full_name;
}

/** @var Contactgroup $contactgroup */
foreach (Contactgroup::on(Database::get()) as $contactgroup) {
$options['Contact Groups']['contactgroup_' . $contactgroup->id] = $contactgroup->name;
}

/** @var Schedule $schedule */
foreach (Schedule::on(Database::get()) as $schedule) {
$options['Schedules']['schedule_' . $schedule->id] = $schedule->name;
}
Expand All @@ -183,7 +195,7 @@ protected function fetchOptions(): array
*/
protected function createRemoveButton(int $pos): ?FormElement
{
if ((int) $this->getValue('recipient-count') === 1) {
if ($this->getValue('recipient-count') === '1') {
return null;
}

Expand Down Expand Up @@ -212,18 +224,21 @@ public function hasValue(): bool
/**
* Get recipients of the escalation
*
* @return array<int, array<string, string>>
* @return array<int, array<string, mixed>>
*/
public function getRecipients(): array
{
$count = (int) $this->getValue('recipient-count');
/** @var int $count */
$count = $this->getValue('recipient-count');

/** @var array<int, array<string, mixed>> $values */
$values = [];
for ($i = 1; $i <= $count; $i++) {
$value = [];
$value['channel_id'] = $this->getValue('val_' . $i);
$value['id'] = $this->getValue('id_' . $i);

/** @var ?string $columnName */
$columnName = $this->getValue('column_' . $i);

if ($columnName === null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ protected function assemble(): void
);

$this->registerElement($addFilterButton);
$showSearchBar = (int) $this->getValue('show-searchbar');
/** @var int $showSearchBar */
$showSearchBar = $this->getValue('show-searchbar');
if ($this->objectFilter !== '' || $addFilterButton->hasBeenPressed()) {
$showSearchBar = 1;
$this->getElement('show-searchbar')->setValue($showSearchBar);
Expand Down
Loading

0 comments on commit 486ef4b

Please sign in to comment.