diff --git a/application/controllers/ContactGroupsController.php b/application/controllers/ContactGroupsController.php index 11570c44..9a9a98cc 100644 --- a/application/controllers/ContactGroupsController.php +++ b/application/controllers/ContactGroupsController.php @@ -7,6 +7,7 @@ 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\View\ContactgroupRenderer; use Icinga\Module\Notifications\Web\Control\SearchBar\ObjectSuggestions; @@ -14,7 +15,8 @@ use Icinga\Module\Notifications\Widget\MemberSuggestions; use Icinga\Web\Notification; use ipl\Html\Form; -use ipl\Html\Text; +use ipl\Html\HtmlElement; +use ipl\Html\TemplateString; use ipl\Stdlib\Filter; use ipl\Web\Compat\CompatController; use ipl\Web\Compat\SearchControls; @@ -22,6 +24,7 @@ use ipl\Web\Control\SortControl; use ipl\Web\Filter\QueryString; use ipl\Web\Layout\MinimalItemLayout; +use ipl\Web\Widget\ActionLink; use ipl\Web\Widget\ButtonLink; use ipl\Web\Widget\Tabs; @@ -78,18 +81,41 @@ public function indexAction(): void $this->addControl($sortControl); $this->addControl($limitControl); $this->addControl($searchBar); - $this->addContent( - (new ButtonLink( - Text::create(t('Add Contact Group')), - Links::contactGroupsAdd()->with(['showCompact' => true, '_disableLayout' => 1]), - 'plus', - ['class' => 'add-new-component'] - ))->openInModal() + + $addButton = new ButtonLink( + t('Add Contact Group'), + Links::contactGroupsAdd()->with(['showCompact' => true, '_disableLayout' => 1]), + 'plus', + ['class' => 'add-new-component'] ); + $emptyStateMessage = null; + if (Contact::on(Database::get())->columns('1')->first() === null) { + $addButton + ->setDisabled() + ->addAttributes(['title' => t('A contact is required to add a contact group')]); + + $emptyStateMessage = new HtmlElement( + 'span', + null, + TemplateString::create( + t( + 'No contact groups found. To add a new contact group,' + .' please {{#link}}add a contact{{/link}} first.' + ), + ['link' => new ActionLink(null, Links::contactAdd(), attributes: ['data-base-target' => '_next'])] + ) + ); + } else { + $addButton->openInModal(); + } + + $this->addContent($addButton); + $this->addContent( (new ObjectList($groups, new ContactgroupRenderer())) ->setItemLayoutClass(MinimalItemLayout::class) + ->setEmptyStateMessage($emptyStateMessage) ); if (! $searchBar->hasBeenSubmitted() && $searchBar->hasBeenSent()) { diff --git a/application/controllers/ContactsController.php b/application/controllers/ContactsController.php index 8a28fad7..bb0afd1d 100644 --- a/application/controllers/ContactsController.php +++ b/application/controllers/ContactsController.php @@ -108,7 +108,7 @@ public function addAction(): void ->on(ContactForm::ON_SUCCESS, function (ContactForm $form) { $form->addContact(); Notification::success(t('New contact has successfully been added')); - $this->redirectNow(Links::contacts()); + $this->redirectNow('__CLOSE__'); })->handleRequest($this->getServerRequest()); $this->addContent($form);