Skip to content

Commit c7e057e

Browse files
ChannelController: Show message instead of add button when no channel type found
ChannelForm: Throw exception in case user access the form via url
1 parent ede7734 commit c7e057e

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

application/controllers/ChannelsController.php

+23-9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Icinga\Module\Notifications\Common\Database;
88
use Icinga\Module\Notifications\Forms\ChannelForm;
9+
use Icinga\Module\Notifications\Model\AvailableChannelType;
910
use Icinga\Module\Notifications\Model\Channel;
1011
use Icinga\Module\Notifications\Web\Control\SearchBar\ObjectSuggestions;
1112
use Icinga\Module\Notifications\Widget\ItemList\ChannelList;
@@ -14,6 +15,7 @@
1415
use Icinga\Web\Widget\Tabs;
1516
use ipl\Html\ValidHtml;
1617
use ipl\Sql\Connection;
18+
use ipl\Sql\Expression;
1719
use ipl\Stdlib\Filter;
1820
use ipl\Web\Common\BaseItemList;
1921
use ipl\Web\Compat\CompatController;
@@ -23,6 +25,7 @@
2325
use ipl\Web\Filter\QueryString;
2426
use ipl\Web\Url;
2527
use ipl\Web\Widget\ButtonLink;
28+
use ipl\Web\Widget\EmptyStateBar;
2629

2730
class ChannelsController extends CompatController
2831
{
@@ -84,16 +87,27 @@ public function indexAction()
8487
$this->addControl($sortControl);
8588
$this->addControl($limitControl);
8689
$this->addControl($searchBar);
87-
$this->addContent(
88-
(new ButtonLink(
89-
t('Add Channel'),
90-
Url::fromPath('notifications/channels/add'),
91-
'plus'
92-
))->setBaseTarget('_next')
93-
->addAttributes(['class' => 'add-new-component'])
94-
);
9590

96-
$this->addContent(new ChannelList($channels));
91+
$typesExists = AvailableChannelType::on($this->db)
92+
->columns([new Expression('1')])
93+
->first() !== null;
94+
95+
if ($typesExists) {
96+
$this->addContent(
97+
(new ButtonLink(
98+
t('Add Channel'),
99+
Url::fromPath('notifications/channels/add'),
100+
'plus'
101+
))->setBaseTarget('_next')
102+
->addAttributes(['class' => 'add-new-component'])
103+
);
104+
105+
$this->addContent(new ChannelList($channels));
106+
} else {
107+
$this->addContent(new EmptyStateBar(
108+
t('No channel types available. Make sure Icinga Notifications is running.')
109+
));
110+
}
97111

98112
if (! $searchBar->hasBeenSubmitted() && $searchBar->hasBeenSent()) {
99113
$this->sendMultipartUpdate();

application/forms/ChannelForm.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Icinga\Module\Notifications\Forms;
66

77
use Icinga\Exception\Http\HttpNotFoundException;
8+
use Icinga\Exception\IcingaException;
89
use Icinga\Module\Notifications\Model\Channel;
910
use Icinga\Module\Notifications\Model\AvailableChannelType;
1011
use Icinga\Module\Notifications\Model\Contact;
@@ -54,6 +55,14 @@ public function __construct(Connection $db)
5455

5556
protected function assemble()
5657
{
58+
$query = AvailableChannelType::on($this->db)
59+
->columns(['type', 'name', 'config_attrs'])
60+
->execute();
61+
62+
if (! $query->hasResult()) {
63+
throw new IcingaException('No channel types available. Make sure Icinga Notifications is running.');
64+
}
65+
5766
$this->addAttributes(['class' => 'channel-form']);
5867
$this->addElement($this->createCsrfCounterMeasure(Session::getSession()->getId()));
5968

@@ -67,8 +76,6 @@ protected function assemble()
6776
]
6877
);
6978

70-
$query = AvailableChannelType::on($this->db)->columns(['type', 'name', 'config_attrs']);
71-
7279
/** @var string[] $typesConfig */
7380
$typesConfig = [];
7481

0 commit comments

Comments
 (0)