<?php 

class AppPdf{
	public $filename;
	public $pdf_renderer;
	
	protected $_parametros;
	
	protected $_vista_cabecera = 'cabecera';
	protected $_vista_pie = 'pie';
	protected $_vista = 'defecto';
	
	
	public function __construct($parametros = array())
	{
		$this->pdf_renderer = new PDFGenerator(false);
		$this->_parametros = $parametros;
	
	
	}
	
	public function setParametro($name, $value){
		$this->_parametros[$name] = $value;
	}
	
	public function getParametro($name){
		if(isset($this->_parametros[$name]))
		return $this->_parametros[$name];
	}
	
	public function __set($name, $value){
		$this->setParametro($name, $value);
	
	}
	public function __get($name){
		return $this->getParametro($name);
	}
	
	public function setVista($vista){
		$this->_vista = $vista;
	}
	
	public function setVistaCabecera($vista){
		$this->_vista_cabecera = $vista;
	}
	
	public function setVistaPie($vista){
		$this->_vista_pie = $vista;
	}
	
	public function render(){
		$html = '';
		$html.= $this->renderCabecera()
		.$this->renderCuerpo()
		.$this->renderPie();
	}
	
	public function renderEnNavegador(){
		$this->generaPdf();
		if (ob_get_level() && ob_get_length() > 0)
			ob_clean();
		return $this->pdf_renderer->render($this->filename, 'I');
	}
	public function descarga(){
		$this->generaPdf();
		if (ob_get_level() && ob_get_length() > 0)
			ob_clean();
		return $this->pdf_renderer->render($this->filename, 'D');
		
	}
	public function generaFichero($name){
		$this->generaPdf();
		return $this->pdf_renderer->render($name, 'F');
	}
	
	protected function generaPdf(){
		if (empty($this->filename))
		{
			$this->filename = 'informe.pdf';
			if (count($this->objects) > 1)
				$this->filename = $template->getBulkFilename();
		}
		$this->pdf_renderer->createHeader($this->renderCabecera());
		$this->pdf_renderer->createFooter($this->renderPie());
		$this->pdf_renderer->createContent($this->renderCuerpo());
		
		$this->pdf_renderer->writePage();
		$render = true;
		
			// clean the output buffer
		
		
		
	}
	
	protected function renderCabecera(){
		return $this->renderTemplate($this->_vista_cabecera);
	}
	protected function renderPie(){
		return $this->renderTemplate($this->_vista_pie);
	}
	protected function renderCuerpo(){
		return $this->renderTemplate($this->_vista);
	}
	protected function renderTemplate($vista){
		ob_start();
		$normal = APPLICATION_PATH . 'templates/pdf/'.$vista . '.phtml';
		if(is_file($normal))
			include $normal;
		else{
			include $over;
		}
		$salida = ob_get_clean();
		return $salida;
	}
	
	
}

