<?php
//----------------------------------------------//
// Multiple Flat Rate Options
// Version 1.2 - 2014.09.12
// Copyright 2012 - 2014 Nyco Agung
//----------------------------------------------//

class ControllerShippingFlatoptions extends Controller {
	private $error = array(); 	
	public function index() {   
		$this->language->load('shipping/flatoptions');
		$this->document->setTitle($this->language->get('heading_title'));		
		$this->load->model('setting/setting');
		//2014.09.12
		$this->load->model('localisation/language');
		$this->data['languages'] = $this->model_localisation_language->getLanguages();
				
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			//DO UPDATE
			$this->data['flatoptions_total'] = count($this->request->post['flatoptions_condition']);
			for($i=1;$i<=$this->data['flatoptions_total'];$i++){
				//$this->request->post['flatoptions_title_'.$i] = $this->request->post['flatoptions_title'][($i-1)];
				foreach($this->data['languages'] as $languages){
					$this->request->post['flatoptions_title_'.$languages['language_id'].'_'.$i] = $this->request->post['flatoptions_title'][$languages['language_id']][($i-1)];
				}				
				$this->request->post['flatoptions_condition_'.$i] = $this->request->post['flatoptions_condition'][($i-1)];
				$this->request->post['flatoptions_min_'.$i] = $this->request->post['flatoptions_min'][($i-1)];
				$this->request->post['flatoptions_max_'.$i] = $this->request->post['flatoptions_max'][($i-1)];
				$this->request->post['flatoptions_min_item_'.$i] = $this->request->post['flatoptions_min_item'][($i-1)];
				$this->request->post['flatoptions_max_item_'.$i] = $this->request->post['flatoptions_max_item'][($i-1)];
				$this->request->post['flatoptions_min_weight_'.$i] = $this->request->post['flatoptions_min_weight'][($i-1)];
				$this->request->post['flatoptions_max_weight_'.$i] = $this->request->post['flatoptions_max_weight'][($i-1)];
				$this->request->post['flatoptions_cost_'.$i] = $this->request->post['flatoptions_cost'][($i-1)];
				$this->request->post['flatoptions_tax_class_id_'.$i] = $this->request->post['flatoptions_tax_class_id'][($i-1)];
				$this->request->post['flatoptions_geo_zone_id_'.$i] = $this->request->post['flatoptions_geo_zone_id'][($i-1)];
				$this->request->post['flatoptions_enabled_'.$i] = $this->request->post['flatoptions_enabled'][($i-1)];
				$this->request->post['flatoptions_order_'.$i] = $this->request->post['flatoptions_order'][($i-1)];
			}
			
			
			unset($this->request->post['flatoptions_title']);
			unset($this->request->post['flatoptions_condition']);
			unset($this->request->post['flatoptions_min']);
			unset($this->request->post['flatoptions_max']);
			unset($this->request->post['flatoptions_min_item']);
			unset($this->request->post['flatoptions_max_item']);
			unset($this->request->post['flatoptions_min_weight']);
			unset($this->request->post['flatoptions_max_weight']);
			unset($this->request->post['flatoptions_cost']);
			unset($this->request->post['flatoptions_tax_class_id']);
			unset($this->request->post['flatoptions_geo_zone_id']);
			unset($this->request->post['flatoptions_enabled']);
			unset($this->request->post['flatoptions_order']);
			
			$this->model_setting_setting->editSetting('flatoptions', $this->request->post);							
			$this->session->data['success'] = $this->language->get('text_success');						
		  	$this->redirect($this->url->link('extension/shipping', 'token=' . $this->session->data['token'], 'SSL'));
		}
				
		$this->data['heading_title'] = $this->language->get('heading_title');
		$this->data['version_no'] = $this->language->get('version_no');
		$this->data['instructions'] = $this->language->get('instructions');
		$this->data['text_custom_title'] = $this->language->get('text_custom_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_none'] = $this->language->get('text_none');
		
