2013-06-07 05:44:37 -04:00
|
|
|
<?php
|
2016-02-08 09:41:00 -05:00
|
|
|
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */
|
2013-06-07 05:44:37 -04:00
|
|
|
|
|
|
|
|
namespace Icinga\Web;
|
|
|
|
|
|
|
|
|
|
use Icinga\Exception\ProgrammingError;
|
|
|
|
|
use Icinga\Application\Platform;
|
2014-10-31 05:27:17 -04:00
|
|
|
use Icinga\Application\Logger;
|
2014-01-23 06:09:48 -05:00
|
|
|
use Icinga\Web\Session;
|
2013-06-07 05:44:37 -04:00
|
|
|
|
2013-06-10 06:37:27 -04:00
|
|
|
/**
|
2013-07-12 05:53:05 -04:00
|
|
|
* // @TODO(eL): Use Notification not as Singleton but within request:
|
|
|
|
|
* <code>
|
|
|
|
|
* <?php
|
|
|
|
|
* $request->[getUser()]->notify('some message', Notification::INFO);
|
|
|
|
|
* </code>
|
2013-06-10 06:37:27 -04:00
|
|
|
*/
|
2013-06-07 05:44:37 -04:00
|
|
|
class Notification
|
|
|
|
|
{
|
2015-07-30 07:42:59 -04:00
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
2013-07-12 05:53:05 -04:00
|
|
|
protected static $instance;
|
2015-03-13 08:49:45 -04:00
|
|
|
|
2015-07-30 07:42:59 -04:00
|
|
|
/**
|
|
|
|
|
* Whether the platform is CLI
|
|
|
|
|
*
|
|
|
|
|
* @var bool
|
|
|
|
|
*/
|
2013-07-12 05:53:05 -04:00
|
|
|
protected $isCli = false;
|
2013-06-10 06:37:27 -04:00
|
|
|
|
2015-07-30 07:42:59 -04:00
|
|
|
/**
|
|
|
|
|
* Notification messages
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2015-03-13 08:49:45 -04:00
|
|
|
protected $messages = array();
|
|
|
|
|
|
2015-07-30 07:42:59 -04:00
|
|
|
/**
|
|
|
|
|
* Session
|
|
|
|
|
*
|
|
|
|
|
* @var Session
|
|
|
|
|
*/
|
|
|
|
|
protected $session;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create the notification instance
|
|
|
|
|
*/
|
|
|
|
|
final private function __construct()
|
2013-06-07 05:44:37 -04:00
|
|
|
{
|
2015-07-30 07:42:59 -04:00
|
|
|
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();
|
|
|
|
|
}
|
2013-06-07 05:44:37 -04:00
|
|
|
}
|
2013-06-10 06:37:27 -04:00
|
|
|
|
2015-07-30 07:42:59 -04:00
|
|
|
/**
|
|
|
|
|
* Get the Notification instance
|
|
|
|
|
*
|
|
|
|
|
* @return Notification
|
|
|
|
|
*/
|
|
|
|
|
public static function getInstance()
|
2013-06-07 05:44:37 -04:00
|
|
|
{
|
2015-07-30 07:42:59 -04:00
|
|
|
if (self::$instance === null) {
|
|
|
|
|
self::$instance = new self();
|
|
|
|
|
}
|
|
|
|
|
return self::$instance;
|
2013-06-07 05:44:37 -04:00
|
|
|
}
|
2013-06-10 06:37:27 -04:00
|
|
|
|
2015-07-30 07:42:59 -04:00
|
|
|
/**
|
|
|
|
|
* Add info notification
|
|
|
|
|
*
|
|
|
|
|
* @param string $msg
|
|
|
|
|
*/
|
|
|
|
|
public static function info($msg)
|
2013-06-07 05:44:37 -04:00
|
|
|
{
|
2015-07-30 07:42:59 -04:00
|
|
|
self::getInstance()->addMessage($msg, self::INFO);
|
2013-06-07 05:44:37 -04:00
|
|
|
}
|
2013-06-10 06:37:27 -04:00
|
|
|
|
2015-07-30 07:42:59 -04:00
|
|
|
/**
|
|
|
|
|
* Add error notification
|
|
|
|
|
*
|
|
|
|
|
* @param string $msg
|
|
|
|
|
*/
|
2013-06-07 05:44:37 -04:00
|
|
|
public static function error($msg)
|
|
|
|
|
{
|
2015-07-30 07:42:59 -04:00
|
|
|
self::getInstance()->addMessage($msg, self::ERROR);
|
2013-06-07 05:44:37 -04:00
|
|
|
}
|
|
|
|
|
|
2015-07-30 07:42:59 -04:00
|
|
|
/**
|
|
|
|
|
* Add success notification
|
|
|
|
|
*
|
|
|
|
|
* @param string $msg
|
|
|
|
|
*/
|
|
|
|
|
public static function success($msg)
|
2013-06-07 05:44:37 -04:00
|
|
|
{
|
2015-07-30 07:42:59 -04:00
|
|
|
self::getInstance()->addMessage($msg, self::SUCCESS);
|
|
|
|
|
}
|
2013-06-07 05:44:37 -04:00
|
|
|
|
2015-07-30 07:42:59 -04:00
|
|
|
/**
|
|
|
|
|
* 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)
|
|
|
|
|
{
|
2014-03-07 18:15:16 -05:00
|
|
|
if ($this->isCli) {
|
2013-06-07 05:44:37 -04:00
|
|
|
$msg = sprintf('[%s] %s', $type, $message);
|
|
|
|
|
switch ($type) {
|
2015-07-30 07:42:59 -04:00
|
|
|
case self::INFO:
|
|
|
|
|
case self::SUCCESS:
|
2014-02-26 05:19:52 -05:00
|
|
|
Logger::info($msg);
|
2013-06-07 05:44:37 -04:00
|
|
|
break;
|
2015-07-30 07:42:59 -04:00
|
|
|
case self::ERROR:
|
2014-02-26 05:19:52 -05:00
|
|
|
Logger::error($msg);
|
2013-06-07 05:44:37 -04:00
|
|
|
break;
|
2015-07-30 07:42:59 -04:00
|
|
|
case self::WARNING:
|
|
|
|
|
Logger::warning($msg);
|
|
|
|
|
break;
|
2013-06-07 05:44:37 -04:00
|
|
|
}
|
2015-07-30 07:42:59 -04:00
|
|
|
} else {
|
|
|
|
|
$this->messages[] = (object) array(
|
|
|
|
|
'type' => $type,
|
|
|
|
|
'message' => $message,
|
|
|
|
|
);
|
2013-06-07 05:44:37 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-30 07:42:59 -04:00
|
|
|
/**
|
2015-07-30 07:45:39 -04:00
|
|
|
* Pop the notification messages
|
2015-07-30 07:42:59 -04:00
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2015-07-30 07:45:39 -04:00
|
|
|
public function popMessages()
|
2013-06-07 05:44:37 -04:00
|
|
|
{
|
2015-03-13 08:49:45 -04:00
|
|
|
$messages = $this->messages;
|
|
|
|
|
$this->messages = array();
|
2014-10-15 07:14:51 -04:00
|
|
|
return $messages;
|
2013-06-07 05:44:37 -04:00
|
|
|
}
|
|
|
|
|
|
2015-07-30 07:42:59 -04:00
|
|
|
/**
|
|
|
|
|
* Get whether notification messages have been added
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function hasMessages()
|
2013-06-07 05:44:37 -04:00
|
|
|
{
|
2015-07-30 07:42:59 -04:00
|
|
|
return ! empty($this->messages);
|
2013-06-07 05:44:37 -04:00
|
|
|
}
|
2015-03-13 08:49:45 -04:00
|
|
|
|
2015-07-30 07:42:59 -04:00
|
|
|
/**
|
|
|
|
|
* Destroy the notification instance
|
|
|
|
|
*/
|
2015-03-13 08:49:45 -04:00
|
|
|
final public function __destruct()
|
|
|
|
|
{
|
|
|
|
|
if ($this->isCli) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-07-30 07:42:59 -04:00
|
|
|
if ($this->hasMessages() && $this->session->get('messages') !== $this->messages) {
|
|
|
|
|
$this->session->set(self::SESSION_KEY, $this->messages);
|
|
|
|
|
$this->session->write();
|
2015-03-13 08:49:45 -04:00
|
|
|
}
|
|
|
|
|
}
|
2013-06-07 05:44:37 -04:00
|
|
|
}
|