]> git.immae.eu Git - github/wallabag/wallabag.git/blame - inc/3rdparty/class.messages.php
Merge branch 'Flattr' of git://github.com/tcitworld/poche into tcitworld-Flattr
[github/wallabag/wallabag.git] / inc / 3rdparty / class.messages.php
CommitLineData
55821e04
NL
1<?php\r
2//--------------------------------------------------------------------------------------------------\r
3// Session-Based Flash Messages v1.0\r
4// Copyright 2012 Mike Everhart (http://mikeeverhart.net)\r
5//\r
6// Licensed under the Apache License, Version 2.0 (the "License");\r
7// you may not use this file except in compliance with the License.\r
8// You may obtain a copy of the License at\r
9//\r
10// http://www.apache.org/licenses/LICENSE-2.0\r
11//\r
12// Unless required by applicable law or agreed to in writing, software\r
13// distributed under the License is distributed on an "AS IS" BASIS,\r
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
15// See the License for the specific language governing permissions and\r
16// limitations under the License.\r
17// \r
18//------------------------------------------------------------------------------\r
19// Description:\r
20//------------------------------------------------------------------------------\r
21//\r
22// Stores messages in Session data to be easily retrieved later on.\r
23// This class includes four different types of messages:\r
24// - Success\r
25// - Error\r
26// - Warning\r
27// - Information\r
28// \r
29// See README for basic usage instructions, or see samples/index.php for more advanced samples\r
30//\r
31//--------------------------------------------------------------------------------------------------\r
32// Changelog\r
33//--------------------------------------------------------------------------------------------------\r
34// \r
35// 2011-05-15 - v1.0 - Initial Version\r
36//\r
37//--------------------------------------------------------------------------------------------------\r
38\r
39class Messages {\r
40 \r
41 //-----------------------------------------------------------------------------------------------\r
42 // Class Variables\r
43 //----------------------------------------------------------------------------------------------- \r
44 var $msgId;\r
45 var $msgTypes = array( 'help', 'info', 'warning', 'success', 'error' );\r
46 var $msgClass = 'messages';\r
47 var $msgWrapper = "<div class='%s %s'><a href='#' class='closeMessage'>X</a>\n%s</div>\n";\r
48 var $msgBefore = '<p>';\r
49 var $msgAfter = "</p>\n";\r
50\r
51 \r
52 /**\r
53 * Constructor\r
54 * @author Mike Everhart\r
55 */\r
56 public function __construct() {\r
57 \r
58 // Generate a unique ID for this user and session\r
59 $this->msgId = md5(uniqid());\r
60 \r
61 // Create the session array if it doesnt already exist\r
62 if( !array_key_exists('flash_messages', $_SESSION) ) $_SESSION['flash_messages'] = array();\r
63 \r
64 }\r
65 \r
66 /**\r
67 * Add a message to the queue\r
68 * \r
69 * @author Mike Everhart\r
70 * \r
71 * @param string $type The type of message to add\r
72 * @param string $message The message\r
73 * @param string $redirect_to (optional) If set, the user will be redirected to this URL\r
74 * @return bool \r
75 * \r
76 */\r
77 public function add($type, $message, $redirect_to=null) {\r
78 \r
79 if( !isset($_SESSION['flash_messages']) ) return false;\r
80 \r
81 if( !isset($type) || !isset($message[0]) ) return false;\r
82\r
83 // Replace any shorthand codes with their full version\r
84 if( strlen(trim($type)) == 1 ) {\r
85 $type = str_replace( array('h', 'i', 'w', 'e', 's'), array('help', 'info', 'warning', 'error', 'success'), $type );\r
86 \r
87 // Backwards compatibility...\r
88 } elseif( $type == 'information' ) {\r
89 $type = 'info'; \r
90 }\r
91 \r
92 // Make sure it's a valid message type\r
93 if( !in_array($type, $this->msgTypes) ) die('"' . strip_tags($type) . '" is not a valid message type!' );\r
94 \r
95 // If the session array doesn't exist, create it\r
96 if( !array_key_exists( $type, $_SESSION['flash_messages'] ) ) $_SESSION['flash_messages'][$type] = array();\r
97 \r
98 $_SESSION['flash_messages'][$type][] = $message;\r
99\r
100 if( !is_null($redirect_to) ) {\r
101 header("Location: $redirect_to");\r
102 exit();\r
103 }\r
104 \r
105 return true;\r
106 \r
107 }\r
108 \r
109 //-----------------------------------------------------------------------------------------------\r
110 // display()\r
111 // print queued messages to the screen\r
112 //-----------------------------------------------------------------------------------------------\r
113 /**\r
114 * Display the queued messages\r
115 * \r
116 * @author Mike Everhart\r
117 * \r
118 * @param string $type Which messages to display\r
119 * @param bool $print True = print the messages on the screen\r
120 * @return mixed \r
121 * \r
122 */\r
123 public function display($type='all', $print=true) {\r
124 $messages = '';\r
125 $data = '';\r
126 \r
127 if( !isset($_SESSION['flash_messages']) ) return false;\r
128 \r
129 if( $type == 'g' || $type == 'growl' ) {\r
130 $this->displayGrowlMessages();\r
131 return true;\r
132 }\r
133 \r
134 // Print a certain type of message?\r
135 if( in_array($type, $this->msgTypes) ) {\r
136 foreach( $_SESSION['flash_messages'][$type] as $msg ) {\r
137 $messages .= $this->msgBefore . $msg . $this->msgAfter;\r
138 }\r
139\r
140 $data .= sprintf($this->msgWrapper, $this->msgClass, $type, $messages);\r
141 \r
142 // Clear the viewed messages\r
143 $this->clear($type);\r
144 \r
145 // Print ALL queued messages\r
146 } elseif( $type == 'all' ) {\r
147 foreach( $_SESSION['flash_messages'] as $type => $msgArray ) {\r
148 $messages = '';\r
149 foreach( $msgArray as $msg ) {\r
150 $messages .= $this->msgBefore . $msg . $this->msgAfter; \r
151 }\r
152 $data .= sprintf($this->msgWrapper, $this->msgClass, $type, $messages);\r
153 }\r
154 \r
155 // Clear ALL of the messages\r
156 $this->clear();\r
157 \r
158 // Invalid Message Type?\r
159 } else { \r
160 return false;\r
161 }\r
162 \r
163 // Print everything to the screen or return the data\r
164 if( $print ) { \r
165 echo $data; \r
166 } else { \r
167 return $data; \r
168 }\r
169 }\r
170 \r
171 \r
172 /**\r
173 * Check to see if there are any queued error messages\r
174 * \r
175 * @author Mike Everhart\r
176 * \r
177 * @return bool true = There ARE error messages\r
178 * false = There are NOT any error messages\r
179 * \r
180 */\r
181 public function hasErrors() { \r
182 return empty($_SESSION['flash_messages']['error']) ? false : true; \r
183 }\r
184 \r
185 /**\r
186 * Check to see if there are any ($type) messages queued\r
187 * \r
188 * @author Mike Everhart\r
189 * \r
190 * @param string $type The type of messages to check for\r
191 * @return bool \r
192 * \r
193 */\r
194 public function hasMessages($type=null) {\r
195 if( !is_null($type) ) {\r
196 if( !empty($_SESSION['flash_messages'][$type]) ) return $_SESSION['flash_messages'][$type]; \r
197 } else {\r
198 foreach( $this->msgTypes as $type ) {\r
199 if( !empty($_SESSION['flash_messages']) ) return true; \r
200 }\r
201 }\r
202 return false;\r
203 }\r
204 \r
205 /**\r
206 * Clear messages from the session data\r
207 * \r
208 * @author Mike Everhart\r
209 * \r
210 * @param string $type The type of messages to clear\r
211 * @return bool \r
212 * \r
213 */\r
214 public function clear($type='all') { \r
215 if( $type == 'all' ) {\r
216 unset($_SESSION['flash_messages']); \r
217 } else {\r
218 unset($_SESSION['flash_messages'][$type]);\r
219 }\r
220 return true;\r
221 }\r
222 \r
223 public function __toString() { return $this->hasMessages(); }\r
224\r
225 public function __destruct() {\r
226 //$this->clear();\r
227 }\r
228\r
229\r
230} // end class\r
231?>