--- /dev/null
+<?php\r
+//--------------------------------------------------------------------------------------------------\r
+// Session-Based Flash Messages v1.0\r
+// Copyright 2012 Mike Everhart (http://mikeeverhart.net)\r
+//\r
+// Licensed under the Apache License, Version 2.0 (the "License");\r
+// you may not use this file except in compliance with the License.\r
+// You may obtain a copy of the License at\r
+//\r
+// http://www.apache.org/licenses/LICENSE-2.0\r
+//\r
+// Unless required by applicable law or agreed to in writing, software\r
+// distributed under the License is distributed on an "AS IS" BASIS,\r
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+// See the License for the specific language governing permissions and\r
+// limitations under the License.\r
+// \r
+//------------------------------------------------------------------------------\r
+// Description:\r
+//------------------------------------------------------------------------------\r
+//\r
+// Stores messages in Session data to be easily retrieved later on.\r
+// This class includes four different types of messages:\r
+// - Success\r
+// - Error\r
+// - Warning\r
+// - Information\r
+// \r
+// See README for basic usage instructions, or see samples/index.php for more advanced samples\r
+//\r
+//--------------------------------------------------------------------------------------------------\r
+// Changelog\r
+//--------------------------------------------------------------------------------------------------\r
+// \r
+// 2011-05-15 - v1.0 - Initial Version\r
+//\r
+//--------------------------------------------------------------------------------------------------\r
+\r
+class Messages {\r
+ \r
+ //-----------------------------------------------------------------------------------------------\r
+ // Class Variables\r
+ //----------------------------------------------------------------------------------------------- \r
+ var $msgId;\r
+ var $msgTypes = array( 'help', 'info', 'warning', 'success', 'error' );\r
+ var $msgClass = 'messages';\r
+ var $msgWrapper = "<div class='%s %s'><a href='#' class='closeMessage'>X</a>\n%s</div>\n";\r
+ var $msgBefore = '<p>';\r
+ var $msgAfter = "</p>\n";\r
+\r
+ \r
+ /**\r
+ * Constructor\r
+ * @author Mike Everhart\r
+ */\r
+ public function __construct() {\r
+ \r
+ // Generate a unique ID for this user and session\r
+ $this->msgId = md5(uniqid());\r
+ \r
+ // Create the session array if it doesnt already exist\r
+ if( !array_key_exists('flash_messages', $_SESSION) ) $_SESSION['flash_messages'] = array();\r
+ \r
+ }\r
+ \r
+ /**\r
+ * Add a message to the queue\r
+ * \r
+ * @author Mike Everhart\r
+ * \r
+ * @param string $type The type of message to add\r
+ * @param string $message The message\r
+ * @param string $redirect_to (optional) If set, the user will be redirected to this URL\r
+ * @return bool \r
+ * \r
+ */\r
+ public function add($type, $message, $redirect_to=null) {\r
+ \r
+ if( !isset($_SESSION['flash_messages']) ) return false;\r
+ \r
+ if( !isset($type) || !isset($message[0]) ) return false;\r
+\r
+ // Replace any shorthand codes with their full version\r
+ if( strlen(trim($type)) == 1 ) {\r
+ $type = str_replace( array('h', 'i', 'w', 'e', 's'), array('help', 'info', 'warning', 'error', 'success'), $type );\r
+ \r
+ // Backwards compatibility...\r
+ } elseif( $type == 'information' ) {\r
+ $type = 'info'; \r
+ }\r
+ \r
+ // Make sure it's a valid message type\r
+ if( !in_array($type, $this->msgTypes) ) die('"' . strip_tags($type) . '" is not a valid message type!' );\r
+ \r
+ // If the session array doesn't exist, create it\r
+ if( !array_key_exists( $type, $_SESSION['flash_messages'] ) ) $_SESSION['flash_messages'][$type] = array();\r
+ \r
+ $_SESSION['flash_messages'][$type][] = $message;\r
+\r
+ if( !is_null($redirect_to) ) {\r
+ header("Location: $redirect_to");\r
+ exit();\r
+ }\r
+ \r
+ return true;\r
+ \r
+ }\r
+ \r
+ //-----------------------------------------------------------------------------------------------\r
+ // display()\r
+ // print queued messages to the screen\r
+ //-----------------------------------------------------------------------------------------------\r
+ /**\r
+ * Display the queued messages\r
+ * \r
+ * @author Mike Everhart\r
+ * \r
+ * @param string $type Which messages to display\r
+ * @param bool $print True = print the messages on the screen\r
+ * @return mixed \r
+ * \r
+ */\r
+ public function display($type='all', $print=true) {\r
+ $messages = '';\r
+ $data = '';\r
+ \r
+ if( !isset($_SESSION['flash_messages']) ) return false;\r
+ \r
+ if( $type == 'g' || $type == 'growl' ) {\r
+ $this->displayGrowlMessages();\r
+ return true;\r
+ }\r
+ \r
+ // Print a certain type of message?\r
+ if( in_array($type, $this->msgTypes) ) {\r
+ foreach( $_SESSION['flash_messages'][$type] as $msg ) {\r
+ $messages .= $this->msgBefore . $msg . $this->msgAfter;\r
+ }\r
+\r
+ $data .= sprintf($this->msgWrapper, $this->msgClass, $type, $messages);\r
+ \r
+ // Clear the viewed messages\r
+ $this->clear($type);\r
+ \r
+ // Print ALL queued messages\r
+ } elseif( $type == 'all' ) {\r
+ foreach( $_SESSION['flash_messages'] as $type => $msgArray ) {\r
+ $messages = '';\r
+ foreach( $msgArray as $msg ) {\r
+ $messages .= $this->msgBefore . $msg . $this->msgAfter; \r
+ }\r
+ $data .= sprintf($this->msgWrapper, $this->msgClass, $type, $messages);\r
+ }\r
+ \r
+ // Clear ALL of the messages\r
+ $this->clear();\r
+ \r
+ // Invalid Message Type?\r
+ } else { \r
+ return false;\r
+ }\r
+ \r
+ // Print everything to the screen or return the data\r
+ if( $print ) { \r
+ echo $data; \r
+ } else { \r
+ return $data; \r
+ }\r
+ }\r
+ \r
+ \r
+ /**\r
+ * Check to see if there are any queued error messages\r
+ * \r
+ * @author Mike Everhart\r
+ * \r
+ * @return bool true = There ARE error messages\r
+ * false = There are NOT any error messages\r
+ * \r
+ */\r
+ public function hasErrors() { \r
+ return empty($_SESSION['flash_messages']['error']) ? false : true; \r
+ }\r
+ \r
+ /**\r
+ * Check to see if there are any ($type) messages queued\r
+ * \r
+ * @author Mike Everhart\r
+ * \r
+ * @param string $type The type of messages to check for\r
+ * @return bool \r
+ * \r
+ */\r
+ public function hasMessages($type=null) {\r
+ if( !is_null($type) ) {\r
+ if( !empty($_SESSION['flash_messages'][$type]) ) return $_SESSION['flash_messages'][$type]; \r
+ } else {\r
+ foreach( $this->msgTypes as $type ) {\r
+ if( !empty($_SESSION['flash_messages']) ) return true; \r
+ }\r
+ }\r
+ return false;\r
+ }\r
+ \r
+ /**\r
+ * Clear messages from the session data\r
+ * \r
+ * @author Mike Everhart\r
+ * \r
+ * @param string $type The type of messages to clear\r
+ * @return bool \r
+ * \r
+ */\r
+ public function clear($type='all') { \r
+ if( $type == 'all' ) {\r
+ unset($_SESSION['flash_messages']); \r
+ } else {\r
+ unset($_SESSION['flash_messages'][$type]);\r
+ }\r
+ return true;\r
+ }\r
+ \r
+ public function __toString() { return $this->hasMessages(); }\r
+\r
+ public function __destruct() {\r
+ //$this->clear();\r
+ }\r
+\r
+\r
+} // end class\r
+?>
\ No newline at end of file