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

	public function index() {
	
		$module = 'be2bill';
/*		
	if((VERSION == '1.4.7')xor(VERSION == '1.4.8')xor(VERSION == '1.4.8b')){
  $redirect = HTTPS_SERVER . 'index.php?route=extension/payment';
  $href_home = HTTPS_SERVER . 'index.php?route=common/home';
  $href_ext = HTTPS_SERVER . 'index.php?route=extension/payment';
  $href_pay = HTTPS_SERVER . 'index.php?route=payment/'.$module;
  }else{
  $redirect = HTTPS_SERVER . 'index.php?route=extension/payment&token=' . $this->session->data['token'];
  $href_home = HTTPS_SERVER . 'index.php?route=common/home&token=' . $this->session->data['token'];
  $href_ext = HTTPS_SERVER . 'index.php?route=extension/payment&token=' . $this->session->data['token'];
  $href_pay = HTTPS_SERVER . 'index.php?route=payment/'.$module.'&token=' . $this->session->data['token'];
  }	
*/	
//   VERSION >= '1.5.0'
  
  $redirect = $this->url->link('extension/payment', 'token=' . $this->session->data['token'], 'SSL');
  $href_home = $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL');
  $href_ext = $this->url->link('extension/payment', 'token=' . $this->session->data['token'], 'SSL');
  $href_pay = $this->url->link('payment/be2bill', 'token=' . $this->session->data['token'], 'SSL');  


		$this->load->language('payment/be2bill');

		$this->document->setTitle($this->language->get('heading_title'));
				
		$this->load->model('setting/setting');
			
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validate())) {
			$this->load->model('setting/setting');
			
			$this->model_setting_setting->editSetting('be2bill', $this->request->post);				
			
			$this->session->data['success'] = $this->language->get('text_success');

			$this->redirect($redirect);
		}

		$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['text_all_zones'] = $this->language->get('text_all_zones');
		$this->data['text_yes'] = $this->language->get('text_yes');
		$this->data['text_no'] = $this->language->get('text_no');
		$this->data['text_authorization'] = $this->language->get('text_authorization');
		$this->data['text_sale'] = $this->language->get('text_sale');
		
		$this->data['entry_site'] = $this->language->get('entry_site');
		$this->data['entry_dsecure'] = $this->language->get('entry_dsecure');

		$this->data['entry_hcltemail'] = $this->language->get('entry_hcltemail');
		$this->data['entry_hcardname'] = $this->language->get('entry_hcardname');
		
		$this->data['entry_test'] = $this->language->get('entry_test');		
		$this->data['entry_merchant_key'] = $this->language->get('entry_merchant_key');		
		$this->data['entry_order_status'] = $this->language->get('entry_order_status');	
		$this->data['entry_geo_zone'] = $this->language->get('entry_geo_zone');
		$this->data['entry_status'] = $this->language->get('entry_status');
		$this->data['entry_sort_order'] = $this->language->get('entry_sort_order');
		
		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');

		$this->data['tab_general'] = $this->language->get('tab_general');

 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		
 		if (isset($this->error['site'])) {
			$this->data['error_site'] = $this->error['site'];
		} else {
			$this->data['error_site'] = '';
		}

		if (isset($this->error['merchant_key'])) { 
			$this->data['error_merchant_key'] = $this->error['merchant_key'];
		} else {
			$this->data['error_merchant_key'] = '';
		}

		$this->data['breadcrumbs'] = array();
//		$this->document->breadcrumbs = array();

   		$this->data['breadcrumbs'][] = array(
//   		$this->document->breadcrumbs[] = array(
       		'href'      => $href_home,
       		'text'      => $this->language->get('text_home'),
      		'separator' => FALSE
   		);

   		$this->data['breadcrumbs'][] = array(
//   		$this->document->breadcrumbs[] = array(
       		'href'      => $href_ext,
       		'text'      => $this->language->get('text_payment'),
      		'separator' => ' :: '
   		);

   		$this->data['breadcrumbs'][] = array(
//   		$this->document->breadcrumbs[] = array(
       		'href'      => $href_pay,
       		'text'      => $this->language->get('heading_title'),
      		'separator' => ' :: '
   		);
				
		$this->data['action'] = $href_pay;
		
		$this->data['cancel'] = $href_ext;
		
		if (isset($this->request->post['be2bill_site'])) {
			$this->data['be2bill_site'] = $this->request->post['be2bill_site'];
		} else {
			$this->data['be2bill_site'] = $this->config->get('be2bill_site');
		}

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

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

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

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

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

		$this->load->model('localisation/order_status');
		
		$this->data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses();
		
		if (isset($this->request->post['be2bill_geo_zone_id'])) {
			$this->data['be2bill_geo_zone_id'] = $this->request->post['be2bill_geo_zone_id'];
		} else {
			$this->data['be2bill_geo_zone_id'] = $this->config->get('be2bill_geo_zone_id'); 
		} 
		
		$this->load->model('localisation/geo_zone');
										
		$this->data['geo_zones'] = $this->model_localisation_geo_zone->getGeoZones();
		
		if (isset($this->request->post['be2bill_status'])) {
			$this->data['be2bill_status'] = $this->request->post['be2bill_status'];
		} else {
			$this->data['be2bill_status'] = $this->config->get('be2bill_status');
		}
		
		if (isset($this->request->post['be2bill_sort_order'])) {
			$this->data['be2bill_sort_order'] = $this->request->post['be2bill_sort_order'];
		} else {
			$this->data['be2bill_sort_order'] = $this->config->get('be2bill_sort_order');
		}
		
		$this->template = 'payment/be2bill.tpl';
		$this->children = array(
			'common/header',	
			'common/footer'	
		);
		
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
	}

	private function validate() {
		if (!$this->user->hasPermission('modify', 'payment/be2bill')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}
		
		if (!$this->request->post['be2bill_site']) {
			$this->error['site'] = $this->language->get('error_site');
		}

		if (!$this->request->post['be2bill_key']) {
			$this->error['merchant_key'] = $this->language->get('error_merchant_key');
		}
		
		if (!$this->error) {
			return TRUE;
		} else {
			return FALSE;
		}	
	}
}
?>
