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

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

		$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('yaccordion', $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['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_segmentingline'] = $this->language->get('entry_segmentingline');
		$data['entry_linew'] = $this->language->get('entry_linew');
		$data['entry_lineh'] = $this->language->get('entry_lineh');
        $data['entry_linec'] = $this->language->get('entry_linec');	
		$data['entry_describe'] = $this->language->get('entry_describe');
		$data['entry_title'] = $this->language->get('entry_title');
		$data['entry_title1'] = $this->language->get('entry_title1');
		$data['entry_text'] = $this->language->get('entry_text');
		$data['entry_text1'] = $this->language->get('entry_text1');
		$data['entry_icon'] = $this->language->get('entry_icon');
		$data['entry_icon1'] = $this->language->get('entry_icon1');
		$data['entry_qiehuan'] = $this->language->get('entry_qiehuan');
		$data['help_qiehuan'] = $this->language->get('help_qiehuan');
		$data['entry_qiehuan1'] = $this->language->get('entry_qiehuan1');
		$data['entry_image'] = $this->language->get('entry_image');
		$data['entry_sort_order'] = $this->language->get('entry_sort_order');

		$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_acordion'] = $this->language->get('entry_acordion');
		$data['entry_acordionbj'] = $this->language->get('entry_acordionbj');
		$data['entry_acordionbjhover'] = $this->language->get('entry_acordionbjhover');
		$data['entry_acordiontit'] = $this->language->get('entry_acordiontit');
		$data['entry_acordiontithover'] = $this->language->get('entry_acordiontithover');
		$data['entry_button1'] = $this->language->get('entry_button1');
		$data['entry_button2'] = $this->language->get('entry_button2');
		$data['entry_buttonname1'] = $this->language->get('entry_buttonname1');
		$data['entry_buttonname2'] = $this->language->get('entry_buttonname2');
		$data['entry_qq1'] = $this->language->get('entry_qq1');
		$data['entry_qq2'] = $this->language->get('entry_qq2');
		$data['entry_alww1'] = $this->language->get('entry_alww1');
		$data['entry_alww2'] = $this->language->get('entry_alww2');
		$data['entry_link1'] = $this->language->get('entry_link1');
		$data['entry_link2'] = $this->language->get('entry_link2');
		$data['entry_buttoncolor1'] = $this->language->get('entry_buttoncolor1');
		$data['entry_buttoncolor2'] = $this->language->get('entry_buttoncolor2');		
		$data['entry_buttonstyle'] = $this->language->get('entry_buttonstyle');
		$data['entry_buttonsize'] = $this->language->get('entry_buttonsize');
		$data['entry_buttoninset'] = $this->language->get('entry_buttoninset');
		$data['entry_buttonborder'] = $this->language->get('entry_buttonborder');
		$data['entry_buttonround'] = $this->language->get('entry_buttonround');
		$data['entry_buttonhover'] = $this->language->get('entry_buttonhover');
        $data['entry_buttond'] = $this->language->get('entry_buttond');	
		
		$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['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'] = '';
		}
		$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/services', 'token=' . $this->session->data['token'], 'SSL')
		);

		if (!isset($this->request->get['module_id'])) {
			$data['action'] = $this->url->link('extension/module/yaccordion', 'token=' . $this->session->data['token'], 'SSL');
		} else {
			$data['action'] = $this->url->link('extension/module/yaccordion', '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['titlename'])) {
			$data['titlename'] = $this->request->post['titlename'];
		} elseif (!empty($module_info)) {
			$data['titlename'] = $module_info['titlename'];
		} else {
			$data['titlename'] = '';
		}
		if (isset($this->request->post['linew'])) {
			$data['linew'] = $this->request->post['linew'];
		} elseif (!empty($module_info)) {
			$data['linew'] = $module_info['linew'];
		} else {
			$data['linew'] = '30';
		}	
		if (isset($this->request->post['lineh'])) {
			$data['lineh'] = $this->request->post['lineh'];
		} elseif (!empty($module_info)) {
			$data['lineh'] = $module_info['lineh'];
		} else {
			$data['lineh'] = '2';
		}
		if (isset($this->request->post['linec'])) {
			$data['linec'] = $this->request->post['linec'];
		} elseif (!empty($module_info)) {
			$data['linec'] = $module_info['linec'];
		} else {
			$data['linec'] = '#000000';
		}
		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['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['acordionbj'])) {
			$data['acordionbj'] = $this->request->post['acordionbj'];
		} elseif (!empty($module_info)) {
			$data['acordionbj'] = $module_info['acordionbj'];
		} else {
			$data['acordionbj'] = '';
		}
		if (isset($this->request->post['acordionbjhover'])) {
			$data['acordionbjhover'] = $this->request->post['acordionbjhover'];
		} elseif (!empty($module_info)) {
			$data['acordionbjhover'] = $module_info['acordionbjhover'];
		} else {
			$data['acordionbjhover'] = '';
		}
		if (isset($this->request->post['acordiontit'])) {
			$data['acordiontit'] = $this->request->post['acordiontit'];
		} elseif (!empty($module_info)) {
			$data['acordiontit'] = $module_info['acordiontit'];
		} else {
			$data['acordiontit'] = '';
		}
		if (isset($this->request->post['acordiontithover'])) {
			$data['acordiontithover'] = $this->request->post['acordiontithover'];
		} elseif (!empty($module_info)) {
			$data['acordiontithover'] = $module_info['acordiontithover'];
		} else {
			$data['acordiontithover'] = '';
		}
		if (isset($this->request->post['buttonname1'])) {
			$data['buttonname1'] = $this->request->post['buttonname1'];
		} elseif (!empty($module_info)) {
			$data['buttonname1'] = $module_info['buttonname1'];
		} else {
			$data['buttonname1'] = '';
		}
		if (isset($this->request->post['buttonname2'])) {
			$data['buttonname2'] = $this->request->post['buttonname2'];
		} elseif (!empty($module_info)) {
			$data['buttonname2'] = $module_info['buttonname2'];
		} else {
			$data['buttonname2'] = '';
		}
		if (isset($this->request->post['qq1'])) {
			$data['qq1'] = $this->request->post['qq1'];
		} elseif (!empty($module_info)) {
			$data['qq1'] = $module_info['qq1'];
		} else {
			$data['qq1'] = '';
		}
		if (isset($this->request->post['qq2'])) {
			$data['qq2'] = $this->request->post['name'];
		} elseif (!empty($module_info)) {
			$data['qq2'] = $module_info['qq2'];
		} else {
			$data['qq2'] = '';
		}
		if (isset($this->request->post['alww1'])) {
			$data['alww1'] = $this->request->post['alww1'];
		} elseif (!empty($module_info)) {
			$data['alww1'] = $module_info['alww1'];
		} else {
			$data['alww1'] = '';
		}
		if (isset($this->request->post['alww2'])) {
			$data['alww2'] = $this->request->post['alww2'];
		} elseif (!empty($module_info)) {
			$data['alww2'] = $module_info['alww2'];
		} else {
			$data['alww2'] = '';
		}
		if (isset($this->request->post['link1'])) {
			$data['link1'] = $this->request->post['link1'];
		} elseif (!empty($module_info)) {
			$data['link1'] = $module_info['link1'];
		} else {
			$data['link1'] = '';
		}
		if (isset($this->request->post['link2'])) {
			$data['link2'] = $this->request->post['link2'];
		} elseif (!empty($module_info)) {
			$data['link2'] = $module_info['link2'];
		} else {
			$data['link2'] = '';
		}
		if (isset($this->request->post['buttoncolor1'])) {
			$data['buttoncolor1'] = $this->request->post['buttoncolor1'];
		} elseif (!empty($module_info)) {
			$data['buttoncolor1'] = $module_info['buttoncolor1'];
		} else {
			$data['buttoncolor1'] = 'white';
		}
		if (isset($this->request->post['buttoncolor2'])) {
			$data['buttoncolor2'] = $this->request->post['buttoncolor2'];
		} elseif (!empty($module_info)) {
			$data['buttoncolor2'] = $module_info['buttoncolor2'];
		} else {
			$data['buttoncolor2'] = 'white';
		}
		if (isset($this->request->post['buttonsize'])) {
			$data['buttonsize'] = $this->request->post['buttonsize'];
		} elseif (!empty($module_info)) {
			$data['buttonsize'] = $module_info['buttonsize'];
		} else {
			$data['buttonsize'] = 'md';
		}		
		if (isset($this->request->post['buttoninset'])) {
			$data['buttoninset'] = $this->request->post['buttoninset'];
		} elseif (!empty($module_info)) {
			$data['buttoninset'] = $module_info['buttoninset'];
		} else {
			$data['buttoninset'] = '';
		}	
		if (isset($this->request->post['buttonborder'])) {
			$data['buttonborder'] = $this->request->post['buttonborder'];
		} elseif (!empty($module_info)) {
			$data['buttonborder'] = $module_info['buttonborder'];
		} else {
			$data['buttonborder'] = '';
		}		
		if (isset($this->request->post['buttonround'])) {
			$data['buttonround'] = $this->request->post['buttonround'];
		} elseif (!empty($module_info)) {
			$data['buttonround'] = $module_info['buttonround'];
		} else {
			$data['buttonround'] = '50';
		}
		if (isset($this->request->post['buttonhover'])) {
			$data['buttonhover'] = $this->request->post['buttonhover'];
		} elseif (!empty($module_info)) {
			$data['buttonhover'] = $module_info['buttonhover'];
		} else {
			$data['buttonhover'] = '';
		}
		if (isset($this->request->post['buttond'])) {
			$data['buttond'] = $this->request->post['buttond'];
		} elseif (!empty($module_info)) {
			$data['buttond'] = $module_info['buttond'];
		} else {
			$data['buttond'] = '';
		}	
		$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(
				'title1'       => $service['title1'],
				'text1'        => $service['text1'],
				'icon1'        => $service['icon1'],
				'qiehuan1'        => $service['qiehuan1'],
				'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/yaccordion.tpl', $data));
	}

	protected function validate() {
		if (!$this->user->hasPermission('modify', 'extension/module/yaccordion')) {
			$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');
		}

		return !$this->error;
	}
}