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