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

		$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('slideprod', $this->request->post);	
			
		$slideprod_settings = array(
		'slideprod_theme'    		=> $this->request->post['slideprod_theme'],
        'slideprod_effect'    		=> $this->request->post['slideprod_effect'],
        'slideprod_hoverpause'      => $this->request->post['slideprod_hoverpause'],
        'slideprod_speed'       	=> $this->request->post['slideprod_speed'],
		'slideprod_changespeed'     => $this->request->post['slideprod_changespeed'],
		'slideprod_controls'  		=> $this->request->post['slideprod_controls'],
		'slideprod_links'  			=> $this->request->post['slideprod_links'],
        );	
			
			$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_enabled'] = $this->language->get('text_enabled');
		$this->data['text_disabled'] = $this->language->get('text_disabled');
		$this->data['text_content_top'] = $this->language->get('text_content_top');
		$this->data['text_content_bottom'] = $this->language->get('text_content_bottom');		
		$this->data['text_column_left'] = $this->language->get('text_column_left');
		$this->data['text_column_right'] = $this->language->get('text_column_right');
		$this->data['text_pleaseselect']    = $this->language->get('text_pleaseselect');	
		$this->data['text_light'] = $this->language->get('text_light');	
		$this->data['text_dark'] = $this->language->get('text_dark');	
		$this->data['text_fade'] = $this->language->get('text_fade');	
		$this->data['text_slideleft'] = $this->language->get('text_slideleft');
		$this->data['text_none'] = $this->language->get('text_none');
		$this->data['text_true'] = $this->language->get('text_true');
		$this->data['text_false'] = $this->language->get('text_false');
		
		$this->data['entry_product'] = $this->language->get('entry_product');
		$this->data['entry_image'] = $this->language->get('entry_image');
		$this->data['entry_layout'] = $this->language->get('entry_layout');
		$this->data['entry_position'] = $this->language->get('entry_position');
		$this->data['entry_theme'] = $this->language->get('entry_theme');
		$this->data['entry_effect'] = $this->language->get('entry_effect');
		$this->data['entry_hoverpause'] = $this->language->get('entry_hoverpause');
		$this->data['entry_speed'] = $this->language->get('entry_speed');
		$this->data['entry_changespeed'] = $this->language->get('entry_changespeed');
		$this->data['entry_controls'] = $this->language->get('entry_controls');
		$this->data['entry_links'] = $this->language->get('entry_links');
		$this->data['entry_status'] = $this->language->get('entry_status');
		$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');
		$this->data['button_add_module'] = $this->language->get('button_add_module');
		$this->data['button_remove'] = $this->language->get('button_remove');
		
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		
		if (isset($this->error['image'])) {
			$this->data['error_image'] = $this->error['image'];
		} else {
			$this->data['error_image'] = array();
		}
				
  		$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/slideprod', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		
		$this->data['action'] = $this->url->link('module/slideprod', 'token=' . $this->session->data['token'], 'SSL');
		
		$this->data['cancel'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL');

		$this->data['token'] = $this->session->data['token'];
		
		if (isset($this->request->post['slideprod_theme'])) {
			$this->data['slideprod_theme'] = $this->request->post['slideprod_theme'];
		} else {
			$this->data['slideprod_theme'] = $this->config->get('slideprod_theme');
		}
		
		if (isset($this->request->post['slideprod_effect'])) {
			$this->data['slideprod_effect'] = $this->request->post['slideprod_effect'];
		} else {
			$this->data['slideprod_effect'] = $this->config->get('slideprod_effect');
		}
		
		if (isset($this->request->post['slideprod_hoverpause'])) {
			$this->data['slideprod_hoverpause'] = $this->request->post['slideprod_hoverpause'];
		} else {
			$this->data['slideprod_hoverpause'] = $this->config->get('slideprod_hoverpause');
		}
		
		if (isset($this->request->post['slideprod_speed'])) {
			$this->data['slideprod_speed'] = $this->request->post['slideprod_speed'];
		} else {
			$this->data['slideprod_speed'] = $this->config->get('slideprod_speed');
		}
		
		if (isset($this->request->post['slideprod_changespeed'])) {
			$this->data['slideprod_changespeed'] = $this->request->post['slideprod_changespeed'];
		} else {
			$this->data['slideprod_changespeed'] = $this->config->get('slideprod_changespeed');
		}
		
		if (isset($this->request->post['slideprod_controls'])) {
			$this->data['slideprod_controls'] = $this->request->post['slideprod_controls'];
		} else {
			$this->data['slideprod_controls'] = $this->config->get('slideprod_controls');
		}
		
		if (isset($this->request->post['slideprod_links'])) {
			$this->data['slideprod_links'] = $this->request->post['slideprod_links'];
		} else {
			$this->data['slideprod_links'] = $this->config->get('slideprod_links');
		}

		if (isset($this->request->post['slideprod_product'])) {
			$this->data['slideprod_product'] = $this->request->post['slideprod_product'];
		} else {
			$this->data['slideprod_product'] = $this->config->get('slideprod_product');
		}	
				
		$this->load->model('catalog/product');
				
		if (isset($this->request->post['slideprod_product'])) {
			$products = explode(',', $this->request->post['slideprod_product']);
		} else {		
			$products = explode(',', $this->config->get('slideprod_product'));
		}
		
		$this->data['products'] = array();
		
		foreach ($products as $product_id) {
			$product_info = $this->model_catalog_product->getProduct($product_id);
			
			if ($product_info) {
				$this->data['products'][] = array(
					'product_id' => $product_info['product_id'],
					'name'       => $product_info['name']
				);
			}
		}	
			
		$this->data['modules'] = array();
		
		if (isset($this->request->post['slideprod_module'])) {
			$this->data['modules'] = $this->request->post['slideprod_module'];
		} elseif ($this->config->get('slideprod_module')) { 
			$this->data['modules'] = $this->config->get('slideprod_module');
		}		
				
		$this->load->model('design/layout');
		
		$this->data['layouts'] = $this->model_design_layout->getLayouts();

		$this->template = 'module/slideprod.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
	}
	
	private function validate() {
		if (!$this->user->hasPermission('modify', 'module/slideprod')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}
		
		if (isset($this->request->post['slideprod_module'])) {
			foreach ($this->request->post['slideprod_module'] as $key => $value) {
				if (!$value['image_width'] || !$value['image_height']) {
					$this->error['image'][$key] = $this->language->get('error_image');
				}
			}
		}
				
		if (!$this->error) {
			return true;
		} else {
			return false;
		}	
	}
}
?>