| | 1 | <?php |
|---|
| | 2 | /** |
|---|
| | 3 | * @package jelix |
|---|
| | 4 | * @subpackage utils |
|---|
| | 5 | * @author Loic Mathaud |
|---|
| | 6 | * @copyright 2008 Loic Mathaud |
|---|
| | 7 | * @link http://www.jelix.org |
|---|
| | 8 | * @licence GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html |
|---|
| | 9 | */ |
|---|
| | 10 | |
|---|
| | 11 | /** |
|---|
| | 12 | * Utility class to log some message in session in order to be displayed in a template |
|---|
| | 13 | * @package jelix |
|---|
| | 14 | * @subpackage utils |
|---|
| | 15 | * @static |
|---|
| | 16 | */ |
|---|
| | 17 | class jMessage { |
|---|
| | 18 | |
|---|
| | 19 | protected static $session_name = 'JELIX_MESSAGE'; |
|---|
| | 20 | |
|---|
| | 21 | |
|---|
| | 22 | /** |
|---|
| | 23 | * Add a message |
|---|
| | 24 | * @param string $message the message |
|---|
| | 25 | * @param string $type the message type ('default' by default) |
|---|
| | 26 | */ |
|---|
| | 27 | public static function add($message, $type = 'default') { |
|---|
| | 28 | if (!isset($_SESSION[self::$session_name])) { |
|---|
| | 29 | $_SESSION[self::$session_name] = array(); |
|---|
| | 30 | } |
|---|
| | 31 | if (!isset($_SESSION[self::$session_name][$type])) { |
|---|
| | 32 | $_SESSION[self::$session_name][$type] = array(); |
|---|
| | 33 | } |
|---|
| | 34 | |
|---|
| | 35 | // Add message |
|---|
| | 36 | $_SESSION[self::$session_name][$type][] = $message; |
|---|
| | 37 | } |
|---|
| | 38 | |
|---|
| | 39 | /** |
|---|
| | 40 | * Clear messages for the given type |
|---|
| | 41 | * @param string $type the message type ('default' by default) |
|---|
| | 42 | */ |
|---|
| | 43 | public static function clear($type = 'default') { |
|---|
| | 44 | if (isset($_SESSION[self::$session_name][$type])) { |
|---|
| | 45 | $_SESSION[self::$session_name][$type] = array(); |
|---|
| | 46 | } |
|---|
| | 47 | } |
|---|
| | 48 | |
|---|
| | 49 | /** |
|---|
| | 50 | * Clear all messages |
|---|
| | 51 | */ |
|---|
| | 52 | public static function clearAll() { |
|---|
| | 53 | if (isset($_SESSION[self::$session_name])) { |
|---|
| | 54 | $_SESSION[self::$session_name] = array(); |
|---|
| | 55 | } |
|---|
| | 56 | } |
|---|
| | 57 | |
|---|
| | 58 | /** |
|---|
| | 59 | * Get messages for the given type |
|---|
| | 60 | * @param string $type the message type ('default' by default) |
|---|
| | 61 | * @return mixed array/null |
|---|
| | 62 | */ |
|---|
| | 63 | public static function get($type = 'default') { |
|---|
| | 64 | if (isset($_SESSION[self::$session_name][$type])) { |
|---|
| | 65 | return $_SESSION[self::$session_name][$type]; |
|---|
| | 66 | } |
|---|
| | 67 | |
|---|
| | 68 | return null; |
|---|
| | 69 | } |
|---|
| | 70 | |
|---|
| | 71 | /** |
|---|
| | 72 | * Get all messages |
|---|
| | 73 | */ |
|---|
| | 74 | public static function getAll() { |
|---|
| | 75 | if (isset($_SESSION[self::$session_name])) { |
|---|
| | 76 | return $_SESSION[self::$session_name]; |
|---|
| | 77 | } |
|---|
| | 78 | |
|---|
| | 79 | return null; |
|---|
| | 80 | } |
|---|
| | 81 | } |