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

/**
 * This class is for rendering system information messages.
 *
 * @package    lnApp
 * @category   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);

		// If we are a CLI session, then we have no session
		if (PHP_SAPI !== 'cli')
			Session::instance()->set('sessionmsgs',self::$_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'))
			self::$_data = $msgs;

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

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

	/**
	 * Render this system message
	 *
	 * @see HTMLRender::render()
	 */
	protected function render() {
		$record = self::$_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 function render_all() {
		// Reload our message from the session
		if ($msgs = Session::instance()->get_once('sessionmsgs'))
			self::$_data = $msgs;

		return parent::render_all();
	}
}
?>