]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/3rdparty/class.messages.php
2 //--------------------------------------------------------------------------------------------------
3 // Session-Based Flash Messages v1.0
4 // Copyright 2012 Mike Everhart (http://mikeeverhart.net)
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
10 // http://www.apache.org/licenses/LICENSE-2.0
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
18 //------------------------------------------------------------------------------
20 //------------------------------------------------------------------------------
22 // Stores messages in Session data to be easily retrieved later on.
23 // This class includes four different types of messages:
29 // See README for basic usage instructions, or see samples/index.php for more advanced samples
31 //--------------------------------------------------------------------------------------------------
33 //--------------------------------------------------------------------------------------------------
35 // 2011-05-15 - v1.0 - Initial Version
37 //--------------------------------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------------------------
45 var $msgTypes = array( 'help', 'info', 'warning', 'success', 'error' );
46 var $msgClass = 'messages';
47 var $msgWrapper = "<div class='%s %s'><a href='#' class='closeMessage'>X</a>\n%s</div>\n";
48 var $msgBefore = '<p>';
49 var $msgAfter = "</p>\n";
54 * @author Mike Everhart
56 public function __construct() {
58 // Generate a unique ID for this user and session
59 $this->msgId
= md5(uniqid());
61 // Create the session array if it doesnt already exist
62 settype($_SESSION, 'array');
63 if( !array_key_exists('flash_messages', $_SESSION) ) $_SESSION['flash_messages'] = array();
68 * Add a message to the queue
70 * @author Mike Everhart
72 * @param string $type The type of message to add
73 * @param string $message The message
74 * @param string $redirect_to (optional) If set, the user will be redirected to this URL
78 public function add($type, $message, $redirect_to=null) {
80 if( !isset($_SESSION['flash_messages']) ) return false;
82 if( !isset($type) || !isset($message[0]) ) return false;
84 // Replace any shorthand codes with their full version
85 if( strlen(trim($type)) == 1 ) {
86 $type = str_replace( array('h', 'i', 'w', 'e', 's'), array('help', 'info', 'warning', 'error', 'success'), $type );
88 // Backwards compatibility...
89 } elseif( $type == 'information' ) {
93 // Make sure it's a valid message type
94 if( !in_array($type, $this->msgTypes
) ) die('"' . strip_tags($type) . '" is not a valid message type!' );
96 // If the session array doesn't exist, create it
97 if( !array_key_exists( $type, $_SESSION['flash_messages'] ) ) $_SESSION['flash_messages'][$type] = array();
99 $_SESSION['flash_messages'][$type][] = $message;
101 if( !is_null($redirect_to) ) {
102 header("Location: $redirect_to");
110 //-----------------------------------------------------------------------------------------------
112 // print queued messages to the screen
113 //-----------------------------------------------------------------------------------------------
115 * Display the queued messages
117 * @author Mike Everhart
119 * @param string $type Which messages to display
120 * @param bool $print True = print the messages on the screen
124 public function display($type='all', $print=true) {
128 if( !isset($_SESSION['flash_messages']) ) return false;
130 if( $type == 'g' || $type == 'growl' ) {
131 $this->displayGrowlMessages();
135 // Print a certain type of message?
136 if( in_array($type, $this->msgTypes) ) {
137 foreach( $_SESSION['flash_messages'][$type] as $msg ) {
138 $messages .= $this->msgBefore . $msg . $this->msgAfter;
141 $data .= sprintf($this->msgWrapper, $this->msgClass, $type, $messages);
143 // Clear the viewed messages
146 // Print ALL queued messages
147 } elseif( $type == 'all' ) {
148 foreach( $_SESSION['flash_messages'] as $type => $msgArray ) {
150 foreach( $msgArray as $msg ) {
151 $messages .= $this->msgBefore . $msg . $this->msgAfter;
153 $data .= sprintf($this->msgWrapper, $this->msgClass, $type, $messages);
156 // Clear ALL of the messages
159 // Invalid Message Type?
164 // Print everything to the screen or return the data
174 * Check to see if there are any queued error messages
176 * @author Mike Everhart
178 * @return bool true = There ARE error messages
179 * false = There are NOT any error messages
182 public function hasErrors() {
183 return empty($_SESSION['flash_messages']['error']) ? false : true;
187 * Check to see if there are any ($type) messages queued
189 * @author Mike Everhart
191 * @param string $type The type of messages to check for
195 public function hasMessages($type=null) {
196 if( !is_null($type) ) {
197 if( !empty($_SESSION['flash_messages'][$type]) ) return $_SESSION['flash_messages'][$type];
199 foreach( $this->msgTypes as $type ) {
200 if( !empty($_SESSION['flash_messages']) ) return true;
207 * Clear messages from the session data
209 * @author Mike Everhart
211 * @param string $type The type of messages to clear
215 public function clear($type='all') {
216 if( $type == 'all' ) {
217 unset($_SESSION['flash_messages']);
219 unset($_SESSION['flash_messages'][$type]);
224 public function __toString() { return $this->hasMessages(); }
226 public function __destruct() {