<?php
/*
* Messages
*
* Copyright(c) 2010, Thomas Shone
* Licensed under the Creative Commons Attribution 3.0 Unported License.
* http://www.shone.co.za
*
* User warning messages
* To be used in a later version of the system
*/
define('ERROR', 'Error');
define('ERROR_INLINE', 'ErrorInline');
define('WARNING', 'Warning');
define('NOTICE', 'Notice');
class Messages
{
protected static $aOldMessages;
protected static $aNewMessages;
public static function HasErrors()
{
return !empty(self::$aNewMessages);
}
public static function AddError($sError)
{
self::$aNewMessages[ERROR][] = $sError;
}
public static function AddInlineError($sKey, $sError)
{
self::$aNewMessages[ERROR_INLINE][$sKey][] = $sError;
}
public static function AddWarning($sError)
{
self::$aNewMessages[WARNING][] = $sError;
}
public static function AddNotice($sError)
{
self::$aNewMessages[NOTICE][] = $sError;
}
public static function GetInlineErrors($sKey)
{
return (
!empty(self::$aOldMessages[ERROR_INLINE][$sKey]) ?
self::$aOldMessages[ERROR_INLINE][$sKey] :
''
);
}
public static function GetMessages()
{
// If we don't have any messages, skip the intersect
if (empty(self::$aOldMessages))
{
return array();
}
// Grab all the messages set
$aTypes = array(
ERROR => true,
WARNING => true,
NOTICE => true
);
return array_intersect_key(self::$aOldMessages, $aTypes);
}
public static function LoadFromSession()
{
$aTypes = array(ERROR, WARNING, NOTICE, ERROR_INLINE);
foreach ($aTypes as $sType)
{
// Grab messages from session
$aMessages = Session::Get('Messages.' . $sType);
if (!empty($aMessages))
{
self::$aOldMessages[$sType] = $aMessages;
// Clear out session entry
Session::Clear('Messages.' . $sType);
}
}
}
public static function StoreInSession()
{
// Stash messages in the session
if (!empty(self::$aNewMessages))
{
foreach (self::$aNewMessages as $sType => $aMessages)
{
Session::Set('Messages.' . $sType, $aMessages);
}
}
}
}
?>