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

		$this->document->setTitle($this->language->get('heading_title'));
		
		$this->load->model('setting/setting');
				
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validate())) {
			$this->model_setting_setting->editSetting('invoice_enhancer', $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('heading_title');
		$this->data['text_yes'] = $this->language->get('text_yes');
		$this->data['text_no'] = $this->language->get('text_no');
		
		$this->data['entry_barcode_type'] = $this->language->get('entry_barcode_type');
		$this->data['entry_invoice_store_logo'] = $this->language->get('entry_invoice_store_logo');
		$this->data['entry_invoice_barcode'] = $this->language->get('entry_invoice_barcode');
		$this->data['entry_invoice_payment_method'] = $this->language->get('entry_invoice_payment_method');
		$this->data['entry_invoice_sku'] = $this->language->get('entry_invoice_sku');
		$this->data['entry_sort_order'] = $this->language->get('entry_sort_order');

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

 		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/invoice_enhancer', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		
		$this->data['action'] = $this->url->link('module/invoice_enhancer', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['cancel'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL');
		
		if (isset($this->request->post['barcode_type'])) {
			$this->data['barcode_type'] = $this->request->post['barcode_type'];
		} else {
			$this->data['barcode_type'] = $this->config->get('barcode_type');
		}	

		if (isset($this->request->post['invoice_store_logo'])) {
			$this->data['invoice_store_logo'] = $this->request->post['invoice_store_logo'];
		} else {
			$this->data['invoice_store_logo'] = $this->config->get('invoice_store_logo');
		}
		
		if (isset($this->request->post['invoice_barcode'])) {
			$this->data['invoice_barcode'] = $this->request->post['invoice_barcode'];
		} else {
			$this->data['invoice_barcode'] = $this->config->get('invoice_barcode');
		}
		
		if (isset($this->request->post['invoice_payment_method'])) {
			$this->data['invoice_payment_method'] = $this->request->post['invoice_payment_method'];
		} else {
			$this->data['invoice_payment_method'] = $this->config->get('invoice_payment_method');
		}	
		
		if (isset($this->request->post['invoice_sku'])) {
			$this->data['invoice_sku'] = $this->request->post['invoice_sku'];
		} else {
			$this->data['invoice_sku'] = $this->config->get('invoice_sku');
		}
		
		$this->template = 'module/invoice_enhancer.tpl';
		$this->children = array(
			'common/header',
			'common/footer'	
		);
		
		$this->response->setOutput($this->render());
	}
	
	private function validate() {
		if (!$this->user->hasPermission('modify', 'module/invoice_enhancer')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}
		
		if (!$this->error) {
			return TRUE;
		} else {
			return FALSE;
		}
	}
}
?>