<?php

/**
 * Classe responsÁvel pelo
 * retorno do formulÁrio
 * à função hookpaymentreturn()
 */
class Moip_Form{
	public $_vars = array();
	public $_html = "";
	
	/**
	 * Obtém os dados pelo objeto $params
	 * 	e transoforma um Array Matricial
	 */	
	function get_data($params)
	{
		global $cookie;
							     
		$address 	= new Address(intval($params['objOrder']->id_address_invoice));
		$customer 	= new Customer(intval($cookie->id_customer));
		$DadosOrder = new Order($params['objOrder']->id);
		$DadosCart 	= new Cart($DadosOrder->id_cart);

		//var_dump($address);

		/** PUXANDO ID_CARTEIRA **/
		$conf 			= Configuration::getMultiple(array('MoIP_BUSINESS'));
		$businessPag 	= array_key_exists('business', $_POST) ? $_POST['business'] : (array_key_exists('MoIP_BUSINESS', $conf) ? $conf['MoIP_BUSINESS'] : '');
		$id_carteira 	= htmlentities($businessPag, ENT_COMPAT, 'UTF-8');
		/** FECHANDO ID_CARTEIRA **/

		/** PUXANDO O ANIVERSÁRIO **/		
		$birthday 	= $customer->birthday;
		$dia		= substr($birthday,8,2);
		$mes		= substr($birthday,5,2);
		$ano		= substr($birthday,0,4);
		/** FECHANDO O ANIVERSÁRIO */
	
		/** PUXANDO O SEXO **/
		$gender = $customer->id_gender;
		$genders = array(1 => 'M', 2 => 'F', 9 => '');
		/** FECHANDO O SEXO **/
		
		$valor		= Tools::convertPrice($params['total_to_pay'],$params['currencyObj']);
		$valor		= number_format($valor, 2, ',', '.');
		$valor		= str_replace(',','',$valor);
		$valor		= str_replace('.','',$valor);
		
		$this->_vars = array(
		
		'id_carteira' 				=> $id_carteira,
		'valor' 					=> $valor,
		'nome'						=> htmlentities(utf8_decode((Configuration::get('PS_SHOP_NAME')) )),
		'descricao'					=> htmlentities(utf8_decode((Configuration::get('PS_SHOP_NAME')) )),
		'id_transacao'				=> $params['objOrder']->id,
		'frete' 					=> 
		str_replace(".","",number_format($DadosCart->getOrderShippingCost(), 2, '.', '') ),
		'peso_compra' 				=> str_replace(".","",number_format($DadosCart->getTotalWeight(), 3, '.', '.') ),
		'pagador_nome' 				=> htmlentities(utf8_decode(($address->firstname . ' ' . $address->lastname) )),
		'pagador_email' 			=> htmlentities(utf8_decode(($customer->email) )),
		'pagador_telefone' 			=> htmlentities(utf8_decode(($address->phone) )),
		'pagador_logradouro'		=> htmlentities(utf8_decode(($address->address1))),
		'pagador_numero' 			=> htmlentities(utf8_decode(($address->address2) )),
		'pagador_complemento' 		=> '',
		'pagador_bairro' 			=> '',
		'pagador_cep' 				=> htmlentities(utf8_decode(($address->postcode) )),
		'pagador_cidade' 			=> htmlentities(utf8_decode(($address->city) )),
		'pagador_estado' 			=> '',
		'pagador_pais' 				=> htmlentities(utf8_decode(($address->country) )),
		'pagador_cpf' 				=> '',
		'pagador_celular' 			=> htmlentities(utf8_decode(($address->phone_mobile) )),
		'pagador_sexo' 				=> htmlentities(utf8_decode(($genders["$gender"]) )),
		'pagador_data_nascimento' 	=> htmlentities(utf8_decode(($dia.$mes.$ano) ))
		);
	}
	
	/**
	 * 	Constroi o form em um foreach
	 * 	da array _vars
	 */
	function _constroi_form($params)
	{
		$this->get_data($params);
		
		/** PRODUÇÃO **/
		$url = "https://www.moip.com.br/PagamentoMoIP.do";
		
		/** TESTES **/
		//$url = "https://desenvolvedor.moip.com.br/sandbox/PagamentoMoIP.do";
		
		/** PEGANDO O BOTÃO **/
		$MoIP					= new MoIP();
		$conf 					= Configuration::getMultiple(array('MoIP_BUTTON'));
		$button 				= array_key_exists('button', $_POST) ? $_POST['button'] : (array_key_exists('MoIP_BUTTON', $conf) ? $conf['MoIP_BUTTON'] : '');
		$btn 					= $MoIP->_botoes[$button];
		
		$array_construct = $this->_vars;
		
		$this->_html .= '
		<!-- FORMULÁRIO MoIP -->
		
		<form action="'.$url.'" method="POST" 
		target="moip" accept-charset="ISO-8859-1" />';
		
		foreach ($array_construct as $_name => $_value )
		{
			$this->_html .= '
			<input type="hidden" name="'.$_name.'" value="'.$_value.'">';
		} 	
		
		$this->_html .= '
		<center>';
		
		if( $button == 0 )
			$this->_html .=  '
			<input type="submit" value="'.$MoIP->textButton.'" class="exclusive_large" />';
			else
			$this->_html .=  '
			<input type="image" src="http://www.moip.com.br/imgs/buttons/'.$btn.'"  
		name="submit" alt="'.$MoIP->textButton.'" style="border: 0;" />';
			
		$this->_html .= '</center>
		</form>
		<!-- /FORMULÁRIO MoIP -->';
		
		return $this->_html;
	}
	
}

?>
