<?php

class Comercializacao_RelacionarproponenteController extends Agro_Controller_Comercializacao {
	
	public function init() {
		$this->view->title = "Gerência de Comercializações » Relacionar Proponentes";
		
		$Session = new Zend_Session_Namespace('proponente');
		$this->view->back = '/comercializacao/proponente';
		$this->view->id_comercializacao = $Session->id_comercializacao;
	}
	
	public function settaxaAction() {
		$Session = new Zend_Session_Namespace('proponente');
		$Session->id_comercializacao = $this->getRequest()->getParam('id');
		
		$this->_redirect('/comercializacao/relacionarproponente');
	}
	
	public function indexAction() {
		$Model = new Comercializacao_Model_ComercializacoesProponente();
		
		if ($this->getRequest()->isPost()) {
			$this->save($this->getRequest()->getParams());
		}
		
		$Session = new Zend_Session_Namespace('proponente');
		$ModelTaxaProponentes = new Comercializacao_Model_RelacionarProponente();
		$arr = $this->getRequest()->getParams();
		$arr['id_comercializacao'] = $Session->id_comercializacao;
		$arr['in'] = 1;
		
		$this->view->paginator = $ModelTaxaProponentes->getData($arr);
	}
	
	public function save(array $post) {
		try {
			$Model = new Comercializacao_Model_RelacionarProponente();
			if (array_key_exists('id_proponente', $post)) {
				if (is_array($post['id_proponente'])) {
					foreach ($post['id_proponente'] as $proponente) {
						$data = array();
						$data['id_comercializacao'] = $post['id_comercializacao'];
						$data['id_proponente'] = $proponente;
						
						$Model->insert($data);
					}
				}
			}
			
			$this->view->showMessage(MSG_SUCCESS, 'success');
		} catch (Exception $e) {
			$this->view->showMessage(MSG_ERROR, 'error');
		}
	}
	
	public function getproponentesAction() {
		//$this->getHelper('ViewRenderer')->setNoRender();
		$this->getHelper('layout')->disableLayout();
		
		$Model = new Comercializacao_Model_RelacionarProponente();
		$arr = $this->getRequest()->getParams();
		$arr['in'] = 0;
		$this->view->paginator = $Model->getData($arr);
	}
	
	public function deleteAction() {
		$this->getHelper('ViewRenderer')->setNoRender();
		$this->getHelper('layout')->disableLayout();
		$Model = new Comercializacao_Model_RelacionarProponente();
		
		if ($Model->delete($this->getRequest()->getParam('id_comercializacao'), $this->getRequest()->getParam('id_proponente'))) {
			$this->view->message(MSG_SUCCESS, 'success', 'afterDelete()');
		} else {
			$this->view->message(MSG_ERROR, 'error');
		}
		flush();
		exit;
	}
}