<?php
class ControllerModuleOptionImage extends Controller {
	private $error = array(); 
	
	public function index() {   
		$this->language->load('module/optionimage');

		$this->document->setTitle($this->language->get('text_heading'));
		
		$this->load->model('setting/setting');
				
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			$this->model_setting_setting->editSetting('optionimage', $this->request->post);		
					
			$this->session->data['success'] = $this->language->get('text_success');
						
			$this->redirect($this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'));
		}
				
		$this->data['heading_title'] = $this->language->get('text_heading');
		
		$this->data['entry_option_image'] = $this->language->get('entry_option_image');
		$this->data['entry_option_image_plus'] = $this->language->get('entry_option_image_plus');
		
		$this->data['text_disabled'] = $this->language->get('text_disabled');
		$this->data['text_enabled'] = $this->language->get('text_enabled');
		$this->data['text_install'] = $this->language->get('text_install');
		$this->data['text_uninstall'] = $this->language->get('text_uninstall');
		
		$this->data['button_back'] = $this->language->get('button_back');

  		$this->data['breadcrumbs'] = array();

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

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_module'),
			'href'      => $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_heading'),
			'href'      => $this->url->link('module/optionimage', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		
		$this->data['back'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL');
		
		$this->data['oi'] = $this->oi();
		$this->data['oip'] = $this->oip();
		
		if ($this->data['oi']) {
			$this->data['oi_action'] = $this->url->link('module/optionimage/oi_uninstall', 'token=' . $this->session->data['token'], 'SSL');
		} else {
			$this->data['oi_action'] = $this->url->link('module/optionimage/oi_install', 'token=' . $this->session->data['token'], 'SSL');
		}
		
		if ($this->data['oip']) {
			$this->data['oip_action'] = $this->url->link('module/optionimage/oip_uninstall', 'token=' . $this->session->data['token'], 'SSL');
		} else {
			$this->data['oip_action'] = $this->url->link('module/optionimage/oip_install', 'token=' . $this->session->data['token'], 'SSL');
		}
						
		$this->template = 'module/optionimage.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
	}
	
	private function oip() {
		$path = substr_replace(DIR_SYSTEM, '', -7);

		if (file_exists($path . 'vqmod/xml/optionimageplus.xml') || is_file($path . 'vqmod/xml/optionimageplus.xml')) {
			return true;
		} else {
			return false;
		}
	}
	
	private function oi() {
		$path = substr_replace(DIR_SYSTEM, '', -7);

		if (file_exists($path . 'vqmod/xml/optionimage.xml') || is_file($path . 'vqmod/xml/optionimage.xml')) {
			return true;
		} else {
			return false;
		}
	}
	
	public function oip_install() {
		$this->language->load('module/optionimage');

		if (!$this->user->hasPermission('modify', 'module/optionimage')) {
			$this->session->data['error'] = $this->language->get('error_permission');
		} else {
			$path = substr_replace(DIR_SYSTEM, '', -7);

			if (file_exists($path . 'vqmod/xml/optionimageplus.xml_') || is_file($path . 'vqmod/xml/optionimageplus.xml_')) {
				rename($path . 'vqmod/xml/optionimageplus.xml_', $path . 'vqmod/xml/optionimageplus.xml');
				
				$query = $this->db->query("SHOW COLUMNS FROM " . DB_PREFIX . "product_option_value");
				$exists = false;
				
				foreach ($query->rows as $result) {
					if ($result['Field'] == 'option_image') {
						$exists = true;
						break;
					}
				}
				
				if (!$exists) {		
					$this->db->query("ALTER TABLE " . DB_PREFIX . "product_option_value ADD option_image varchar(255) NOT NULL AFTER quantity");
				}
				
				$this->session->data['success'] = $this->language->get('text_success');
				
				if ($this->oi()) {
					$this->oi_uninstall();
				}
			} else {
				$this->session->data['error'] = $this->language->get('error_install');
			}
		}

		$this->redirect($this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'));
	}
	
	public function oip_uninstall() {
		$this->language->load('module/optionimage');

		if (!$this->user->hasPermission('modify', 'module/optionimage')) {
			$this->session->data['error'] = $this->language->get('error_permission');
		} else {
			$path = substr_replace(DIR_SYSTEM, '', -7);

			if (file_exists($path . 'vqmod/xml/optionimageplus.xml') || is_file($path . 'vqmod/xml/optionimageplus.xml')) {
				rename($path . 'vqmod/xml/optionimageplus.xml', $path . 'vqmod/xml/optionimageplus.xml_');
				
				$query = $this->db->query("SHOW COLUMNS FROM " . DB_PREFIX . "product_option_value");
				$exists = false;
				
				foreach ($query->rows as $result) {
					if ($result['Field'] == 'option_image') {
						$exists = true;
						break;
					}
				}
				
				if ($exists) {
					$this->db->query("ALTER TABLE " . DB_PREFIX . "product_option_value DROP option_image");
				}
				
				$this->session->data['success'] = $this->language->get('text_success');
			} else {
				$this->session->data['error'] = $this->language->get('error_uninstall');
			}
		}

		$this->redirect($this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'));
	}
	
	public function oi_install() {
		$this->language->load('module/optionimage');

		if (!$this->user->hasPermission('modify', 'module/optionimage')) {
			$this->session->data['error'] = $this->language->get('error_permission');
		} else {
			$path = substr_replace(DIR_SYSTEM, '', -7);

			if (file_exists($path . 'vqmod/xml/optionimage.xml_') || is_file($path . 'vqmod/xml/optionimage.xml_')) {
				rename($path . 'vqmod/xml/optionimage.xml_', $path . 'vqmod/xml/optionimage.xml');
				
				$this->session->data['success'] = $this->language->get('text_success');
				
				if ($this->oip()) {
					$this->oip_uninstall();
				}
			} else {
				$this->session->data['error'] = $this->language->get('error_install');
			}
		}

		$this->redirect($this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'));
	}
	
	public function oi_uninstall() {
		$this->language->load('module/optionimage');

		if (!$this->user->hasPermission('modify', 'module/optionimage')) {
			$this->session->data['error'] = $this->language->get('error_permission');
		} else {
			$path = substr_replace(DIR_SYSTEM, '', -7);

			if (file_exists($path . 'vqmod/xml/optionimage.xml') || is_file($path . 'vqmod/xml/optionimage.xml')) {
				rename($path . 'vqmod/xml/optionimage.xml', $path . 'vqmod/xml/optionimage.xml_');
				
				$this->session->data['success'] = $this->language->get('text_success');
			} else {
				$this->session->data['error'] = $this->language->get('error_install');
			}
		}

		$this->redirect($this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'));
	}
}
?>