<?php

include_once CARABI_DIR.'/includes/paragrafer.inc';

class smarty_paragrapher extends paragrafer {

	protected $templateN;
	protected $templateFN;
	protected $smarty;
	protected $tpl;



	public function __construct( $smarty, $template='default' ){
	        // TODO exit if no smarty
		$this->smarty = $smarty;
		$this->templateN = $template;
		$this->templateFN = "{$template}.tpl.html";
		$this->key = $key;
	}



	public function display(){
		// TODO
	}



	public function fetch(){
		$this->tpl = $this->smarty->createTemplate( $this->templateFN );
		
		//$this->tpl->registerPlugin( "function",
			//"get_paragrapherItems",
			//array( $this, '_smarty_function_get_paragrapherItems' ) );
		
		$this->redim();
		$this->tpl->assign('p', $this->items);
		
		return $this->tpl->fetch();
	}



	// Args: $caption, $value
	// Args: $caption, simpleProperty
	public function add_p( $caption, $value ){
		if( $value instanceof simpleProperty ){
			parent::add_p( $caption, $value->get_display() );
		}else{;
			parent::add_p( $caption, $value );
		}
	}



	public function add_pmulti( $caption, $values, $more_values=null ){
		if (!is_object($values)) {
			if (count($values) < 2 && empty($values[0])) {
				return;
			}
		}
		parent::add_pmulti( $caption, array_merge($this->getArray($values), $this->getArray($more_values) ) );
	}



	public function add_pmulticols( $caption, $values, $more_values=null ){
		if (!is_object($values)) {
			if (count($values) < 2 && empty($values[0])) {
				return;
			}
		}
		parent::add_pmulticols( $caption, array_merge($this->getArray($values), $this->getArray($more_values) ) );
	}



	public function add_content( $content ){
		$this->items []= array( 'SIMPLECONTENT', $content );
	}



	protected function getArray( $val ){
		if( $val instanceof simpleProperty ){
			if( $val->isMulti() ){
				$val = $val->get_values( 'DISPLAY' );
				$val = array_filter($val, function($v){
					return ($v==' ...'?false:true);
				});
			}else{
				if( $dummy = $val->get_DISPLAY() ){
					$val = array( $val->get_DISPLAY() );
				}else
					$val = array();
			}
		}elseif(empty($val)){
			$val = array();
		}elseif( is_scalar($val) ){
			if( is_string($val) ){
				$val = explode( ';', $val );
			}else{
				$val = array( $val );
			}
		}elseif( !is_array($val) ){
			$val = array();
		}

		return array_filter( $val, function($v){
			return !empty($v);
		} );
	}



	public function _smarty_function_get_paragrapherItems( $params, $tpl ){
		$assign = isset($params['assign']) ? $params['assign'] : 'p';
		$tpl->assign( $assign, $this->items );
	}



}




// TRASH ############################################################################################################



	/*protected function TFPrefix(){
		return "show_{$this->templateN}";
	}*/
	        /*// для 3RC4
		$this->tpl = $this->smarty->createTemplate( $this->templateFN );
		//TODO почему функция регистрируется в смарте а не в тэмплэйте ?
		$this->smarty->register->templateFunction( "show_{$this->templateN}", array( $this, 'show_paragrafer' ) );
		return $this->tpl->fetch();
		*/

		/*// для >3.0.4
		$this->tpl = $this->smarty->createTemplate( $this->templateFN );
		$this->tpl->registerPlugin( "function",
			"show_{$this->templateN}",
			array( $this, 'show_paragrafer' ) );
		return $this->tpl->fetch();
		*/

	/*public function add_map( $simpleDocument, $allow_map_provider_choose=false, $parent_simpleDocument=null ){
		$this->items []= array( 'MAP', $simpleDocument, ($allow_map_provider_choose?true:false), $parent_simpleDocument );
	}*/

	/*protected function getPropDisplay( $simpleProperty ){
		if( $simpleProperty->isMulti() ){
			$val = $simpleProperty->get_values( 'DISPLAY' );
			$val = array_filter($val, function($v){
				return ($v==' ...'?false:true);
			});
		}else{
			if( $dummy = $simpleProperty->get_DISPLAY() ){
				$val = array( $val->get_DISPLAY() );
			}else
				$val = array();
		}
	}*/
	/*public function do_paragrapher( $params, $content, $template, &$repeat ){
		static $p_i;
		$params['use_box'] = $params['use_box'] ? true : false;
		$assign = isset($params['assign']) ? $params['assign'] : 'p';

		if( empty($content) ){
			// do init
			$p_i = 0;

			//assign first;
			$template->assign( $assign, $this->items[$p_i] );
		}else{
			// assign next
			$repeat = ( ++$p_i < count($this->items) );
			$template->assign( $assign, $this->items[$p_i] );

			// return
			return $content;
		}

	}*/

	/*public function show_paragrafer( $vars, $smarty ){
		$use_box = $vars['use_box'];
		if($use_box)
			$is_inbox=false;
		else
			$is_inbox=true;
		// TODO Проверку на существование функций
		for( $x=0, $y=count( $this->items ); $x<$y; $x++){

			if( !$is_inbox && $this->items[$x][0] == 'H1'){
				$is_inbox = true;
				$this->smarty->callTemplateFunction(
					"{$this->TFPrefix()}_box_begin",
					array( 'item'=>$this->items[$x] ),
					$this->tpl
				);
			}elseif( $is_inbox ){
				switch( $this->items[$x][0] ){

					case 'H1':
						if( $use_box ){
							$this->smarty->callTemplateFunction( "{$this->TFPrefix()}_box_end", null, $this->tpl );
							$is_inbox=false;

							$this->smarty->callTemplateFunction(
								"{$this->TFPrefix()}_box_begin",
								array( 'item'=>$this->items[$x] ),
								$this->tpl
							);
							$is_inbox=true;
						}
	
					break;

					case 'H2':
						$this->smarty->callTemplateFunction(
							"{$this->TFPrefix()}_title",
							array( 'item'=>$this->items[$x] ),
							$this->tpl
						);
					break;

					case 'P':
						$this->smarty->callTemplateFunction(
							"{$this->TFPrefix()}_text",
							array( 'item'=>$this->items[$x] ),
							$this->tpl
						);
					break;

					case 'DIVIDER':
						$this->smarty->callTemplateFunction(
							"{$this->TFPrefix()}_divider",
							array( 'item'=>$this->items[$x] ),
							$this->tpl
						);
					break;

					case 'P_MULTI':
						$this->smarty->callTemplateFunction(
							"{$this->TFPrefix()}_p_multi",
							array( 'item'=>$this->items[$x] ),
							$this->tpl
						);
					break;

					case 'P_MULTICOLS':
						$this->smarty->callTemplateFunction(
							"{$this->TFPrefix()}_p_multicols",
							array( 'item'=>$this->items[$x] ),
							$this->tpl
						);
					break;

					case 'SIMPLECONTENT':
						$this->smarty->callTemplateFunction(
							"{$this->TFPrefix()}_simplecontent",
							array( 'item'=>$this->items[$x] ),
							$this->tpl
						);
					break;
				}
			}
		}
		if( $use_box && $is_inbox ){
			$this->smarty->callTemplateFunction( "{$this->TFPrefix()}_box_end", null, $this->tpl );
			$is_inbox=false;
		}
	}/**/
/*

		$this->tpl->registerPlugin( "block",
			"paragrapher",
			array( $this, 'do_paragrapher' ) );

*/
?>