]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - class.messages.php
e60bd3a19c3e1db9e914ed3c10a7e3e1a2e74d4a
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 if( !array_key_exists('flash_messages', $_SESSION) ) $_SESSION['flash_messages'] = array();
67 * Add a message to the queue
69 * @author Mike Everhart
71 * @param string $type The type of message to add
72 * @param string $message The message
73 * @param string $redirect_to (optional) If set, the user will be redirected to this URL
77 public function add($type, $message, $redirect_to=null) {
79 if( !isset($_SESSION['flash_messages']) ) return false;
81 if( !isset($type) || !isset($message[0]) ) return false;
83 // Replace any shorthand codes with their full version
84 if( strlen(trim($type)) == 1 ) {
85 $type = str_replace( array('h', 'i', 'w', 'e', 's'), array('help', 'info', 'warning', 'error', 'success'), $type );
87 // Backwards compatibility...
88 } elseif( $type == 'information' ) {
92 // Make sure it's a valid message type
93 if( !in_array($type, $this->msgTypes
) ) die('"' . strip_tags($type) . '" is not a valid message type!' );
95 // If the session array doesn't exist, create it
96 if( !array_key_exists( $type, $_SESSION['flash_messages'] ) ) $_SESSION['flash_messages'][$type] = array();
98 $_SESSION['flash_messages'][$type][] = $message;
100 if( !is_null($redirect_to) ) {
101 header("Location: $redirect_to");
109 //-----------------------------------------------------------------------------------------------
111 // print queued messages to the screen
112 //-----------------------------------------------------------------------------------------------
114 * Display the queued messages
116 * @author Mike Everhart
118 * @param string $type Which messages to display
119 * @param bool $print True = print the messages on the screen
123 public function display($type='all', $print=true) {
127 if( !isset($_SESSION['flash_messages']) ) return false;
129 if( $type == 'g' || $type == 'growl' ) {
130 $this->displayGrowlMessages();
134 // Print a certain type of message?
135 if( in_array($type, $this->msgTypes) ) {
136 foreach( $_SESSION['flash_messages'][$type] as $msg ) {
137 $messages .= $this->msgBefore . $msg . $this->msgAfter;
140 $data .= sprintf($this->msgWrapper, $this->msgClass, $type, $messages);
142 // Clear the viewed messages
145 // Print ALL queued messages
146 } elseif( $type == 'all' ) {
147 foreach( $_SESSION['flash_messages'] as $type => $msgArray ) {
149 foreach( $msgArray as $msg ) {
150 $messages .= $this->msgBefore . $msg . $this->msgAfter;
152 $data .= sprintf($this->msgWrapper, $this->msgClass, $type, $messages);
155 // Clear ALL of the messages
158 // Invalid Message Type?
163 // Print everything to the screen or return the data
173 * Check to see if there are any queued error messages
175 * @author Mike Everhart
177 * @return bool true = There ARE error messages
178 * false = There are NOT any error messages
181 public function hasErrors() {
182 return empty($_SESSION['flash_messages']['error']) ? false : true;
186 * Check to see if there are any ($type) messages queued
188 * @author Mike Everhart
190 * @param string $type The type of messages to check for
194 public function hasMessages($type=null) {
195 if( !is_null($type) ) {
196 if( !empty($_SESSION['flash_messages'][$type]) ) return $_SESSION['flash_messages'][$type];
198 foreach( $this->msgTypes as $type ) {
199 if( !empty($_SESSION['flash_messages']) ) return true;
206 * Clear messages from the session data
208 * @author Mike Everhart
210 * @param string $type The type of messages to clear
214 public function clear($type='all') {
215 if( $type == 'all' ) {
216 unset($_SESSION['flash_messages']);
218 unset($_SESSION['flash_messages'][$type]);
223 public function __toString() { return $this->hasMessages(); }
225 public function __destruct() {