├── views ├── messages │ ├── notice_view.php │ └── error_view.php └── example_view.php ├── config └── message.php ├── controllers └── example.php ├── LICENSE ├── README.markdown └── libraries └── Message.php /views/messages/notice_view.php: -------------------------------------------------------------------------------- 1 |
2 | Ok 3 |
4 | 5 |

6 | 7 |
8 |
9 | -------------------------------------------------------------------------------- /views/messages/error_view.php: -------------------------------------------------------------------------------- 1 |
2 | Error 3 |
4 | 5 | 6 | 7 |

8 | 9 | 10 | 11 |
12 |
13 | -------------------------------------------------------------------------------- /config/message.php: -------------------------------------------------------------------------------- 1 | '; 4 | $config['message_suffix'] = '

'; 5 | $config['message_folder'] = 'messages/'; 6 | $config['message_view'] = 'message'; // without the _view suffix 7 | $config['wrapper_prefix'] = '
'; 8 | $config['wrapper_suffix'] = '
'; 9 | -------------------------------------------------------------------------------- /controllers/example.php: -------------------------------------------------------------------------------- 1 | load->helper('url'); 10 | $this->load->library('message'); 11 | } 12 | 13 | function index() 14 | { 15 | $this->load->view('example_view'); 16 | } 17 | 18 | function message() 19 | { 20 | $this->message->set('message','this is just a message'); 21 | $this->index(); 22 | } 23 | 24 | function notice() 25 | { 26 | $data = array( 27 | 'message'=>'this is just a message', 28 | 'notice'=>'this is just a notice' 29 | ); 30 | 31 | $this->message->set($data); 32 | $this->index(); 33 | } 34 | 35 | function error() 36 | { 37 | $this->message->set('message','this is just a message'); 38 | $this->message->set('error','this is an error'); 39 | 40 | redirect('example'); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2010 - 2011, Isset Internet Professionals 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.markdown: -------------------------------------------------------------------------------- 1 | # CodeIgniter-Message 2 | 3 | The Message library (originally based on http://codeigniter.com/wiki/Message/) 4 | is a fairly straightforward Codeigniter library that makes it easier 5 | to set different type of messages such as errors and notifications. Traditionally you'd 6 | use $this->session->set_flashdata() but this can result in a lot of code for something 7 | so simple. 8 | 9 | ## Configuration 10 | 11 | All configuration is optional. It gives you a handy way of setting default values for 12 | your message container, the html element you want to wrap your individual messages with, 13 | a default view to load your message in, or load a specific view based on a certain 14 | message group. If the configuration file is not present nor any views are present, the 15 | message library will just output the message without any markup. This might be preferable 16 | when passing messages to javascript plugins, like Growl. 17 | 18 | what you would like to wrap your individual messages with 19 | 20 | $config['message_prefix'] = '

'; 21 | $config['message_suffix'] = '

'; 22 | 23 | what you would like the container to be of your messages 24 | 25 | $config['wrapper_prefix'] = '
'; 26 | $config['wrapper_suffix'] = '
'; 27 | 28 | the folder to search for partial views with trailing slash 29 | 30 | $config['message_folder'] = 'messages/'; 31 | 32 | the default view to format messages 33 | 34 | $config['message_view'] = 'message_view';` 35 | 36 | ## Basic Functions 37 | 38 | Set a message 39 | 40 | $this->message->set('notice','this is just a notice'); 41 | 42 | Set an array of messages 43 | 44 | $data = array( 45 | 'message'=>'this is just a message', 46 | 'notice'=>'this is just a notice' 47 | ); 48 | 49 | $this->message->set($data); 50 | 51 | Return all messages 52 | 53 | $messages = $this->message->get(); 54 | 55 | Return a group of messages 56 | 57 | $messages = $this->message->get('notice'); 58 | 59 | Show all messages 60 | 61 | echo $this->message->display(); 62 | 63 | Show a group of messages 64 | 65 | echo $this->message->display('notice'); 66 | 67 | ## Automagic functions 68 | 69 | If you have a message_folder defined in the config, any message key that matches a view will be autoloaded: 70 | 71 | $this->message->set('notice','this is just a notice'); 72 | 73 | will autoload the file views/message_folder_name/notice_view.php 74 | The basic layout for any view looks like this: 75 | 76 | foreach ($messages as $message): 77 | echo $message; 78 | endforeach; 79 | 80 | This gives you freedom to style any message any way you want from within a view. 81 | 82 | ## Installation 83 | 84 | Drop the libraries/Message.php file into your application/libraries folder. 85 | 86 | ## Requirements 87 | 88 | * Codeigniter 1.7.X or newer 89 | 90 | ## License 91 | 92 | This library is licensed under the MIT license, a copy of this license can be found in the 93 | file called "LICENSE". 94 | 95 | ## Extra 96 | 97 | If you happen to encounter a bug or would like to request a feature you can use the 98 | bugtracker provided by GitHub or send an Email to github@isset.nl. 99 | -------------------------------------------------------------------------------- /views/example_view.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | Welcome to Message Library Example 4 | 5 | 77 | 78 | 79 | 80 |

