<?php

class Solicitacao_DepartamentoController extends Agro_Controller_Action {
	
	public function init() {
		$this->view->title = 'Solicitações';
		
		$Departamentos = new Solicitacao_Model_Departamentos();
		$this->view->departamento = $Departamentos->getCombo(); 
		
		$Tarefas = new Solicitacao_Model_Tarefa();
		$this->view->status = $Tarefas->getComboStatus('SOLICITACAO');
	}
	
	public function getModel() { return new Solicitacao_Model_Departamentos(); }
	
	public function getForm() { return new Solicitacao_Form_Departamento(); }
	
	public function indexAction() {
		$this->view->title .= ' » Solicitações do Departamento';
		$this->view->form = $this->getForm();		
	}
	
	public function tarefasAction() {
		$this->isAjax(true);

		$Departamentos = new Solicitacao_Model_Departamentos();
		$this->view->departamentos = $Departamentos->fetchTarefasDepartamento();
	}
	
	public function listAction() {

		$this->getHelper('layout')->disableLayout();

		$Model = $this->getModel();
		
		$this->view->departamentoUsuario = $Model->getDepartamentoUsuarioLogado();
		
		$where = $this->getFilter();

		// Habilita ou Desabilita a paginacao pelo combo na tela
		$this->_paginator = $this->getRequest()->getParam('show_paginacao');
		
		$recordsPerPage = ($this->recordsPerPage ? $this->recordsPerPage :  RECORDS_PER_PAGE);

		$Rs = $Model->fetchAllPaginator($where, $this->getRequest()->getParam('page'), $recordsPerPage, $this->_paginator);	

		$this->view->paginator = $Rs;
	}	
	
	public function getFilter() {
		$where = array();

		if ($this->getRequest()->getParam('id')) {
			$where[] = array (
					'field' => 't.id',
					'operator' => '=',
					'value' => $this->getRequest()->getParam('id')
			);
			
		}else{
		
			if ($this->getRequest()->getParam('id_departamento')) {
				$where[] = array (
					'field' => 't.id_departamento',
					'operator' => '=',
					'value' => $this->getRequest()->getParam('id_departamento')
				);
			}
	
			if ($this->getRequest()->getParam('id_tarefa_tipo')) {
				$where[] = array (
					'field' => 'id_tarefa_tipo',
					'operator' => '=',
					'value' => $this->getRequest()->getParam('id_tarefa_tipo')
				);
			}		
	
			if ($this->getRequest()->getParam('ds_titulo')) {
				$where[] = array (
					'field' => 'ds_titulo',
					'operator' => 'ilike',
					'value' => '%'.$this->getRequest()->getParam('ds_titulo').'%'
				);
			}		
			
			if ($this->getRequest()->getParam('id_prioridade')) {
				$where[] = array (
					'field' => 'id_prioridade',
					'operator' => '=',
					'value' => $this->getRequest()->getParam('id_prioridade')
				);
			}
	
			if ($this->getRequest()->getParam('id_usuario_criador')) {
				$where[] = array (
					'field' => 't.id_usuario_criacao',
					'operator' => '=',
					'value' => $this->getRequest()->getParam('id_usuario_criador')
				);
			}		
	
			if ($this->getRequest()->getParam('id_usuario_solicitante')) {
				$where[] = array (
					'field' => 'cf.id_usuario',
					'operator' => '=',
					'value' => $this->getRequest()->getParam('id_usuario_solicitante')
				);
			}	
	
			if ($this->getRequest()->getParam('id_usuario_delegado') > 1) {
				$where[] = array (
					'field' => 'id_executor',
					'operator' => '=',
					'value' => $this->getRequest()->getParam('id_usuario_delegado')
				);
				
			}else if($this->getRequest()->getParam('id_usuario_delegado') == 1){
				$where[] = array (
					'field' => 'id_executor',
					'operator' => 'is',
					'value' => 'null'
				);			
			}					
	
			if ($this->getRequest()->getParam('id_status')) {
				$where[] = array (
					'field' => 's.id',
					'operator' => '=',
					'value' => $this->getRequest()->getParam('id_status')
				);
			}
				
		}
		return $where;
	}	

}