<?php
/*
 * Copyright Guntis Endzelis <guntis.endzelis@partneris.lv>
 *
 */

define('PRICE_INPUT_HELPER_VERSION', "1.3");

class ControllerModulePriceInputHelper extends Controller {
	
	private $error = array(); 

	public function install() {
		
		//$this->load->model('module/price_input_helper');
		
		if (!$this->user->hasPermission('modify', 'extension/module')) {
			$this->redirect($this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'));
		} else {
			
			if (file_exists(DIR_CATALOG.'../vqmod/xml/price_input_helper.xml_')) {
				rename(DIR_CATALOG.'../vqmod/xml/price_input_helper.xml_', DIR_CATALOG.'../vqmod/xml/price_input_helper.xml');
			}
			
		
			$dirCache = DIR_CATALOG.'../vqmod/vqcache/';
			foreach(glob($dirCache.'*.*') as $fileCache){ 
				unlink($fileCache);
			}
			
			$this->load->model('setting/setting');
			$data = array();
			$data['autodetect'] = 1; // 1= detects tax value automatically based store current address and default customer group
			$data['pricemultiplyer'] = 1; //numeric, defautl to one
			$data['status'] = 1; //enabled
			
			$this->model_setting_setting->editSetting('price_input_helper', $data);
			
			//$this->model_module_price_input_helper->install();
			
			$this->redirect($this->url->link('module/price_input_helper', 'token=' . $this->session->data['token'], 'SSL'));
		}
	}

	public function index() {
		$this->load->language('module/price_input_helper');

		$this->document->setTitle($this->language->get('heading_title'));
		$this->document->setTitle('Price Input Helper');
		$this->document->addScript('http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js');
		$this->load->model('setting/setting');
		
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			
			$this->model_setting_setting->editSetting('price_input_helper', $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'));
		}
				//~ To remove
				//~ 'text_content_top',
				//~ 'text_content_bottom',
				//~ 'text_column_left',
				//~ 'text_column_right',
				//~ 
				//~ 'entry_layout',
				//~ 'entry_limit',
				//~ 'entry_image',
				//~ 
				//~ 'entry_position',
				
		$text_strings = array(
				'entry_version',
				'heading_title',
				'text_enabled',
				'text_disabled',
				'entry_status',
				'entry_sort_order',
				'button_save',
				'button_cancel',
				'entry_pricemultiplyer',
				'entry_status',
				'entry_autodetect',
				'text_autodetect_yes',
				'text_autodetect_no'
		);
		
		foreach ($text_strings as $text) {
			$this->data[$text] = $this->language->get($text);
		}
		$config_data = array(
			'pricemultiplyer',
			'status',
			'autodetect'
		);
		
		foreach ($config_data as $conf) {
			if (isset($this->request->post[$conf])) {
				$this->data[$conf] = $this->request->post[$conf];
			} else {
				$this->data[$conf] = $this->config->get($conf);
			}
		}
		
		//setting default values
		if (!isset($this->data['autodetect'])) $this->data['autodetect'] = 1; //autodetect by default
		
		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		
  		$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('heading_title'),
			'href'      => $this->url->link('module/price_input_helper', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		
		$this->data['action'] = $this->url->link('module/price_input_helper', 'token=' . $this->session->data['token'], 'SSL');
		
		$this->data['cancel'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL');
	
		//Choose which template file will be used to display this request.
		$this->template = 'module/price_input_helper.tpl';
		$this->children = array(
			'common/header',
			'common/footer',
		);

		//Send the output.
		$this->response->setOutput($this->render());
	}
	
	private function validate() {
		if (!$this->user->hasPermission('modify', 'module/price_input_helper')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}
		if (empty($this->request->post['pricemultiplyer'])) {
			$this->request->post['pricemultiplyer'] = 1;
		}
		return (!$this->error);
	}


}
?>
