Skip to content

Instantly share code, notes, and snippets.

@NaszvadiG
Forked from itspriddle/ci_flash_helper.php
Created April 17, 2014 08:29
Show Gist options
  • Save NaszvadiG/10964592 to your computer and use it in GitHub Desktop.
Save NaszvadiG/10964592 to your computer and use it in GitHub Desktop.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter
*
* An open source application development framework for PHP 4.3.2 or newer
*
* @package CodeIgniter
* @author Rick Ellis
* @copyright Copyright (c) 2006, EllisLab, Inc.
* @license http://www.codeignitor.com/user_guide/license.html
* @link http://www.codeigniter.com
* @since Version 1.0
* @filesource
*/
/**
* Flash Helper
*
* Uses the native Session/Flashdata functions from CI 1.6 to display
* 'Flash Notices'
*
* @package CodeIgniter
* @subpackage Helpers
* @category Helpers
* @author Joshua Priddle <josh.priddle@hostrocket.com>
* @version 1.0
*/
class flash {
/**
* Add Message to Flash Notice log
*
* Takes a string and appends it to the _FlashNotice array
*
* NOTE: There are also individual helper functions for warning and success messages
*
* @static
* @access public
* @param string $message to be displayed
* @return void
*/
static function add($message, $type = 'warning')
{
$CI =& get_instance();
if ($CI->session->userdata("{$CI->session->flashdata_key}:new:NOTICE") !== FALSE)
{
$tmp = $CI->session->userdata("{$CI->session->flashdata_key}:new:NOTICE");
$FN = (is_array($tmp) ? $tmp : array($tmp));
}
else
{
$FN = array();
}
$FN[] = array($type => $message);
$CI->session->set_flashdata(array('NOTICE' => $FN));
}
// ------------------------------------------------------------------------
/**
* Add a warning message
*
* @static
* @access public
* @param string $message to be displayed
* @return void
*/
static function warning($message)
{
self::add($message, 'warning');
}
// ------------------------------------------------------------------------
/**
* Add a success message
*
* @static
* @access public
* @param string $message to be displayed
* @return void
*/
static function success($message)
{
self::add($message, 'success');
}
// ------------------------------------------------------------------------
/**
* Display Flash Notice Messages
*
* Displays the HTML for the Flash Notice Message if any messages have been stored
*
* @static
* @access public
* @param array $layout HTML to use for output (optional)
* @return void
*/
static function display($template = NULL)
{
$CI =& get_instance();
$FN = $CI->session->flashdata('NOTICE');
if ($FN === FALSE) return;
if ($template == NULL)
{
$template = self::_default_template();
}
$out = $template['header'];
foreach ($FN as $F)
{
foreach ($F as $type => $msg)
{
$msg = trim($msg);
if (substr($msg, 0, 3) != '<p>' && substr($msg, -4) != '</p>' && strpos($msg, '<p>') === FALSE)
{
$msg = "<p>$msg</p>";
}
$out .= str_ireplace("{type}", $type, $template['m_header']);
$out .= str_ireplace("{message}", $msg, $template['content']);
$out .= $template['m_footer'];
}
}
$out .= $template['footer'];
echo $out;
}
// ------------------------------------------------------------------------
/**
* Default Template
*
* Provides a default template for use with flash notices.
*
* @static
* @access public
* @param array $layout HTML to use for output (optional)
* @return void
*/
private function _default_template()
{
$layout_default['header'] = "<div id=\"warnings\">\n";
// The overall footer HTML for the Flash Notice
$layout_default['footer'] = "</div>\n";
// The header HTML for each message
// Use {type} to print the notice type
$layout_default['m_header'] = "\t<div class=\"{type}\">\n";
// The content HTML for each message
// Use {message} to print the message
$layout_default['content'] = "{message}\n";
// The footer HTML for each message
$layout_default['m_footer'] = "\t</div>\n";
return $layout_default;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment