<?php

class ControllerExtensionModuleCouponForTrackingEvent extends Controller {

	public function eventDeleteCoupon(&$route, &$args) {
		if($this->config->get('module_coupon_for_tracking_status')) {
			$coupon_id = is_array($args) ? $args[0] : $args;
			if(preg_match("/^\d+$/", $coupon_id) && $coupon_id) {
				$this->load->model('extension/coupon_for_tracking');
				$this->model_extension_coupon_for_tracking->edit($coupon_id);
			}
		}		
	}
	
	public function eventCouponFormTBefore(&$route, &$data, &$template) {
		if (!$this->config->get('module_coupon_for_tracking_status')) {	
			$data['module_coupon_for_tracking_status'] = false;
			return;
		}
		$this->load->language('extension/module/coupon_for_tracking');
		$data['module_coupon_for_tracking_status'] = true;
		$data['text_none'] = $this->language->get('text_none');
		$data['entry_affiliate'] = $this->language->get('entry_affiliate');
		$data['help_affiliate'] = $this->language->get('help_affiliate');
		$data['text_clear'] = $this->language->get('text_clear');
		$this->load->model('extension/coupon_for_tracking');		

		if (isset($this->request->post['coupon_for_tracking_affiliate'])) {
			$data['coupon_for_tracking_affiliate'] = $this->request->post['coupon_for_tracking_affiliate'];
		} else if (isset($this->request->request['coupon_id'])) {
			$data['coupon_for_tracking_affiliate'] = $this->model_extension_coupon_for_tracking->getCouponAssoc($this->request->request['coupon_id']);
		} else {
			$data['coupon_for_tracking_affiliate'] = '0';
		}		
		
		if($data['coupon_for_tracking_affiliate']) {
			$_c = explode('_', $data['coupon_for_tracking_affiliate']);
			if(sizeof($_c) < 2) array_unshift($_c, 'a');
			if($_c[0] == 'a') {
				$this->load->model('customer/customer');
				$_aff = $this->model_customer_customer->getCustomer($_c[1]);
				$data['coupon_for_tracking_affiliate_name'] = $_aff ? ($_aff['firstname'] . ' ' . $_aff['lastname'] . '(' . $_aff['email'] . ')') : '';
			} else {
				$this->load->model('extension/coupon_for_tracking');
				$_res = $this->model_extension_coupon_for_tracking->getMarketing($_c[1]);
				$data['coupon_for_tracking_affiliate_name'] = $_res ? $_res['name'] : '';
			}
		} else {
			$data['coupon_for_tracking_affiliate_name'] = '';
		}				
	}
	
	public function eventGetCoupon(&$route, &$args, &$output) {
		if($this->config->get('module_coupon_for_tracking_status') && is_array($output) && isset($output['coupon_id'])) {
			$coupon_id = $output['coupon_id'];
			$this->load->model('extension/coupon_for_tracking');
			$output['coupon_for_tracking_affiliate'] = $this->model_extension_coupon_for_tracking->getCouponAssoc($coupon_id);
		}
	}
	
	public function eventAddCoupon(&$route, &$args, &$output) {
		if($this->config->get('module_coupon_for_tracking_status')) {
			$coupon_id = $output;
			if(!$coupon_id || !preg_match("/^\d+$/", $coupon_id)) return;
			if(isset($args[0]) && isset($args[0]['coupon_for_tracking_affiliate'])) {
				$aid = $args[0]['coupon_for_tracking_affiliate'];
			} else if(isset($args['coupon_for_tracking_affiliate'])) {
				$aid = $args['coupon_for_tracking_affiliate'];
			} else {
				$aid = false;
			}
			$this->load->model('extension/coupon_for_tracking');
			if(isset($aid)) {
				$this->model_extension_coupon_for_tracking->edit($coupon_id, $aid);
			} else {
				$this->model_extension_coupon_for_tracking->edit($coupon_id);
			}		
		}		
	}

	public function eventEditCoupon(&$route, &$args) {
		if($this->config->get('module_coupon_for_tracking_status')) {
			$coupon_id = $args[0];
			$data = $args[1];
			
			$this->load->model('extension/coupon_for_tracking');
			if(is_array($data) && isset($data['coupon_for_tracking_affiliate'])) {
				$this->model_extension_coupon_for_tracking->edit($coupon_id, $data['coupon_for_tracking_affiliate']);
			} else {
				$this->model_extension_coupon_for_tracking->edit($coupon_id);
			}			
		}
	}
	
}
