<?php

class Sistema_CategoriaController extends Agro_Controller_Action {
	public $_columnList = array(
		'ds_nome_categoria' => 'Nome',
		'ds_chave' => 'Chave'
	);
	
	public function init() {
		$this->view->title = 'Sistema » Cadastro » Categorias';
	}
	
	public function setBack() {
		$this->view->back = '/sistema/categoria';
	}
	
	public function getModel() {
		return new Sistema_Model_Categorias();
	}
	
	public function getForm() {
		return new Sistema_Form_Categoria();
	}
	
	public function getFilter() {
		$where = array();
		
		if ($this->getRequest()->getParam('ds_nome_categoria')) {
			$where[] = array (
				'field' => 'ds_nome_categoria',
				'operator' => 'ilike',
				'value' => '%'.$this->getRequest()->getParam('ds_nome_categoria').'%'
			);
		}
		
		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 beforeDelete() {
		$Empresa = new Sistema_Model_Empresas();
		$Empresa->delete($this->getRequest()->getParam('id'), 'id_empresa');
	}
}