From 8ca6cc32b2c789a3149861159ad258f2cb9491e3 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 28 Apr 2024 14:39:39 +0200 Subject: Adding upstream version 2.11.4. Signed-off-by: Daniel Baumann --- library/Icinga/Web/Notification.php | 220 ++++++++++++++++++++++++++++++++++++ 1 file changed, 220 insertions(+) create mode 100644 library/Icinga/Web/Notification.php (limited to 'library/Icinga/Web/Notification.php') diff --git a/library/Icinga/Web/Notification.php b/library/Icinga/Web/Notification.php new file mode 100644 index 0000000..6f33a32 --- /dev/null +++ b/library/Icinga/Web/Notification.php @@ -0,0 +1,220 @@ + + * [getUser()]->notify('some message', Notification::INFO); + * + */ +class Notification +{ + /** + * Notification type info + * + * @var string + */ + const INFO = 'info'; + + /** + * Notification type error + * + * @var string + */ + const ERROR = 'error'; + + /** + * Notification type success + * + * @var string + */ + const SUCCESS = 'success'; + + /** + * Notification type warning + * + * @var string + */ + const WARNING = 'warning'; + + /** + * Name of the session key for notification messages + * + * @var string + */ + const SESSION_KEY = 'session'; + + /** + * Singleton instance + * + * @var self + */ + protected static $instance; + + /** + * Whether the platform is CLI + * + * @var bool + */ + protected $isCli = false; + + /** + * Notification messages + * + * @var array + */ + protected $messages = array(); + + /** + * Session + * + * @var Session + */ + protected $session; + + /** + * Create the notification instance + */ + final private function __construct() + { + if (Platform::isCli()) { + $this->isCli = true; + return; + } + + $this->session = Session::getSession(); + $messages = $this->session->get(self::SESSION_KEY); + if (is_array($messages)) { + $this->messages = $messages; + $this->session->delete(self::SESSION_KEY); + $this->session->write(); + } + } + + /** + * Get the Notification instance + * + * @return Notification + */ + public static function getInstance() + { + if (self::$instance === null) { + self::$instance = new self(); + } + return self::$instance; + } + + /** + * Add info notification + * + * @param string $msg + */ + public static function info($msg) + { + self::getInstance()->addMessage($msg, self::INFO); + } + + /** + * Add error notification + * + * @param string $msg + */ + public static function error($msg) + { + self::getInstance()->addMessage($msg, self::ERROR); + } + + /** + * Add success notification + * + * @param string $msg + */ + public static function success($msg) + { + self::getInstance()->addMessage($msg, self::SUCCESS); + } + + /** + * Add warning notification + * + * @param string $msg + */ + public static function warning($msg) + { + self::getInstance()->addMessage($msg, self::WARNING); + } + + /** + * Add a notification message + * + * @param string $message + * @param string $type + */ + protected function addMessage($message, $type = self::INFO) + { + if ($this->isCli) { + $msg = sprintf('[%s] %s', $type, $message); + switch ($type) { + case self::INFO: + case self::SUCCESS: + Logger::info($msg); + break; + case self::ERROR: + Logger::error($msg); + break; + case self::WARNING: + Logger::warning($msg); + break; + } + } else { + $this->messages[] = (object) array( + 'type' => $type, + 'message' => $message, + ); + } + } + + /** + * Pop the notification messages + * + * @return array + */ + public function popMessages() + { + $messages = $this->messages; + $this->messages = array(); + return $messages; + } + + /** + * Get whether notification messages have been added + * + * @return bool + */ + public function hasMessages() + { + return ! empty($this->messages); + } + + /** + * Destroy the notification instance + */ + final public function __destruct() + { + if ($this->isCli) { + return; + } + if ($this->hasMessages() && $this->session->get('messages') !== $this->messages) { + $this->session->set(self::SESSION_KEY, $this->messages); + $this->session->write(); + } + } +} -- cgit v1.2.3