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

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

		$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('mapapicwkj', $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_cwkj_wxts'] = $this->language->get('text_cwkj_wxts');

		$data['entry_name'] = $this->language->get('entry_name');
		$data['entry_key'] = $this->language->get('entry_key');
		$data['entry_coordinate'] = $this->language->get('entry_coordinate');
		$data['help_key'] = $this->language->get('help_key');
		$data['help_coordinate'] = $this->language->get('help_coordinate');
		$data['entry_cname'] = $this->language->get('entry_cname');
		$data['entry_cdescribe'] = $this->language->get('entry_cdescribe');
		$data['entry_cadress'] = $this->language->get('entry_cadress');
		$data['entry_telephone'] = $this->language->get('entry_telephone');
		$data['entry_status'] = $this->language->get('entry_status');

		$data['entry_section'] = $this->language->get('entry_section');
		$data['entry_sectionbj'] = $this->language->get('entry_sectionbj');
		$data['entry_sectionjbbj'] = $this->language->get('entry_sectionjbbj');
		$data['entry_sectiontitle'] = $this->language->get('entry_sectiontitle');
		$data['entry_sectiondescribe'] = $this->language->get('entry_sectiondescribe');
		$data['entry_sectionptop'] = $this->language->get('entry_sectionptop');
        $data['entry_sectionpbuttom'] = $this->language->get('entry_sectionpbuttom');
		$data['entry_iconstyle'] = $this->language->get('entry_iconstyle');
		$data['entry_miconbj'] = $this->language->get('entry_miconbj');
		$data['entry_miconcolor'] = $this->language->get('entry_miconcolor');
		$data['entry_miconcolorc'] = $this->language->get('entry_miconcolorc');
		$data['entry_hoverbj'] = $this->language->get('entry_hoverbj');
		$data['entry_hovertit'] = $this->language->get('entry_hovertit');
		$data['entry_miconround'] = $this->language->get('entry_miconround');
		$data['entry_miconsize'] = $this->language->get('entry_miconsize');
		$data['entry_miconborder'] = $this->language->get('entry_miconborder');
		$data['entry_miconbc'] = $this->language->get('entry_miconbc');
		$data['entry_miconwidth'] = $this->language->get('entry_miconwidth');
        $data['entry_miconheight'] = $this->language->get('entry_miconheight');
		$data['entry_wgdlg'] = $this->language->get('entry_wgdlg');
		$data['help_wgdlg'] = $this->language->get('help_wgdlg');
		$data['entry_wgdlg1'] = $this->language->get('entry_wgdlg1');
		$data['entry_wgdlg2'] = $this->language->get('entry_wgdlg2');
		$data['entry_emall'] = $this->language->get('entry_emall');
		$data['entry_weblink'] = $this->language->get('entry_weblink');
		$data['entry_qq'] = $this->language->get('entry_qq');
		$data['entry_weibo'] = $this->language->get('entry_weibo');
		
		$data['button_save'] = $this->language->get('button_save');
		$data['button_cancel'] = $this->language->get('button_cancel');

		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['key'])) {
			$data['error_key'] = $this->error['key'];
		} else {
			$data['error_key'] = '';
		}
		if (isset($this->error['wgdlg1'])) {
			$data['error_wgdlg1'] = $this->error['wgdlg1'];
		} else {
			$data['error_wgdlg1'] = '';
		}
		if (isset($this->error['wgdlg2'])) {
			$data['error_wgdlg2'] = $this->error['wgdlg2'];
		} else {
			$data['error_wgdlg2'] = '';
		}
		if (isset($this->error['coordinate'])) {
			$data['error_coordinate'] = $this->error['coordinate'];
		} else {
			$data['error_coordinate'] = '';
		}
		
		$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')
		);

		if (!isset($this->request->get['module_id'])) {
			$data['breadcrumbs'][] = array(
				'text' => preg_replace('/<(.*?)strong>/i', '', $this->language->get('heading_title')),
				'href' => $this->url->link('extension/module/mapapicwkj', 'token=' . $this->session->data['token'], 'SSL')
			);
		} else {
			$data['breadcrumbs'][] = array(
				'text' => preg_replace('/<(.*?)strong>/i', '', $this->language->get('heading_title')),
				'href' => $this->url->link('extension/module/mapapicwkj', 'token=' . $this->session->data['token'] . '&module_id=' . $this->request->get['module_id'], 'SSL')
			);
		}

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

		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['key'])) {
			$data['key'] = $this->request->post['key'];
		} elseif (!empty($module_info)) {
			$data['key'] = $module_info['key'];
		} else {
			$data['key'] = '';
		}
		
		if (isset($this->request->post['coordinate'])) {
			$data['coordinate'] = $this->request->post['coordinate'];
		} elseif (!empty($module_info)) {
			$data['coordinate'] = $module_info['coordinate'];
		} else {
			$data['coordinate'] = '';
		}				
		if (isset($this->request->post['cname'])) {
			$data['cname'] = $this->request->post['cname'];
		} elseif (!empty($module_info)) {
			$data['cname'] = $module_info['cname'];
		} else {
			$data['cname'] = '';
		}
		if (isset($this->request->post['cdescribe'])) {
			$data['cdescribe'] = $this->request->post['cdescribe'];
		} elseif (!empty($module_info)) {
			$data['cdescribe'] = $module_info['cdescribe'];
		} else {
			$data['cdescribe'] = '';
		}
		if (isset($this->request->post['cadress'])) {
			$data['cadress'] = $this->request->post['cadress'];
		} elseif (!empty($module_info)) {
			$data['cadress'] = $module_info['cadress'];
		} else {
			$data['cadress'] = '';
		}

		if (isset($this->request->post['telephone'])) {
			$data['telephone'] = $this->request->post['telephone'];
		} elseif (!empty($module_info)) {
			$data['telephone'] = $module_info['telephone'];
		} else {
			$data['telephone'] = '';
		}
		if (isset($this->request->post['sectionbj'])) {
			$data['sectionbj'] = $this->request->post['sectionbj'];
		} elseif (!empty($module_info)) {
			$data['sectionbj'] = $module_info['sectionbj'];
		} else {
			$data['sectionbj'] = '';
		}
		if (isset($this->request->post['sectionjbbj'])) {
			$data['sectionjbbj'] = $this->request->post['sectionjbbj'];
		} elseif (!empty($module_info)) {
			$data['sectionjbbj'] = $module_info['sectionjbbj'];
		} else {
			$data['sectionjbbj'] = '';
		}
		if (isset($this->request->post['sectiontitle'])) {
			$data['sectiontitle'] = $this->request->post['sectiontitle'];
		} elseif (!empty($module_info)) {
			$data['sectiontitle'] = $module_info['sectiontitle'];
		} else {
			$data['sectiontitle'] = '';
		}		
		if (isset($this->request->post['sectiondescribe'])) {
			$data['sectiondescribe'] = $this->request->post['sectiondescribe'];
		} elseif (!empty($module_info)) {
			$data['sectiondescribe'] = $module_info['sectiondescribe'];
		} else {
			$data['sectiondescribe'] = '';
		}
		if (isset($this->request->post['sectionptop'])) {
			$data['sectionptop'] = $this->request->post['sectionptop'];
		} elseif (!empty($module_info)) {
			$data['sectionptop'] = $module_info['sectionptop'];
		} else {
			$data['sectionptop'] = '70';
		}
		if (isset($this->request->post['sectionpbuttom'])) {
			$data['sectionpbuttom'] = $this->request->post['sectionpbuttom'];
		} elseif (!empty($module_info)) {
			$data['sectionpbuttom'] = $module_info['sectionpbuttom'];
		} else {
			$data['sectionpbuttom'] = '70';
		}
		if (isset($this->request->post['miconbj'])) {
			$data['miconbj'] = $this->request->post['miconbj'];
		} elseif (!empty($module_info)) {
			$data['miconbj'] = $module_info['miconbj'];
		} else {
			$data['miconbj'] = '';
		}
		if (isset($this->request->post['miconcolorc'])) {
			$data['miconcolorc'] = $this->request->post['miconcolorc'];
		} elseif (!empty($module_info)) {
			$data['miconcolorc'] = $module_info['miconcolorc'];
		} else {
			$data['miconcolorc'] = '';
		}		
		if (isset($this->request->post['miconcolor'])) {
			$data['miconcolor'] = $this->request->post['miconcolor'];
		} elseif (!empty($module_info)) {
			$data['miconcolor'] = $module_info['miconcolor'];
		} else {
			$data['miconcolor'] = '';
		}
		if (isset($this->request->post['hoverbj'])) {
			$data['hoverbj'] = $this->request->post['hoverbj'];
		} elseif (!empty($module_info)) {
			$data['hoverbj'] = $module_info['hoverbj'];
		} else {
			$data['hoverbj'] = '#f8f8f8';
		}
		if (isset($this->request->post['hovertit'])) {
			$data['hovertit'] = $this->request->post['hovertit'];
		} elseif (!empty($module_info)) {
			$data['hovertit'] = $module_info['hovertit'];
		} else {
			$data['hovertit'] = '#333333';
		}
		if (isset($this->request->post['miconround'])) {
			$data['miconround'] = $this->request->post['miconround'];
		} elseif (!empty($module_info)) {
			$data['miconround'] = $module_info['miconround'];
		} else {
			$data['miconround'] = '50';
		}
		if (isset($this->request->post['miconsize'])) {
			$data['miconsize'] = $this->request->post['miconsize'];
		} elseif (!empty($module_info)) {
			$data['miconsize'] = $module_info['miconsize'];
		} else {
			$data['miconsize'] = '18';
		}	
		if (isset($this->request->post['miconborder'])) {
			$data['miconborder'] = $this->request->post['miconborder'];
		} elseif (!empty($module_info)) {
			$data['miconborder'] = $module_info['miconborder'];
		} else {
			$data['miconborder'] = '0';
		}
		if (isset($this->request->post['miconbc'])) {
			$data['miconbc'] = $this->request->post['miconbc'];
		} elseif (!empty($module_info)) {
			$data['miconbc'] = $module_info['miconbc'];
		} else {
			$data['miconbc'] = '';
		}
		if (isset($this->request->post['miconwidth'])) {
			$data['miconwidth'] = $this->request->post['miconwidth'];
		} elseif (!empty($module_info)) {
			$data['miconwidth'] = $module_info['miconwidth'];
		} else {
			$data['miconwidth'] = '40';
		}
		if (isset($this->request->post['miconheight'])) {
			$data['miconheight'] = $this->request->post['miconheight'];
		} elseif (!empty($module_info)) {
			$data['miconheight'] = $module_info['miconheight'];
		} else {
			$data['miconheight'] = '40';
		}
		if (isset($this->request->post['wgdlg1'])) {
			$data['wgdlg1'] = $this->request->post['wgdlg1'];
		} elseif (!empty($module_info)) {
			$data['wgdlg1'] = $module_info['wgdlg1'];
		} else {
			$data['wgdlg1'] = '6';
		}
		if (isset($this->request->post['wgdlg2'])) {
			$data['wgdlg2'] = $this->request->post['wgdlg2'];
		} elseif (!empty($module_info)) {
			$data['wgdlg2'] = $module_info['wgdlg2'];
		} else {
			$data['wgdlg2'] = '6';
		}
		if (isset($this->request->post['emall'])) {
			$data['emall'] = $this->request->post['emall'];
		} elseif (!empty($module_info)) {
			$data['emall'] = $module_info['emall'];
		} else {
			$data['emall'] = '';
		}
		if (isset($this->request->post['weblink'])) {
			$data['weblink'] = $this->request->post['weblink'];
		} elseif (!empty($module_info)) {
			$data['weblink'] = $module_info['weblink'];
		} else {
			$data['weblink'] = '';
		}
		if (isset($this->request->post['qq'])) {
			$data['qq'] = $this->request->post['qq'];
		} elseif (!empty($module_info)) {
			$data['qq'] = $module_info['qq'];
		} else {
			$data['qq'] = '';
		}
		if (isset($this->request->post['weibo'])) {
			$data['weibo'] = $this->request->post['weibo'];
		} elseif (!empty($module_info)) {
			$data['weibo'] = $module_info['weibo'];
		} else {
			$data['weibo'] = '';
		}
		if (isset($this->request->post['status'])) {
			$data['status'] = $this->request->post['status'];
		} elseif (!empty($module_info)) {
			$data['status'] = $module_info['status'];
		} else {
			$data['status'] = '';
		}

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

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

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

		if ((utf8_strlen($this->request->post['name']) < 3) || (utf8_strlen($this->request->post['name']) > 64)) {
			$this->error['name'] = $this->language->get('error_name');
		}

		if ((utf8_strlen($this->request->post['key']) < 3) || (utf8_strlen($this->request->post['key']) > 64)) {
			$this->error['key'] = $this->language->get('error_key');
		}
		
		if ((utf8_strlen($this->request->post['coordinate']) < 3) || (utf8_strlen($this->request->post['coordinate']) > 64)) {
			$this->error['coordinate'] = $this->language->get('error_coordinate');
		}
		if ((utf8_strlen($this->request->post['wgdlg1']) < 1) || (utf8_strlen($this->request->post['wgdlg1']) > 64)) {
			$this->error['wgdlg1'] = $this->language->get('error_wgdlg1');
		}
		if ((utf8_strlen($this->request->post['wgdlg2']) < 1) || (utf8_strlen($this->request->post['wgdlg2']) > 64)) {
			$this->error['wgdlg2'] = $this->language->get('error_wgdlg2');
		}
		return !$this->error;
	}
}
