Welcome Guest, Not a member yet? Register   Sign In
Notices helper
#1

[eluser]Bramme[/eluser]
Assume you have the typical form processing structure in your controller that comes with the validation library, stuff like this:
Code:
if ($this->validation->run() != FALSE)
{
    // do some stuff
    
    // query some database
    
    // insert some data
    
    // return if the data was added succesfully or not.
}
else
{
    // display validation->error_string.
}
Personally, I like to redirect my users after (succesful) form submission back to the form and pop up a nice css styled notice that let them know their form was submitted (or not).

To do this with as less code as possible in my views and controller, I wrote a little helper that handles success and error messages (and redirects) but also handles validation->error_string without redirecting.

notices_helper.php:
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('send_notice'))
{
    function send_notice($array, $redirect = TRUE)
    {
        $ci =& get_instance();
        $ci->load->library('session');
        $ci->load->helper('url');
        
        if($redirect)
        {
            $ci->session->set_flashdata('notice', $array);
            redirect($ci->uri->uri_string());
        }
        else
        {
            $ci->session->set_userdata('notice', $array);
        }
    }    
}

if ( ! function_exists('get_notice'))
{
    function get_notice()
    {
        $ci =& get_instance();
        $ci->load->library('session');
        
        if ($ci->session->flashdata('notice') != FALSE OR $ci->session->userdata('notice') != FALSE)
        {
            $array = $ci->session->flashdata('notice') != FALSE ? $ci->session->flashdata('notice') : $ci->session->userdata('notice');
            if ($array['class'] == 'validation_error')
            {
                $notice = "<div class=\"error\">\n\t";
                $notice .= "<p>During the validation of your form, the following things went wrong:</p>\n\t<ul>\n\t\t";
                $notice .= "\t".$array['message'];
                $notice .= "\n\t</ul>\n</div>";
            }
            else
            {
                $notice = "<div class=\"".$array['class']."\">\n\t<p>".$array['message']."</p>\n</div>";
            }
            
            if ($ci->session->userdata('notice') != FALSE)
            {
                $ci->session->unset_userdata('notice');
            }
        }
        else
        {
            $notice = '';
        }
        
        return $notice;
    }
}
How do you use it?
Code:
if ($this->validation->run() != FALSE)
{
    if ( ! $this->db->insert('news', $insert))
    {
        $notice = array('class' => 'error', 'message' => 'Something went wrong with the query');
    }
    else
    {
        $notice = array('class' => 'success', 'message' => 'Item successfully added');
    }
    send_notice($notice);
}
else
{
    if($this->validation->error_string != FALSE) send_notice(array('class' => 'validation_error', 'message' => $this->validation->error_string), FALSE);
}

And in your view you simply put
Code:
&lt;?php echo get_notice(); ?&gt;

I'm setting up a sandbox CI application so I can show a demo, I'll add that in a bit and let you guys know.
#2

[eluser]xwero[/eluser]
improvements
- instead of the send_notice accepting an are it would be better if it accepted the class and the message removing the need to create an array.
Code:
function send_notice($class, $message, $redirect = '')
    {
        $ci =& get_instance();
        $ci->load->library('session');
        $ci->load->helper('url');
        
        $array = compact('class','message');
        $ci->session->set_flashdata('notice', $array);
        
        if(! empty($redirect))
        { // not everyone want to redirect the page to the current url
           if($redirect == 'self'){ $redirect = $ci->uri->uri_string(); }
           redirect($redirect);
        }
    }
The redirect shouldn't occur without the explicit knowledge of the developer because it's a big fat stop sign. What if the developer wants to add other things after he sets the notice and then redirect. The set_notice function you made has to be at the bottom of the followed control structure path which is a pretty bold presumption.

- the example use of the send_notice would be better as follows, using my modification
Code:
if ($this->validation->run() == TRUE)
{ // if the run method isn't false it true because it has a boolean return
    if ( ! $this->db->insert('news', $insert))
    {
        send_notice('error','Something went wrong with the query','self');
    }
    else
    {
        send_notice('success','Item successfully added','self');
    }
}
else
{ // the error_string is never FALSE and it only is empty if there are no rules so the if is not needed
    send_notice('validation_error',$this->validation->error_string);
}

- the get_notice function should be language neutral or if you want to add a string use a language file.

- as mentioned in the thread that got you to create this helper i mentioned adding a div is limiting but you limit it even further by using a list which assumes the validation error strings are in a li tag. And who says the other messages are only strings? What if a developer adds a marked up message? Or he wants to attach an id to the container? Limiting the developer and the designer is not a good idea.

What you could do is to create a custom element function
Code:
function tag($name,$attributes='',$content = '')
{
   $tag = '<'.$name;
  
   if(! empty($attributes)){ $tag .= ' '.$attributes; }

   $tag .= (empty($content))?'>':'>'.$content.'</'.$name.'>';

   return $tag;
}
And then use this in your send_notice function.
#3

[eluser]Bramme[/eluser]
Thanks for the feedback xwero. I agree the helper is quite limiting, but I like it like that. This is for my own use and I will probably always use a structure like this.

I'm going to implement some off your improvements but certainly not all.

I posted this code mostly to set others on their way to create their own set of helpers. Though I agree it's not a bad idea to make it less limiting.
#4

[eluser]ontguy[/eluser]
Take a look at the Messages library/helper http://codeigniter.com/wiki/Messages/. There maybe some overlap in that helper and what you've developed.




Theme © iAndrew 2016 - Forum software by © MyBB