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

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

		$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('ydohang', $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_qq_open_signup'] = $this->language->get('text_qq_open_signup');
		$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['text_cwkj_wxts'] = $this->language->get('text_cwkj_wxts');
		
		$data['entry_status'] = $this->language->get('entry_status');
		$data['entry_name'] = $this->language->get('entry_name');
		$data['entry_image'] = $this->language->get('entry_image');
		$data['entry_width'] = $this->language->get('entry_width');
		$data['entry_height'] = $this->language->get('entry_height');
		$data['entry_imagewidth'] = $this->language->get('entry_imagewidth');
		$data['entry_imageheight'] = $this->language->get('entry_imageheight');
		$data['entry_titlename'] = $this->language->get('entry_titlename');
		$data['entry_bgcolortitle'] = $this->language->get('entry_bgcolortitle');
		$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_titlecss'] = $this->language->get('entry_titlecss');
		$data['entry_bordercss'] = $this->language->get('entry_bordercss');
		$data['entry_describe'] = $this->language->get('entry_describe');
		$data['entry_title'] = $this->language->get('entry_title');
		$data['entry_text'] = $this->language->get('entry_text');
		$data['entry_icon'] = $this->language->get('entry_icon');
		$data['entry_buttonname'] = $this->language->get('entry_buttonname');
		$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_column'] = $this->language->get('entry_column');
		$data['entry_columns'] = $this->language->get('entry_columns');
		$data['help_column'] = $this->language->get('help_column');
		$data['entry_mm'] = $this->language->get('entry_mm');
		$data['entry_mtitle'] = $this->language->get('entry_mtitle');
		$data['entry_mbgcolor'] = $this->language->get('entry_mbgcolor');
		$data['entry_mpa'] = $this->language->get('entry_mpa');
		$data['entry_mborder'] = $this->language->get('entry_mborder');
		$data['entry_mbcolor'] = $this->language->get('entry_mbcolor');
		$data['entry_rptop'] = $this->language->get('entry_rptop');
        $data['entry_rpbuttom'] = $this->language->get('entry_rpbuttom');
		$data['entry_linkcolor'] = $this->language->get('entry_linkcolor');
		$data['entry_segmentingline'] = $this->language->get('entry_segmentingline');
		$data['entry_rtwolw'] = $this->language->get('entry_rtwolw');
		$data['entry_rtwolh'] = $this->language->get('entry_rtwolh');
        $data['entry_rtwolc'] = $this->language->get('entry_rtwolc');
		$data['entry_autoplay'] = $this->language->get('entry_autoplay');
		$data['entry_main'] = $this->language->get('entry_main');
		$data['entry_speed'] = $this->language->get('entry_speed');
		$data['entry_scroll'] = $this->language->get('entry_scroll');
		$data['entry_button1'] = $this->language->get('entry_button1');
		$data['entry_button2'] = $this->language->get('entry_button2');
		$data['entry_rorow'] = $this->language->get('entry_rorow');
		$data['entry_fade'] = $this->language->get('entry_fade');
		$data['entry_boxround'] = $this->language->get('entry_boxround');
		$data['entry_boxposition'] = $this->language->get('entry_boxposition');
		$data['entry_boxshadow'] = $this->language->get('entry_boxshadow');
		$data['entry_boxshadowhover'] = $this->language->get('entry_boxshadowhover');      
		$data['entry_mpppa'] = $this->language->get('entry_mpppa');	
		$data['entry_mbgcolorjb'] = $this->language->get('entry_mbgcolorjb');
		
		$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['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['image'])) {
			$data['error_image'] = $this->error['image'];
		} else {
			$data['error_image'] = '';
		}

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

		if (isset($this->error['height'])) {
			$data['error_height'] = $this->error['height'];
		} else {
			$data['error_height'] = '';
		}
		if (isset($this->error['name'])) {
			$data['error_name'] = $this->error['name'];
		} else {
			$data['error_name'] = '';
		}				
		$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/ydohang', 'token=' . $this->session->data['token'], 'SSL')
		);

		if (!isset($this->request->get['module_id'])) {
			$data['action'] = $this->url->link('extension/module/ydohang', 'token=' . $this->session->data['token'], 'SSL');
		} else {
			$data['action'] = $this->url->link('extension/module/ydohang', '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'] = '';
		}
		$this->load->model('tool/image');

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

		if (is_file(DIR_IMAGE . $data['image'])) {
			$thumb = $data['image'];
		} else {
			$thumb = 'no_image.png';
		}

		$data['thumb'] = $this->model_tool_image->resize($thumb, 100, 100);

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

		if (isset($this->request->post['height'])) {
			$data['height'] = $this->request->post['height'];
		} elseif (!empty($module_info)) {
			$data['height'] = $module_info['height'];
		} else {
			$data['height'] = '750';
		}
		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['describe'])) {
			$data['describe'] = $this->request->post['name'];
		} elseif (!empty($module_info)) {
			$data['describe'] = $module_info['describe'];
		} else {
			$data['describe'] = '';
		}
		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['bordercss'])) {
			$data['titlecss'] = $this->request->post['bordercss'];
		} elseif (!empty($module_info)) {
			$data['bordercss'] = $module_info['bordercss'];
		} else {
			$data['bordercss'] = '';
		}	
		if (isset($this->request->post['column'])) {
			$data['column'] = $this->request->post['column'];
		} elseif (!empty($module_info)) {
			$data['column'] = $module_info['column'];
		} else {
			$data['column'] = '5';
		}
		if (isset($this->request->post['mtitle'])) {
			$data['mtitle'] = $this->request->post['mtitle'];
		} elseif (!empty($module_info)) {
			$data['mtitle'] = $module_info['mtitle'];
		} else {
			$data['mtitle'] = '';
		}
		if (isset($this->request->post['mbgcolor'])) {
			$data['mbgcolor'] = $this->request->post['mbgcolor'];
		} elseif (!empty($module_info)) {
			$data['mbgcolor'] = $module_info['mbgcolor'];
		} else {
			$data['mbgcolor'] = '';
		}
		if (isset($this->request->post['mpa'])) {
			$data['mpa'] = $this->request->post['mpa'];
		} elseif (!empty($module_info)) {
			$data['mpa'] = $module_info['mpa'];
		} else {
			$data['mpa'] = '20';
		}
		if (isset($this->request->post['mborder'])) {
			$data['mborder'] = $this->request->post['mborder'];
		} elseif (!empty($module_info)) {
			$data['mborder'] = $module_info['mborder'];
		} else {
			$data['mborder'] = '';
		}	
		if (isset($this->request->post['mbcolor'])) {
			$data['mbcolor'] = $this->request->post['mbcolor'];
		} elseif (!empty($module_info)) {
			$data['mbcolor'] = $module_info['mbcolor'];
		} else {
			$data['mbcolor'] = '';
		}	
		if (isset($this->request->post['rptop'])) {
			$data['rptop'] = $this->request->post['rptop'];
		} elseif (!empty($module_info)) {
			$data['rptop'] = $module_info['rptop'];
		} else {
			$data['rptop'] = '70';
		}
		if (isset($this->request->post['rpbuttom'])) {
			$data['rpbuttom'] = $this->request->post['rpbuttom'];
		} elseif (!empty($module_info)) {
			$data['rpbuttom'] = $module_info['rpbuttom'];
		} else {
			$data['rpbuttom'] = '70';
		}
		if (isset($this->request->post['linkcolor'])) {
			$data['linkcolor'] = $this->request->post['linkcolor'];
		} elseif (!empty($module_info)) {
			$data['linkcolor'] = $module_info['linkcolor'];
		} else {
			$data['linkcolor'] = '';
		}
		if (isset($this->request->post['rtwolw'])) {
			$data['rtwolw'] = $this->request->post['rtwolw'];
		} elseif (!empty($module_info)) {
			$data['rtwolw'] = $module_info['rtwolw'];
		} else {
			$data['rtwolw'] = '30';
		}	
		if (isset($this->request->post['rtwolh'])) {
			$data['rtwolh'] = $this->request->post['rtwolh'];
		} elseif (!empty($module_info)) {
			$data['rtwolh'] = $module_info['rtwolh'];
		} else {
			$data['rtwolh'] = '2';
		}
		if (isset($this->request->post['rtwolc'])) {
			$data['rtwolc'] = $this->request->post['rtwolc'];
		} elseif (!empty($module_info)) {
			$data['rtwolc'] = $module_info['rtwolc'];
		} else {
			$data['rtwolc'] = '#000000';
		}
		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['main'])) {
			$data['main'] = $this->request->post['main'];
		} elseif (!empty($module_info)) {
			$data['main'] = $module_info['main'];
		} else {
			$data['main'] = '';
		}
		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['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['rorow'])) {
			$data['rorow'] = $this->request->post['rorow'];
		} elseif (!empty($module_info)) {
			$data['rorow'] = $module_info['rorow'];
		} else {
			$data['rorow'] = '2';
		}
		if (isset($this->request->post['boxround'])) {
			$data['boxround'] = $this->request->post['boxround'];
		} elseif (!empty($module_info)) {
			$data['boxround'] = $module_info['boxround'];
		} else {
			$data['boxround'] = '5';
		}
		if (isset($this->request->post['boxposition'])) {
			$data['boxposition'] = $this->request->post['boxposition'];
		} elseif (!empty($module_info)) {
			$data['boxposition'] = $module_info['boxposition'];
		} else {
			$data['boxposition'] = 'center';
		}
		if (isset($this->request->post['boxshadow'])) {
			$data['boxshadow'] = $this->request->post['boxshadow'];
		} elseif (!empty($module_info)) {
			$data['boxshadow'] = $module_info['boxshadow'];
		} else {
			$data['boxshadow'] = '11';
		}
		if (isset($this->request->post['boxshadowhover'])) {
			$data['boxshadowhover'] = $this->request->post['boxshadowhover'];
		} elseif (!empty($module_info)) {
			$data['boxshadowhover'] = $module_info['boxshadowhover'];
		} else {
			$data['boxshadowhover'] = '21';
		}
		if (isset($this->request->post['mpppa'])) {
			$data['mpppa'] = $this->request->post['mpppa'];
		} elseif (!empty($module_info)) {
			$data['mpppa'] = $module_info['mpppa'];
		} else {
			$data['mpppa'] = '10';
		}
		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['mbgcolorjb'])) {
			$data['mbgcolorjb'] = $this->request->post['mbgcolorjb'];
		} elseif (!empty($module_info)) {
			$data['mbgcolorjb'] = $module_info['mbgcolorjb'];
		} else {
			$data['mbgcolorjb'] = '';
		}

	
		$this->load->model('localisation/language');

		$data['languages'] = $this->model_localisation_language->getLanguages();

		$this->load->model('tool/image');

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

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

		foreach ($services as $service) {

			$data['services'][] = array(
				'title'       => $service['title'],
				'link'        => $service['link'],
				'sort_order'  => $service['sort_order']
			);
		}

		$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/ydohang.tpl', $data));
	}

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

		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');
		}
		if ((utf8_strlen($this->request->post['name']) < 0) || (utf8_strlen($this->request->post['name']) > 64)) {
			$this->error['name'] = $this->language->get('error_name');
		}
		return !$this->error;
	}
}