2013-06-07 11:44:37 +02:00
|
|
|
<?php
|
2013-06-10 12:37:27 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
2013-07-22 16:03:36 +02:00
|
|
|
/**
|
2013-10-23 15:10:33 +02:00
|
|
|
* This file is part of Icinga Web 2.
|
|
|
|
*
|
|
|
|
* Icinga Web 2 - Head for multiple monitoring backends.
|
2013-07-22 16:03:36 +02:00
|
|
|
* Copyright (C) 2013 Icinga Development Team
|
2013-09-04 18:27:16 +02:00
|
|
|
*
|
2013-07-22 16:03:36 +02:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2013-09-04 18:27:16 +02:00
|
|
|
*
|
2013-07-22 16:03:36 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2013-09-04 18:27:16 +02:00
|
|
|
*
|
2013-07-22 16:03:36 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2013-09-04 18:27:16 +02:00
|
|
|
*
|
2013-10-23 15:10:33 +02:00
|
|
|
* @copyright 2013 Icinga Development Team <info@icinga.org>
|
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
|
|
|
|
* @author Icinga Development Team <info@icinga.org>
|
|
|
|
*
|
2013-07-22 16:03:36 +02:00
|
|
|
*/
|
2013-06-10 12:37:27 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
2013-06-07 11:44:37 +02:00
|
|
|
|
|
|
|
namespace Icinga\Web;
|
|
|
|
|
|
|
|
use Icinga\Exception\ProgrammingError;
|
|
|
|
use Icinga\Application\Platform;
|
2014-02-26 11:19:52 +01:00
|
|
|
use Icinga\Logger\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
|
|
|
|
{
|
2013-07-12 11:53:05 +02:00
|
|
|
protected static $instance;
|
|
|
|
protected $isCli = false;
|
2013-06-10 12:37:27 +02:00
|
|
|
|
2013-06-07 11:44:37 +02:00
|
|
|
public static function info($msg)
|
|
|
|
{
|
|
|
|
self::getInstance()->addMessage($msg, 'info');
|
|
|
|
}
|
2013-06-10 12:37:27 +02:00
|
|
|
|
2013-06-07 11:44:37 +02:00
|
|
|
public static function success($msg)
|
|
|
|
{
|
|
|
|
self::getInstance()->addMessage($msg, 'success');
|
|
|
|
}
|
2013-06-10 12:37:27 +02:00
|
|
|
|
2013-06-07 11:44:37 +02:00
|
|
|
public static function warning($msg)
|
|
|
|
{
|
|
|
|
self::getInstance()->addMessage($msg, 'warning');
|
|
|
|
}
|
2013-06-10 12:37:27 +02:00
|
|
|
|
2013-06-07 11:44:37 +02:00
|
|
|
public static function error($msg)
|
|
|
|
{
|
|
|
|
self::getInstance()->addMessage($msg, 'error');
|
|
|
|
}
|
|
|
|
|
2013-07-12 11:53:05 +02:00
|
|
|
protected function addMessage($message, $type = 'info')
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
2013-07-12 11:53:05 +02:00
|
|
|
if (! in_array(
|
2013-06-07 11:44:37 +02:00
|
|
|
$type,
|
|
|
|
array(
|
|
|
|
'info',
|
|
|
|
'error',
|
|
|
|
'warning',
|
|
|
|
'success'
|
|
|
|
)
|
2013-07-12 11:53:05 +02:00
|
|
|
)) {
|
2013-06-07 11:44:37 +02:00
|
|
|
throw new ProgrammingError(
|
|
|
|
sprintf(
|
|
|
|
'"%s" is not a valid notification type',
|
|
|
|
$type
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
case 'info':
|
|
|
|
case 'success':
|
2014-02-26 11:19:52 +01:00
|
|
|
Logger::info($msg);
|
2013-06-07 11:44:37 +02:00
|
|
|
break;
|
|
|
|
case 'warning':
|
2014-02-26 11:19:52 +01:00
|
|
|
Logger::warn($msg);
|
2013-06-07 11:44:37 +02:00
|
|
|
break;
|
|
|
|
case 'error':
|
2014-02-26 11:19:52 +01:00
|
|
|
Logger::error($msg);
|
2013-06-07 11:44:37 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-12 11:53:05 +02:00
|
|
|
$mo = (object) array(
|
|
|
|
'type' => $type,
|
2013-06-07 11:44:37 +02:00
|
|
|
'message' => $message,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Get, change, set - just to be on the safe side:
|
2014-01-23 12:09:48 +01:00
|
|
|
$session = Session::getSession();
|
|
|
|
$msgs = $session->messages;
|
2013-06-07 11:44:37 +02:00
|
|
|
$msgs[] = $mo;
|
2014-01-23 12:09:48 +01:00
|
|
|
$session->messages = $msgs;
|
2014-05-26 16:41:47 +02:00
|
|
|
$session->write();
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function hasMessages()
|
|
|
|
{
|
2014-01-23 12:09:48 +01:00
|
|
|
$session = Session::getSession();
|
|
|
|
return !empty($session->messages);
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getMessages()
|
|
|
|
{
|
2014-01-23 12:09:48 +01:00
|
|
|
$session = Session::getSession();
|
|
|
|
$msgs = $session->messages;
|
|
|
|
$session->messages = array();
|
2014-05-26 16:41:47 +02:00
|
|
|
$session->write();
|
2013-06-07 11:44:37 +02:00
|
|
|
return $msgs;
|
|
|
|
}
|
|
|
|
|
|
|
|
final private function __construct()
|
|
|
|
{
|
2014-01-23 12:09:48 +01:00
|
|
|
$session = Session::getSession();
|
|
|
|
if (!is_array($session->get('messages'))) {
|
|
|
|
$session->messages = array();
|
2013-07-12 11:53:05 +02:00
|
|
|
}
|
2013-06-07 11:44:37 +02:00
|
|
|
|
|
|
|
if (Platform::isCli()) {
|
2013-07-12 11:53:05 +02:00
|
|
|
$this->is_cli = true;
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getInstance()
|
|
|
|
{
|
|
|
|
if (self::$instance === null) {
|
|
|
|
self::$instance = new Notification();
|
|
|
|
}
|
|
|
|
return self::$instance;
|
|
|
|
}
|
|
|
|
}
|