Skip to content

Commit 4cdba0c

Browse files
ChannelController: Disable the Add button and show message when no channel found
ChannelForm: Throw exception in case user access the form via url
1 parent 4efa60e commit 4cdba0c

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

application/controllers/ChannelsController.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Icinga\Module\Notifications\Common\Database;
88
use Icinga\Module\Notifications\Common\Links;
99
use Icinga\Module\Notifications\Forms\ChannelForm;
10+
use Icinga\Module\Notifications\Model\AvailableChannelType;
1011
use Icinga\Module\Notifications\Model\Channel;
1112
use Icinga\Module\Notifications\View\ChannelRenderer;
1213
use Icinga\Module\Notifications\Web\Control\SearchBar\ObjectSuggestions;
@@ -15,6 +16,7 @@
1516
use Icinga\Web\Widget\Tab;
1617
use Icinga\Web\Widget\Tabs;
1718
use ipl\Sql\Connection;
19+
use ipl\Sql\Expression;
1820
use ipl\Stdlib\Filter;
1921
use ipl\Web\Compat\CompatController;
2022
use ipl\Web\Compat\SearchControls;
@@ -84,15 +86,25 @@ public function indexAction()
8486
$this->addControl($sortControl);
8587
$this->addControl($limitControl);
8688
$this->addControl($searchBar);
87-
$this->addContent(
88-
(new ButtonLink(t('Add Channel'), Links::channelAdd(), 'plus'))
89-
->setBaseTarget('_next')
90-
->addAttributes(['class' => 'add-new-component'])
91-
);
9289

90+
$addButton = (new ButtonLink(
91+
t('Add Channel'),
92+
Links::channelAdd(),
93+
'plus',
94+
['class' => 'add-new-component']
95+
))->setBaseTarget('_next');
96+
97+
$emptyStateMessage = null;
98+
if (AvailableChannelType::on($this->db)->columns([new Expression('1')])->first() === null) {
99+
$emptyStateMessage = t('No channel types available. Make sure Icinga Notifications is running.');
100+
$addButton->disable($emptyStateMessage);
101+
}
102+
103+
$this->addContent($addButton);
93104
$this->addContent(
94105
(new ObjectList($channels, new ChannelRenderer()))
95106
->setItemLayoutClass(MinimalItemLayout::class)
107+
->setEmptyStateMessage($emptyStateMessage)
96108
);
97109

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

application/forms/ChannelForm.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Icinga\Module\Notifications\Forms;
66

77
use DateTime;
8+
use Icinga\Exception\ConfigurationError;
89
use Icinga\Exception\Http\HttpNotFoundException;
910
use Icinga\Module\Notifications\Model\AvailableChannelType;
1011
use Icinga\Module\Notifications\Model\Channel;
@@ -55,8 +56,19 @@ public function __construct(Connection $db)
5556
$this->db = $db;
5657
}
5758

59+
/**
60+
* @throws ConfigurationError
61+
*/
5862
protected function assemble()
5963
{
64+
$query = AvailableChannelType::on($this->db)
65+
->columns(['type', 'name', 'config_attrs'])
66+
->execute();
67+
68+
if (! $query->hasResult()) {
69+
throw new ConfigurationError('No channel types available. Make sure Icinga Notifications is running.');
70+
}
71+
6072
$this->addAttributes(['class' => 'channel-form']);
6173
$this->addElement($this->createCsrfCounterMeasure(Session::getSession()->getId()));
6274

@@ -70,8 +82,6 @@ protected function assemble()
7082
]
7183
);
7284

73-
$query = AvailableChannelType::on($this->db)->columns(['type', 'name', 'config_attrs']);
74-
7585
/** @var string[] $typesConfig */
7686
$typesConfig = [];
7787

0 commit comments

Comments
 (0)