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

		$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('accessories', $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_image_width'] = $this->language->get('text_image_width');
		$this->data['text_image_height'] = $this->language->get('text_image_height');
		$this->data['text_popup_width'] = $this->language->get('text_popup_width');
		$this->data['text_popup_height'] = $this->language->get('text_popup_height');

		$this->data['entry_heading_box'] = $this->language->get('entry_heading_box');
		$this->data['entry_image'] = $this->language->get('entry_image');
		$this->data['entry_popup'] = $this->language->get('entry_popup');
		$this->data['entry_description'] = $this->language->get('entry_description');
		$this->data['entry_options_product'] = $this->language->get('entry_options_product');
		
		$this->data['info_heading_box'] = $this->language->get('info_heading_box');
		$this->data['info_image'] = $this->language->get('info_image');
		$this->data['info_popup'] = $this->language->get('info_popup');
		$this->data['info_description'] = $this->language->get('info_description');
		$this->data['info_options_product'] = $this->language->get('info_options_product');
		
		$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/accessories', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		
		$this->data['action'] = $this->url->link('module/accessories', 'token=' . $this->session->data['token'], 'SSL');
		
		$this->data['cancel'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL');
		
		$this->data['modules'] = array();
		
		if (isset($this->request->post['accessories_module'])) {
			$this->data['modules'] = $this->request->post['accessories_module'];
		} elseif ($this->config->get('accessories_module')) { 
			$this->data['modules'] = $this->config->get('accessories_module');
		}	
		
		$this->load->model('localisation/language');

		$languages = $this->model_localisation_language->getLanguages();
		
		$this->data['languages'] = $languages;
		
		foreach ($languages as $language) {
			if (isset($this->request->post['heading_box' . $language['language_id']])) {
				$this->data['heading_box' . $language['language_id']] = $this->request->post['heading_box' . $language['language_id']];
			} else {
				$this->data['heading_box' . $language['language_id']] = $this->config->get('heading_box' . $language['language_id']);
			}
			
			if (isset($this->request->post['text_options_product_' . $language['language_id']])) {
				$this->data['text_options_product' . $language['language_id']] = $this->request->post['text_options_product' . $language['language_id']];
			} else {
				$this->data['text_options_product' . $language['language_id']] = $this->config->get('text_options_product' . $language['language_id']);
			}
		}
		
		if (isset($this->request->post['image_width'])) {
			$this->data['image_width'] = $this->request->post['image_width'];
		} else {
			$this->data['image_width'] = $this->config->get('image_width');
		}
		
		if (isset($this->request->post['image_height'])) {
			$this->data['image_height'] = $this->request->post['image_height'];
		} else {
			$this->data['image_height'] = $this->config->get('image_height');
		}
		
		if (isset($this->request->post['popup_width'])) {
			$this->data['popup_width'] = $this->request->post['popup_width'];
		} else {
			$this->data['popup_width'] = $this->config->get('popup_width');
		}
		
		if (isset($this->request->post['popup_height'])) {
			$this->data['popup_height'] = $this->request->post['popup_height'];
		} else {
			$this->data['popup_height'] = $this->config->get('popup_height');
		}
		
		if (isset($this->request->post['description_accessories_product'])) {
			$this->data['description_accessories_product'] = $this->request->post['description_accessories_product'];
		} else {
			$this->data['description_accessories_product'] = $this->config->get('description_accessories_product');
		}
						
		$this->template = 'module/accessories.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
	}
	
	private function validate() {
		if (!$this->user->hasPermission('modify', 'module/accessories')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}
		
		if (!$this->error) {
			return true;
		} else {
			return false;
		}	
	}
	public function install(){
		$this->load->model('catalog/accessories');
		$this->model_catalog_accessories->install();
		return TRUE;
	}
	public function uninstall(){
		$this->load->model('catalog/accessories');
		$this->model_catalog_accessories->uninstall();
		return TRUE;
	}
}
?>