Skip to content

Commit bc29c37

Browse files
ContactGroupsController: Show add contact link if no contact exists
- ContactsContoller: Closing the container is sufficient.
1 parent 4efa60e commit bc29c37

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

application/controllers/ContactGroupsController.php

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,24 @@
77
use Icinga\Module\Notifications\Common\Database;
88
use Icinga\Module\Notifications\Common\Links;
99
use Icinga\Module\Notifications\Forms\ContactGroupForm;
10+
use Icinga\Module\Notifications\Model\Contact;
1011
use Icinga\Module\Notifications\Model\Contactgroup;
1112
use Icinga\Module\Notifications\View\ContactgroupRenderer;
1213
use Icinga\Module\Notifications\Web\Control\SearchBar\ObjectSuggestions;
1314
use Icinga\Module\Notifications\Widget\ItemList\ObjectList;
1415
use Icinga\Module\Notifications\Widget\MemberSuggestions;
1516
use Icinga\Web\Notification;
1617
use ipl\Html\Form;
17-
use ipl\Html\Text;
18+
use ipl\Html\HtmlElement;
19+
use ipl\Html\TemplateString;
1820
use ipl\Stdlib\Filter;
1921
use ipl\Web\Compat\CompatController;
2022
use ipl\Web\Compat\SearchControls;
2123
use ipl\Web\Control\LimitControl;
2224
use ipl\Web\Control\SortControl;
2325
use ipl\Web\Filter\QueryString;
2426
use ipl\Web\Layout\MinimalItemLayout;
27+
use ipl\Web\Widget\ActionLink;
2528
use ipl\Web\Widget\ButtonLink;
2629
use ipl\Web\Widget\Tabs;
2730

@@ -78,18 +81,44 @@ public function indexAction(): void
7881
$this->addControl($sortControl);
7982
$this->addControl($limitControl);
8083
$this->addControl($searchBar);
81-
$this->addContent(
82-
(new ButtonLink(
83-
Text::create(t('Add Contact Group')),
84-
Links::contactGroupsAdd()->with(['showCompact' => true, '_disableLayout' => 1]),
85-
'plus',
86-
['class' => 'add-new-component']
87-
))->openInModal()
84+
85+
$addButton = new ButtonLink(
86+
t('Add Contact Group'),
87+
Links::contactGroupsAdd()->with(['showCompact' => true, '_disableLayout' => 1]),
88+
'plus',
89+
['class' => 'add-new-component']
8890
);
8991

92+
$emptyStateMessage = null;
93+
if (Contact::on(Database::get())->columns('1')->first() === null) {
94+
$addButton
95+
->setDisabled()
96+
->addAttributes(['title' => t('A contact is required to add a contact group')]);
97+
98+
$emptyStateMessage = new HtmlElement(
99+
'span',
100+
null,
101+
TemplateString::create(
102+
$this->translate(
103+
'No contacts found. To add a new contact group, please {{#link}}add a Contact{{/link}} first.'
104+
),
105+
['link' => new ActionLink(
106+
null,
107+
Links::contactAdd(),
108+
attributes: ['data-base-target' => '_next']
109+
)]
110+
)
111+
);
112+
} else {
113+
$addButton->openInModal();
114+
}
115+
116+
$this->addContent($addButton);
117+
90118
$this->addContent(
91119
(new ObjectList($groups, new ContactgroupRenderer()))
92120
->setItemLayoutClass(MinimalItemLayout::class)
121+
->setEmptyStateMessage($emptyStateMessage)
93122
);
94123

95124
if (! $searchBar->hasBeenSubmitted() && $searchBar->hasBeenSent()) {

application/controllers/ContactsController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public function addAction(): void
108108
->on(ContactForm::ON_SUCCESS, function (ContactForm $form) {
109109
$form->addContact();
110110
Notification::success(t('New contact has successfully been added'));
111-
$this->redirectNow(Links::contacts());
111+
$this->redirectNow('__CLOSE__');
112112
})->handleRequest($this->getServerRequest());
113113

114114
$this->addContent($form);

0 commit comments

Comments
 (0)