From c67b87309649dc002a3a4abdbb433331fac68d76 Mon Sep 17 00:00:00 2001 From: Yonas Habteab Date: Tue, 31 Aug 2021 17:12:22 +0200 Subject: [PATCH] Show a notification for every successful action --- .../CommandTransportController.php | 8 +++ .../Instance/ToggleInstanceFeaturesForm.php | 71 +++++++++++++++++++ .../Command/Object/AcknowledgeProblemForm.php | 24 +++++++ .../forms/Command/Object/AddCommentForm.php | 22 ++++++ .../Command/Object/DeleteCommentForm.php | 13 ++++ .../Command/Object/DeleteDowntimeForm.php | 13 ++++ .../Command/Object/ProcessCheckResultForm.php | 23 ++++++ .../Object/RemoveAcknowledgementForm.php | 24 +++++++ .../Command/Object/ScheduleCheckForm.php | 22 ++++++ .../Object/ScheduleHostDowntimeForm.php | 10 +++ .../Object/ScheduleServiceDowntimeForm.php | 10 +++ .../Object/SendCustomNotificationForm.php | 24 +++++++ .../Object/ToggleObjectFeaturesForm.php | 55 ++++++++++++++ 13 files changed, 319 insertions(+) diff --git a/application/controllers/CommandTransportController.php b/application/controllers/CommandTransportController.php index 15264eea..09071eab 100644 --- a/application/controllers/CommandTransportController.php +++ b/application/controllers/CommandTransportController.php @@ -11,6 +11,7 @@ use Icinga\Forms\ConfirmRemovalForm; use Icinga\Module\Icingadb\Command\Transport\CommandTransportConfig; use Icinga\Module\Icingadb\Forms\ApiTransportForm; use Icinga\Module\Icingadb\Widget\ItemList\CommandTransportList; +use Icinga\Web\Notification; use ipl\Html\HtmlString; use ipl\Web\Widget\ButtonLink; @@ -64,6 +65,8 @@ class CommandTransportController extends ConfigController Filter::where('name', $transportName) ); + Notification::success(sprintf(t('Updated command transport "%s" successfully'), $transportName)); + $this->redirectNow('icingadb/command-transport'); }); @@ -80,6 +83,9 @@ class CommandTransportController extends ConfigController $form = new ApiTransportForm(); $form->on(ApiTransportForm::ON_SUCCESS, function (ApiTransportForm $form) { (new CommandTransportConfig())->insert('transport', $form->getValues()); + + Notification::success(t('Created command transport successfully')); + $this->redirectNow('icingadb/command-transport'); }); @@ -103,6 +109,8 @@ class CommandTransportController extends ConfigController Filter::where('name', $transportName) ); + Notification::success(sprintf(t('Removed command transport "%s" successfully'), $transportName)); + $this->redirectNow('icingadb/command-transport'); }); diff --git a/application/forms/Command/Instance/ToggleInstanceFeaturesForm.php b/application/forms/Command/Instance/ToggleInstanceFeaturesForm.php index 58136730..17c7d6da 100644 --- a/application/forms/Command/Instance/ToggleInstanceFeaturesForm.php +++ b/application/forms/Command/Instance/ToggleInstanceFeaturesForm.php @@ -7,6 +7,7 @@ namespace Icinga\Module\Icingadb\Forms\Command\Instance; use Icinga\Module\Icingadb\Command\Instance\ToggleInstanceFeatureCommand; use Icinga\Module\Icingadb\Common\Auth; use Icinga\Module\Icingadb\Forms\Command\CommandForm; +use Icinga\Web\Notification; use ipl\Orm\Model; use ipl\Web\FormDecorator\IcingaFormDecorator; @@ -18,6 +19,13 @@ class ToggleInstanceFeaturesForm extends CommandForm protected $featureStatus; + /** + * ToggleFeature(s) being used to submit this form + * + * @var ToggleInstanceFeatureCommand[] + */ + protected $submittedFeatures = []; + public function __construct($featureStatus) { $this->featureStatus = $featureStatus; @@ -37,6 +45,67 @@ class ToggleInstanceFeaturesForm extends CommandForm ]; $this->getAttributes()->add('class', 'instance-features'); + + $this->on(self::ON_SUCCESS, function () { + foreach ($this->submittedFeatures as $feature) { + $enabled = $feature->getEnabled(); + switch ($feature->getFeature()) { + case ToggleInstanceFeatureCommand::FEATURE_ACTIVE_HOST_CHECKS: + if ($enabled) { + $message = t('Enabled active host checks successfully'); + } else { + $message = t('Disabled active host checks successfully'); + } + + break; + case ToggleInstanceFeatureCommand::FEATURE_ACTIVE_SERVICE_CHECKS: + if ($enabled) { + $message = t('Enabled active service checks successfully'); + } else { + $message = t('Disabled active service checks successfully'); + } + + break; + case ToggleInstanceFeatureCommand::FEATURE_EVENT_HANDLERS: + if ($enabled) { + $message = t('Enabled event handlers successfully'); + } else { + $message = t('Disabled event handlers checks successfully'); + } + + break; + case ToggleInstanceFeatureCommand::FEATURE_FLAP_DETECTION: + if ($enabled) { + $message = t('Enabled flap detection successfully'); + } else { + $message = t('Disabled flap detection successfully'); + } + + break; + case ToggleInstanceFeatureCommand::FEATURE_NOTIFICATIONS: + if ($enabled) { + $message = t('Enabled notifications successfully'); + } else { + $message = t('Disabled notifications successfully'); + } + + break; + case ToggleInstanceFeatureCommand::FEATURE_PERFORMANCE_DATA: + if ($enabled) { + $message = t('Enabled performance data successfully'); + } else { + $message = t('Disabled performance data successfully'); + } + + break; + default: + $message = t('Invalid feature option'); + break; + } + + Notification::success($message); + } + }); } protected function assembleElements() @@ -76,6 +145,8 @@ class ToggleInstanceFeaturesForm extends CommandForm $command->setFeature($feature); $command->setEnabled((int) $featureState); + $this->submittedFeatures[] = $command; + yield $command; } } diff --git a/application/forms/Command/Object/AcknowledgeProblemForm.php b/application/forms/Command/Object/AcknowledgeProblemForm.php index 2f8450c2..5632de38 100644 --- a/application/forms/Command/Object/AcknowledgeProblemForm.php +++ b/application/forms/Command/Object/AcknowledgeProblemForm.php @@ -10,6 +10,8 @@ use Icinga\Application\Config; use Icinga\Module\Icingadb\Command\Object\AcknowledgeProblemCommand; use Icinga\Module\Icingadb\Common\Auth; use Icinga\Module\Icingadb\Forms\Command\CommandForm; +use Icinga\Module\Icingadb\Model\Host; +use Icinga\Web\Notification; use ipl\Html\Attributes; use ipl\Html\HtmlElement; use ipl\Html\Text; @@ -21,6 +23,28 @@ class AcknowledgeProblemForm extends CommandForm { use Auth; + public function __construct() + { + $this->on(self::ON_SUCCESS, function () { + $countObjects = count($this->getObjects()); + if (current($this->getObjects()) instanceof Host) { + $message = sprintf(tp( + 'Acknowledged problem successfully', + 'Acknowledged problem on %d hosts successfully', + $countObjects + ), $countObjects); + } else { + $message = sprintf(tp( + 'Acknowledged problem successfully', + 'Acknowledged problem on %d services successfully', + $countObjects + ), $countObjects); + } + + Notification::success($message); + }); + } + protected function assembleElements() { $this->addHtml(new HtmlElement( diff --git a/application/forms/Command/Object/AddCommentForm.php b/application/forms/Command/Object/AddCommentForm.php index 24b623d2..f95870ea 100644 --- a/application/forms/Command/Object/AddCommentForm.php +++ b/application/forms/Command/Object/AddCommentForm.php @@ -10,6 +10,8 @@ use Icinga\Application\Config; use Icinga\Module\Icingadb\Command\Object\AddCommentCommand; use Icinga\Module\Icingadb\Common\Auth; use Icinga\Module\Icingadb\Forms\Command\CommandForm; +use Icinga\Module\Icingadb\Model\Host; +use Icinga\Web\Notification; use ipl\Html\Attributes; use ipl\Html\HtmlElement; use ipl\Html\Text; @@ -21,6 +23,26 @@ class AddCommentForm extends CommandForm { use Auth; + public function __construct() + { + $this->on(self::ON_SUCCESS, function () { + $countObjects = count($this->getObjects()); + if (current($this->getObjects()) instanceof Host) { + $message = sprintf( + tp('Added comment successfully', 'Added comment to %d hosts successfully', $countObjects), + $countObjects + ); + } else { + $message = sprintf( + tp('Added comment successfully', 'Added comment to %d services successfully', $countObjects), + $countObjects + ); + } + + Notification::success($message); + }); + } + protected function assembleElements() { $this->addHtml(new HtmlElement( diff --git a/application/forms/Command/Object/DeleteCommentForm.php b/application/forms/Command/Object/DeleteCommentForm.php index 30db8f7e..e7ced804 100644 --- a/application/forms/Command/Object/DeleteCommentForm.php +++ b/application/forms/Command/Object/DeleteCommentForm.php @@ -7,6 +7,7 @@ namespace Icinga\Module\Icingadb\Forms\Command\Object; use Icinga\Module\Icingadb\Command\Object\DeleteCommentCommand; use Icinga\Module\Icingadb\Common\Auth; use Icinga\Module\Icingadb\Forms\Command\CommandForm; +use Icinga\Web\Notification; use ipl\Orm\Model; use ipl\Web\Common\RedirectOption; use ipl\Web\Widget\Icon; @@ -16,6 +17,18 @@ class DeleteCommentForm extends CommandForm use Auth; use RedirectOption; + public function __construct() + { + $this->on(self::ON_SUCCESS, function () { + $countObjects = count($this->getObjects()); + + Notification::success(sprintf( + tp('Removed comment successfully', 'Removed comment from %d objects successfully', $countObjects), + $countObjects + )); + }); + } + protected function assembleElements() { $this->addElement($this->createRedirectOption()); diff --git a/application/forms/Command/Object/DeleteDowntimeForm.php b/application/forms/Command/Object/DeleteDowntimeForm.php index 808e4990..7ccc9721 100644 --- a/application/forms/Command/Object/DeleteDowntimeForm.php +++ b/application/forms/Command/Object/DeleteDowntimeForm.php @@ -7,6 +7,7 @@ namespace Icinga\Module\Icingadb\Forms\Command\Object; use Icinga\Module\Icingadb\Command\Object\DeleteDowntimeCommand; use Icinga\Module\Icingadb\Common\Auth; use Icinga\Module\Icingadb\Forms\Command\CommandForm; +use Icinga\Web\Notification; use ipl\Orm\Model; use ipl\Web\Common\RedirectOption; use ipl\Web\Widget\Icon; @@ -18,6 +19,18 @@ class DeleteDowntimeForm extends CommandForm protected $defaultAttributes = ['class' => 'inline']; + public function __construct() + { + $this->on(self::ON_SUCCESS, function () { + $countObjects = count($this->getObjects()); + + Notification::success(sprintf( + tp('Removed downtime successfully', 'Removed downtime from %d objects successfully', $countObjects), + $countObjects + )); + }); + } + protected function assembleElements() { $this->addElement($this->createRedirectOption()); diff --git a/application/forms/Command/Object/ProcessCheckResultForm.php b/application/forms/Command/Object/ProcessCheckResultForm.php index 566284f5..27b75e3f 100644 --- a/application/forms/Command/Object/ProcessCheckResultForm.php +++ b/application/forms/Command/Object/ProcessCheckResultForm.php @@ -8,6 +8,7 @@ use Icinga\Module\Icingadb\Command\Object\ProcessCheckResultCommand; use Icinga\Module\Icingadb\Common\Auth; use Icinga\Module\Icingadb\Forms\Command\CommandForm; use Icinga\Module\Icingadb\Model\Host; +use Icinga\Web\Notification; use ipl\Html\Attributes; use ipl\Html\HtmlElement; use ipl\Html\Text; @@ -19,6 +20,28 @@ class ProcessCheckResultForm extends CommandForm { use Auth; + public function __construct() + { + $this->on(self::ON_SUCCESS, function () { + $countObjects = count($this->getObjects()); + if (current($this->getObjects()) instanceof Host) { + $message = sprintf(tp( + 'Submitted passive check result successfully', + 'Submitted passive check result for %d hosts successfully', + $countObjects + ), $countObjects); + } else { + $message = sprintf(tp( + 'Submitted passive check result successfully', + 'Submitted passive check result for %d services successfully', + $countObjects + ), $countObjects); + } + + Notification::success($message); + }); + } + protected function assembleElements() { $this->addHtml(new HtmlElement( diff --git a/application/forms/Command/Object/RemoveAcknowledgementForm.php b/application/forms/Command/Object/RemoveAcknowledgementForm.php index c967bd14..ec34abcb 100644 --- a/application/forms/Command/Object/RemoveAcknowledgementForm.php +++ b/application/forms/Command/Object/RemoveAcknowledgementForm.php @@ -7,6 +7,8 @@ namespace Icinga\Module\Icingadb\Forms\Command\Object; use Icinga\Module\Icingadb\Command\Object\RemoveAcknowledgementCommand; use Icinga\Module\Icingadb\Common\Auth; use Icinga\Module\Icingadb\Forms\Command\CommandForm; +use Icinga\Module\Icingadb\Model\Host; +use Icinga\Web\Notification; use ipl\Orm\Model; use ipl\Web\Widget\Icon; @@ -14,6 +16,28 @@ class RemoveAcknowledgementForm extends CommandForm { use Auth; + public function __construct() + { + $this->on(self::ON_SUCCESS, function () { + $countObjects = count($this->getObjects()); + if (current($this->getObjects()) instanceof Host) { + $message = sprintf(tp( + 'Removed acknowledgment successfully', + 'Removed acknowledgment from %d hosts successfully', + $countObjects + ), $countObjects); + } else { + $message = sprintf(tp( + 'Removed acknowledgment successfully', + 'Removed acknowledgment from %d services successfully', + $countObjects + ), $countObjects); + } + + Notification::success($message); + }); + } + protected $defaultAttributes = ['class' => 'inline']; protected function assembleElements() diff --git a/application/forms/Command/Object/ScheduleCheckForm.php b/application/forms/Command/Object/ScheduleCheckForm.php index 5c294ebd..b6bd8365 100644 --- a/application/forms/Command/Object/ScheduleCheckForm.php +++ b/application/forms/Command/Object/ScheduleCheckForm.php @@ -9,6 +9,8 @@ use DateTime; use Icinga\Module\Icingadb\Command\Object\ScheduleCheckCommand; use Icinga\Module\Icingadb\Common\Auth; use Icinga\Module\Icingadb\Forms\Command\CommandForm; +use Icinga\Module\Icingadb\Model\Host; +use Icinga\Web\Notification; use ipl\Html\Attributes; use ipl\Html\HtmlElement; use ipl\Html\Text; @@ -20,6 +22,26 @@ class ScheduleCheckForm extends CommandForm { use Auth; + public function __construct() + { + $this->on(self::ON_SUCCESS, function () { + $countObjects = count($this->getObjects()); + if (current($this->getObjects()) instanceof Host) { + $message = sprintf( + tp('Scheduled check successfully', 'Scheduled check for %d hosts successfully', $countObjects), + $countObjects + ); + } else { + $message = sprintf( + tp('Scheduled check successfully', 'Scheduled check for %d services successfully', $countObjects), + $countObjects + ); + } + + Notification::success($message); + }); + } + protected function assembleElements() { $this->addHtml(new HtmlElement( diff --git a/application/forms/Command/Object/ScheduleHostDowntimeForm.php b/application/forms/Command/Object/ScheduleHostDowntimeForm.php index e09bb96f..24f154e7 100644 --- a/application/forms/Command/Object/ScheduleHostDowntimeForm.php +++ b/application/forms/Command/Object/ScheduleHostDowntimeForm.php @@ -9,6 +9,7 @@ use DateTime; use Icinga\Application\Config; use Icinga\Module\Icingadb\Command\Object\PropagateHostDowntimeCommand; use Icinga\Module\Icingadb\Command\Object\ScheduleHostDowntimeCommand; +use Icinga\Web\Notification; use ipl\Orm\Model; use ipl\Web\FormDecorator\IcingaFormDecorator; @@ -35,6 +36,15 @@ class ScheduleHostDowntimeForm extends ScheduleServiceDowntimeForm $flexibleDuration = $config->get('settings', 'hostdowntime_flexible_duration', 'PT2H'); $this->flexibleDuration = new DateInterval($flexibleDuration); + + $this->on(self::ON_SUCCESS, function () { + $countObjects = count($this->getObjects()); + + Notification::success(sprintf( + tp('Scheduled downtime successfully', 'Scheduled downtime for %d hosts successfully', $countObjects), + $countObjects + )); + }); } protected function assembleElements() diff --git a/application/forms/Command/Object/ScheduleServiceDowntimeForm.php b/application/forms/Command/Object/ScheduleServiceDowntimeForm.php index 0ff2b66a..e2f35769 100644 --- a/application/forms/Command/Object/ScheduleServiceDowntimeForm.php +++ b/application/forms/Command/Object/ScheduleServiceDowntimeForm.php @@ -10,6 +10,7 @@ use Icinga\Application\Config; use Icinga\Module\Icingadb\Command\Object\ScheduleServiceDowntimeCommand; use Icinga\Module\Icingadb\Common\Auth; use Icinga\Module\Icingadb\Forms\Command\CommandForm; +use Icinga\Web\Notification; use ipl\Html\Attributes; use ipl\Html\HtmlElement; use ipl\Html\Text; @@ -57,6 +58,15 @@ class ScheduleServiceDowntimeForm extends CommandForm $flexibleDuration = $config->get('settings', 'servicedowntime_flexible_duration', 'PT2H'); $this->flexibleDuration = new DateInterval($flexibleDuration); + + $this->on(self::ON_SUCCESS, function () { + $countObjects = count($this->getObjects()); + + Notification::success(sprintf( + tp('Scheduled downtime successfully', 'Scheduled downtime for %d services successfully', $countObjects), + $countObjects + )); + }); } protected function assembleElements() diff --git a/application/forms/Command/Object/SendCustomNotificationForm.php b/application/forms/Command/Object/SendCustomNotificationForm.php index afc96eab..6e4db343 100644 --- a/application/forms/Command/Object/SendCustomNotificationForm.php +++ b/application/forms/Command/Object/SendCustomNotificationForm.php @@ -8,6 +8,8 @@ use Icinga\Application\Config; use Icinga\Module\Icingadb\Command\Object\SendCustomNotificationCommand; use Icinga\Module\Icingadb\Common\Auth; use Icinga\Module\Icingadb\Forms\Command\CommandForm; +use Icinga\Module\Icingadb\Model\Host; +use Icinga\Web\Notification; use ipl\Html\Attributes; use ipl\Html\HtmlElement; use ipl\Html\Text; @@ -19,6 +21,28 @@ class SendCustomNotificationForm extends CommandForm { use Auth; + public function __construct() + { + $this->on(self::ON_SUCCESS, function () { + $countObjects = count($this->getObjects()); + if (current($this->getObjects()) instanceof Host) { + $message = sprintf(tp( + 'Sent custom notification successfully', + 'Sent custom notification for %d hosts successfully', + $countObjects + ), $countObjects); + } else { + $message = sprintf(tp( + 'Sent custom notification successfully', + 'Sent custom notification for %d services successfully', + $countObjects + ), $countObjects); + } + + Notification::success($message); + }); + } + protected function assembleElements() { $this->addHtml(new HtmlElement( diff --git a/application/forms/Command/Object/ToggleObjectFeaturesForm.php b/application/forms/Command/Object/ToggleObjectFeaturesForm.php index ceaa6484..b544e917 100644 --- a/application/forms/Command/Object/ToggleObjectFeaturesForm.php +++ b/application/forms/Command/Object/ToggleObjectFeaturesForm.php @@ -7,6 +7,7 @@ namespace Icinga\Module\Icingadb\Forms\Command\Object; use Icinga\Module\Icingadb\Command\Object\ToggleObjectFeatureCommand; use Icinga\Module\Icingadb\Common\Auth; use Icinga\Module\Icingadb\Forms\Command\CommandForm; +use Icinga\Web\Notification; use ipl\Orm\Model; use ipl\Web\FormDecorator\IcingaFormDecorator; @@ -20,6 +21,13 @@ class ToggleObjectFeaturesForm extends CommandForm protected $featureStatus; + /** + * ToggleFeature(s) being used to submit this form + * + * @var ToggleObjectFeatureCommand[] + */ + protected $submittedFeatures = []; + public function __construct($featureStatus) { $this->featureStatus = $featureStatus; @@ -47,6 +55,51 @@ class ToggleObjectFeaturesForm extends CommandForm ]; $this->getAttributes()->add('class', 'object-features'); + + $this->on(self::ON_SUCCESS, function () { + foreach ($this->submittedFeatures as $feature) { + $enabled = $feature->getEnabled(); + switch ($feature->getFeature()) { + case ToggleObjectFeatureCommand::FEATURE_ACTIVE_CHECKS: + if ($enabled) { + $message = t('Enabled active checks successfully'); + } else { + $message = t('Disabled active checks successfully'); + } + + break; + case ToggleObjectFeatureCommand::FEATURE_EVENT_HANDLER: + if ($enabled) { + $message = t('Enabled event handler successfully'); + } else { + $message = t('Disabled event handler checks successfully'); + } + + break; + case ToggleObjectFeatureCommand::FEATURE_FLAP_DETECTION: + if ($enabled) { + $message = t('Enabled flap detection successfully'); + } else { + $message = t('Disabled flap detection successfully'); + } + + break; + case ToggleObjectFeatureCommand::FEATURE_NOTIFICATIONS: + if ($enabled) { + $message = t('Enabled notifications successfully'); + } else { + $message = t('Disabled notifications successfully'); + } + + break; + default: + $message = t('Invalid feature option'); + break; + } + + Notification::success($message); + } + }); } protected function assembleElements() @@ -109,6 +162,8 @@ class ToggleObjectFeaturesForm extends CommandForm $command->setFeature($feature); $command->setEnabled((int) $featureState); + $this->submittedFeatures[] = $command; + yield $command; } }