<?php
class ControllerextensionModulegallery extends Controller {
	private $error = array();

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

		$this->document->setTitle(preg_replace('/<(.*?)strong>/i', '', $this->language->get('heading_title')));

		$this->load->model('extension/module');

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			if (!isset($this->request->get['module_id'])) {
				$this->model_extension_module->addModule('gallery', $this->request->post);
			} else {
				$this->model_extension_module->editModule($this->request->get['module_id'], $this->request->post);
			}

			$this->session->data['success'] = $this->language->get('text_success');

			$this->response->redirect($this->url->link('extension/extension', 'token=' . $this->session->data['token'] . '&type=module', true));
		}

		$data['heading_title'] = preg_replace('/<(.*?)strong>/i', '', $this->language->get('heading_title'));

		$data['text_edit'] = $this->language->get('text_edit');
		$data['text_enabled'] = $this->language->get('text_enabled');
		$data['text_disabled'] = $this->language->get('text_disabled');
		$data['text_yes'] = $this->language->get('text_yes');
		$data['text_no'] = $this->language->get('text_no');
		$data['text_mainc'] = $this->language->get('text_mainc');
		$data['text_mainfw'] = $this->language->get('text_mainfw');
		
		$data['entry_status'] = $this->language->get('entry_status');
		$data['entry_name'] = $this->language->get('entry_name');
		$data['entry_titlename'] = $this->language->get('entry_titlename');
		$data['entry_bgcolortitle'] = $this->language->get('entry_bgcolortitle');
		$data['entry_imagepicstyle'] = $this->language->get('entry_imagepicstyle');
		$data['entry_bgcolor'] = $this->language->get('entry_bgcolor');
		$data['entry_bgcolorcss'] = $this->language->get('entry_bgcolorcss');
		$data['entry_titlejbcss'] = $this->language->get('entry_titlejbcss');
		$data['entry_titlejbsmcss'] = $this->language->get('entry_titlejbsmcss');
		$data['entry_describe'] = $this->language->get('entry_describe');
		$data['entry_width'] = $this->language->get('entry_width');
		$data['entry_height'] = $this->language->get('entry_height');
		$data['entry_link'] = $this->language->get('entry_link');
		$data['entry_image'] = $this->language->get('entry_image');
		$data['entry_sort_order'] = $this->language->get('entry_sort_order');
		$data['entry_picn'] = $this->language->get('entry_picn');
		$data['entry_picpa'] = $this->language->get('entry_picpa');
		$data['entry_columnrow'] = $this->language->get('entry_columnrow');		
		$data['entry_topp'] = $this->language->get('entry_topp');
		$data['entry_buttonp'] = $this->language->get('entry_buttonp');		
		$data['entry_segmentingline'] = $this->language->get('entry_segmentingline');
		$data['entry_iplinew'] = $this->language->get('entry_iplinew');		
		$data['entry_iplineh'] = $this->language->get('entry_iplineh');		
		$data['entry_iplinec'] = $this->language->get('entry_iplinec');
		$data['entry_main'] = $this->language->get('entry_main');
		$data['entry_autoplay'] = $this->language->get('entry_autoplay');
		$data['entry_fade'] = $this->language->get('entry_fade');
		$data['entry_speed'] = $this->language->get('entry_speed');
        $data['entry_columns'] = $this->language->get('entry_columns');
        $data['entry_column'] = $this->language->get('entry_column');
		$data['help_column'] = $this->language->get('help_column');		
		$data['entry_rows'] = $this->language->get('entry_rows');
		$data['entry_scroll'] = $this->language->get('entry_scroll');	
		$data['entry_sectionid'] = $this->language->get('entry_sectionid');
        $data['entry_sectiontab'] = $this->language->get('entry_sectiontab');		
		$data['button_save'] = $this->language->get('button_save');
		$data['button_cancel'] = $this->language->get('button_cancel');
		$data['button_add'] = $this->language->get('button_add');
		$data['button_remove'] = $this->language->get('button_remove');
		$data['entry_boxborder'] = $this->language->get('entry_boxborder');
		$data['entry_boxcolor'] = $this->language->get('entry_boxcolor');
		$data['entry_boxround'] = $this->language->get('entry_boxround');
		$data['entry_boxshadow'] = $this->language->get('entry_boxshadow');
		$data['entry_boxshadowhover'] = $this->language->get('entry_boxshadowhover');
		
		$data['error_name'] = $this->language->get('error_name');

		if (isset($this->error['warning'])) {
			$data['error_warning'] = $this->error['warning'];
		} else {
			$data['error_warning'] = '';
		}

		if (isset($this->error['name'])) {
			$data['error_name'] = $this->error['name'];
		} else {
			$data['error_name'] = '';
		}
		if (isset($this->error['describe'])) {
			$data['error_describe'] = $this->error['describe'];
		} else {
			$data['error_describe'] = '';
		}
		if (isset($this->error['width'])) {
			$data['error_width'] = $this->error['width'];
		} else {
			$data['error_width'] = '';
		}
		if (isset($this->error['sectionid'])) {
			$data['error_sectionid'] = $this->error['sectionid'];
		} else {
			$data['error_sectionid'] = '';
		}
		if (isset($this->error['height'])) {
			$data['error_height'] = $this->error['height'];
		} else {
			$data['error_height'] = '';
		}
		$data['breadcrumbs'] = array();

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

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_module'),
			'href' => $this->url->link('extension/extension', 'token=' . $this->session->data['token'], 'SSL')
		);

		$data['breadcrumbs'][] = array(
			'text' => preg_replace('/<(.*?)strong>/i', '', $this->language->get('heading_title')),
			'href' => $this->url->link('extension/module/gallery', 'token=' . $this->session->data['token'], 'SSL')
		);

		if (!isset($this->request->get['module_id'])) {
			$data['action'] = $this->url->link('extension/module/gallery', 'token=' . $this->session->data['token'], 'SSL');
		} else {
			$data['action'] = $this->url->link('extension/module/gallery', 'token=' . $this->session->data['token'] . '&module_id=' . $this->request->get['module_id'], 'SSL');
		}

		$data['cancel'] = $this->url->link('extension/extension', 'token=' . $this->session->data['token'] . '&type=module', true);

		if (isset($this->request->get['module_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
			$module_info = $this->model_extension_module->getModule($this->request->get['module_id']);
		}

		$data['token'] = $this->session->data['token'];

		if (isset($this->request->post['status'])) {
			$data['status'] = $this->request->post['status'];
		} elseif (!empty($module_info)) {
			$data['status'] = $module_info['status'];
		} else {
			$data['status'] = '';
		}

		if (isset($this->request->post['name'])) {
			$data['name'] = $this->request->post['name'];
		} elseif (!empty($module_info)) {
			$data['name'] = $module_info['name'];
		} else {
			$data['name'] = '';
		}
		if (isset($this->request->post['sectionid'])) {
			$data['sectionid'] = $this->request->post['sectionid'];
		} elseif (!empty($module_info)) {
			$data['sectionid'] = $module_info['sectionid'];
		} else {
			$data['sectionid'] = '';
		}
		if (isset($this->request->post['boxborder'])) {
			$data['boxborder'] = $this->request->post['boxborder'];
		} elseif (!empty($module_info)) {
			$data['boxborder'] = $module_info['boxborder'];
		} else {
			$data['boxborder'] = '0';
		}	
		if (isset($this->request->post['boxcolor'])) {
			$data['boxcolor'] = $this->request->post['boxcolor'];
		} elseif (!empty($module_info)) {
			$data['boxcolor'] = $module_info['boxcolor'];
		} else {
			$data['boxcolor'] = '';
		}		
		if (isset($this->request->post['boxround'])) {
			$data['boxround'] = $this->request->post['boxround'];
		} elseif (!empty($module_info)) {
			$data['boxround'] = $module_info['boxround'];
		} else {
			$data['boxround'] = '';
		}
		if (isset($this->request->post['boxshadow'])) {
			$data['boxshadow'] = $this->request->post['boxshadow'];
		} elseif (!empty($module_info)) {
			$data['boxshadow'] = $module_info['boxshadow'];
		} else {
			$data['boxshadow'] = '';
		}
		if (isset($this->request->post['boxshadowhover'])) {
			$data['boxshadowhover'] = $this->request->post['boxshadowhover'];
		} elseif (!empty($module_info)) {
			$data['boxshadowhover'] = $module_info['boxshadowhover'];
		} else {
			$data['boxshadowhover'] = '';
		}
		if (isset($this->request->post['sectiontab'])) {
			$data['sectiontab'] = $this->request->post['sectiontab'];
		} elseif (!empty($module_info)) {
			$data['sectiontab'] = $module_info['sectiontab'];
		} else {
			$data['sectiontab'] = '';
		}
		if (isset($this->request->post['titlename'])) {
			$data['titlename'] = $this->request->post['titlename'];
		} elseif (!empty($module_info)) {
			$data['titlename'] = $module_info['titlename'];
		} else {
			$data['titlename'] = '';
		}
		if (isset($this->request->post['bgcolor'])) {
			$data['bgcolor'] = $this->request->post['bgcolor'];
		} elseif (!empty($module_info)) {
			$data['bgcolor'] = $module_info['bgcolor'];
		} else {
			$data['bgcolor'] = '';
		}
		if (isset($this->request->post['bgcolorcss'])) {
			$data['bgcolorcss'] = $this->request->post['bgcolorcss'];
		} elseif (!empty($module_info)) {
			$data['bgcolorcss'] = $module_info['bgcolorcss'];
		} else {
			$data['bgcolorcss'] = '';
		}
		if (isset($this->request->post['titlecss'])) {
			$data['titlecss'] = $this->request->post['titlecss'];
		} elseif (!empty($module_info)) {
			$data['titlecss'] = $module_info['titlecss'];
		} else {
			$data['titlecss'] = '';
		}		
		if (isset($this->request->post['titlesmcss'])) {
			$data['titlesmcss'] = $this->request->post['titlesmcss'];
		} elseif (!empty($module_info)) {
			$data['titlesmcss'] = $module_info['titlesmcss'];
		} else {
			$data['titlesmcss'] = '';
		}
		if (isset($this->request->post['describe'])) {
			$data['describe'] = $this->request->post['describe'];
		} elseif (!empty($module_info)) {
			$data['describe'] = $module_info['describe'];
		} else {
			$data['describe'] = '';
		}
		if (isset($this->request->post['picpa'])) {
			$data['picpa'] = $this->request->post['picpa'];
		} elseif (!empty($module_info)) {
			$data['picpa'] = $module_info['picpa'];
		} else {
			$data['picpa'] = '10';
		}	
		if (isset($this->request->post['width'])) {
			$data['width'] = $this->request->post['width'];
		} elseif (!empty($module_info)) {
			$data['width'] = $module_info['width'];
		} else {
			$data['width'] = 380;
		}

		if (isset($this->request->post['height'])) {
			$data['height'] = $this->request->post['height'];
		} elseif (!empty($module_info)) {
			$data['height'] = $module_info['height'];
		} else {
			$data['height'] = 200;
		}
		if (isset($this->request->post['topp'])) {
			$data['topp'] = $this->request->post['topp'];
		} elseif (!empty($module_info)) {
			$data['topp'] = $module_info['topp'];
		} else {
			$data['topp'] = '20';
		}
		if (isset($this->request->post['buttonp'])) {
			$data['buttonp'] = $this->request->post['buttonp'];
		} elseif (!empty($module_info)) {
			$data['buttonp'] = $module_info['buttonp'];
		} else {
			$data['buttonp'] = '70';
		}
		if (isset($this->request->post['iplinew'])) {
			$data['iplinew'] = $this->request->post['iplinew'];
		} elseif (!empty($module_info)) {
			$data['iplinew'] = $module_info['iplinew'];
		} else {
			$data['iplinew'] = '30';
		}
		if (isset($this->request->post['iplineh'])) {
			$data['iplineh'] = $this->request->post['iplineh'];
		} elseif (!empty($module_info)) {
			$data['iplineh'] = $module_info['iplineh'];
		} else {
			$data['iplineh'] = '2';
		}
		if (isset($this->request->post['iplinec'])) {
			$data['iplinec'] = $this->request->post['iplinec'];
		} elseif (!empty($module_info)) {
			$data['iplinec'] = $module_info['iplinec'];
		} else {
			$data['iplinec'] = '#000000';
		}
		if (isset($this->request->post['main'])) {
			$data['main'] = $this->request->post['main'];
		} elseif (!empty($module_info)) {
			$data['main'] = $module_info['main'];
		} else {
			$data['main'] = '';
		}
		if (isset($this->request->post['autoplay'])) {
			$data['autoplay'] = $this->request->post['autoplay'];
		} elseif (!empty($module_info)) {
			$data['autoplay'] = $module_info['autoplay'];
		} else {
			$data['autoplay'] = '';
		}
		if (isset($this->request->post['fade'])) {
			$data['fade'] = $this->request->post['fade'];
		} elseif (!empty($module_info)) {
			$data['fade'] = $module_info['fade'];
		} else {
			$data['fade'] = '';
		}
		if (isset($this->request->post['speed'])) {
			$data['speed'] = $this->request->post['speed'];
		} elseif (!empty($module_info)) {
			$data['speed'] = $module_info['speed'];
		} else {
			$data['speed'] = '3000';
		}
		if (isset($this->request->post['column'])) {
			$data['column'] = $this->request->post['column'];
		} elseif (!empty($module_info)) {
			$data['column'] = $module_info['column'];
		} else {
			$data['column'] = '3';
		}
		if (isset($this->request->post['rows'])) {
			$data['rows'] = $this->request->post['rows'];
		} elseif (!empty($module_info)) {
			$data['rows'] = $module_info['rows'];
		} else {
			$data['rows'] = '2';
		}
		if (isset($this->request->post['scroll'])) {
			$data['scroll'] = $this->request->post['scroll'];
		} elseif (!empty($module_info)) {
			$data['scroll'] = $module_info['scroll'];
		} else {
			$data['scroll'] = '1';
		}
		if (isset($this->request->post['imagepicstyle'])) {
			$data['imagepicstyle'] = $this->request->post['imagepicstyle'];
		} elseif (!empty($module_info)) {
			$data['imagepicstyle'] = $module_info['imagepicstyle'];
		} else {
			$data['imagepicstyle'] = '';
		}		
		$this->load->model('tool/image');

		if (isset($this->request->post['image'])) {
			$images = $this->request->post['image'];
		} elseif (isset($module_info['image'])) {
			$images = $module_info['image'];
		} else {
			$images = array();
		}

		$data['images'] = array();

		foreach ($images as $img) {
			if (is_file(DIR_IMAGE . $img['image'])) {
				$image = $img['image'];
				$thumb = $img['image'];
			} else {
				$image = '';
				$thumb = 'no_image.png';
			}

			$data['images'][] = array(
				'link'        => $img['link'],
				'image'       => $image,
				'thumb'       => $this->model_tool_image->resize($thumb, 200, 50),
				'sort_order'  => $img['sort_order']
			);
		}

		$data['placeholder'] = $this->model_tool_image->resize('no_image.png', 200, 50);

		$data['header'] = $this->load->controller('common/header');
		$data['column_left'] = $this->load->controller('common/column_left');
		$data['footer'] = $this->load->controller('common/footer');

		$this->response->setOutput($this->load->view('extension/module/gallery.tpl', $data));
	}

	protected function validate() {
		if (!$this->user->hasPermission('modify', 'extension/module/gallery')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		if ((utf8_strlen($this->request->post['name']) < 2) || (utf8_strlen($this->request->post['name']) > 64)) {
			$this->error['name'] = $this->language->get('error_name');
		}
		if ((utf8_strlen($this->request->post['sectionid']) < 1) || (utf8_strlen($this->request->post['sectionid']) > 64)) {
			$this->error['sectionid'] = $this->language->get('error_name');
		}	
		if (!$this->request->post['width']) {
			$this->error['width'] = $this->language->get('error_width');
		}

		if (!$this->request->post['height']) {
			$this->error['height'] = $this->language->get('error_height');
		}
		return !$this->error;
	}
}