<?php

class Sinistro2_TipolaudopreliminarController extends Agro_Controller_Action {
	public function init()
	{
		$this->view->title = "Sinistro » Cadastro » Laudos Preliminares";
		$this->view->back = "/sinistro2/tipolaudopreliminar";
	}
	
	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_preliminares/');
					$file = $adapter->getFileInfo();
					
					if ($file["ds_nome_arquivo"] && $adapter->receive($file['ds_nome_arquivo']['name'])) {
						$params["ds_nome_arquivo"] = $file['ds_nome_arquivo']['name'];
					}
					
					$rowLaudo = $model->save($params);

					if ($rowLaudo) {
						$this->view->ds_nome_arquivo = $rowLaudo->ds_nome_arquivo;
						$this->view->showMessage(MSG_SUCCESS, 'success');
						$this->view->form->populate($rowLaudo->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());
				}
				
			} elseif ($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'];
			}
			
		} catch (Exception $e) {
			if ($e->getCode() == 1) {
				$this->view->showMessage($e->getMessage(), 'error');
			} else {
				$this->view->showMessage('Erro ao Tipo Laudo Preliminar', 'error');
			}
		}
	}
	
	public function setBack() {
		$this->view->back = '/sinistro2/tipolaudopreliminar';
	}
	
	public function getModel() {
		return new Sinistro2_Model_TipoLaudoPreliminar();
	}
	
	public function getForm() {
		return new Sinistro2_Form_TipoLaudoPreliminar();
	}
	
	public function getFilter() {
		$where = array();
		
		if ($this->getRequest()->getParam('ds_nome_tipo_laudo_preliminar')) {
			$where[] = array (
				'field' => 'ds_nome_tipo_laudo_preliminar',
				'operator' => 'ilike',
				'value' => '%'.$this->getRequest()->getParam('ds_nome_tipo_laudo_preliminar').'%'
			);
		}
		
		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;
		}
	}
}