Skip to content
Derek Jones edited this page Jul 5, 2012 · 7 revisions

Category:Libraries::Message

Introduction

This is version 2 of a library I wrote a while back. The first one supported only success, error, notice and warning messages, as well as message groups, but this new library supports anything you want. It's a complete rewrite, much more efficient (about a quarter of the original code).

You can set a message to appear on the current page, or on the next page, such as after a redirect.

You can create message groups, for instance, if you have a feedback form and a newsletter signup form on the same page, you can group your messages so that they're kept separate in the view.

You can set an HTML wrapper around the messages, this is useful if you want to use css and javascript to show the messages overtop of your content.

v2.1 Added the ability to get an array of messages, in case you don't want to echo them.

Requirements:

Requires:

  • Sessions - If you set a message to appear on the next page, such as after a redirect, it requires a session.

Usage:

Use of the Message system is quite minimal:

Load/Autoload Libraries

$this->load->library('message');

Examples

Setting a Message

// an error
$this->message->set('You forgot a required field.', 'error');

// a notice after a redirect
$this->message->set('Your account will expire in 5 days.', 'notice', TRUE);

// a success message after a redirect in a specific group
$this->message->set('Your subscription has been received.', 'success', TRUE, 'newsletter');

Retreiving Messages and Outputting as HTML

In your View:

$this->message->display();

If the message was set in the controller without redirecting, this will display the following:

<div class="messages-error">
   <p>You forgot a required field.</p>
</div>

Or after a redirect:

<div class="messages-notice">
   <p>Your account will expire in 5 days.</p>
</div>

To display a group of messages, do this in your view where you want them to display:

$this->message->display('my_group');

And if you want to set a wrapper, it requires an array with 2 paramaters. In this example we'll set the group to FALSE, which will give us all non-grouped messages:

$this->message->display(FALSE, array('<div id="messages">', '</div>'));

You can also set a default wrapper in a config file. Make a file message.php in your /application/config/ folder, with the following code:

$config['wrapper'] = array('<div id="messages">', '</div>');

You can get an array of all messages, or just messages of a certain type, and get them by group.

This will get all success messages:

$messages = $this->message->get('success');

This will get all messages in a group:

$message = $this->message->get(FALSE, 'newsletter');

Library 'Message.php'

Put this in your /application/libraries/ folder:

&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Message:: a library for giving feedback to the user
 *
 * @author  Adam Jackett
 * @url http://www.darkhousemedia.com/
 * @version 2.1
 */

class CI_Message {
    
    var $CI;
    var $messages = array();
    var $wrapper = array('', '');

    function CI_Message($config){    
        $this->CI =& get_instance();        
        $this->CI->load->library('session');
        
        if($this->CI->session->flashdata('_messages')) $this->messages = $this->CI->session->flashdata('_messages');
        if(isset($config['wrapper'])) $this->wrapper = $config['wrapper'];
    }
    
    function set($message, $type, $flash=FALSE, $group=FALSE){
        if(!is_array($message)) $message = array($message);
        foreach($message as $msg){
            $obj = new stdClass();
            $obj->message = $msg;
            $obj->type = $type;
            $obj->flash = $flash;
            $obj->group = $group;
            $this->messages[] = $obj;
        }
        
        $flash_messages = array();
        foreach($this->messages as $msg){
            if($msg->flash) $flash_messages[] = $msg;
        }
        if(count($flash_messages)) $this->CI->session->set_flashdata('_messages', $flash_messages);
    }
    
    function get($type=FALSE, $group=FALSE){
        $output = array();
        if(count($this->messages)){            
            foreach($this->messages as $msg){
                if($type !== FALSE){
                    $output[] = $msg->message;
                } else {
                    if(!isset($output[$msg->type])) $output[$msg->type] = array();
                    $output[$msg->type][] = $msg->message;
                }
            }
        }
        return $output;
    }
    
    function display($group=FALSE, $wrapper=FALSE){
        if(count($this->messages)){
            $output = $this->get(FALSE, $group);
            echo ($wrapper !== FALSE ? $wrapper[0] : $this->wrapper[0])."\r\n";
            foreach($output as $type => $messages){
                echo '<div class="message-'.$type.'">'."\r\n";
                foreach($messages as $msg){
                    echo '<p>'.$msg.'</p>'."\r\n";
                }
                echo '</div>'."\r\n";
            }
            echo ($wrapper !== FALSE ? $wrapper[1] : $this->wrapper[1])."\r\n";
        }        
    }

}
Clone this wiki locally