		$this->data['entry_title'] = $this->language->get('entry_title');
		$this->data['entry_condition'] = $this->language->get('entry_condition');
		$this->data['entry_condition_pleaseselect'] = $this->language->get('entry_condition_pleaseselect');
		$this->data['entry_condition_byorder'] = $this->language->get('entry_condition_byorder');
		$this->data['entry_condition_byitem'] = $this->language->get('entry_condition_byitem');
		$this->data['entry_condition_byweight'] = $this->language->get('entry_condition_byweight');
		$this->data['entry_condition_byorderitem'] = $this->language->get('entry_condition_byorderitem');
		$this->data['entry_condition_byorderweight'] = $this->language->get('entry_condition_byorderweight');
		$this->data['entry_condition_byitemweight'] = $this->language->get('entry_condition_byitemweight');
		$this->data['entry_order'] = $this->language->get('entry_order');
		$this->data['entry_item'] = $this->language->get('entry_item');		
		$this->data['entry_weight'] = $this->language->get('entry_weight');		
		$this->data['entry_min'] = $this->language->get('entry_min');
		$this->data['entry_max'] = $this->language->get('entry_max');
		$this->data['entry_cost'] = $this->language->get('entry_cost');
		$this->data['entry_tax_class'] = $this->language->get('entry_tax_class');
		$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['entry_add'] = $this->language->get('entry_add');
		$this->data['entry_remove'] = $this->language->get('entry_remove');
		
		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');

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

  		$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_shipping'),
			'href'      => $this->url->link('extension/shipping', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('shipping/flatoptions', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		
		$this->data['action'] = $this->url->link('shipping/flatoptions', 'token=' . $this->session->data['token'], 'SSL');		
		$this->data['cancel'] = $this->url->link('extension/shipping', 'token=' . $this->session->data['token'], 'SSL');
		
		//GET NUMBER OF FLATOPTIONS
		$query = $this->db->query("SELECT count('key') as total FROM " . DB_PREFIX . "setting as setting WHERE setting.key like 'flatoptions_condition_%'");		
		$this->data['flatoptions_total'] = $query->row['total'];
		$this->data['flatoptions_custom_title'] = $this->config->get('flatoptions_custom_title');
		$this->data['flatoptions_sort_order'] = $this->config->get('flatoptions_sort_order');
		$this->data['flatoptions_status'] = $this->config->get('flatoptions_status');
		
		//2014.09.12
		for($i=1;$i<=$this->data['flatoptions_total'];$i++){
			$this->data['flatoptions_condition_'.$i] = $this->config->get('flatoptions_condition_'.$i);
			$this->data['flatoptions_min_'.$i] = $this->config->get('flatoptions_min_'.$i);
			$this->data['flatoptions_max_'.$i] = $this->config->get('flatoptions_max_'.$i);
			$this->data['flatoptions_min_item_'.$i] = $this->config->get('flatoptions_min_item_'.$i);
			$this->data['flatoptions_max_item_'.$i] = $this->config->get('flatoptions_max_item_'.$i);
			$this->data['flatoptions_min_weight_'.$i] = $this->config->get('flatoptions_min_weight_'.$i);
			$this->data['flatoptions_max_weight_'.$i] = $this->config->get('flatoptions_max_weight_'.$i);
			$this->data['flatoptions_cost_'.$i] = $this->config->get('flatoptions_cost_'.$i);
			$this->data['flatoptions_tax_class_id_'.$i] = $this->config->get('flatoptions_tax_class_id_'.$i);
			$this->data['flatoptions_geo_zone_id_'.$i] = $this->config->get('flatoptions_geo_zone_id_'.$i);
			$this->data['flatoptions_enabled_'.$i] = $this->config->get('flatoptions_enabled_'.$i);
			$this->data['flatoptions_order_'.$i] = $this->config->get('flatoptions_order_'.$i);
			foreach($this->data['languages'] as $languages){
				$this->data['flatoptions_title_'.$languages['language_id'].'_'.$i] = $this->config->get('flatoptions_title_'.$languages['language_id'].'_'.$i);
			}
		}		
		
		
		$this->load->model('localisation/tax_class');		
		$this->data['tax_classes'] = $this->model_localisation_tax_class->getTaxClasses();		
		$this->load->model('localisation/geo_zone');		
		$this->data['geo_zones'] = $this->model_localisation_geo_zone->getGeoZones();								
		$this->template = 'shipping/flatoptions.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);				
		$this->response->setOutput($this->render());
	}
	
	private function validate() {
		if (!$this->user->hasPermission('modify', 'shipping/flatoptions')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}		
		if (!$this->error) {
			return true;
		} else {
			return false;
		}	
	}
}
?>