Skip to content

Commit db44d8a

Browse files
committed
Utilize ContactRepository where suitable
1 parent a085ff5 commit db44d8a

3 files changed

Lines changed: 95 additions & 253 deletions

File tree

application/controllers/ContactController.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
use Icinga\Authentication\User\UserBackend;
1212
use Icinga\Data\Selectable;
1313
use Icinga\Module\Notifications\Common\Database;
14+
use Icinga\Module\Notifications\Repository\ContactRepository;
1415
use Icinga\Module\Notifications\Web\Form\ContactForm;
1516
use Icinga\Repository\Repository;
1617
use Icinga\Web\Notification;
1718
use ipl\Html\Contract\Form;
19+
use ipl\Sql\Connection;
1820
use ipl\Web\Compat\CompatController;
1921
use ipl\Web\FormElement\SearchSuggestions;
2022

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

2830
public function indexAction(): void
2931
{
30-
$contactId = $this->params->getRequired('id');
31-
32-
$form = (new ContactForm(Database::get()))
33-
->loadContact($contactId)
32+
$contact = (new ContactRepository(Database::get()))
33+
->find((int) $this->params->getRequired('id'));
34+
if ($contact === null) {
35+
$this->httpNotFound($this->translate('Contact not found'));
36+
}
37+
38+
$form = (new ContactForm())
39+
->setContact($contact)
3440
->on(Form::ON_SUBMIT, function (ContactForm $form) {
35-
$form->editContact();
41+
$contact = $form->getContact();
42+
Database::get()->transaction(fn (Connection $db) => (new ContactRepository($db))->update($contact));
3643
Notification::success(sprintf(
37-
t('Contact "%s" has successfully been saved'),
38-
$form->getContactName()
44+
$this->translate('Contact "%s" has successfully been saved'),
45+
$contact->full_name
3946
));
4047

4148
$this->redirectNow('__CLOSE__');
4249
})->on(ContactForm::ON_REMOVE, function (ContactForm $form) {
43-
$form->removeContact();
50+
$contact = $form->getContact();
51+
Database::get()->transaction(fn (Connection $db) => (new ContactRepository($db))->delete($contact));
4452
Notification::success(sprintf(
45-
t('Deleted contact "%s" successfully'),
46-
$form->getContactName()
53+
$this->translate('Deleted contact "%s" successfully'),
54+
$contact->full_name
4755
));
4856

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

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

5462
$this->addContent($form);
5563
}

application/controllers/ContactsController.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Icinga\Module\Notifications\Common\Links;
1111
use Icinga\Module\Notifications\Model\Channel;
1212
use Icinga\Module\Notifications\Model\Contact;
13+
use Icinga\Module\Notifications\Repository\ContactRepository;
1314
use Icinga\Module\Notifications\View\ContactRenderer;
1415
use Icinga\Module\Notifications\Web\Control\SearchBar\ObjectSuggestions;
1516
use Icinga\Module\Notifications\Web\Form\ContactForm;
@@ -129,9 +130,10 @@ public function addAction(): void
129130
{
130131
$this->addTitleTab($this->translate('Create Contact'));
131132

132-
$form = (new ContactForm($this->db))
133+
$form = (new ContactForm())
133134
->on(Form::ON_SUBMIT, function (ContactForm $form) {
134-
$form->addContact();
135+
$contact = $form->getContact();
136+
Database::get()->transaction(fn (Connection $db) => (new ContactRepository($db))->create($contact));
135137
Notification::success($this->translate('New contact has successfully been added'));
136138
$this->switchToSingleColumnLayout();
137139
})->handleRequest($this->getServerRequest());

0 commit comments

Comments
 (0)