<?php defined('SYSPATH') or die('No direct access allowed.');

/**
 * This class is for rendering system information messages.
 *
 * @package    lnApp
 * @category   lnApp/Helpers
 * @author     Deon George
 * @copyright  (c) 2009-2013 Deon George
 * @license    http://dev.leenooks.net/license.html
 */
abstract class lnApp_SystemMessage extends HTMLRender {
	protected static $_data = array();
	protected static $_spacer = '';
	protected static $_c = 0;
	protected $_items = array('body','title','type');

	protected static $_required_keys = array('title','body','type');

	public function __call($name,$args=NULL) {
		parent::__call($name,$args);

		Session::instance()->set('sessionmsgs',static::$_data);
		return $this;
	}

	/**
	 * Add a system message to be rendered
	 *
	 * @param array System Message attributes
	 * @deprecated
	 */
	public static function add($msg,$prepend=FALSE) {
		if ($msgs = Session::instance()->get_once('sessionmsgs'))
			static::$_data = $msgs;

		parent::add($msg);
		static::$_c = count(static::$_data);

		// Save our messages in our session, so that we get them for redirects
		Session::instance()->set('sessionmsgs',static::$_data);
	}

	/**
	 * Render this system message
	 *
	 * @see HTMLRender::render()
	 */
	protected function render() {
		$record = static::$_data[$this->_x];

		$output = '';

		$output .= sprintf('<div class="alert %s">',empty($record['type']) ? '' : 'alert-'.$record['type']);
		$output .= '<button type="button" class="close" data-dismiss="alert">&times;</button>';
		switch ($record['type']) {
			case 'error':
				$output .= '<i class="icon-ban-circle"></i>';
				break;
			case 'success':
				$output .= '<i class="icon-check"></i>';
				break;
			case 'warning':
				$output .= '<i class="icon-warning-sign"></i>';
				break;
			case 'info':
			default:
				$output .= '<i class="icon-info-sign"></i>';
		}

		$output .= sprintf(' <strong>%s</strong>: %s',$record['title'],$record['body']);
		$output .= '</div> <!-- /alert -->';

		return $output;
	}

        /**
         * Render all of these items
         */
        public static function render_all() {
		// Reload our message from the session
		if ($msgs = Session::instance()->get_once('sessionmsgs'))
			static::$_data = $msgs;

		return parent::render_all();
        }

}
?>
