]> git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/3rdparty/class.messages.php
Merge pull request #595 from wallabag/dev
[github/wallabag/wallabag.git] / inc / 3rdparty / class.messages.php
1 <?php
2 //--------------------------------------------------------------------------------------------------
3 // Session-Based Flash Messages v1.0
4 // Copyright 2012 Mike Everhart (http://mikeeverhart.net)
5 //
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
9 //
10 // http://www.apache.org/licenses/LICENSE-2.0
11 //
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.
17 //
18 //------------------------------------------------------------------------------
19 // Description:
20 //------------------------------------------------------------------------------
21 //
22 // Stores messages in Session data to be easily retrieved later on.
23 // This class includes four different types of messages:
24 // - Success
25 // - Error
26 // - Warning
27 // - Information
28 //
29 // See README for basic usage instructions, or see samples/index.php for more advanced samples
30 //
31 //--------------------------------------------------------------------------------------------------
32 // Changelog
33 //--------------------------------------------------------------------------------------------------
34 //
35 // 2011-05-15 - v1.0 - Initial Version
36 //
37 //--------------------------------------------------------------------------------------------------
38
39 class Messages {
40
41 //-----------------------------------------------------------------------------------------------
42 // Class Variables
43 //-----------------------------------------------------------------------------------------------
44 var $msgId;
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";
50
51
52 /**
53 * Constructor
54 * @author Mike Everhart
55 */
56 public function __construct() {
57
58 // Generate a unique ID for this user and session
59 $this->msgId = md5(uniqid());
60
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();
64
65 }
66
67 /**
68 * Add a message to the queue
69 *
70 * @author Mike Everhart
71 *
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
75 * @return bool
76 *
77 */
78 public function add($type, $message, $redirect_to=null) {
79
80 if( !isset($_SESSION['flash_messages']) ) return false;
81
82 if( !isset($type) || !isset($message[0]) ) return false;
83
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 );
87
88 // Backwards compatibility...
89 } elseif( $type == 'information' ) {
90 $type = 'info';
91 }
92
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!' );
95
96 // If the session array doesn't exist, create it
97 if( !array_key_exists( $type, $_SESSION['flash_messages'] ) ) $_SESSION['flash_messages'][$type] = array();
98
99 $_SESSION['flash_messages'][$type][] = $message;
100
101 if( !is_null($redirect_to) ) {
102 header("Location: $redirect_to");
103 exit();
104 }
105
106 return true;
107
108 }
109
110 //-----------------------------------------------------------------------------------------------
111 // display()
112 // print queued messages to the screen
113 //-----------------------------------------------------------------------------------------------
114 /**
115 * Display the queued messages
116 *
117 * @author Mike Everhart
118 *
119 * @param string $type Which messages to display
120 * @param bool $print True = print the messages on the screen
121 * @return mixed
122 *
123 */
124 public function display($type='all', $print=true) {
125 $messages = '';
126 $data = '';
127
128 if( !isset($_SESSION['flash_messages']) ) return false;
129
130 if( $type == 'g' || $type == 'growl' ) {
131 $this->displayGrowlMessages();
132 return true;
133 }
134
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;
139 }
140
141 $data .= sprintf($this->msgWrapper, $this->msgClass, $type, $messages);
142
143 // Clear the viewed messages
144 $this->clear($type);
145
146 // Print ALL queued messages
147 } elseif( $type == 'all' ) {
148 foreach( $_SESSION['flash_messages'] as $type => $msgArray ) {
149 $messages = '';
150 foreach( $msgArray as $msg ) {
151 $messages .= $this->msgBefore . $msg . $this->msgAfter;
152 }
153 $data .= sprintf($this->msgWrapper, $this->msgClass, $type, $messages);
154 }
155
156 // Clear ALL of the messages
157 $this->clear();
158
159 // Invalid Message Type?
160 } else {
161 return false;
162 }
163
164 // Print everything to the screen or return the data
165 if( $print ) {
166 echo $data;
167 } else {
168 return $data;
169 }
170 }
171
172
173 /**
174 * Check to see if there are any queued error messages
175 *
176 * @author Mike Everhart
177 *
178 * @return bool true = There ARE error messages
179 * false = There are NOT any error messages
180 *
181 */
182 public function hasErrors() {
183 return empty($_SESSION['flash_messages']['error']) ? false : true;
184 }
185
186 /**
187 * Check to see if there are any ($type) messages queued
188 *
189 * @author Mike Everhart
190 *
191 * @param string $type The type of messages to check for
192 * @return bool
193 *
194 */
195 public function hasMessages($type=null) {
196 if( !is_null($type) ) {
197 if( !empty($_SESSION['flash_messages'][$type]) ) return $_SESSION['flash_messages'][$type];
198 } else {
199 foreach( $this->msgTypes as $type ) {
200 if( !empty($_SESSION['flash_messages']) ) return true;
201 }
202 }
203 return false;
204 }
205
206 /**
207 * Clear messages from the session data
208 *
209 * @author Mike Everhart
210 *
211 * @param string $type The type of messages to clear
212 * @return bool
213 *
214 */
215 public function clear($type='all') {
216 if( $type == 'all' ) {
217 unset($_SESSION['flash_messages']);
218 } else {
219 unset($_SESSION['flash_messages'][$type]);
220 }
221 return true;
222 }
223
224 public function __toString() { return $this->hasMessages(); }
225
226 public function __destruct() {
227 //$this->clear();
228 }
229
230
231 } // end class
232 ?>