Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions application/controllers/ContactController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
use Icinga\Authentication\User\UserBackend;
use Icinga\Data\Selectable;
use Icinga\Module\Notifications\Common\Database;
use Icinga\Module\Notifications\Repository\ContactRepository;
use Icinga\Module\Notifications\Web\Form\ContactForm;
use Icinga\Repository\Repository;
use Icinga\Web\Notification;
use ipl\Html\Contract\Form;
use ipl\Sql\Connection;
use ipl\Web\Compat\CompatController;
use ipl\Web\FormElement\SearchSuggestions;

Expand All @@ -27,29 +29,35 @@ public function init(): void

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

$form = (new ContactForm(Database::get()))
->loadContact($contactId)
$contact = (new ContactRepository(Database::get()))
->find((int) $this->params->getRequired('id'));
if ($contact === null) {
$this->httpNotFound($this->translate('Contact not found'));
}

$form = (new ContactForm())
->setContact($contact)
->on(Form::ON_SUBMIT, function (ContactForm $form) {
$form->editContact();
$contact = $form->getContact();
Database::get()->transaction(fn (Connection $db) => (new ContactRepository($db))->update($contact));
Notification::success(sprintf(
t('Contact "%s" has successfully been saved'),
$form->getContactName()
$this->translate('Contact "%s" has successfully been saved'),
$contact->full_name
));

$this->redirectNow('__CLOSE__');
})->on(ContactForm::ON_REMOVE, function (ContactForm $form) {
$form->removeContact();
$contact = $form->getContact();
Database::get()->transaction(fn (Connection $db) => (new ContactRepository($db))->delete($contact));
Notification::success(sprintf(
t('Deleted contact "%s" successfully'),
$form->getContactName()
$this->translate('Deleted contact "%s" successfully'),
$contact->full_name
));

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

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

$this->addContent($form);
}
Expand Down
6 changes: 4 additions & 2 deletions application/controllers/ContactsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Icinga\Module\Notifications\Common\Links;
use Icinga\Module\Notifications\Model\Channel;
use Icinga\Module\Notifications\Model\Contact;
use Icinga\Module\Notifications\Repository\ContactRepository;
use Icinga\Module\Notifications\View\ContactRenderer;
use Icinga\Module\Notifications\Web\Control\SearchBar\ObjectSuggestions;
use Icinga\Module\Notifications\Web\Form\ContactForm;
Expand Down Expand Up @@ -129,9 +130,10 @@ public function addAction(): void
{
$this->addTitleTab($this->translate('Create Contact'));

$form = (new ContactForm($this->db))
$form = (new ContactForm())
->on(Form::ON_SUBMIT, function (ContactForm $form) {
$form->addContact();
$contact = $form->getContact();
Database::get()->transaction(fn (Connection $db) => (new ContactRepository($db))->create($contact));
Notification::success($this->translate('New contact has successfully been added'));
$this->switchToSingleColumnLayout();
})->handleRequest($this->getServerRequest());
Expand Down
125 changes: 90 additions & 35 deletions application/controllers/ScheduleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@
use Icinga\Module\Notifications\Forms\RotationConfigForm;
use Icinga\Module\Notifications\Forms\ScheduleForm;
use Icinga\Module\Notifications\Model\Schedule;
use Icinga\Module\Notifications\Repository\RotationRepository;
use Icinga\Module\Notifications\Repository\ScheduleRepository;
use Icinga\Module\Notifications\Widget\Detail\ScheduleDetail;
use Icinga\Module\Notifications\Widget\TimezoneWarning;
use Icinga\Web\Session;
use ipl\Html\Attributes;
use ipl\Html\Contract\Form;
use ipl\Html\Html;
use ipl\Sql\Connection;
use ipl\Stdlib\Filter;
use ipl\Web\Compat\CompatController;
use ipl\Web\Url;
Expand All @@ -41,11 +44,7 @@ public function indexAction(): void
{
$id = (int) $this->params->getRequired('id');

$query = Schedule::on(Database::get())
->filter(Filter::equal('schedule.id', $id));

/** @var ?Schedule $schedule */
$schedule = $query->first();
$schedule = (new ScheduleRepository(Database::get()))->find($id);
if ($schedule === null) {
$this->httpNotFound(t('Schedule not found'));
}
Expand Down Expand Up @@ -113,20 +112,38 @@ public function settingsAction(): void
$this->setTitle($this->translate('Edit Schedule'));
$scheduleId = (int) $this->params->getRequired('id');

$form = new ScheduleForm(Database::get());
$schedule = (new ScheduleRepository(Database::get()))->find($scheduleId);
if ($schedule === null) {
$this->httpNotFound(t('Schedule not found'));
}

$form = new ScheduleForm();
$form->setShowRemoveButton();
$form->loadSchedule($scheduleId);
$form->setSchedule($schedule);
$form->setSubmitLabel($this->translate('Save Changes'));
$form->setAction($this->getRequest()->getUrl()->getAbsoluteUrl());
$form->on(Form::ON_SUBMIT, function ($form) use ($scheduleId) {
$form->editSchedule($scheduleId);
$form->setAction($this->getRequest()->getUrl()->setParam('showCompact')->getAbsoluteUrl());
$form->on(Form::ON_SUBMIT, function (ScheduleForm $form) {
$schedule = $form->getSchedule();

if ($form->hasBeenDuplicated()) {
Database::get()->transaction(function (Connection $db) use ($schedule) {
(new ScheduleRepository($db))->duplicate($schedule);
});
} else {
Database::get()->transaction(function (Connection $db) use ($schedule) {
(new ScheduleRepository($db))->update($schedule);
});
}

$this->sendExtraUpdates(['#col1']);
$this->redirectNow(Links::schedule($scheduleId));
$this->redirectNow(Links::schedule($schedule->id));
});
$form->on(Form::ON_SENT, function ($form) use ($scheduleId) {
$form->on(Form::ON_SENT, function (ScheduleForm $form) use ($scheduleId) {
if ($form->hasBeenRemoved()) {
$form->removeSchedule($scheduleId);
$schedule = $form->getSchedule();
Database::get()->transaction(function (Connection $db) use ($schedule) {
(new ScheduleRepository($db))->delete($schedule);
});

$this->redirectNow('__CLOSE__');
}
Expand All @@ -140,15 +157,18 @@ public function settingsAction(): void
public function addAction(): void
{
$this->setTitle($this->translate('Create Schedule'));
$form = (new ScheduleForm(Database::get()))
$form = (new ScheduleForm())
->setShowTimezoneSuggestionInput()
->setAction($this->getRequest()->getUrl()->setParam('showCompact')->getAbsoluteUrl())
->on(Form::ON_SUBMIT, function (ScheduleForm $form) {
$scheduleId = $form->addSchedule();
$schedule = $form->getSchedule();
Database::get()->transaction(function (Connection $db) use ($schedule) {
(new ScheduleRepository($db))->create($schedule);
});

$this->sendExtraUpdates(['#col1']);
$this->getResponse()->setHeader('X-Icinga-Container', 'col2');
$this->redirectNow(Links::schedule($scheduleId));
$this->redirectNow(Links::schedule($schedule->id));
})
->handleRequest($this->getServerRequest());

Expand All @@ -166,7 +186,7 @@ public function addRotationAction(): void
$this->addContent(new TimezoneWarning($scheduleTimezone));
}

$form = new RotationConfigForm($scheduleId, Database::get(), $displayTimezone, $scheduleTimezone);
$form = new RotationConfigForm($scheduleId, $displayTimezone, $scheduleTimezone);
$form->setAction($this->getRequest()->getUrl()->setParam('showCompact')->getAbsoluteUrl());
$form->setSuggestionUrl(Url::fromPath('notifications/suggest/recipient'));
$form->on(Form::ON_SENT, function ($form) {
Expand All @@ -181,7 +201,10 @@ public function addRotationAction(): void
}
});
$form->on(Form::ON_SUBMIT, function (RotationConfigForm $form) use ($scheduleId) {
$form->addRotation();
$rotation = $form->getRotation();
Database::get()->transaction(function (Connection $db) use ($rotation) {
(new RotationRepository($db))->create($rotation);
});
$this->sendExtraUpdates(['#col1']);
$this->closeModalAndRefreshRelatedView(Links::schedule($scheduleId));
});
Expand All @@ -196,36 +219,58 @@ public function addRotationAction(): void
public function editRotationAction(): void
{
$id = (int) $this->params->getRequired('id');
$scheduleId = (int) $this->params->getRequired('schedule');
$scheduleTimezone = $this->getScheduleTimezone($scheduleId);
$displayTimezone = $this->getDisplayTimezoneFromSession($scheduleId, $scheduleTimezone);
$this->setTitle($this->translate('Edit Rotation'));

if ($displayTimezone !== $scheduleTimezone) {
$this->addContent(new TimezoneWarning($scheduleTimezone));
$rotation = (new RotationRepository(Database::get()))->find($id);
if ($rotation === null) {
$this->httpNotFound(t('Rotation not found'));
}

$form = new RotationConfigForm($scheduleId, Database::get(), $displayTimezone, $scheduleTimezone);
$displayTimezone = $this->getDisplayTimezoneFromSession($rotation->schedule_id, $rotation->schedule->timezone);
if ($displayTimezone !== $rotation->schedule->timezone) {
$this->addContent(new TimezoneWarning($rotation->schedule->timezone));
}

$form = new RotationConfigForm($rotation->schedule_id, $displayTimezone, $rotation->schedule->timezone);
$form->disableModeSelection();
$form->setShowRemoveButton();
$form->loadRotation($id);
$form->setRotation($rotation);
$form->setSubmitLabel($this->translate('Save Changes'));
$form->setAction($this->getRequest()->getUrl()->setParam('showCompact')->getAbsoluteUrl());
$form->setSuggestionUrl(Url::fromPath('notifications/suggest/recipient'));
$form->on(Form::ON_SUBMIT, function (RotationConfigForm $form) use ($id, $scheduleId) {
$form->editRotation($id);
$form->on(Form::ON_SUBMIT, function (RotationConfigForm $form) {
// TODO: The rotation may not be changed by the user but the repository implementation
// right now doesn't detect this and tries to generate rules for no members
$rotation = $form->getRotation();

if ($form->hasBeenDuplicated()) {
Database::get()->transaction(function (Connection $db) use ($rotation) {
(new RotationRepository($db))->duplicate($rotation);
});
} else {
Database::get()->transaction(function (Connection $db) use ($rotation) {
(new RotationRepository($db))->update($rotation);
});
}

$this->sendExtraUpdates(['#col1']);
$this->closeModalAndRefreshRelatedView(Links::schedule($scheduleId));
$this->closeModalAndRefreshRelatedView(Links::schedule($rotation->schedule_id));
});
$form->on(Form::ON_SENT, function (RotationConfigForm $form) use ($id, $scheduleId) {
$form->on(Form::ON_SENT, function (RotationConfigForm $form) {
if ($form->hasBeenRemoved()) {
$form->removeRotation($id);
$rotation = $form->getRotation();
Database::get()->transaction(function (Connection $db) use ($rotation) {
(new RotationRepository($db))->delete($rotation);
});
$this->sendExtraUpdates(['#col1']);
$this->closeModalAndRefreshRelatedView(Links::schedule($scheduleId));
$this->closeModalAndRefreshRelatedView(Links::schedule($rotation->schedule_id));
} elseif ($form->hasBeenWiped()) {
$form->wipeRotation();
$rotation = $form->getRotation();
Database::get()->transaction(function (Connection $db) use ($rotation) {
(new RotationRepository($db))->wipe($rotation);
});
$this->sendExtraUpdates(['#col1']);
$this->closeModalAndRefreshRelatedView(Links::schedule($scheduleId));
$this->closeModalAndRefreshRelatedView(Links::schedule($rotation->schedule_id));
} elseif (! $form->hasBeenSubmitted()) {
foreach ($form->getPartUpdates() as $update) {
if (! is_array($update)) {
Expand All @@ -248,10 +293,20 @@ public function moveRotationAction(): void
{
$this->assertHttpMethod('POST');

$form = new MoveRotationForm(Database::get());
$form = new MoveRotationForm();
$form->on(Form::ON_SUBMIT, function (MoveRotationForm $form) {
$repo = new RotationRepository(Database::get());
$rotation = $repo->find($form->getValue('rotation'));
if ($rotation === null) {
$this->httpNotFound(t('Rotation not found'));
}

Database::get()->transaction(function () use ($form, $repo, $rotation) {
$repo->move($rotation, $form->getValue('priority'));
});

$this->sendExtraUpdates(['#col1']);
$this->redirectNow(Links::schedule($form->getScheduleId()));
$this->redirectNow(Links::schedule($rotation->schedule_id));
});

$form->handleRequest($this->getServerRequest());
Expand Down
3 changes: 2 additions & 1 deletion application/forms/ContactGroupForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Icinga\Module\Notifications\Model\Rotation;
use Icinga\Module\Notifications\Model\RotationMember;
use Icinga\Module\Notifications\Model\RuleEscalationRecipient;
use Icinga\Module\Notifications\Repository\RotationRepository;
use Icinga\Web\Session;
use ipl\Html\FormElement\SubmitElement;
use ipl\Html\HtmlDocument;
Expand Down Expand Up @@ -356,7 +357,7 @@ public function removeContactgroup(): void

/** @var Rotation $rotation */
foreach ($rotations as $rotation) {
$rotation->delete();
(new RotationRepository($this->db))->delete($rotation);
}
}
}
Expand Down
Loading
Loading