Message Library Example Page

81 | 82 | message->display(); ?> 83 | 84 |

Click here to show a simple red message without redirect and with basic styling added to it from the config:

85 | $this->message->set('message','this is just a message'); 86 | 87 |

Click here to show a simple red message and a notice styled by a view (in application/views/messages/notice_view.php) without redirect using an array:

88 | $data = array(
89 |   'message'=>'this is just a message',
90 |   'notice'=>'this is just a notice'
91 | );
92 |
93 | $this->message->set($data);
94 | 95 |

Click here to show a simple red message and an error styled by a view (in application/views/messages/error_view.php) with redirect:

96 | 97 | $this->message->set('message','this is just a message');
98 | $this->message->set('error','this is an error'); 99 |
100 | 101 |

Basic Functions

102 |

Set a message

103 | $this->message->set('notice','this is just a notice'); 104 | 105 |

Set an array of messages

106 | $data = array(
107 |   'message'=>'this is just a message',
108 |   'notice'=>'this is just a notice'
109 | );
110 |
111 | $this->message->set($data);
112 | 113 |

Return all messages

114 | 115 | $messages = $this->message->get(); 116 | 117 | 118 |

Return a group of messages

119 | 120 | $messages = $this->message->get('notice'); 121 | 122 | 123 |

Show all messages

124 | 125 | echo $this->message->display(); 126 | 127 | 128 |

Show a group of messages

