<?php

class ControllerExtensionModuleAccountCombine extends Controller {
	private $error = array();
	private $name = 'module_account_combine';
	private $settings;

	protected $_events = array(
		array('add_affiliate', 'catalog/model/account/customer/addAffiliate/before', 'extension/module/account_combine/event/eventCustomerAddAffiliateBefore')
	);


	public function install() {					
		ignore_user_abort(1);
		$this->load->model('extension/account_combine');
		$this->settings = $this->model_extension_account_combine->getDefaultSettings();
		$this->load->model('setting/setting');
		if(!sizeof($this->model_setting_setting->getSetting($this->name))) {
			$_settings = array();
			foreach($this->settings as $k => $v) {
				$_settings[$this->name . '_' . $k] = $v;				
			}
			$this->model_setting_setting->editSetting($this->name, $_settings);
		}

		$this->addPermissions();

		$this->load->model('setting/event');
		foreach($this->_events as $_e) {
			if(!$this->model_setting_event->getEventByCode($this->name . '_' . $_e[0])) $this->model_setting_event->addEvent($this->name . '_' . $_e[0], $_e[1], $_e[2]);
		}

	}
	
	public function uninstall() {
		if (!$this->user->hasPermission('modify', 'extension/module/account_combine')) return;		
		$this->removePermissions();
		$this->load->model('setting/setting');
		$this->model_setting_setting->deleteSetting($this->name);

		$this->load->model('setting/event');
		foreach($this->_events as $_e) {
			$this->model_setting_event->deleteEventByCode($this->name . '_' . $_e[0]);
		}

	}
	
	public function index() {		
		$this->load->model('extension/account_combine');
		$this->settings = $this->model_extension_account_combine->getDefaultSettings();	
		$this->load->model('setting/setting');
		
		$rform = $this->config->get($this->name . '_rform');
		$cform = $this->config->get($this->name . '_cform');
		//if(!$cform['website_textarea'] && !$rform['website_textarea'] && ((isset($this->request->post[$this->name . '_rform']['website_textarea']) && $this->request->post[$this->name . '_rform']['website_textarea']) || (isset($this->request->post[$this->name . '_cform']['website_textarea']) && $this->request->post[$this->name . '_cform']['website_textarea']))) $this->db->query("ALTER TABLE " . DB_PREFIX . "customer_affiliate MODIFY website TEXT NOT NULL");		
		
		$data = $this->load->language('extension/module/account_combine');
		foreach(array('entry_fields', 'entry_fields_use', 'entry_form', 'text_bank') as $_k) {	
			if(!isset($data[$_k])) $data[$_k] = array();
		}
		foreach($data as $k => $v) {				
			foreach(array('entry_fields', 'entry_fields_use', 'entry_form', 'text_bank') as $_k) {				
				if(substr($k, 0, strlen($_k) + 1) === ($_k . '_')) {
					$data[$_k][substr($k, strlen($_k) + 1)] = $v;
					continue;
				}
			}
		}	
		
		if($this->config->get('module_tracking_input_status')) {
			$data['tracking_input_info'] = sprintf($this->language->get('text_tracking_input_info'), $this->url->link('extension/module/tracking_input', 'user_token=' . $this->session->data['user_token'], true));
		} else {
			$data['tracking_input_info'] = false;
		}

		if (!isset($this->session->data['user_token'])) $this->session->data['user_token'] = '';		
		$data['user_token'] = $this->session->data['user_token'];		
		
		if($this->request->server['REQUEST_METHOD'] == 'POST') {
			if ($this->user->hasPermission('modify', 'extension/module/account_combine')) {	
				$toDB = array();
				foreach($this->request->post as $k => $v) {
					if(substr($k, 0, 15) === 'account_combine') $toDB['module_' . $k] = $v;
				}
				$this->model_setting_setting->editSetting($this->name, array_merge($this->model_setting_setting->getSetting($this->name), $toDB));
				$this->session->data['success'] = $this->language->get('text_success');
				$this->response->redirect($this->url->link('marketplace/extension', 'type=module&user_token=' . $this->session->data['user_token'], true));
			} else {
				$this->error['warning'] = $this->language->get('error_permission');
			}
		}

		$this->load->model('customer/customer_group');
		$fields = $this->config->get($this->name . '_fields');
		$data['aff_fields'] = $this->config->get($this->name . '__fields');
		$data['payment_methods'] = $this->config->get($this->name . '__payment_methods');
		$data['payment_methods_json'] = json_encode($data['payment_methods']);
		foreach($fields as $field => $_v) {
			if(in_array($field, $data['payment_methods'])) {
				$data[$field . '_fields'] = array_keys($_v);
			}
		}			
		$data['customer_groups'] = $this->model_customer_customer_group->getCustomerGroups();		

		
		$data['text_none'] = $this->language->get('text_none');
		$data['link_customer_group_now'] = html_entity_decode($this->url->link('extension/module/account_combine/set_customer_group', 'user_token=' . $this->session->data['user_token'], true), ENT_QUOTES, 'UTF-8');
		
		$this->document->setTitle($this->language->get('heading_title'));

		$data['heading_title'] = $this->language->get('heading_title');

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

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

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

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

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

		foreach ($this->settings as $s => $_v) {
			if (isset($this->request->post['account_combine_' . $s])) {
				$data['account_combine_' . $s] = $this->request->post['account_combine_' . $s];
			} else {
				$data['account_combine_' . $s] = $this->config->get($this->name . '_' . $s);
			}
		}

		$data['action'] = $this->url->link('extension/module/account_combine', '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/account_combine', $data));
	}
	
	public function set_customer_group() {
		if(!$this->user->hasPermission('modify', 'extension/module/account_combine') || !isset($this->request->post['customer_group_id']) || !preg_match("/^\d+/", $this->request->post['customer_group_id'])) {
			$this->response->setOutput('-1');
			return;
		}
		$this->load->model('customer/customer_group');
		if(!$this->model_customer_customer_group->getCustomerGroup($this->request->post['customer_group_id'])) {
			$this->response->setOutput('-1');
			return;
		} 
		$this->load->model('extension/account_combine');
		$this->response->setOutput($this->model_extension_account_combine->setAllCustomerGroup($this->request->post['customer_group_id']));	
	}		
	

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

	protected function removePermissions() {
		$this->load->model('user/user_group');
		$routes = array('extension/module/account_combine');
		foreach($routes as $route) {
			foreach(array('access', 'modify') as $perm_type) {
				if($this->user->hasPermission($perm_type, $route)) {
					$this->model_user_user_group->removePermission($this->user->getGroupId(), $perm_type, $route);				
				}
			}
		}
	}

	

}

