<?php
class ControllerExtensionModuleAffClassic extends Controller {
	private $error = array();

	protected $mroute = 'extension/module/aff_classic';
	protected $mcode = 'module_aff_classic';
	
	protected $_events = array(
		array('admin_aff_transaction', 'admin/model/extension/aff_classic/addTransaction/after', 'extension/module/aff_classic/mail_affiliate_transaction'),
		array('admin_add_transaction_c', 'admin/controller/customer/customer/addtransaction/before',  'extension/module/aff_classic/event/eventAddTransactionCBefore'),
		array('admin_customer_form_t', 'admin/view/customer/customer_form/before',  'extension/module/aff_classic/event/eventCustomerFormTBefore'),		
		array('admin_customer_transaction_t', 'admin/view/customer/customer_transaction/before',  'extension/module/aff_classic/event/eventCustomerTransactionTBefore'),	
		array('delete_customer', 'admin/model/customer/customer/deleteCustomer/before', 'extension/module/aff_classic/event/eventDeleteCustomerBefore'),
		array('delete_order', 'catalog/model/checkout/order/deleteOrder/before', 'extension/module/aff_classic/event/eventDeleteOrderBefore'),
		array('aff_transaction', 'catalog/model/extension/aff_classic/addTransaction/after', 'extension/module/aff_classic/mail_affiliate_transaction'),		
		array('account_transaction_t', 'catalog/view/account/transaction/before',  'extension/module/aff_classic/event/eventAccountTransactionTBefore'),	
		array('get_transactions', 'catalog/model/account/transaction/getTransactions/before', 'extension/module/aff_classic/event/eventAccountGetTransactionsBefore'),	
		array('get_total_transactions', 'catalog/model/account/transaction/getTotalTransactions/before', 'extension/module/aff_classic/event/eventAccountGetTotalTransactionsBefore'),		
		
		array('customer_delete_transaction_by_oid', 'catalog/model/account/customer/deleteTransactionByOrderId/before', 'extension/module/aff_classic/event/eventCustomerDeleteTransactionByOrderIdBefore'),
		array('customer_get_total_transactions_by_oid', 'catalog/model/account/customer/getTotalTransactionsByOrderId/before', 'extension/module/aff_classic/event/eventCustomerGetTotalTransactionsByOrderIdBefore'),
		array('customer_add_transaction', 'catalog/model/account/customer/addTransaction/before', 'extension/module/aff_classic/event/eventCustomerAddTransactionBefore')		
	);
	
	protected $_default_settings = array(
		'status' => '0',
		'commission_to_credits' => '0',
		'commission_table' => '1',
		'transfer_multiplier' => '1',
	);
	
	public function index() {
		$this->load->language($this->mroute);
		$this->load->model('setting/setting');

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

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			if(!$this->request->post[$this->mcode . '_status']) {
				foreach($this->request->post as $k => $v) {
					if(strpos($k, $this->mcode . '_') === 0) $this->request->post[$k] = '0';
				}
			} else if(!$this->request->post[$this->mcode . '_commission_table']) {
				$this->request->post[$this->mcode . '_commission_to_credits'] = '0';
			}
			$this->model_setting_setting->editSetting($this->mcode, $this->request->post);
			$this->model_setting_setting->editSetting('report_affiliate_transaction', 	array(
					'report_affiliate_transaction_status' => ($this->request->post[$this->mcode . '_status'] && $this->request->post[$this->mcode . '_commission_table'] ? 1 : 0),
					'report_affiliate_transaction_sort_order' => $this->config->get('report_affiliate_transaction_sort_order')
				)
			);		
			
			$this->session->data['success'] = $this->language->get('text_success');
			$this->response->redirect($this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=module', 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/dashboard', 'user_token=' . $this->session->data['user_token'], true)
		);

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

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('heading_title'),
			'href' => $this->url->link($this->mroute, 'user_token=' . $this->session->data['user_token'], true)
		);

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

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

		foreach($this->_default_settings as $k => $v) {
			if (isset($this->request->post[$this->mcode . '_' . $k])) {
				$data[$this->mcode . '_classic_' . $k] = $this->request->post[$this->mcode . '_' . $k];
			} else {
				$data[$this->mcode . '_' . $k] = $this->config->get($this->mcode . '_' . $k);
			}
		}

		$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($this->mroute, $data));
	}

	protected function validate() {
		if (!$this->user->hasPermission('modify', $this->mroute)) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		return !$this->error;
	}


	public function install() {
		$this->load->model('extension/aff_classic');
		$this->model_extension_aff_classic->install();	
		$this->addPermissions();
		
		$this->load->model('setting/setting');
		$settings = array();
		foreach($this->_default_settings as $k => $v) {
			$settings[$this->mcode . '_' . $k] = $v;
		}
		$this->model_setting_setting->editSetting($this->mcode, $settings);
		
		$this->load->model('setting/extension');
		$this->model_setting_extension->install('report', 'affiliate_transaction');
		
		$this->model_setting_setting->editSetting('report_affiliate_transaction', array(
			'report_affiliate_transaction_status' => 0,
			'report_affiliate_transaction_sort_order' => ''
		));		
	
		$this->load->model('setting/event');
		foreach($this->_events as $_e) {
			if(!$this->model_setting_event->getEventByCode($this->mcode . '_' . $_e[0])) $this->model_setting_event->addEvent($this->mcode . '_' . $_e[0], $_e[1], $_e[2]);
		}
	}

	public function uninstall() {
		if (!$this->user->hasPermission('modify', $this->mroute)) return;
		$this->load->model('setting/setting');
		$this->model_setting_setting->deleteSetting($this->mcode);
		
		$this->load->model('setting/extension');
		$this->model_setting_extension->uninstall('report', 'affiliate_transaction');
		$this->model_setting_setting->deleteSetting('report_affiliate_transaction');
		
		$this->load->model('setting/event');
		foreach($this->_events as $_e) {
			$this->model_setting_event->deleteEventByCode($this->mcode . '_' . $_e[0]);
		}
	}

	protected function addPermissions() {
		$this->load->model('user/user_group');
		$routes = array($this->mroute, 'extension/module/aff_classic/event', 'extension/module/aff_classic/mail_affiliate_transaction', 'extension/report/affiliate_transaction');
		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);				
				}
			}
		}
	}
	
}