129 | 130 | echo $this->message->display('notice'); 131 | 132 | 133 | 134 | -------------------------------------------------------------------------------- /libraries/Message.php: -------------------------------------------------------------------------------- 1 | session->set_flashdata() but this can result in a lot of code for something 8 | * so simple. The Message library in many ways works similar to the session library but is 9 | * meant to be easier to use and reduce the amount of code. 10 | * 11 | * A basic example of using the Message library looks like the following (this should be 12 | * executed in a controller): 13 | * 14 | * $this->load->library('message'); 15 | * $this->message->set('info', 'Hello, world!'); 16 | * 17 | * I've skipped the configuration part (for now) but as you can see it only takes 2 lines 18 | * of code to set a message. Retrieving this message is quite easy too and looks like the 19 | * following (this goes into a view): 20 | * 21 | * message->display() ) { echo $this->message->display(); } ?> 22 | * 23 | * For more information you should check the README. 24 | * 25 | * @author Jeroen vd. Gulik, Isset Internet Professionals 26 | * @link http://isset.nl/ 27 | * @package Message Library 28 | * @version 1.2 29 | * @license MIT License 30 | * 31 | * Copyright (C) 2010 - 2011, Isset Internet Professionals 32 | * 33 | * Permission is hereby granted, free of charge, to any person obtaining a copy 34 | * of this software and associated documentation files (the "Software"), to deal 35 | * in the Software without restriction, including without limitation the rights 36 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 37 | * copies of the Software, and to permit persons to whom the Software is 38 | * furnished to do so, subject to the following conditions: 39 | * 40 | * The above copyright notice and this permission notice shall be included in 41 | * all copies or substantial portions of the Software. 42 | * 43 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 44 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 45 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 46 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 47 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 48 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 49 | * THE SOFTWARE. 50 | */ 51 | class Message 52 | { 53 | /** 54 | * Variable containing a reference to the Codeigniter instance. 55 | * 56 | * @access private 57 | * @var object 58 | */ 59 | private $CI; 60 | 61 | /** 62 | * Array containing all messages that were set using the set() method. 63 | * 64 | * @access private 65 | * @var array 66 | */ 67 | private $messages = array(); 68 | 69 | /** 70 | * Prefix to use for all messages. 71 | * 72 | * @access private 73 | * @var string 74 | */ 75 | private $message_prefix = ''; 76 | 77 | /** 78 | * Suffix to use for all messages. 79 | * 80 | * @access private 81 | * @var string 82 | */ 83 | private $message_suffix = ''; 84 | 85 | /** 86 | * The directory in which all messages are stored. 87 | * 88 | * @access private 89 | * @var string 90 | */ 91 | private $message_folder = ''; 92 | 93 | /** 94 | * The view for the current message. 95 | * 96 | * @access private 97 | * @var string 98 | */ 99 | private $message_view = ''; 100 | 101 | /** 102 | * The prefix for the messages container. 103 | * 104 | * @access private 105 | * @var string 106 | */ 107 | private $wrapper_prefix = ''; 108 | 109 | /** 110 | * The suffix for the messages container. 111 | * 112 | * @access private 113 | * @var string 114 | */ 115 | private $wrapper_suffix = ''; 116 | 117 | /** 118 | * Constructor method, called whenever the library is loaded using $this->load->library() 119 | * 120 | * @access public 121 | * @param array $config Associative array containing all configuration options. 122 | * @return object 123 | */ 124 | public function __construct($config = array()) 125 | { 126 | $this->CI =& get_instance(); 127 | $this->CI->load->library('session'); 128 | 129 | if ($this->CI->session->userdata('_messages')) 130 | { 131 | $this->messages = $this->CI->session->userdata('_messages'); 132 | } 133 | 134 | if ( count($config) > 0 ) 135 | { 136 | $this->initialize($config); 137 | } 138 | 139 | log_message('debug', "Message Class Initialized"); 140 | } 141 | 142 | /** 143 | * Initializes the library by setting all custom configuration options based 144 | * on the specified array. 145 | * 146 | * @example 147 | * $this->load->library('message'); 148 | * $this->message->initialize(array( 149 | * 'wrapper_prefix' => 'container_' 150 | * )); 151 | * 152 | * @access public 153 | * @param array $config Associative array containing all user-defined configuration options. 154 | * @return void 155 | */ 156 | public function initialize($config = array()) 157 | { 158 | foreach ( $config as $key => $val ) 159 | { 160 | if ( isset($this->$key) ) 161 | { 162 | $this->$key = $val; 163 | } 164 | } 165 | } 166 | 167 | /** 168 | * Adds a new message to the internal storage. The first argument is either a string 169 | * or an array of message groups (e.g. "error" or "info") and the second argument 170 | * a value for each of these messages. If the first argument is set as an array 171 | * the second argument will be ignored. 172 | * 173 | * @example 174 | * $this->message->set('error', 'Bummer! Somebody sat on the tubes!'); 175 | * $this->message->set(array( 176 | * 'error' => 'Woops, seems something is broken!', 177 | * 'info' => 'Hello, world!' 178 | * )); 179 | * 180 | * @access public 181 | * @param array/string $groups Either a string containing the group name or an 182 | * array of key/value combinations of each group and it's value. 183 | * @param string $message The message to display whenever the first argument 184 | * was a string. 185 | * @return void 186 | */ 187 | public function set($groups, $message = NULL) 188 | { 189 | if ( is_string($groups) ) 190 | { 191 | $groups = array($groups => $message); 192 | } 193 | 194 | if ( count($groups) > 0 ) 195 | { 196 | foreach ( $groups as $group => $value ) 197 | { 198 | // Let's skip empty messages 199 | if ( empty($value) ) 200 | { 201 | continue; 202 | } 203 | 204 | // Technically not always required but it ensures the group is always there. 205 | if ( !isset($this->messages[$group]) ) 206 | { 207 | $this->messages[$group] = array(); 208 | } 209 | 210 | $this->messages[$group][] = $value; 211 | } 212 | 213 | $this->CI->session->set_userdata('_messages', $this->messages); 214 | } 215 | } 216 | 217 | /** 218 | * Fetches all messages for the specified group. If no group was found this 219 | * method will return FALSE instead of an array. 220 | * 221 | * @example 222 | * $this->library->get('error'); 223 | * 224 | * @access public 225 | * @param string $group The name of the group you want to retrieve. 226 | * @return array/boolean 227 | */ 228 | public function get($group = FALSE) 229 | { 230 | // Do we have something to show? 231 | if ( count($this->messages) == 0 ) 232 | { 233 | return FALSE; 234 | } 235 | 236 | // If a group is specified we'll return it, otherwise we'll return all items 237 | if ( isset($group) AND !empty($group) ) 238 | { 239 | if ( isset($this->messages[$group]) ) 240 | { 241 | return $this->messages[$group]; 242 | } 243 | else 244 | { 245 | return FALSE; 246 | } 247 | } 248 | else 249 | { 250 | return $this->messages; 251 | } 252 | } 253 | 254 | /** 255 | * Retrieves all messages and formats them using the corresponding views for each message. 256 | * If you want to return the raw messages use the get() method instead. 257 | * 258 | * @example 259 | * echo $this->message->display('error'); 260 | * 261 | * @access public 262 | * @param string $group The name of the group you want to output. 263 | * @return string 264 | */ 265 | public function display($group = FALSE) 266 | { 267 | // do we have something to show? 268 | if ( $this->get($group) === FALSE) 269 | { 270 | return FALSE; 271 | } 272 | 273 | // Let's format the data 274 | $output = $this->format_output($group); 275 | 276 | // Clear our message cache 277 | $this->CI->session->unset_userdata('_messages'); 278 | 279 | return $output; 280 | } 281 | 282 | /** 283 | * Formats a group of messages based on the corresponding view. 284 | * 285 | * @access private 286 | * @param string $by_group The name of the group to format. 287 | * @return string 288 | */ 289 | private function format_output($by_group = FALSE) 290 | { 291 | $output = NULL; 292 | 293 | // loop through the groups and cascade through format options 294 | foreach ( $this->messages as $group => $messages ) 295 | { 296 | // was a group set? if so skip all groups that do not match 297 | if ( $by_group !== FALSE && $group != $by_group ) 298 | { 299 | continue; 300 | } 301 | 302 | // does a view partial exist? 303 | if ( file_exists(APPPATH.'views/'.$this->message_folder.$group.'_view' . EXT) ) 304 | { 305 | $output .= $this->CI->load->view($this->message_folder.$group.'_view', array('messages'=>$messages), TRUE); 306 | } 307 | // does a default view partial exist? 308 | elseif ( file_exists(APPPATH.'views/'.$this->message_folder.$this->message_view.'_view' . EXT) ) 309 | { 310 | $output .= $this->CI->load->view($this->message_folder.$this->message_view.'_view', array('messages'=>$messages), TRUE); 311 | } 312 | // fallback to default values (possibly set by config) 313 | else 314 | { 315 | $output .= $this->wrapper_prefix . PHP_EOL; 316 | 317 | foreach ( $messages as $msg ) 318 | { 319 | $output .= $this->message_prefix . $msg . $this->message_suffix . PHP_EOL; 320 | } 321 | 322 | $output .= $this->wrapper_suffix . PHP_EOL; 323 | } 324 | } 325 | 326 | return $output; 327 | } 328 | } 329 | 330 | /* End of file Message.php */ 331 | /* Location: ./application/libraries/Message.php */ 332 | --------------------------------------------------------------------------------