<?php
class ControllerExtensionModuleTrackingInput extends Controller {

	public function index($setting = false) {
		if(!$setting || !$this->config->get('module_tracking_input_status') || $setting['language_id'] != $this->config->get('config_language_id') || (isset($this->session->data['tracking_input_show']) && !$this->session->data['tracking_input_show']) || ($this->config->get('module_tracking_input_no_cookie_only') && (isset($this->request->request['tracking']) || isset($this->request->cookie['tracking']))) || ($this->config->get('module_tracking_input_show') == 'once' && isset($this->request->cookie['__octfsh__']) && (!isset($this->session->data['tracking_input_show']) || !$this->session->data['tracking_input_show']))) return '';
		
		$this->document->addScript('catalog/view/javascript/triyp.min.js');		
		$this->session->data['tracking_input_show'] = true;
		if($this->config->get('module_tracking_input_show') == 'once') setcookie('__octfsh__', '1', time() + 2592000, '/');

		$data['show_close_button'] = $this->config->get('module_tracking_input_show_close_button');		

		$data['send_link'] = html_entity_decode($this->url->link('extension/module/tracking_input/send', '', (isset($this->request->server['HTTPS']) && $this->request->server['HTTPS'] == 'on' ? true : false)), ENT_QUOTES, 'UTF-8');
		$data['close_link'] = html_entity_decode($this->url->link('extension/module/tracking_input/close', '', (isset($this->request->server['HTTPS']) && $this->request->server['HTTPS'] == 'on' ? true : false)), ENT_QUOTES, 'UTF-8');		
		$data['text_thankyou'] = isset($setting['text_thankyou']) && utf8_strlen($setting['text_thankyou']) > 0 ? addcslashes(str_replace(array("\r\n", "\n", "\r"), array(' ', ' ', ' '), html_entity_decode($setting['text_thankyou'], ENT_QUOTES, 'UTF-8')), "'") : '';
		$data['error_message'] = isset($setting['error_message']) && utf8_strlen($setting['error_message']) > 0 ? addcslashes(str_replace(array("\r\n", "\n", "\r"), array(' ', ' ', ' '), html_entity_decode($setting['error_message'], ENT_QUOTES, 'UTF-8')), "'") : '';
		$data['json'] = array();
		foreach(array('send_link', 'close_link', 'text_thankyou', 'error_message') as $_v) {
			$data['json'][$_v] = $data[$_v];	
		}
		$data['json'] = json_encode($data['json']);		
		
		$data['text_message'] = html_entity_decode($setting['text'], ENT_QUOTES, 'UTF-8');
		$data['text_heading'] = html_entity_decode($setting['text_heading'], ENT_QUOTES, 'UTF-8');
		$data['send_button'] = $setting['button'];
		$this->load->language('affiliate/tracking_input');
		$data['text_loading'] = $this->language->get('text_please_wait');
	
		$_tpl = 'extension/module/tracking_input_' . (isset($setting['template']) ? $setting['template'] : ('default_' . (substr($setting['position'], 0, 3) === 'col' ? 'column' : 'row')));
		return $this->load->view($_tpl, $data);
	}
	
	public function send() {
		$_REQUEST['format'] = $_GET['format'] = 'raw';
		$this->load->model('account/customer');
		if(!isset($this->request->post['t']) || !strlen(($this->request->post['t'] = trim($this->request->post['t'])))) {
			$this->response->setOutput('error');			
		} else {		
			$mark = false;
			$aff = $this->model_account_customer->getAffiliateByTracking($this->request->post['t']);
			if(!$aff && !isset($this->request->cookie['tracking']) && $this->config->get('module_tracking_input_allow_marketing')) {
				$this->load->model('checkout/marketing');
				$mark = $this->model_checkout_marketing->getMarketingByCode($this->request->post['t']);
			}			
			if(!$mark && (!$aff || !$aff['status'])) {
				$this->response->setOutput('error');			
			} else {
				if($this->config->get('module_mta_status')) {
					$_exp =$this->config->get('mta_ypx_expire');
					$tracking_cookie_expire = $_exp ? time() + 86400 * $_exp : 0;
				} else {
					$tracking_cookie_expire = time() + 86400000;
				}
				$this->session->data['tracking_input_show'] = false;
				if(isset($this->request->cookie['tracking'])) setcookie('tracking', '', time() - 86400, '/');
				setcookie('tracking', $this->request->post['t'], $tracking_cookie_expire, '/');
				$this->session->data['tracking'] = $this->request->post['t'];
				if($mark) $this->db->query("UPDATE `" . DB_PREFIX . "marketing` SET clicks = (clicks + 1) WHERE `code` = '" . $this->db->escape($this->request->post['t']) . "'");				
				$this->response->setOutput($this->config->get('module_tracking_input_redirect') ? '1' : '-1');
			}
		}
	}
	
	public function close() {
		if($this->config->get('module_tracking_input_show') != 'page') $this->session->data['tracking_input_show'] = false;
	}

	public function check_tracking() {
		$_REQUEST['format'] = $_GET['format'] = 'raw';
		$ret = array('unique' => false);
		if($this->config->get('module_tracking_input_status') && isset($this->request->post['tracking_input']) && $this->_check($this->request->post['tracking_input'])) $ret['unique'] = true;

		$this->response->addHeader('Content-Type: application/json');		
		$this->response->setOutput(json_encode($ret));
	}

	public function update() {
		$_REQUEST['format'] = $_GET['format'] = 'raw';		
		if($this->config->get('module_tracking_input_status') && $this->config->get('module_tracking_input_edit_code') && isset($this->request->post['tracking_input']) && $this->_check($this->request->post['tracking_input'])) {
			$out = '1';
			$this->load->model('account/customer');	
			$this->model_account_customer->editTracking($this->request->post['tracking_input']);
		} else {
			$out = '0';
		}		
		$this->response->setOutput($out);		
	}
	
	private function _check($code) {
		if(!preg_match("/^[a-z0-9_\-]+\z/", $code) || strlen($code) > 64) return false;
		$this->load->model('account/customer');	
		if($this->customer->isLogged()) {
			$aff = $this->model_account_customer->getAffiliate($this->customer->getId());
			if($aff) {
				//if(!$aff['status']) return false;
				if($aff['tracking'] == $code) return true;
			}
		}
		
		$a = $this->model_account_customer->getAffiliateByTracking($code);	
		if(!$a && $this->config->get('tracking_input_allow_coupon')) {			
			$this->load->model('extension/total/coupon');
			$a = $this->model_extension_total_coupon->getCoupon($code);
		}
		if(!$a) {
			$this->load->model('checkout/marketing');
			$a = $this->model_checkout_marketing->getMarketingByCode($code);
		}
		return (!$a ? true : false);
	}

	
}

