<?php

class Produto_TiposoloController extends Agro_Controller_Action {
	public $_columnList = array(
		'ds_tipo_solo' => 'Tipo',
		'ds_chave' => 'Chave',
		'ds_descricao_solo' => 'Descrição'
	);
	
	public function init() {
		$this->view->title = 'Produto » Cadastro » Tipo de Solo';
	}
	
	public function getModel() {
		return new Produto_Model_Tiposolo();
	}
	
	public function getForm() {
		return new Produto_Form_Tiposolo();
	}
	
	public function getFilter() {
		$where = array();
		
		if ($this->getRequest()->getParam('ds_tipo_solo')) {
			$where[] = array(
				'field' => 'ds_tipo_solo',
				'operator' => 'ilike',
				'value' => '%'.$this->getRequest()->getParam('ds_tipo_solo').'%'
			);
		}
		if ($this->getRequest()->getParam('ds_chave')) {
			$where[] = array(
				'field' => 'ds_chave',
				'operator' => 'ilike',
				'value' => '%'.$this->getRequest()->getParam('ds_chave').'%'
			);
		}
		if ($this->getRequest()->getParam('ds_descricao_solo')) {
			$where[] = array(
				'field' => 'ds_descricao_solo',
				'operator' => 'ilike',
				'value' => '%'.$this->getRequest()->getParam('ds_descricao_solo').'%'
			);
		}
		
		if($this->getRequest()->getParam('fl_ativo')) {
			$where[] = array(
				'field' => 'fl_ativo',
				'operator' => ' = ',
				'value' =>  $this->getRequest()->getParam('fl_ativo')
			);
		}
		
		return $where;
	}
	
	public function save(array $post) {
		try {
			$Model = $this->getModel();

			
			$data = array();
			$data['ds_tipo_solo'] = $post['ds_tipo_solo'];
			$data['ds_chave'] = $post['ds_chave'];
			$data['ds_descricao_solo'] = $post['ds_descricao_solo'];

			if ($post['id']) {
				$Model->update($data, "id = {$post['id']}");
			} else {
				$Model->insert($data);
			}
			
			$this->_redirect('/produto/tiposolo');
		} catch (Exception $e) {
			$this->view->showMessage(MSG_ERROR, 'error', $e);
		}
	}

}