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

/**
 * This class is for rendering HTML blocks or widget.
 *
 * It will provide a title and body for the content.
 *
 * @package    lnApp
 * @category   Helpers
 * @author     Deon George
 * @copyright  (c) 2009-2013 Deon George
 * @license    http://dev.leenooks.net/license.html
 * @uses       Style
 */
abstract class lnApp_Block extends HTMLRender {
	protected static $_data = array();
	protected static $_spacer = '';
	protected static $_c = 0;
	protected $_items = array('body','id','scrollable','span','title','title_icon','type');

	protected static $_required_keys = array('body');

	/**
	 * Add a block to be rendered
	 *
	 * @param array Block attributes
	 * @deprecated
	 */
	public static function add($block,$prepend=FALSE) {
		// Any body objects should be converted to a string, so that any calls to Style/Script are processed
		if (isset($block['body']))
			$block['body'] = (string)$block['body'];

		parent::add($block);

		// Detect any style sheets.
		if (! empty($block['style']) && is_array($block['style']))
			foreach ($block['style'] as $data=>$media)
				Style::add(array(
					'type'=>'file',
					'data'=>$data,
					'media'=>$media,
				));
	}

	/**
	 * Render this Block
	 */
	protected function render() {
		$record = self::$_data[$this->_x];

		$output = '';
		$output .= sprintf('<div class="col-md-%s %s">',Arr::get($record,'span',12),Arr::get($record,'scrollable',''));
		$output .= '<div class="widget stacked">';

		if (! empty($record['title']))
			$output .= sprintf('<div class="widget-header"><i class="fa %s"></i><h3>%s</h3></div>',Arr::get($record,'title_icon','fa-square'),Arr::get($record,'title','NO Title!'));

		$output .= '<div class="widget-content">';

		if (! empty($record['type']))
			switch ($record['type']) {
				case 'form': $output .= Form::open(NULL,(empty($record['id']) ? NULL : array('id'=>$record['id'])));
					$output .= (string)$record['body'];
					$output .= Form::close();
					break;

				case 'form-horizontal': $output .= Form::open(NULL,Arr::merge(array('class'=>'form-horizontal','role'=>'form','enctype'=>'multipart/form-data','data-toggle'=>'validator'),(empty($record['id']) ? array() : array('id'=>$record['id']))));
					$output .= (string)$record['body'];
					$output .= Form::close();
					break;

				default:
					$output .= (string)$record['body'];
			}

		else
			$output .= (string)$record['body'];

		$output .= '</div> <!-- /widget-content -->';
		$output .= '</div> <!-- /widget-stacked -->';
		$output .= '</div> <!-- /col-md -->';

		return $output;
	}
}
?>