<?php

class Produto_TaxaController extends Zend_Controller_Action {
	public $Db;
	public $Tipo;
	
	/**
	 * Construtor da classe
	 *
	 */
	public function init() {
		$this->view->title = 'Gerência de Tipos de Produto';
		$this->Tipo = new Produto_Model_Tipos();
	}
	
	/**
	 * Tela principal
	 *
	 */
	public function indexAction() {
		$this->view->form = new Produto_Form_Tipo();
	}
	
	/**
	 * Listagem dos Dados
	 *
	 */
	public function listAction() {
		$this->getHelper('layout')->disableLayout();
		
		$where = '';
		if ($this->getRequest()->isPost()) {
			$where[] = array(
				'field' => 'ds_tipo_produto',
				'operator' => 'ilike',
				'value' => '%'.$this->getRequest()->getParam('ds_tipo_produto').'%'
			);
			
			if ($this->getRequest()->getParam('fl_ativo')) {
				$where[] = array(
					'field' => 'fl_ativo',
					'operator' => '=',
					'value' => $this->getRequest()->getParam('fl_ativo')
				);
			}
		}
		
		$this->view->paginator = $this->Tipo->fetchAllPaginator($where, $this->getRequest()->getParam('page'));
	}
	
	/**
	 * Formulário de Cadastro
	 *
	 */
	public function formAction() {
		$this->view->form = new Produto_Form_Tipo();
		$this->view->title .= ' » Cadastro';
		
		if ($this->getRequest()->getParam('id')) {
			$Row = $this->Tipo->find($this->getRequest()->getParam('id'))->current()->toArray();
			$this->view->form->populate($Row);
		}
	}
	
	/**
	 * Método para salvar o registro no banco
	 *
	 */
	public function saveAction() {
		$this->getHelper('layout')->disableLayout();
		$this->getHelper('ViewRenderer')->setNoRender();
		
		$form = new Produto_Form_Tipo();
		
		if ($this->getRequest()->isPost()) {
			$post = $this->getRequest()->getPost();
			
			if ($form->isValid($post)) {
				if ($this->Tipo->save($post)) {
					$this->view->message(MSG_SUCCESS, 'success');
				} else {
					$this->view->message(MSG_ERROR, 'error');
				}
			} else {
				$errors = $form->getMessages();
				$fields = array();
				if (is_array($errors)) {
					foreach ($errors as $field => $error) {
						$fields['error'][] = $field;
					}
				}
				
				$this->view->message(MSG_FORM_ERROR, 'error', $fields);
			}
		}
	}
	
	/**
	 * Método para deletar um registro
	 *
	 */
	public function deleteAction() {
		$this->getHelper('layout')->disableLayout();
		$this->getHelper('ViewRenderer')->setNoRender();
		
		if ($this->Tipo->delete($this->getRequest()->getParam('id'))) {
			$this->view->message(MSG_SUCCESS, 'success');
		} else {
			$this->view->message(MSG_ERROR, 'error');
		}
	}
	
	/**
	 * Ativar/Desativar o registro
	 *
	 */
	public function activeAction() {
		$this->getHelper('ViewRenderer')->setNoRender();
		$this->getHelper('layout')->disableLayout();
		if ($this->getRequest()->getParam('id')) {
			$Row = $this->Tipo->find($this->getRequest()->getParam('id'))->current()->toArray();
			$data = array();
			
			if ($Row['fl_ativo']) {
				$data['fl_ativo'] = 0;
				$img = "inactive.png";
			} else {
				$data['fl_ativo'] = 1;
				$img = "active.png";
			}
			
			if ($this->Tipo->update($data, "id=".$this->getRequest()->getParam('id'))) {
				$this->view->message(MSG_SUCCESS, 'success', $img);
			} else {
				$this->view->message(MSG_ERROR, 'error');
			}
		}
	}
}
