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;
|
|
|
|
use Icinga\Application\Logger as Log;
|
2013-07-12 11:53:05 +02:00
|
|
|
use Icinga\Authentication\Manager as AuthManager;
|
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
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-07-12 11:53:05 +02:00
|
|
|
if ($this->is_cli) {
|
2013-06-07 11:44:37 +02:00
|
|
|
$msg = sprintf('[%s] %s', $type, $message);
|
|
|
|
switch ($type) {
|
|
|
|
case 'info':
|
|
|
|
case 'success':
|
|
|
|
Log::info($msg);
|
|
|
|
break;
|
|
|
|
case 'warning':
|
|
|
|
Log::warn($msg);
|
|
|
|
break;
|
|
|
|
case 'error':
|
|
|
|
Log::error($msg);
|
|
|
|
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:
|
|
|
|
$msgs = $this->session->messages;
|
|
|
|
$msgs[] = $mo;
|
|
|
|
$this->session->messages = $msgs;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function hasMessages()
|
|
|
|
{
|
2013-07-12 11:53:05 +02:00
|
|
|
return ! empty($this->session->messages);
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getMessages()
|
|
|
|
{
|
|
|
|
$msgs = $this->session->messages;
|
|
|
|
$this->session->messages = array();
|
|
|
|
return $msgs;
|
|
|
|
}
|
|
|
|
|
|
|
|
final private function __construct()
|
|
|
|
{
|
2013-07-12 11:53:05 +02:00
|
|
|
$this->session = AuthManager::getInstance()->getSession();
|
|
|
|
if (!is_array($this->session->get('messages'))) {
|
2013-06-07 11:44:37 +02:00
|
|
|
$this->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;
|
|
|
|
}
|
|
|
|
}
|