_session_id = '_system_messages'; $this->reset()->mergeWithSession(); } /** * Cloning is not allowed * */ // private function __clone() // { // } /** * Singleton is not required, we go for factory instead * @return eMessage */ public static function getInstance() { // if(null == self::$_instance) // { // self::$_instance = new self(); // } return e107::getMessage(); } /** * Set message session id * @param string $name * @return object $this */ public function setSessionId($name = '') { $sid = $name.'_system_messages'; if($this->_session_id != $sid) { if(session_id()) { $session = $this->getSessionHandler(); $session->set($sid, $session->get($this->_session_id, true)); // move if(!$session->has($sid)) $session->set($sid, array()); // be sure it's array } $this->_session_id = $sid; } return $this; } /** * Get session handler * @return unknown_type */ public function getSessionHandler() { if(null === $this->_session_handler) { $session = e107::getSession(); if(!$session->has($this->_session_id)) $session->set($this->_session_id, array()); $this->_session_handler = $session; } return $this->_session_handler; } /** * Add message to a type stack and default message stack * If $message is array, $message[0] will be the message stack and * $message[1] the message itself * * @param string|array $message * @param string $type * @param boolean $session * @return eMessage */ public function add($message, $type = E_MESSAGE_INFO, $session = false) { if(empty($message)) return $this; $mstack = 'default'; $msg = $message; if(is_array($message)) { $mstack = $message[1]; $msg = $message[0]; } if(empty($msg)) return $this; if(!$session) { if($this->isType($type)) $this->_sysmsg[$type][$mstack][] = $msg; return $this; } $this->addSession($message, $type); return $this; } /** * Alias of {@link add()}. * Should be used for dealing with messages with custom message stacks. * Supports message arrays. * * @param string|array $message message(s) * @param string $mstack defaults to 'default' * @param string $type [optional] * @param boolean $sesion [optional] * @return eMessage */ public function addStack($message, $mstack = 'default', $type = E_MESSAGE_INFO, $session = false) { if(!is_array($message)) { $message = array($message); } foreach ($message as $m) { $this->add(array($m, $mstack), $type, $session); } return $this; } /** * Add success message * * @param string $message * @param string $mstack message stack, default value is 'default' * @param boolean $session * @return eMessage */ public function addSuccess($message, $mstack = 'default', $session = false) { return $this->addStack($message, $mstack, E_MESSAGE_SUCCESS, $session); } /** * Add error message * * @param string $message * @param string $mstack message stack, default value is 'default' * @param boolean $session * @return eMessage */ public function addError($message, $mstack = 'default', $session = false) { return $this->addStack($message, $mstack, E_MESSAGE_ERROR, $session); } /** * Add warning message * * @param string $message * @param string $mstack message stack, default value is 'default' * @param boolean $session * @return eMessage */ public function addWarning($message, $mstack = 'default', $session = false) { return $this->addStack($message, $mstack, E_MESSAGE_WARNING, $session); } /** * Add info message * * @param string $message * @param string $mstack message stack, default value is 'default' * @param boolean $session * @return eMessage */ public function addInfo($message, $mstack = 'default', $session = false) { return $this->addStack($message, $mstack, E_MESSAGE_INFO, $session); } /** * Add debug message * * @param string $message * @param string $mstack message stack, default value is 'default' * @param boolean $session * @return eMessage */ public function addDebug($message, $mstack = 'default', $session = false) { return $this->addStack($message, $mstack, E_MESSAGE_DEBUG, $session); } /** * Add message to a _SESSION type stack * If $message is array, $message[0] will be the message stack and * $message[1] the message itself * * @param string|array $message * @param string $type * @return eMessage */ public function addSession($message, $type = E_MESSAGE_INFO) { if(empty($message) || !session_id()) return $this; $mstack = 'default'; if(is_array($message)) { $mstack = $message[1]; $message = $message[0]; } $SESSION = $this->getSessionHandler()->get($this->_session_id); if($this->isType($type)) { $SESSION[$type][$mstack][] = $message; $this->getSessionHandler()->set($this->_session_id, $SESSION); } return $this; } /** * Alias of {@link addSession()}. * Should be used for dealing with messages with custom message stacks. * Supports message arrays. * * @param string|array $message message(s) * @param string $mstack defaults to 'default' * @param string $type [optional] * @param boolean $sesion [optional] * @return eMessage */ public function addSessionStack($message, $mstack = 'default', $type = E_MESSAGE_INFO) { if(!is_array($message)) { $message = array($message); } foreach ($message as $m) { $this->addSession(array($m, $mstack), $type); } return $this; } /** * Get type title (multi-language) * * @param string $type * @param string $message_stack * @return string title */ public static function getTitle($type, $message_stack = 'default') { if($message_stack && $message_stack != 'default' && defined('EMESSLAN_TITLE_'.strtoupper($type.'_'.$message_stack))) { return constant('EMESSLAN_TITLE_'.strtoupper($type.'_'.$message_stack)); } return defsettrue('EMESSLAN_TITLE_'.strtoupper($type), ''); } /** * Message getter * * @param string $type valid type * @param string $mstack message stack name * @param bool $raw force array return * @param bool $reset reset message type stack * @return string|array message */ public function get($type, $mstack = 'default', $raw = false, $reset = true) { $message = isset($this->_sysmsg[$type][$mstack]) ? $this->_sysmsg[$type][$mstack] : ''; if($reset) $this->reset($type, $mstack, false); return (true === $raw ? $message : self::formatMessage($mstack, $type, $message)); } /** * Get all messages for a stack * * @param string $mstack message stack name * @param bool $raw force array return * @param bool $reset reset message type stack * @return array messages */ public function getAll($mstack = 'default', $raw = false, $reset = true) { $ret = array(); foreach ($this->_get_types() as $type) { $message = $this->get($type, $mstack, $raw, $reset); if(!empty($message)) { $ret[$type] = $message; } } return $ret; } /** * Session message getter * * @param string $type valid type * @param string $mstack message stack * @param bool $raw force array return * @param bool $reset reset session message type stack * @return string|array session message */ public function getSession($type, $mstack = 'default', $raw = false, $reset = true) { if(!session_id()) return null; $SESSION = $this->getSessionHandler()->get($this->_session_id); $message = isset($SESSION[$type][$mstack]) ? $SESSION[$type][$mstack] : ''; if($reset) $this->resetSession($type, $mstack); return (true === $raw ? $message : self::formatMessage($mstack, $type, $message)); } /** * Get all session messages for a stack * * @param string $mstack message stack name * @param bool $raw force array return * @param bool $reset reset message type stack * @return array session messages */ public function getAllSession($mstack = 'default', $raw = false, $reset = true) { if(!session_id()) return array(); $ret = array(); foreach ($this->_get_types() as $type) { $message = $this->getSession($type, $mstack, $raw, $reset); if(!empty($message)) { $ret[$type] = $message; } } return $ret; } /** * Output all accumulated messages * * @param string $mstack message stack name * @param bool $session merge with session messages * @param bool $reset reset all messages * @param bool $raw force return type array * @return array|string messages */ public function render($mstack = 'default', $session = false, $reset = true, $raw = false) { if($session) { $this->mergeWithSession(true, $mstack); } $ret = array(); foreach ($this->_get_types() as $type) { if(E_MESSAGE_DEBUG === $type && !deftrue('E107_DEBUG_LEVEL')) { continue; } $message = $this->get($type, $mstack, $raw); if(!empty($message)) { $ret[$type] = $message; } } if($reset) $this->reset(false, $mstack); if(true === $raw || empty($ret)) return ($raw ? $ret : ''); //changed to class return "
"; } /** * Create message block markup based on its type. * * @param string $mstack * @param string $type * @param array|string $message * @return string */ public static function formatMessage($mstack, $type, $message) { if (empty($message)) return ''; elseif (is_array($message)) { $message = " \n "; } return "
* e107::getMessage()->success('Success', false);
* //calls internal $this->addStack('Success', E_MESSAGE_SUCCESS, false);
*
* @param string $method valid message type
* @param array $arguments array(0 => (string) message, [optional] 1 =>(boolean) session, [optional] 2=> message stack )
* @return eMessage
* @throws Exception
*/
function __call($method, $arguments) {
if($this->isType($method))
{
$this->addStack($arguments[0], vartrue($arguments[2], 'default'), $method, (isset($arguments[1]) && !empty($arguments[1])));
return $this;
}
throw new Exception('Method eMessage::'.$method.' does not exist!');//FIXME - e107Exception handler
}
/**
* Automate DB system messages
* NOTE: default value of $output parameter will be changed to false (no output by default) in the future
*
* @param integer|bool $update return result of db::db_Query
* @param string $type update|insert|update
* @param string $success forced success message
* @param string $failed forced error message
* @param bool $output false suppress any function output
* @return integer|bool db::db_Query result
*/
// TODO - This function often needs to be available BEFORE header.php is loaded.
// It has been copied from admin_update() in e107_admin/header.php
public function autoMessage($update, $type = 'update', $success = false, $failed = false, $output = false)
{
if (($type == 'update' && $update) || ($type == 'insert' && $update !== false))
{
$this->add(($success ? $success : ($type == 'update' ? LAN_UPDATED : LAN_CREATED)), E_MESSAGE_SUCCESS);
}
elseif ($type == 'delete' && $update)
{
$this->add(($success ? $success : LAN_DELETED), E_MESSAGE_SUCCESS);
}
elseif (!mysql_errno())
{
if ($type == 'update')
{
$this->add(LAN_NO_CHANGE.' '.LAN_TRY_AGAIN, E_MESSAGE_INFO);
}
elseif ($type == 'delete')
{
$this->add(LAN_DELETED_FAILED.' '.LAN_TRY_AGAIN, E_MESSAGE_INFO);
}
}
else
{
switch ($type)
{
case 'insert':
$msg = LAN_CREATED_FAILED;
break;
case 'delete':
$msg = LAN_DELETED_FAILED;
break;
default:
$msg = LAN_UPDATED_FAILED;
break;
}
$text = ($failed ? $failed : $msg." - ".LAN_TRY_AGAIN)."--- Message --- ".$message." |