Revize c1bdc1c4
Přidáno uživatelem Michal Kliment před více než 9 roky(ů)
application/helpers/status.php | ||
---|---|---|
|
||
/**
|
||
* Helper for creating queue of status flash messages.
|
||
* Messages are stored into session var.
|
||
* Messages are stored into session var or in memory.
|
||
*
|
||
* @author Ondřej Fibich
|
||
* @package Helper
|
||
... | ... | |
const TYPE_INFO = 3;
|
||
/** Session var name */
|
||
const SESSION_VAR_NAME = 'status_message';
|
||
/** Non-session storage */
|
||
private static $messages_in_mem = array();
|
||
|
||
/**
|
||
* CSS classes for message types
|
||
... | ... | |
self::TYPE_ERROR => 'status_message_error',
|
||
self::TYPE_INFO => 'status_message_info',
|
||
);
|
||
|
||
/**
|
||
* Adds info message to queue.
|
||
*
|
||
* @param string $message Info message
|
||
* @param string $translate Enable auto-translation of message
|
||
* @param array $args Arguments of message
|
||
*/
|
||
public static function info($message, $translate = TRUE, $args = array())
|
||
{
|
||
self::_add_message(TRUE, self::TYPE_INFO, $message, $translate, $args);
|
||
}
|
||
|
||
/**
|
||
* Adds success message to queue.
|
||
... | ... | |
*/
|
||
public static function success($message, $translate = TRUE, $args = array())
|
||
{
|
||
self::_add_message(self::TYPE_SUCCESS, $message, $translate, $args);
|
||
self::_add_message(TRUE, self::TYPE_SUCCESS, $message, $translate, $args);
|
||
}
|
||
|
||
/**
|
||
... | ... | |
*/
|
||
public static function warning($message, $translate = TRUE, $args = array())
|
||
{
|
||
self::_add_message(self::TYPE_WARNING, $message, $translate, $args);
|
||
self::_add_message(TRUE, self::TYPE_WARNING, $message, $translate, $args);
|
||
}
|
||
|
||
/**
|
||
* Adds error message to queue.
|
||
*
|
||
* @param string $message Info message
|
||
* @param Exception $exception Exception that lead to this error
|
||
* @param string $translate Enable auto-translation of message
|
||
* @param array $args Arguments of message
|
||
*/
|
||
public static function error($message, $exception = NULL,
|
||
$translate = TRUE, $args = array())
|
||
{
|
||
self::_add_message(TRUE, self::TYPE_ERROR, $message, $translate, $args, $exception);
|
||
}
|
||
|
||
/**
|
||
* Adds success message to memory queue.
|
||
*
|
||
* @param string $message Info message
|
||
* @param string $translate Enable auto-translation of message
|
||
* @param array $args Arguments of message
|
||
*/
|
||
public static function msuccess($message, $translate = TRUE, $args = array())
|
||
{
|
||
self::_add_message(FALSE, self::TYPE_SUCCESS, $message, $translate, $args);
|
||
}
|
||
|
||
/**
|
||
* Adds warning message to memory queue.
|
||
*
|
||
* @param string $message Info message
|
||
* @param string $translate Enable auto-translation of message
|
||
* @param array $args Arguments of message
|
||
*/
|
||
public static function error($message, $translate = TRUE, $args = array())
|
||
public static function mwarning($message, $translate = TRUE, $args = array())
|
||
{
|
||
self::_add_message(self::TYPE_ERROR, $message, $translate, $args);
|
||
self::_add_message(FALSE, self::TYPE_WARNING, $message, $translate, $args);
|
||
}
|
||
|
||
/**
|
||
* Adds error message to memory queue.
|
||
*
|
||
* @param string $message Info message
|
||
* @param Exception $exception Exception that lead to this error
|
||
* @param string $translate Enable auto-translation of message
|
||
* @param array $args Arguments of message
|
||
*/
|
||
public static function merror($message, $exception = NULL,
|
||
$translate = TRUE, $args = array())
|
||
{
|
||
self::_add_message(FALSE, self::TYPE_ERROR, $message, $translate, $args, $exception);
|
||
}
|
||
|
||
/**
|
||
... | ... | |
* @param string $translate Enable auto-translation of message
|
||
* @param array $args Arguments of message
|
||
*/
|
||
public static function info($message, $translate = TRUE, $args = array())
|
||
public static function minfo($message, $translate = TRUE, $args = array())
|
||
{
|
||
self::_add_message(self::TYPE_INFO, $message, $translate, $args);
|
||
self::_add_message(FALSE, self::TYPE_INFO, $message, $translate, $args);
|
||
}
|
||
|
||
/**
|
||
... | ... | |
*/
|
||
public static function render()
|
||
{
|
||
$messages = self::_get_once();
|
||
// group memory and session messages
|
||
$all_messages = array(self::_get_once(), self::$messages_in_mem);
|
||
$rendered_messages = '';
|
||
|
||
foreach ($messages as $type => $message)
|
||
// render all (memory and session)
|
||
foreach ($all_messages as $messages)
|
||
{
|
||
$class = @self::$css_classes[$type];
|
||
$rendered_messages .= "<div class=\"status-message $class\">$message</div>";
|
||
foreach ($messages as $message)
|
||
{
|
||
foreach ($message as $type => $content)
|
||
{
|
||
$class = @self::$css_classes[$type];
|
||
$text = $content['message'];
|
||
|
||
if (!empty($content['exception']) &&
|
||
$content['exception'] instanceof Exception)
|
||
{
|
||
$e = $content['exception'];
|
||
$text .= "<div class=\"status-message-exception\">"
|
||
. "<a href=\"#\" onclick=\"status_exception_expander(this)\">"
|
||
. __('Show details') . "</a>"
|
||
. "<div class=\"status-message-exception-body\"><b>"
|
||
. $e->getMessage() . " at " . $e->getFile() . ":"
|
||
. $e->getLine() . " (" . $e->getCode() . ")"
|
||
. "</b><br /><em style=\"font-weight: normal\">"
|
||
. nl2br($e->getTraceAsString())
|
||
. "</em></div></div>";
|
||
}
|
||
|
||
$rendered_messages .= "<div class=\"status-message $class\">$text</div>";
|
||
}
|
||
}
|
||
}
|
||
|
||
// clean mem
|
||
self::$messages_in_mem = array();
|
||
|
||
return $rendered_messages;
|
||
}
|
||
|
||
/**
|
||
* Adds message to session var in queue order
|
||
*
|
||
* @param boolean $session Store to session?
|
||
* @param integer $type Type of message (one of type constants)
|
||
* @param string $message Message to strore
|
||
* @param string $translate Enable auto-translation of message
|
||
* @param array $args Arguments of message
|
||
* @param Exception $exception Exception related to message
|
||
*/
|
||
private static function _add_message(
|
||
$type, $message, $translate = TRUE, $args = array())
|
||
$session, $type, $message, $translate = TRUE, $args = array(),
|
||
$exception = NULL)
|
||
{
|
||
if (!empty($message))
|
||
{
|
||
// translate if enabled
|
||
$message = ($translate) ? __($message, $args) : $message;
|
||
// merge old messages with new
|
||
$messages = self::_get_once() + array
|
||
(
|
||
$type => $message
|
||
);
|
||
// set message
|
||
Session::instance()->set_flash(self::SESSION_VAR_NAME, $messages);
|
||
$message = ($translate) ? __($message, $args) : $message;
|
||
// store
|
||
if ($session) // session
|
||
{
|
||
// merge old messages with new
|
||
$messages = self::_get_once();
|
||
$messages[] = array
|
||
(
|
||
$type => array
|
||
(
|
||
'message' => $message,
|
||
'exception' => $exception
|
||
)
|
||
);
|
||
// set message
|
||
Session::instance()->set_flash(self::SESSION_VAR_NAME, $messages);
|
||
}
|
||
else // memory
|
||
{
|
||
self::$messages_in_mem[] = array
|
||
(
|
||
$type => array
|
||
(
|
||
'message' => $message,
|
||
'exception' => $exception
|
||
)
|
||
);
|
||
}
|
||
}
|
||
}
|
||
|
Také k dispozici: Unified diff
Release 1.1.0