<?php

class Sistema_AssessoriaEnderecoController extends Agro_Controller_Action {
	protected $_paginator = false;

	public function getModel() {
		return new Sistema_Model_AssessoriasEnderecos();
	}

	public function getForm() {
		return new Sistema_Form_AssessoriaEndereco();
	}

	public function indexAction() {
		$Assessoria = new Sistema_Model_Assessorias();
		$this->view->form = $this->getForm();

		$Row = $Assessoria->find($this->getRequest()->getParam('id'))->current();
		$this->view->title = "Gerência de Assessorias » {$Row->ds_nome_assessoria} » Endereços";
		$this->view->back = '/sistema/assessoria';
		$this->view->assessoria = $Row->id;

		if($this->getRequest()->isPost() && count($this->getRequest()->getPost())) {
			$Model = $this->getModel();
			if($Model->save($this->getRequest()->getPost())) {
				$this->view->showMessage(MSG_SUCCESS, 'success');
			} else {
				$this->view->showMessage(MSG_ERROR, 'error');
			}
		}
	}

	public function salvaenderecoAction() {
		$this->getHelper('ViewRenderer')->setNoRender();
		$this->getHelper('layout')->disableLayout();

		$Model = $this->getModel();
		try {
			if ($Model->save($this->getRequest()->getParams())) {
				$this->getResponse()->setBody('Endereço salvo com sucesso.');
			} else {
				$this->view->showMessage(MSG_ERROR, 'error');
			}
		} catch(Exception $e) {

		}
	}

	public function getFilter() {
		$where[] = array(
			'field' => 'id_assessoria',
			'operator' => '=',
			'value' => $this->getRequest()->getParam('id')
		);

		return $where;
	}
}