]>
Commit | Line | Data |
---|---|---|
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 | |
39 | class 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 | settype($_SESSION, 'array'); | |
63 | if( !array_key_exists('flash_messages', $_SESSION) ) $_SESSION['flash_messages'] = array();\r | |
64 | \r | |
65 | }\r | |
66 | \r | |
67 | /**\r | |
68 | * Add a message to the queue\r | |
69 | * \r | |
70 | * @author Mike Everhart\r | |
71 | * \r | |
72 | * @param string $type The type of message to add\r | |
73 | * @param string $message The message\r | |
74 | * @param string $redirect_to (optional) If set, the user will be redirected to this URL\r | |
75 | * @return bool \r | |
76 | * \r | |
77 | */\r | |
78 | public function add($type, $message, $redirect_to=null) {\r | |
79 | \r | |
80 | if( !isset($_SESSION['flash_messages']) ) return false;\r | |
81 | \r | |
82 | if( !isset($type) || !isset($message[0]) ) return false;\r | |
83 | \r | |
84 | // Replace any shorthand codes with their full version\r | |
85 | if( strlen(trim($type)) == 1 ) {\r | |
86 | $type = str_replace( array('h', 'i', 'w', 'e', 's'), array('help', 'info', 'warning', 'error', 'success'), $type );\r | |
87 | \r | |
88 | // Backwards compatibility...\r | |
89 | } elseif( $type == 'information' ) {\r | |
90 | $type = 'info'; \r | |
91 | }\r | |
92 | \r | |
93 | // Make sure it's a valid message type\r | |
94 | if( !in_array($type, $this->msgTypes) ) die('"' . strip_tags($type) . '" is not a valid message type!' );\r | |
95 | \r | |
96 | // If the session array doesn't exist, create it\r | |
97 | if( !array_key_exists( $type, $_SESSION['flash_messages'] ) ) $_SESSION['flash_messages'][$type] = array();\r | |
98 | \r | |
99 | $_SESSION['flash_messages'][$type][] = $message;\r | |
100 | \r | |
101 | if( !is_null($redirect_to) ) {\r | |
102 | header("Location: $redirect_to");\r | |
103 | exit();\r | |
104 | }\r | |
105 | \r | |
106 | return true;\r | |
107 | \r | |
108 | }\r | |
109 | \r | |
110 | //-----------------------------------------------------------------------------------------------\r | |
111 | // display()\r | |
112 | // print queued messages to the screen\r | |
113 | //-----------------------------------------------------------------------------------------------\r | |
114 | /**\r | |
115 | * Display the queued messages\r | |
116 | * \r | |
117 | * @author Mike Everhart\r | |
118 | * \r | |
119 | * @param string $type Which messages to display\r | |
120 | * @param bool $print True = print the messages on the screen\r | |
121 | * @return mixed \r | |
122 | * \r | |
123 | */\r | |
124 | public function display($type='all', $print=true) {\r | |
125 | $messages = '';\r | |
126 | $data = '';\r | |
127 | \r | |
128 | if( !isset($_SESSION['flash_messages']) ) return false;\r | |
129 | \r | |
130 | if( $type == 'g' || $type == 'growl' ) {\r | |
131 | $this->displayGrowlMessages();\r | |
132 | return true;\r | |
133 | }\r | |
134 | \r | |
135 | // Print a certain type of message?\r | |
136 | if( in_array($type, $this->msgTypes) ) {\r | |
137 | foreach( $_SESSION['flash_messages'][$type] as $msg ) {\r | |
138 | $messages .= $this->msgBefore . $msg . $this->msgAfter;\r | |
139 | }\r | |
140 | \r | |
141 | $data .= sprintf($this->msgWrapper, $this->msgClass, $type, $messages);\r | |
142 | \r | |
143 | // Clear the viewed messages\r | |
144 | $this->clear($type);\r | |
145 | \r | |
146 | // Print ALL queued messages\r | |
147 | } elseif( $type == 'all' ) {\r | |
148 | foreach( $_SESSION['flash_messages'] as $type => $msgArray ) {\r | |
149 | $messages = '';\r | |
150 | foreach( $msgArray as $msg ) {\r | |
151 | $messages .= $this->msgBefore . $msg . $this->msgAfter; \r | |
152 | }\r | |
153 | $data .= sprintf($this->msgWrapper, $this->msgClass, $type, $messages);\r | |
154 | }\r | |
155 | \r | |
156 | // Clear ALL of the messages\r | |
157 | $this->clear();\r | |
158 | \r | |
159 | // Invalid Message Type?\r | |
160 | } else { \r | |
161 | return false;\r | |
162 | }\r | |
163 | \r | |
164 | // Print everything to the screen or return the data\r | |
165 | if( $print ) { \r | |
166 | echo $data; \r | |
167 | } else { \r | |
168 | return $data; \r | |
169 | }\r | |
170 | }\r | |
171 | \r | |
172 | \r | |
173 | /**\r | |
174 | * Check to see if there are any queued error messages\r | |
175 | * \r | |
176 | * @author Mike Everhart\r | |
177 | * \r | |
178 | * @return bool true = There ARE error messages\r | |
179 | * false = There are NOT any error messages\r | |
180 | * \r | |
181 | */\r | |
182 | public function hasErrors() { \r | |
183 | return empty($_SESSION['flash_messages']['error']) ? false : true; \r | |
184 | }\r | |
185 | \r | |
186 | /**\r | |
187 | * Check to see if there are any ($type) messages queued\r | |
188 | * \r | |
189 | * @author Mike Everhart\r | |
190 | * \r | |
191 | * @param string $type The type of messages to check for\r | |
192 | * @return bool \r | |
193 | * \r | |
194 | */\r | |
195 | public function hasMessages($type=null) {\r | |
196 | if( !is_null($type) ) {\r | |
197 | if( !empty($_SESSION['flash_messages'][$type]) ) return $_SESSION['flash_messages'][$type]; \r | |
198 | } else {\r | |
199 | foreach( $this->msgTypes as $type ) {\r | |
200 | if( !empty($_SESSION['flash_messages']) ) return true; \r | |
201 | }\r | |
202 | }\r | |
203 | return false;\r | |
204 | }\r | |
205 | \r | |
206 | /**\r | |
207 | * Clear messages from the session data\r | |
208 | * \r | |
209 | * @author Mike Everhart\r | |
210 | * \r | |
211 | * @param string $type The type of messages to clear\r | |
212 | * @return bool \r | |
213 | * \r | |
214 | */\r | |
215 | public function clear($type='all') { \r | |
216 | if( $type == 'all' ) {\r | |
217 | unset($_SESSION['flash_messages']); \r | |
218 | } else {\r | |
219 | unset($_SESSION['flash_messages'][$type]);\r | |
220 | }\r | |
221 | return true;\r | |
222 | }\r | |
223 | \r | |
224 | public function __toString() { return $this->hasMessages(); }\r | |
225 | \r | |
226 | public function __destruct() {\r | |
227 | //$this->clear();\r | |
228 | }\r | |
229 | \r | |
230 | \r | |
231 | } // end class\r | |
232 | ?> |