<?php

class Sistema_BairroController extends Agro_Controller_Action {
	public $_columnList = array(
		'ds_nome_bairro' => 'Nome',
		'ds_nome_municipio' => 'Municipio',
		'ds_nome_estado' => 'Estado'
	);
	
	public function init() {
		$this->view->title = "Sistema » CEP's » Bairros";
	}
	
	public function setBack() {
		$this->view->back = '/sistema/bairro';
	}
	
	public function getForm() {
		return new Sistema_Form_Bairro();
	}
	
	public function getModel() {
		return new Sistema_Model_Bairros();
	}
	
	public function getFilter() {
		$where = array();
		
		if ($this->getRequest()->getParam('ds_nome_bairro')) {
			$where[] = array(
				'field' => 'ds_nome_bairro',
				'operator' => 'ilike',
				'value' => '%'.$this->getRequest()->getParam('ds_nome_bairro').'%'
			);
		}
		
		if ($this->getRequest()->getParam('id_estado')) {
			$where[] = array(
				'field' => 'id_estado',
				'operator' => '=',
				'value' => $this->getRequest()->getParam('id_estado')
			);
		}
		
		if ($this->getRequest()->getParam('id_municipio')) {
			$where[] = array(
				'field' => 'id_municipio',
				'operator' => '=',
				'value' => $this->getRequest()->getParam('id_municipio')
			);
		}
		
		if ($this->getRequest()->getParam('fl_ativo')) {
			$where[] = array(
				'field' => 'fl_ativo',
				'operator' => '=',
				'value' => $this->getRequest()->getParam('fl_ativo')
			);
		}
		return $where;
	}
	
	public function getbairrosAction() {
		$this->getHelper('ViewRenderer')->setNoRender();
		$this->getHelper('layout')->disableLayout();
		
		if ($this->getRequest()->getParam('id')) {
			$Bairro = new Sistema_Model_Bairros();
			$bairros = $Bairro->getCombo($this->getRequest()->getParam('id'));
			
			$opt = array();
			if (is_array($bairros)) {
				foreach ($bairros as $key => $text) {
					$opt[] = array('id'=>$key,'label'=>$text);
				}
			}
			
			$Json = new Zend_Json();
			$this->getResponse()->setBody($Json->encode($opt));
		}
	}	
	
	
	
}