Skip to content

Commit

Permalink
Incremental config updates (#187)
Browse files Browse the repository at this point in the history
  • Loading branch information
nilmerg authored Jul 12, 2024
2 parents dd29ea3 + 013a57d commit c677b3c
Show file tree
Hide file tree
Showing 48 changed files with 1,714 additions and 702 deletions.
28 changes: 8 additions & 20 deletions application/controllers/ChannelController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,30 @@

use Icinga\Module\Notifications\Common\Database;
use Icinga\Module\Notifications\Forms\ChannelForm;
use Icinga\Module\Notifications\Model\Channel;
use Icinga\Web\Notification;
use ipl\Sql\Connection;
use ipl\Stdlib\Filter;
use ipl\Web\Compat\CompatController;

class ChannelController extends CompatController
{
/** @var Connection */
private $db;

public function init()
public function init(): void
{
$this->assertPermission('config/modules');

$this->db = Database::get();
}

public function indexAction()
public function indexAction(): void
{
$channel = Channel::on($this->db);
$channelId = $this->params->getRequired('id');

$channel->filter(Filter::equal('id', $channelId));

$channel = $channel->first();

$this->addTitleTab(sprintf(t('Channel: %s'), $channel->name));

$form = (new ChannelForm($this->db, $channelId))
->populate($channel)
$form = (new ChannelForm(Database::get()))
->loadChannel($channelId)
->on(ChannelForm::ON_SUCCESS, function (ChannelForm $form) {
if ($form->getPressedSubmitElement()->getName() === 'delete') {
$form->removeChannel();
Notification::success(sprintf(
t('Deleted channel "%s" successfully'),
$form->getValue('name')
));
} else {
$form->editChannel();
Notification::success(sprintf(
t('Channel "%s" has successfully been saved'),
$form->getValue('name')
Expand All @@ -53,6 +39,8 @@ public function indexAction()
$this->redirectNow('__CLOSE__');
})->handleRequest($this->getServerRequest());

$this->addTitleTab(sprintf(t('Channel: %s'), $form->getChannelName()));

$this->addContent($form);
}
}
6 changes: 4 additions & 2 deletions application/controllers/ChannelsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ public function indexAction()
$sortControl = $this->createSortControl(
$channels,
[
'name' => t('Name'),
'type' => t('Type')
'name' => t('Name'),
'type' => t('Type'),
'changed_at' => t('Changed At')
]
);

Expand Down Expand Up @@ -104,6 +105,7 @@ public function addAction()
$this->addTitleTab(t('Add Channel'));
$form = (new ChannelForm($this->db))
->on(ChannelForm::ON_SUCCESS, function (ChannelForm $form) {
$form->addChannel();
Notification::success(
sprintf(
t('New channel %s has successfully been added'),
Expand Down
32 changes: 9 additions & 23 deletions application/controllers/ContactController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,51 +15,37 @@

class ContactController extends CompatController
{
/** @var Connection */
private $db;

public function init()
public function init(): void
{
$this->assertPermission('notifications/config/contacts');

$this->db = Database::get();
}

public function indexAction()
public function indexAction(): void
{
$contact = Contact::on($this->db);
$contactId = $this->params->getRequired('id');

$contact->filter(Filter::equal('id', $contactId));

$contact = $contact->first();

$this->addTitleTab(sprintf(t('Contact: %s'), $contact->full_name));

$form = (new ContactForm($this->db, $contactId))
->populate($contact)
$form = (new ContactForm(Database::get()))
->loadContact($contactId)
->on(ContactForm::ON_SUCCESS, function (ContactForm $form) {
$form->addOrUpdateContact();
/** @var FieldsetElement $contactElement */
$contactElement = $form->getElement('contact');
$form->editContact();
Notification::success(sprintf(
t('Contact "%s" has successfully been saved'),
$contactElement->getValue('full_name')
$form->getContactName()
));

$this->redirectNow('__CLOSE__');
})->on(ContactForm::ON_REMOVE, function (ContactForm $form) {
$form->removeContact();
/** @var FieldsetElement $contactElement */
$contactElement = $form->getElement('contact');
Notification::success(sprintf(
t('Deleted contact "%s" successfully'),
$contactElement->getValue('full_name')
$form->getContactName()
));

$this->redirectNow('__CLOSE__');
})->handleRequest($this->getServerRequest());

$this->addTitleTab(sprintf(t('Contact: %s'), $form->getContactName()));

$this->addContent($form);
}
}
23 changes: 15 additions & 8 deletions application/controllers/ContactGroupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
use Icinga\Module\Notifications\Common\Database;
use Icinga\Module\Notifications\Common\Links;
use Icinga\Module\Notifications\Forms\ContactGroupForm;
use Icinga\Module\Notifications\Model\Contact;
use Icinga\Module\Notifications\Model\Contactgroup;
use Icinga\Module\Notifications\Model\ContactgroupMember;
use Icinga\Module\Notifications\Widget\ItemList\ContactList;
use Icinga\Web\Notification;
use ipl\Html\Attributes;
Expand Down Expand Up @@ -44,7 +46,13 @@ public function indexAction(): void

$this->addControl(new HtmlElement('div', new Attributes(['class' => 'header']), Text::create($group->name)));

$this->addControl($this->createPaginationControl($group->contact));
$contacts = Contact::on(Database::get())
->filter(Filter::all(
Filter::equal('contactgroup_member.contactgroup_id', $groupId),
Filter::equal('contactgroup_member.deleted', 'n')
));

$this->addControl($this->createPaginationControl($contacts));
$this->addControl($this->createLimitControl());

$this->addContent(
Expand All @@ -56,7 +64,7 @@ public function indexAction(): void
))->openInModal()
);

$this->addContent(new ContactList($group->contact));
$this->addContent(new ContactList($contacts));

$this->addTitleTab(t('Contact Group'));
$this->setTitle(sprintf(t('Contact Group: %s'), $group->name));
Expand Down Expand Up @@ -90,12 +98,11 @@ public function editAction(): void
}
})
->on(Form::ON_SUCCESS, function (ContactGroupForm $form) use ($groupId) {
if ($form->editGroup()) {
Notification::success(sprintf(
t('Successfully updated contact group %s'),
$form->getValue('group_name')
));
}
$form->editGroup();
Notification::success(sprintf(
t('Successfully updated contact group %s'),
$form->getValue('group_name')
));

$this->closeModalAndRefreshRemainingViews(Links::contactGroup($groupId));
})
Expand Down
3 changes: 2 additions & 1 deletion application/controllers/ContactGroupsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public function indexAction(): void
$sortControl = $this->createSortControl(
$groups,
[
'name' => t('Group Name'),
'name' => t('Group Name'),
'changed_at' => t('Changed At')
]
);

Expand Down
9 changes: 5 additions & 4 deletions application/controllers/ContactsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public function indexAction()
$sortControl = $this->createSortControl(
$contacts,
[
'full_name' => t('Full Name'),
'full_name' => t('Full Name'),
'changed_at' => t('Changed At')
]
);

Expand Down Expand Up @@ -101,15 +102,15 @@ public function indexAction()
$this->getTabs()->activate('contacts');
}

public function addAction()
public function addAction(): void
{
$this->addTitleTab(t('Add Contact'));

$form = (new ContactForm($this->db))
->on(ContactForm::ON_SUCCESS, function (ContactForm $form) {
$form->addOrUpdateContact();
$form->addContact();
Notification::success(t('New contact has successfully been added'));
$this->redirectNow(Url::fromPath('notifications/contacts'));
$this->redirectNow(Links::contacts());
})->handleRequest($this->getServerRequest());

$this->addContent($form);
Expand Down
28 changes: 11 additions & 17 deletions application/controllers/EventRuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Icinga\Module\Notifications\Forms\EventRuleForm;
use Icinga\Module\Notifications\Forms\SaveEventRuleForm;
use Icinga\Module\Notifications\Model\Incident;
use Icinga\Module\Notifications\Model\ObjectExtraTag;
use Icinga\Module\Notifications\Model\Rule;
use Icinga\Module\Notifications\Web\Control\SearchBar\ExtraTagSuggestions;
use Icinga\Module\Notifications\Widget\EventRuleConfig;
Expand Down Expand Up @@ -62,21 +61,9 @@ public function indexAction(): void
);
}

$disableRemoveButton = false;
if (ctype_digit($ruleId)) {
$incidents = Incident::on(Database::get())
->with('rule')
->filter(Filter::equal('rule.id', $ruleId));

if ($incidents->count() > 0) {
$disableRemoveButton = true;
}
}

$saveForm = (new SaveEventRuleForm())
->setShowRemoveButton()
->setShowDismissChangesButton($cache !== null)
->setRemoveButtonDisabled($disableRemoveButton)
->setSubmitButtonDisabled($cache === null)
->setSubmitLabel($this->translate('Save Changes'))
->on(SaveEventRuleForm::ON_SUCCESS, function ($form) use ($ruleId, $eventRuleConfig) {
Expand Down Expand Up @@ -151,7 +138,7 @@ public function indexAction(): void
public function fromDb(int $ruleId): array
{
$query = Rule::on(Database::get())
->withoutColumns('timeperiod_id')
->columns(['id', 'name', 'object_filter'])
->filter(Filter::equal('id', $ruleId));

$rule = $query->first();
Expand All @@ -161,12 +148,20 @@ public function fromDb(int $ruleId): array

$config = iterator_to_array($rule);

foreach ($rule->rule_escalation as $re) {
$ruleEscalations = $rule
->rule_escalation
->withoutColumns(['changed_at', 'deleted']);

foreach ($ruleEscalations as $re) {
foreach ($re as $k => $v) {
$config[$re->getTableName()][$re->position][$k] = $v;
}

foreach ($re->rule_escalation_recipient as $recipient) {
$escalationRecipients = $re
->rule_escalation_recipient
->withoutColumns(['changed_at', 'deleted']);

foreach ($escalationRecipients as $recipient) {
$config[$re->getTableName()][$re->position]['recipient'][] = iterator_to_array($recipient);
}
}
Expand Down Expand Up @@ -248,7 +243,6 @@ public function editAction(): void
->setAction(Url::fromRequest()->getAbsoluteUrl())
->on(Form::ON_SUCCESS, function ($form) use ($ruleId, $cache, $config) {
$config['name'] = $form->getValue('name');
$config['is_active'] = $form->getValue('is_active');

if ($cache || $ruleId === '-1') {
$this->sessionNamespace->set($ruleId, $config);
Expand Down
3 changes: 2 additions & 1 deletion application/controllers/EventRulesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public function indexAction(): void
$sortControl = $this->createSortControl(
$eventRules,
[
'name' => t('Name'),
'name' => t('Name'),
'changed_at' => t('Changed At')
]
);

Expand Down
4 changes: 2 additions & 2 deletions application/controllers/ScheduleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function settingsAction(): void
$this->setTitle($this->translate('Edit Schedule'));
$scheduleId = (int) $this->params->getRequired('id');

$form = new ScheduleForm();
$form = new ScheduleForm(Database::get());
$form->setShowRemoveButton();
$form->loadSchedule($scheduleId);
$form->setSubmitLabel($this->translate('Save Changes'));
Expand All @@ -95,7 +95,7 @@ public function settingsAction(): void
public function addAction(): void
{
$this->setTitle($this->translate('New Schedule'));
$form = (new ScheduleForm())
$form = (new ScheduleForm(Database::get()))
->setAction($this->getRequest()->getUrl()->getAbsoluteUrl())
->on(Form::ON_SUCCESS, function (ScheduleForm $form) {
$scheduleId = $form->addSchedule();
Expand Down
5 changes: 4 additions & 1 deletion application/controllers/SchedulesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ public function indexAction(): void
$limitControl = $this->createLimitControl();
$sortControl = $this->createSortControl(
$schedules,
['schedule.name' => t('Name')]
[
'schedule.name' => t('Name'),
'changed_at' => t('Changed At')
]
);

$paginationControl = $this->createPaginationControl($schedules);
Expand Down
25 changes: 8 additions & 17 deletions application/controllers/SourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

class SourceController extends CompatController
{
public function init()
public function init(): void
{
$this->assertPermission('config/modules');
}
Expand All @@ -23,38 +23,29 @@ public function indexAction(): void
{
$sourceId = (int) $this->params->getRequired('id');

/** @var ?Source $source */
$source = Source::on(Database::get())
->filter(Filter::equal('id', $sourceId))
->first();
if ($source === null) {
$this->httpNotFound($this->translate('Source not found'));
}

$form = (new SourceForm(Database::get(), $sourceId))
->populate($source)
$form = (new SourceForm(Database::get()))
->loadSource($sourceId)
->on(SourceForm::ON_SUCCESS, function (SourceForm $form) {
/** @var string $sourceName */
$sourceName = $form->getValue('name');

/** @var FormSubmitElement $pressedButton */
$pressedButton = $form->getPressedSubmitElement();
if ($pressedButton->getName() === 'delete') {
$form->removeSource();
Notification::success(sprintf(
$this->translate('Deleted source "%s" successfully'),
$sourceName
$form->getSourceName()
));
} else {
$form->editSource();
Notification::success(sprintf(
$this->translate('Updated source "%s" successfully'),
$sourceName
$form->getSourceName()
));
}

$this->switchToSingleColumnLayout();
})->handleRequest($this->getServerRequest());

$this->addTitleTab(sprintf($this->translate('Source: %s'), $source->name));
$this->addTitleTab(sprintf($this->translate('Source: %s'), $form->getSourceName()));
$this->addContent($form);
}
}
Loading

0 comments on commit c677b3c

Please sign in to comment.