<?php

class ControllerExtensionTotalTrackingDiscount extends Controller {
	private $error = array();
	private $name = 'tracking_discount';

	private $_events = array(
		array('customer_form_t', 'admin/view/customer/customer_form/before',  'extension/total/tracking_discount/event/eventCustomerFormTBefore'),		
		array('admin_get_affiliate', 'admin/model/customer/customer/getAffiliate/after', 'extension/total/tracking_discount/event/eventGetAffiliate'),
		array('admin_get_affiliate_by_tracking', 'admin/model/customer/customer/getAffliateByTracking/after', 'extension/total/tracking_discount/event/eventGetAffiliate'),		
		array('admin_delete_customer', 'admin/model/customer/customer/deleteCustomer/before', 'extension/total/tracking_discount/event/eventDeleteAffiliate'),		
		array('admin_add_customer', 'admin/model/customer/customer/addCustomer/after', 'extension/total/tracking_discount/event/eventAddEditAffiliate'),
		array('admin_edit_customer', 'admin/model/customer/customer/editCustomer/after', 'extension/total/tracking_discount/event/eventAddEditAffiliate'),
	);		
	
	
	public function install() {
		$this->load->model('extension/tracking_discount');
		$this->model_extension_tracking_discount->install();
		
		$this->addPermissions();

		$this->load->model('setting/event');
		foreach($this->_events as $_e) {
			if(!$this->model_setting_event->getEventByCode('total_' . $this->name . '_' . $_e[0])) $this->model_setting_event->addEvent('total_' . $this->name . '_' . $_e[0], $_e[1], $_e[2]);
		}		
	}	
	
	public function index() {
		$data = $this->load->language('extension/total/' . $this->name);
		$data['text_fixed'] = sprintf($data['text_fixed'], $this->config->get('config_currency'));

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

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

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validate())) {
			if (!$this->user->hasPermission('modify', 'extension/total/' . $this->name)) $this->response->redirect($this->url->link('error/permission', 'user_token=' . $this->session->data['user_token'], true));			
			$p = array();
			foreach($this->request->post as $_k => $_v) {
				if(strpos($_k, $this->name) === 0) $p['total_' . $_k] = $_v;
			}
			$this->model_setting_setting->editSetting('total_' . $this->name, $p);

			$this->session->data['success'] = $this->language->get('text_success');

			$this->response->redirect($this->url->link('marketplace/extension', 'type=total&user_token=' . $this->session->data['user_token'], true));
		}

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

   		$data['breadcrumbs'] = array();

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

   		$data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_total'),
			'href'      => $this->url->link('marketplace/extension', 'type=total&user_token=' . $this->session->data['user_token'], true),
      		'separator' => ' :: '
   		);

   		$data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
		'href'      => $this->url->link('extension/total/' . $this->name, 'user_token=' . $this->session->data['user_token'], true),
      		'separator' => ' :: '
   		);

   		$this->document->breadcrumbs[] = array(
       		'text'      => $this->language->get('heading_title'),
       		'href'      => $this->url->link('extension/total/' . $this->name, 'user_token=' . $this->session->data['user_token'], true),
      		'separator' => ' :: '
   		);

		$data['action'] = $this->url->link('extension/total/' . $this->name, 'user_token=' . $this->session->data['user_token'], true);

		$data['cancel'] = $this->url->link('marketplace/extension', 'type=total&user_token=' . $this->session->data['user_token'], true);
		
		foreach(array('amount', 'type', 'fixed_max', 'fixed_per_item', 'when', 'period', 'what', 'no_coupon', 'status', 'sort_order', 'apply_to') as $_v) {
			$_v = $this->name . '_' . $_v;
			if (isset($this->request->post[$_v])) {
				$data[$_v] = $this->request->post[$_v];
			} else {				
				$data[$_v] = $this->config->get('total_' . $_v);
				if($_v == $this->name . '_sort_order' && !$data[$_v]) $data[$_v] = (intval($this->config->get('sub_total_sort_order'))) + 1;
			}		
		}

		$this->load->model('localisation/tax_class');

		$data['tax_classes'] = $this->model_localisation_tax_class->getTaxClasses();
		$_whats = array('total', 'all_all', 'all_one', 'min_all', 'min_one', 'max_all', 'max_one');
		$data['whats'] = array();
		foreach($_whats as $_what) {
			$data['whats'][$_what] = $data['text_what_' . $_what];
		}
		$data['header'] = $this->load->controller('common/header');
		$data['column_left'] = $this->load->controller('common/column_left');
		$data['footer'] = $this->load->controller('common/footer');

		$this->response->setOutput($this->load->view('extension/total/' . $this->name, $data));
	}

	private function validate() {		
		if (!$this->user->hasPermission('modify', 'extension/total/' . $this->name)) {
			$this->error['warning'] = $this->language->get('error_permission');
		} else {		
			$errors = array(
				'amount' => "/^\d+(?:\.\d+)?$/",
				'fixed_max' => "/^\d+(?:\.\d+)?$/",
				'period' => "/^\d+$/",
				'sort_order' => "/^\d+$/"
			);
			$required = array('amount');
		
			$_errors = array();
			foreach($errors as $k => $preg) {
				if(!in_array($k, $required) && !strlen($this->request->post[$this->name . '_' . $k])) continue;
				if(!preg_match($preg, $this->request->post[$this->name . '_' . $k])) $_errors[] = $this->language->get('error_' . $k);
			}
		
			if(sizeof($_errors) > 0) $this->error['warning'] = implode('<br />', $_errors);
		}		
		return !$this->error;
	}
	
	public function uninstall() {	
		if (!$this->user->hasPermission('modify', 'extension/total/' . $this->name)) return;		
		$this->load->model('setting/setting');
		$this->model_setting_setting->deleteSetting('total_' . $this->name);		
		
		$this->load->model('setting/event');
		foreach($this->_events as $_e) {
			$this->model_setting_event->deleteEventByCode('total_' . $this->name . '_' . $_e[0]);
		}		
	}	
	
	protected function addPermissions() {
		$this->load->model('user/user_group');
		$routes = array('extension/total/' . $this->name);
		foreach($routes as $route) {
			foreach(array('access', 'modify') as $perm_type) {
				if(!$this->user->hasPermission($perm_type, $route)) {
					$this->model_user_user_group->addPermission($this->user->getGroupId(), $perm_type, $route);				
				}
			}
		}
	}	
	
}

