<?php
class ControllerModulemoduloFutura extends Controller {
	private $error = array(); 

	public function index() {   
		$this->load->language('module/modulofutura');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('setting/setting');

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validate()) ) {
			$this->model_setting_setting->editSettingModuloFutura('modulofutura', $this->request->post);
			
			$this->session->data['success'] = $this->language->get('text_success');

			$this->redirect($this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'));
		}

		$this->data['heading_title']      = $this->language->get('heading_title');
		$this->data['text_enabled']       = $this->language->get('text_enabled');
		$this->data['text_disabled']      = $this->language->get('text_disabled');
		$this->data['entry_status']       = $this->language->get('entry_status');
		$this->data['button_save']        = $this->language->get('button_save');
		$this->data['button_cancel']      = $this->language->get('button_cancel');
		$this->data['entry_order_status_pagseguro']   = $this->language->get('entry_order_status_pagseguro');
		$this->data['entry_order_status_mercadopago'] = $this->language->get('entry_order_status_mercadopago');
		$this->data['entry_order_status_cielo'] = $this->language->get('entry_order_status_cielo');
		$this->data['entry_order_status'] = $this->language->get('entry_order_status');
		$this->data['entry_order_status_cancel'] = $this->language->get('entry_order_status_cancel');
		$this->data['language_default']   = $this->language->get('language_default');
		$this->data['store_default']      = $this->language->get('store_default');
		$this->data['unidade_cm']         = $this->language->get('unidade_cm');
		$this->data['unidade_peso_kg']    = $this->language->get('unidade_peso_kg');
		$this->data['text_ftpconf']       = $this->language->get('text_ftpconf');
		
		$this->data['text_ServerFTP']     = $this->language->get('text_ServerFTP');
		$this->data['text_UserFTP']       = $this->language->get('text_UserFTP');
		$this->data['text_senhaFTP']      = $this->language->get('text_senhaFTP');
		$this->data['text_PortaFTP']      = $this->language->get('text_PortaFTP');
		$this->data['text_diretorioftp']  = $this->language->get('text_diretorioftp');
		$this->data['text_Grlconf']       = $this->language->get('text_Grlconf');
								
		
		$this->data['link'] = $this->url->link('catalog/product/update', 'token=' . $this->session->data['token'], 'SSL');

		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		}else{
			$this->data['error_warning'] = '';
		}
		
		if (isset($this->error['futura_serverftp'])) {
			$this->data['error_serverftp'] = $this->error['futura_serverftp'];
		} else {
			$this->data['error_serverftp'] = '';
		}
		
		if (isset($this->error['futura_userftp'])) {
			$this->data['error_userftp'] = $this->error['futura_userftp'];
		} else {
			$this->data['error_userftp'] = '';
		}
		
		if (isset($this->error['futura_senhaftp'])) {
			$this->data['error_senhaftp'] = $this->error['futura_senhaftp'];
		} else {
			$this->data['error_senhaftp'] = '';
		}

		$this->data['breadcrumbs'] = array();
		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
			'separator' => false
		);

		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_module'),
			'href'      => $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'),
			'separator' => ' :: '
		);

		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('module/modulofutura', 'token=' . $this->session->data['token'], 'SSL'),
			'separator' => ' :: '
		);

		$this->data['action'] = $this->url->link('module/modulofutura', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['cancel'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL');
		
		// chama a view
		$this->template = 'module/modulofutura.tpl';
		$this->children = array(
			'common/header',
			'common/footer',
		);	


		if (isset($this->request->post['select_order_status_id'])) {
			$this->data['select_order_status_id'] = $this->request->post['select_order_status_id'];
		} else {
			$this->data['select_order_status_id'] = $this->config->get('select_order_status_id');
		} 
		
		if (isset($this->request->post['select_order_status_pagseguro_id'])) {
			$this->data['select_order_status_pagseguro_id'] = $this->request->post['select_order_status_pagseguro_id'];
		} else {
			$this->data['select_order_status_pagseguro_id'] = $this->config->get('select_order_status_pagseguro_id');
		} 
		
		if (isset($this->request->post['select_order_status_mercadopago_id'])) {
			$this->data['select_order_status_mercadopago_id'] = $this->request->post['select_order_status_mercadopago_id'];
		} else {
			$this->data['select_order_status_mercadopago_id'] = $this->config->get('select_order_status_mercadopago_id');
		} 
		
		if (isset($this->request->post['select_order_status_cielo_id'])) {
			$this->data['select_order_status_cielo_id'] = $this->request->post['select_order_status_cielo_id'];
		} else {
			$this->data['select_order_status_cielo_id'] = $this->config->get('select_order_status_cielo_id');
		} 

		if (isset($this->request->post['select_order_status_canc_id'])) {
			$this->data['select_order_status_canc_id'] = $this->request->post['select_order_status_canc_id'];
		} else {
			$this->data['select_order_status_canc_id'] = $this->config->get('select_order_status_canc_id');
		} 				
		
		if (isset($this->request->post['select_store_default'])) {
			$this->data['select_store_default'] = $this->request->post['select_store_default'];
		} else {
			$this->data['select_store_default'] = $this->config->get('select_store_default');
		} 		
		
		if (isset($this->request->post['select_language_default'])) {
			$this->data['select_language_default'] = $this->request->post['select_language_default'];
		} else {
			$this->data['select_language_default'] = $this->config->get('select_language_default');
		} 
		
		if (isset($this->request->post['select_peso'])) {
			$this->data['select_peso'] = $this->request->post['select_peso'];
		} else {
			$this->data['select_peso'] = $this->config->get('select_peso');
		} 
		
		if (isset($this->request->post['select_comprimento'])) {
			$this->data['select_comprimento'] = $this->request->post['select_comprimento'];
		} else {
			$this->data['select_comprimento'] = $this->config->get('select_comprimento');
		} 
		
		if (isset($this->request->post['futura_serverftp'])) {
			$this->data['futura_serverftp'] = $this->request->post['futura_serverftp'];
		} else {
			$this->data['futura_serverftp'] = $this->config->get('futura_serverftp');
		} 
		
		if (isset($this->request->post['futura_userftp'])) {
			$this->data['futura_userftp'] = $this->request->post['futura_userftp'];
		} else {
			$this->data['futura_userftp'] = $this->config->get('futura_userftp');
		} 
		
		if (isset($this->request->post['futura_senhaftp'])) {
			$this->data['futura_senhaftp'] = $this->request->post['futura_senhaftp'];
		} else {
			$this->data['futura_senhaftp'] = $this->config->get('futura_senhaftp');
		} 	

		if (isset($this->request->post['futura_portaftp'])) {
			$this->data['futura_portaftp'] = $this->request->post['futura_portaftp'];
		} else {
			$this->data['futura_portaftp'] = $this->config->get('futura_portaftp');
		} 	

		if (isset($this->request->post['futura_diretorioftp'])) {
			$this->data['futura_diretorioftp'] = $this->request->post['futura_diretorioftp'];
		} else {
			$this->data['futura_diretorioftp'] = $this->config->get('futura_diretorioftp');
		}
		
		$this->load->model('localisation/order_status');		
		$this->data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses();		
		
		$this->load->model('localisation/language');
		$this->data['languages'] = $this->model_localisation_language->getLanguages();
		
		$this->load->model('localisation/weight_class');		
		$this->data['weight_classes'] = $this->model_localisation_weight_class->getWeightClasses();
		
		$this->load->model('setting/store');		
		$this->data['stores'] = $this->model_setting_store->getStores();
		
		$this->load->model('localisation/length_class');		
		$this->data['length_classes'] = $this->model_localisation_length_class->getLengthClasses();

		$this->response->setOutput($this->render());
	}
	
	private function validate() {
		if (!$this->user->hasPermission('modify', 'module/modulofutura')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}	

		if (!$this->request->post['futura_serverftp']) {
			$this->error['futura_serverftp'] = $this->language->get('error_serverftp');
		}
		
		if (!$this->request->post['futura_userftp']) {
			$this->error['futura_userftp'] = $this->language->get('error_userftp');
		}
		
		if (!$this->request->post['futura_senhaftp']) {
			$this->error['futura_senhaftp'] = $this->language->get('error_senhaftp');
		}
		
		if (!$this->error) {
			return true;
		} else {
			return false;
		}
				
	}    
}
?>