<?php

class ControllerExtensionModuleCouponForTracking extends Controller {

	private $name = 'coupon_for_tracking';
	
	private $_settings = array('status', 'always', 'session_only');
	
	private $_events = array(
		array('admin_delete_coupon', 'admin/model/marketing/coupon/deleteCoupon/before', 'extension/module/coupon_for_tracking/event/eventDeleteCoupon'),
		array('admin_add_coupon', 'admin/model/marketing/coupon/addCoupon/after',  'extension/module/coupon_for_tracking/event/eventAddCoupon'),	
		array('admin_edit_coupon', 'admin/model/marketing/coupon/editCoupon/before',  'extension/module/coupon_for_tracking/event/eventEditCoupon'),				
		array('admin_get_coupon', 'admin/model/marketing/coupon/getCoupon/after',  'extension/module/coupon_for_tracking/event/eventGetCoupon'),				
		array('admin_get_coupon_by_code', 'admin/model/marketing/coupon/getCouponByCode/after',  'extension/module/coupon_for_tracking/event/eventGetCoupon'),		
		array('admin_coupon_form_t', 'admin/view/marketing/coupon_form/before',  'extension/module/coupon_for_tracking/event/eventCouponFormTBefore'),	
	);
	
	public function index() {
		if (!$this->user->hasPermission('modify', 'extension/module/' . $this->name)) $this->response->redirect('error/permission', 'user_token=' . $this->session->data['user_token'], true);		
		$data = $this->load->language('extension/module/' . $this->name);	
		$data['entry_session_only'] = sprintf($data['entry_session_only'], (defined('MTA_YPX_HAS_MTA') && MTA_YPX_HAS_MTA && !$this->config->get('mta_ypx_no_aff_in_cust_acc') ? $data['text_session_only_mta'] : ''));
		$this->document->setTitle($this->language->get('heading_title'));
		foreach($this->_settings as $_s) {
			$data['module_' . $this->name . '_' . $_s] = $this->config->get('module_' . $this->name . '_' . $_s);
		}

		if(isset($this->request->post['module_' . $this->name . '_always'])) {
			if (!$this->user->hasPermission('modify', 'extension/module/' . $this->name)) $this->response->redirect($this->url->link('error/permission', 'user_token=' . $this->session->data['user_token'], true));			
			$this->load->model('setting/setting');
			$_settings = array();
			foreach($this->_settings as $_s) {
				$_settings['module_' . $this->name . '_' . $_s] = ($this->request->post['module_' . $this->name . '_' . $_s] ? 1 : 0);
				$data['module_' . $this->name . '_' . $_s] = $_settings['module_' . $this->name . '_' . $_s];
			}			
			$this->model_setting_setting->editSetting('module_' . $this->name, $_settings);
		}
		$data['classname'] = $this->name;
  		$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_module'),
			'href'      => $this->url->link('marketplace/extension', 'type=module&user_token=' . $this->session->data['user_token'], true),
      		'separator' => ' :: '
   		);

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

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

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

		$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/module/' . $this->name, $data));
	}
	
	public function install() {
		$this->load->model('extension/coupon_for_tracking');
		$this->model_extension_coupon_for_tracking->install();
		
		$this->load->model('setting/setting');
		$this->model_setting_setting->editSetting('module_' . $this->name, array(
			'module_' . $this->name . '_status' => 0,
			'module_' . $this->name . '_always' => 0,
			'module_' . $this->name . '_session_only' => 0
		));	

		$this->addPermissions();
				
		$this->load->model('setting/event');
		foreach($this->_events as $_e) {
			if(!$this->model_setting_event->getEventByCode('module_' . $this->name . '_' . $_e[0])) $this->model_setting_event->addEvent('module_' . $this->name . '_' . $_e[0], $_e[1], $_e[2]);
		}		
	}
	
	public function uninstall() {
		if (!$this->user->hasPermission('modify', 'extension/module/' . $this->name)) return;
		$this->load->model('setting/setting');		
		$this->model_setting_setting->deleteSetting('module_' . $this->name);
		$this->load->model('setting/event');
		foreach($this->_events as $_e) {
			$this->model_setting_event->deleteEventByCode('module_' . $this->name . '_' . $_e[0]);
		}	
	}
	
	protected function addPermissions() {
		$this->load->model('user/user_group');
		$routes = array('extension/module/' . $this->name, 'extension/module/' . $this->name . '/event');
		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);				
				}
			}
		}
	}
	
}
