<?php

class Produto_NomenclaturaController extends Agro_Controller_Action {
	public $_columnList = array(
		'ds_nomenclatura' => 'Nome'
	);
	
	public function init() {
		$this->view->title = 'Produto » Cadastro » Nomenclaturas';
	}
	
	public function getModel() {
		return new Produto_Model_Nomenclaturas();
	}
	
	public function getForm() {
		return new Produto_Form_Nomenclatura();
	}
	
	public function getFilter() {
		$where = array();
		
		if ($this->getRequest()->getParam('ds_nomenclatura')) {
			$where[] = array(
				'field' => 'ds_nomenclatura',
				'operator' => 'ilike',
				'value' => '%'.$this->getRequest()->getParam('ds_nomenclatura').'%'
			);
		}
		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_nomenclatura'] = $post['ds_nomenclatura'];
			$data['ds_descricao_nomenclatura'] = $post['ds_descricao_nomenclatura'];
			
			if ($post['id']) {
				$Model->update($data, "id = {$post['id']}");
			} else {
				$Model->insert($data);
			}
			
			$this->_redirect('/produto/nomenclatura');
		} catch (Exception $e) {
			$this->view->showMessage(MSG_ERROR, 'error');
		}
	}
}
