From 245eab0350a879e2f93b9814b66f44eb2539ca90 Mon Sep 17 00:00:00 2001 From: Eric Haughee Date: Fri, 25 May 2012 11:11:02 -0300 Subject: [PATCH] Added load view type 'redirect' compatible flash messages. --- flash_helper.php | 81 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 64 insertions(+), 17 deletions(-) diff --git a/flash_helper.php b/flash_helper.php index 2d9ccac..4edb14d 100755 --- a/flash_helper.php +++ b/flash_helper.php @@ -25,41 +25,72 @@ * @category Helpers * @author Chris Monnat (https://github.com/mrtopher) * @commiter Obedi Ferreira (https://github.com/obxhdx) + * @modifier Eric Haughee (https://github.com/ehaughee) * @link http://www.christophermonnat.com/2009/05/building-applications-using-codeigniter-part-3-helpers/ + * */ /** - * Display formatted flash message. + * Display formatted flash message. Compatible with + * a CodeIgniter redirect() and loading a view. * * @access public * @param string|array * @return string */ -function display_flash($name) +function flash($name = 'flash_message') { $CI =& get_instance(); - if($CI->session->flashdata($name)) - { - $flash = $CI->session->flashdata($name); + if($CI->session->flashdata($name)) + { + $flash = $CI->session->flashdata($name); - if (is_array($flash['message'])) { - $msg = '
'; + if (is_array($flash['message'])) + { + $msg = '
'; + $msg .= ''; - foreach ($flash['message'] as $flash_message) { - $msg .= $flash_message . '
'; - } + foreach ($flash['message'] as $flash_message) + { + $msg .= $flash_message . '
'; + } - return $msg . '
'; - } else { - return '
' . $flash['message'] . '
'; - } - } + return $msg . '
'; + } + else + { + return "
+ {$flash['message']} +
"; + } + } + else if (isset($_SESSION["flash"]) && !empty($_SESSION["flash"])) + { + /** + * This code derived from: http://codeigniter.com/wiki/Simple_FlashNotice_helper/ + */ + + $flash = $_SESSION["flash"]; + unset($_SESSION["flash"]); + + $msg = ''; + foreach($flash as $key => $val) + { + $msg .= "
+ + $val +
"; + } + + return $msg; + } } /** - * Save provided message as a flash variable. + * Save provided message as a flash variable for display + * after a CodeIgniter redirect (redirect()). * * @access public * @param string @@ -67,7 +98,7 @@ function display_flash($name) * @param string * @param boolean */ -function set_flash($name, $message_type, $message, $redirect=FALSE) +function setflash($message, $message_type, $redirect = FALSE, $name = 'flash_message') { $CI =& get_instance(); $CI->session->set_flashdata($name, array('message_type' => $message_type, 'message' => $message)); @@ -76,5 +107,21 @@ function set_flash($name, $message_type, $message, $redirect=FALSE) redirect($redirect); } + +/** + * Save provided message as a flash variable for display + * after loading a view ($this->load->view()). + * Source: http://codeigniter.com/wiki/Simple_FlashNotice_helper/ + * + * @access public + * @param string + * @param string + */ +function setviewflash($error = "An error has occurred", $type = "error") +{ + $_SESSION["flash"][$type] = $error; +} + /* End of file flash_helper.php */ /* Location: ./application/helpers/flash_helper.php */ +?> \ No newline at end of file