Skip to content

Commit a9cb565

Browse files
authored
ContactGroupsController: Show add contact link if no contact exists (#340)
2 parents a1c7630 + 987224f commit a9cb565

File tree

2 files changed

+51
-18
lines changed

2 files changed

+51
-18
lines changed

application/controllers/ContactGroupsController.php

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,25 @@
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\Channel;
11+
use Icinga\Module\Notifications\Model\Contact;
1012
use Icinga\Module\Notifications\Model\Contactgroup;
1113
use Icinga\Module\Notifications\View\ContactgroupRenderer;
1214
use Icinga\Module\Notifications\Web\Control\SearchBar\ObjectSuggestions;
1315
use Icinga\Module\Notifications\Widget\ItemList\ObjectList;
1416
use Icinga\Module\Notifications\Widget\MemberSuggestions;
1517
use Icinga\Web\Notification;
1618
use ipl\Html\Form;
17-
use ipl\Html\Text;
19+
use ipl\Html\HtmlString;
20+
use ipl\Html\TemplateString;
1821
use ipl\Stdlib\Filter;
1922
use ipl\Web\Compat\CompatController;
2023
use ipl\Web\Compat\SearchControls;
2124
use ipl\Web\Control\LimitControl;
2225
use ipl\Web\Control\SortControl;
2326
use ipl\Web\Filter\QueryString;
2427
use ipl\Web\Layout\MinimalItemLayout;
28+
use ipl\Web\Widget\ActionLink;
2529
use ipl\Web\Widget\ButtonLink;
2630
use ipl\Web\Widget\Tabs;
2731

@@ -46,8 +50,8 @@ public function indexAction(): void
4650
$sortControl = $this->createSortControl(
4751
$groups,
4852
[
49-
'name' => t('Group Name'),
50-
'changed_at' => t('Changed At')
53+
'name' => $this->translate('Group Name'),
54+
'changed_at' => $this->translate('Changed At')
5155
]
5256
);
5357

@@ -78,25 +82,54 @@ public function indexAction(): void
7882
$this->addControl($sortControl);
7983
$this->addControl($limitControl);
8084
$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()
85+
86+
$addButton = new ButtonLink(
87+
$this->translate('Add Contact Group'),
88+
Links::contactGroupsAdd()->with(['showCompact' => true, '_disableLayout' => 1]),
89+
'plus',
90+
['class' => 'add-new-component']
8891
);
8992

93+
$emptyStateMessage = null;
94+
if (Contact::on(Database::get())->columns('1')->limit(1)->first() === null) {
95+
if (Channel::on(Database::get())->columns('1')->limit(1)->first() === null) {
96+
$addButton->disable($this->translate('A channel is required to add a contact group'));
97+
98+
$emptyStateMessage = TemplateString::create(
99+
// translators: %1$s will be replaced by a line break
100+
'No contact groups found.%1$sTo add new contact group, please {{#link}}configure a Channel{{/link}}'
101+
. ' first.%1$sOnce done, you should proceed by creating your first contact.',
102+
['link' => (new ActionLink(null, Links::channelAdd()))->setBaseTarget('_next')],
103+
[HtmlString::create('<br>')]
104+
);
105+
} else {
106+
$emptyStateMessage = TemplateString::create(
107+
$this->translate(
108+
'No contact groups found. Do not forget to also'
109+
. ' {{#link}}create your first contact!{{/link}}'
110+
),
111+
['link' => (new ActionLink(null, Links::contactAdd()))->setBaseTarget('_next')]
112+
);
113+
114+
$addButton->openInModal();
115+
}
116+
} else {
117+
$addButton->openInModal();
118+
}
119+
120+
$this->addContent($addButton);
121+
90122
$this->addContent(
91123
(new ObjectList($groups, new ContactgroupRenderer()))
92124
->setItemLayoutClass(MinimalItemLayout::class)
125+
->setEmptyStateMessage($emptyStateMessage)
93126
);
94127

95128
if (! $searchBar->hasBeenSubmitted() && $searchBar->hasBeenSent()) {
96129
$this->sendMultipartUpdate();
97130
}
98131

99-
$this->setTitle(t('Contact Groups'));
132+
$this->setTitle($this->translate('Contact Groups'));
100133
$this->getTabs()->activate('contact-groups');
101134
}
102135

@@ -118,15 +151,15 @@ public function addAction(): void
118151
->on(Form::ON_SUCCESS, function (ContactGroupForm $form) {
119152
$groupIdentifier = $form->addGroup();
120153

121-
Notification::success(t('New contact group has been successfully added'));
154+
Notification::success($this->translate('New contact group has been successfully added'));
122155
$this->sendExtraUpdates(['#col1']);
123156
$this->getResponse()->setHeader('X-Icinga-Container', 'col2');
124157
$this->redirectNow(Links::contactGroup($groupIdentifier));
125158
})
126159
->handleRequest($this->getServerRequest());
127160

128161
$this->addContent($form);
129-
$this->setTitle(t('Add Contact Group'));
162+
$this->setTitle($this->translate('Add Contact Group'));
130163
}
131164

132165
public function completeAction(): void
@@ -163,19 +196,19 @@ public function getTabs(): Tabs
163196
{
164197
return parent::getTabs()
165198
->add('schedules', [
166-
'label' => t('Schedules'),
199+
'label' => $this->translate('Schedules'),
167200
'url' => Links::schedules(),
168201
'baseTarget' => '_main'
169202
])->add('event-rules', [
170-
'label' => t('Event Rules'),
203+
'label' => $this->translate('Event Rules'),
171204
'url' => Links::eventRules(),
172205
'baseTarget' => '_main'
173206
])->add('contacts', [
174-
'label' => t('Contacts'),
207+
'label' => $this->translate('Contacts'),
175208
'url' => Links::contacts(),
176209
'baseTarget' => '_main'
177210
])->add('contact-groups', [
178-
'label' => t('Contact Groups'),
211+
'label' => $this->translate('Contact Groups'),
179212
'url' => Links::contactGroups(),
180213
'baseTarget' => '_main'
181214
]);

application/controllers/ContactsController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public function addAction(): void
130130
->on(Form::ON_SUBMIT, function (ContactForm $form) {
131131
$form->addContact();
132132
Notification::success($this->translate('New contact has successfully been added'));
133-
$this->redirectNow(Links::contacts());
133+
$this->switchToSingleColumnLayout();
134134
})->handleRequest($this->getServerRequest());
135135

136136
$this->addContent($form);

0 commit comments

Comments
 (0)