From 16a8d4a0f3181535f55e4a8d6b88efc94ed9e71e Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Thu, 30 Jan 2025 10:41:21 +0100 Subject: [PATCH 1/2] ChannelController: Disable the `Add button` and show message when no channel found ChannelForm: Throw exception in case user access the form via url --- .../controllers/ChannelsController.php | 22 ++++++++++++++----- application/forms/ChannelForm.php | 14 ++++++++++-- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/application/controllers/ChannelsController.php b/application/controllers/ChannelsController.php index 0858de438..20c2d413b 100644 --- a/application/controllers/ChannelsController.php +++ b/application/controllers/ChannelsController.php @@ -7,6 +7,7 @@ use Icinga\Module\Notifications\Common\Database; use Icinga\Module\Notifications\Common\Links; use Icinga\Module\Notifications\Forms\ChannelForm; +use Icinga\Module\Notifications\Model\AvailableChannelType; use Icinga\Module\Notifications\Model\Channel; use Icinga\Module\Notifications\View\ChannelRenderer; use Icinga\Module\Notifications\Web\Control\SearchBar\ObjectSuggestions; @@ -15,6 +16,7 @@ use Icinga\Web\Widget\Tab; use Icinga\Web\Widget\Tabs; use ipl\Sql\Connection; +use ipl\Sql\Expression; use ipl\Stdlib\Filter; use ipl\Web\Compat\CompatController; use ipl\Web\Compat\SearchControls; @@ -84,15 +86,25 @@ public function indexAction() $this->addControl($sortControl); $this->addControl($limitControl); $this->addControl($searchBar); - $this->addContent( - (new ButtonLink(t('Add Channel'), Links::channelAdd(), 'plus')) - ->setBaseTarget('_next') - ->addAttributes(['class' => 'add-new-component']) - ); + $addButton = (new ButtonLink( + t('Add Channel'), + Links::channelAdd(), + 'plus', + ['class' => 'add-new-component'] + ))->setBaseTarget('_next'); + + $emptyStateMessage = null; + if (AvailableChannelType::on($this->db)->columns([new Expression('1')])->first() === null) { + $emptyStateMessage = t('No channel types available. Make sure Icinga Notifications is running.'); + $addButton->disable($emptyStateMessage); + } + + $this->addContent($addButton); $this->addContent( (new ObjectList($channels, new ChannelRenderer())) ->setItemLayoutClass(MinimalItemLayout::class) + ->setEmptyStateMessage($emptyStateMessage) ); if (! $searchBar->hasBeenSubmitted() && $searchBar->hasBeenSent()) { diff --git a/application/forms/ChannelForm.php b/application/forms/ChannelForm.php index eb7e02bf1..86c4aa66e 100644 --- a/application/forms/ChannelForm.php +++ b/application/forms/ChannelForm.php @@ -5,6 +5,7 @@ namespace Icinga\Module\Notifications\Forms; use DateTime; +use Icinga\Exception\ConfigurationError; use Icinga\Exception\Http\HttpNotFoundException; use Icinga\Module\Notifications\Model\AvailableChannelType; use Icinga\Module\Notifications\Model\Channel; @@ -55,8 +56,19 @@ public function __construct(Connection $db) $this->db = $db; } + /** + * @throws ConfigurationError + */ protected function assemble() { + $query = AvailableChannelType::on($this->db) + ->columns(['type', 'name', 'config_attrs']) + ->execute(); + + if (! $query->hasResult()) { + throw new ConfigurationError('No channel types available. Make sure Icinga Notifications is running.'); + } + $this->addAttributes(['class' => 'channel-form']); $this->addElement($this->createCsrfCounterMeasure(Session::getSession()->getId())); @@ -70,8 +82,6 @@ protected function assemble() ] ); - $query = AvailableChannelType::on($this->db)->columns(['type', 'name', 'config_attrs']); - /** @var string[] $typesConfig */ $typesConfig = []; From 9fc7837829b4920264b6a8e260f25af2ce905132 Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Wed, 23 Jul 2025 14:35:32 +0200 Subject: [PATCH 2/2] Add method return types and remove superfluous $db property --- .../controllers/ChannelsController.php | 20 +++++++------------ application/forms/ChannelForm.php | 6 +++--- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/application/controllers/ChannelsController.php b/application/controllers/ChannelsController.php index 20c2d413b..33362594c 100644 --- a/application/controllers/ChannelsController.php +++ b/application/controllers/ChannelsController.php @@ -15,7 +15,6 @@ use Icinga\Web\Notification; use Icinga\Web\Widget\Tab; use Icinga\Web\Widget\Tabs; -use ipl\Sql\Connection; use ipl\Sql\Expression; use ipl\Stdlib\Filter; use ipl\Web\Compat\CompatController; @@ -30,22 +29,17 @@ class ChannelsController extends CompatController { use SearchControls; - /** @var Connection */ - private $db; - /** @var Filter\Rule Filter from query string parameters */ private $filter; - public function init() + public function init(): void { $this->assertPermission('config/modules'); - - $this->db = Database::get(); } - public function indexAction() + public function indexAction(): void { - $channels = Channel::on($this->db); + $channels = Channel::on(Database::get()); $this->mergeTabs($this->Module()->getConfigTabs()); $this->getTabs()->activate('channels'); @@ -95,7 +89,7 @@ public function indexAction() ))->setBaseTarget('_next'); $emptyStateMessage = null; - if (AvailableChannelType::on($this->db)->columns([new Expression('1')])->first() === null) { + if (AvailableChannelType::on(Database::get())->columns([new Expression('1')])->first() === null) { $emptyStateMessage = t('No channel types available. Make sure Icinga Notifications is running.'); $addButton->disable($emptyStateMessage); } @@ -112,10 +106,10 @@ public function indexAction() } } - public function addAction() + public function addAction(): void { $this->addTitleTab(t('Add Channel')); - $form = (new ChannelForm($this->db)) + $form = (new ChannelForm(Database::get())) ->on(ChannelForm::ON_SUCCESS, function (ChannelForm $form) { $form->addChannel(); Notification::success( @@ -142,7 +136,7 @@ public function completeAction(): void public function searchEditorAction(): void { $editor = $this->createSearchEditor( - Channel::on($this->db), + Channel::on(Database::get()), [ LimitControl::DEFAULT_LIMIT_PARAM, SortControl::DEFAULT_SORT_PARAM, diff --git a/application/forms/ChannelForm.php b/application/forms/ChannelForm.php index 86c4aa66e..19d1868a7 100644 --- a/application/forms/ChannelForm.php +++ b/application/forms/ChannelForm.php @@ -59,7 +59,7 @@ public function __construct(Connection $db) /** * @throws ConfigurationError */ - protected function assemble() + protected function assemble(): void { $query = AvailableChannelType::on($this->db) ->columns(['type', 'name', 'config_attrs']) @@ -164,7 +164,7 @@ protected function assemble() } } - public function isValid() + public function isValid(): bool { if ($this->getPressedSubmitElement()->getName() === 'delete') { $csrfElement = $this->getElement('CSRFToken'); @@ -179,7 +179,7 @@ public function isValid() return parent::isValid(); } - public function hasBeenSubmitted() + public function hasBeenSubmitted(): bool { if ($this->getPressedSubmitElement() !== null && $this->getPressedSubmitElement()->getName() === 'delete') { return true;