<?php

class Sinistro2_TipolaudofinalController extends Agro_Controller_Action {
	public function init() {
		$this->view->title = "Sinistro » Cadastro » Laudos Finais";
		$this->view->back = "/sinistro2/tipolaudofinal";
	}
	
	public function formAction(){
		try {
			$this->view->form = $this->getForm();
			$this->view->title .= ' » Formulário';
			$Model = $this->getModel();
			$id=false;
			
			if ($this->getRequest()->isPost()) {
				if ($this->view->form->isValid($this->getRequest()->getPost())) {
					$params = $this->getRequest()->getParams();

					$Adapter = new Zend_File_Transfer_Adapter_Http();
					$Adapter->setDestination(APPLICATION_PATH.'/upload_files/planilhas_laudos_finais/');
					$file = $Adapter->getFileInfo();
					
					if ($file["ds_nome_arquivo"]){	
						if($Adapter->receive($file['ds_nome_arquivo']['name'])){
							$params["ds_nome_arquivo"] = $file['ds_nome_arquivo']['name'];
						} 
					}

					$Row = $Model->save($params);						
					
					if ($Row) {
						$this->view->ds_nome_arquivo = $Row->ds_nome_arquivo;
						$this->view->showMessage(MSG_SUCCESS, 'success');
						$this->view->form->populate($Row->toArray());
						
					} else {
						if ($Model->getMessages()) {
							$this->view->showMessage($Model->getMessages(), 'error');
						} else {
							$this->view->showMessage(MSG_ERROR, 'error');
						}
						
						$this->view->form->populate($this->getRequest()->getParams());
					}
					
					
				} else {
					$Errors = $this->view->form->getMessages();
					$this->view->showMessage($this->getHtmlFormError($Errors), 'error');
					$this->view->form->populate($this->getRequest()->getParams());
				}
				
			} else if ($this->getRequest()->getParam('id')) {
				$id = $this->getRequest()->getParam('id');
			}
			
			if ($id){
				$row = $this->getRow();
				$this->view->form->populate($row);
				$this->view->ds_nome_arquivo = $row["ds_nome_arquivo"];
				$this->view->can_import_xml = $row['fl_import_xml'];
			}
			
		} catch (Exception $e) {
			$this->view->showMessage($e->getMessage(), 'error');
		}
	}
	
	public function setBack() {
		$this->view->back = '/sinistro2/tipolaudofinal';
	}
	
	public function getModel() {
		return new Sinistro2_Model_TipoLaudoFinal();
	}
	
	public function getForm() {
		return new Sinistro2_Form_TipoLaudoFinal();
	}
	
	public function getFilter() {
		$where = array();
		
		if ($this->getRequest()->getParam('ds_nome_tipo_laudo_final')) {
			$where[] = array (
				'field' => 'ds_nome_tipo_laudo_final',
				'operator' => 'ilike',
				'value' => '%'.$this->getRequest()->getParam('ds_nome_tipo_laudo_final').'%'
			);
		}
		
		if ($this->getRequest()->getParam('ds_chave')) {
			$where[] = array (
				'field' => 'ds_chave',
				'operator' => 'ilike',
				'value' => '%'.$this->getRequest()->getParam('ds_chave').'%'
			);
		}
		
		if ($this->getRequest()->getParam('fl_ativo')) {
			$where[] = array (
				'field' => 'fl_ativo',
				'operator' => '=',
				'value' => $this->getRequest()->getParam('fl_ativo')
			);
		}
		
		return $where;
	}
	
	public function showAction(){
		$this->getHelper('layout')->disableLayout();
		
		$Model = $this->getModel();
		
		try {
			$Row = $Model->find($this->getRequest()->getParam("id"))->current();
			if ($Row){
				$this->view->params = $Row->toArray();	
			} else {
				$this->view->params = false;
			}
			
		} catch (Exception $e) {
			$this->view->params = false;
		}
	}
}