<?php 
class AppFormularioDinamico extends AppFormulario{
	
	public function __construct($options = null)
	{
		if (is_array($options)) {
			$this->setOptions($options);
		} elseif ($options instanceof Zend_Config) {
			$this->setConfig($options);
		}
	
	
		// Extensions...
		$this->init();
		
	}
	public function regenera(){
		$this->init();
		$this->aplicarEstilo();
	}
	
	public function init(){
		parent::init();
		
		$this->accion->setValue('guardarl');
		
		$this->setElementosEnLinea(true);
		$this->setLabelSuperior(false);
		//$evento_formulario = new EVRefrescaGrid(EVJquery::APP_EVENTO_SUBMIT);
		//$this->addEvento($evento_formulario);
		
		
		
	}
	
	
	protected function aplicarEstilo(){
	
	
	
		$this->generaDecoradores();
		
			$this->addAttribs(array('class'=> 'form-inline form_grid'));
		
	
		$this->aplicarEstiloFormulario();
	
	}
	
	public function generaDecoradores(){
		$this->dec_form_normal=array(
				'FormElements',
				'Form'
	
		);
	
	
	
		$getId = create_function('$decorator',
				'return $decorator->getElement()->getId()
                                             . "-element";');
			
		$getId = create_function('$content, $element, array $options',
				'return $element->getId();');
			
	
	
		$this->dec_elemento_normal = array(
				'ViewHelper',
				'Errors',
				array(array('data'=>'HtmlTag'),array('tag'=>'div', 'class'=>'controls')),
			
		);
	
	
	
		$this->dec_fichero_normal = array(
				'File',
				'Errors',
				array(array('data'=>'HtmlTag'),array('tag'=>'div', 'class'=>'controls')),
				array(array('labelDtClose' => 'HtmlTag'), array('tag' => 'label', 'closeOnly' => true, 'placement' => 'prepend')),
				'Label',
				array(array('labelDtOpen' => 'HtmlTag'), array('tag' => 'label', 'openOnly' => true, 'placement' => 'prepend', 'class' => 'control-label')),
				array('Description', array('tag' => 'a', 'class' => 'description', 'data-toggle'=>"tooltip", 'title'=>"first tooltip")),
				array(array('row'=>'HtmlTag'),array('tag'=>'div', 'class'=>'control-group'))
		);
	
	
	
	
		$this->dec_elementocheck_normal = array(
				'ViewHelper',
				'Errors',
				array(array('data'=>'HtmlTag'),array('tag'=>'div', 'class'=>'controls')),
		);
	
	
	
	
		$this->dec_submit_normal = array(
				'ViewHelper',
				'Errors',
				array(array('data'=>'HtmlTag'),array('tag'=>'div', 'class'=>'controls')),
				array(array('row'=>'HtmlTag'),array('tag'=>'div', 'class'=>'control-group'))
		);
	
	
	}
	
	
	public function guardarlAccion($datos){
	
		$objeto = $this->getObjeto();
		
		if($objeto instanceof AppObjeto){
			$id = $_REQUEST[$objeto->getPrimary()];
			$objeto->load($id);
			if($this->isValid($datos)){
		
				
				
		
				$valores = $this->getValues();
		
				$objeto->setDatos($_POST);
				$objeto->save();
				$ficheros = array();
		
				foreach ($this->getElements() as $element){
					if($element instanceof EFFichero){
						$valor = $element->recibir($objeto->id);
						if($valor){
							$ficheros[$element->getName()] = $valor;
						}
		
					}
				}
				if(count($ficheros)>0){
					$objeto->setDatos($ficheros);
					$objeto->save();
				}
		
				
				AppAjax::setValores($objeto->nombreId(), $objeto->id);
		
				/*if(isset($_REQUEST['destino_autocomplete'])&& $_REQUEST['destino_autocomplete']){
					AppAjax::setValores($_REQUEST['destino_autocomplete'], $objeto->id);
				}elseif(strlen($this->_url_traseditar)>0){
					AppAjax::setRedirigir(BASEURL . $this->_url_traseditar);
				}*/
		
		
			}else{
				foreach ($this->getElements() as $element){
					if($element instanceof AppElementoFormulario){
						if(count($element->getMessages()>1))
						foreach($element->getMessages() as $mensaje){
							AppAjax::setMensajeError($element->identificadorScript(), $mensaje);
						}
					}
						
				}
			}
		}
	}
	
	
	public function render(Zend_View_Interface $view = null){
	
		$html = '';
		$html .= '<form id="'.$this->getName().'"';
		foreach ($this->getAttribs() as $key => $value){
				
			$html .= ' '.$key.'="'.$value.'" ';
		}
		$html .= ' method="'.$this->getMethod().'" ';
	
		$html .= ' action="'.$this->getAction().'" ';
		$html .= ' enctype="'.$this->getEnctype().'"';
		$html .='>';
	
	
		foreach($this->_elementos as $elemento){
			
			$html .= '<div class="controls">'.$elemento->renderInput().'</div>';
		}
		$html .='</form>';
		$html .= '';
		return $html;
	
	
	}
	
}