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

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

		$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('navmenuten', $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_image'] = $this->language->get('entry_image');
		$data['entry_width'] = $this->language->get('entry_width');
		$data['entry_height'] = $this->language->get('entry_height');
		$data['entry_bgcolortitle'] = $this->language->get('entry_bgcolortitle');
		$data['entry_bgcolor'] = $this->language->get('entry_bgcolor');
		$data['entry_minheight'] = $this->language->get('entry_minheight');
		$data['entry_htx'] = $this->language->get('entry_htx');
		$data['entry_titlejbcss'] = $this->language->get('entry_titlejbcss');
		$data['entry_titlecss'] = $this->language->get('entry_titlecss');
		$data['entry_title'] = $this->language->get('entry_title');
		$data['entry_link'] = $this->language->get('entry_link');
		$data['help_link'] = $this->language->get('help_link');
		$data['entry_image'] = $this->language->get('entry_image');
		$data['entry_sort_order'] = $this->language->get('entry_sort_order');
		$data['entry_navp'] = $this->language->get('entry_navp');
		$data['help_navp'] = $this->language->get('help_navp');
		$data['entry_qyxx1'] = $this->language->get('entry_qyxx1');
		$data['entry_qyxx2'] = $this->language->get('entry_qyxx2');
		$data['entry_qyxx3'] = $this->language->get('entry_qyxx3');
		$data['entry_qyxx4'] = $this->language->get('entry_qyxx4');
		$data['entry_icon1'] = $this->language->get('entry_icon1');
		$data['entry_nametitle1'] = $this->language->get('entry_nametitle1');
		$data['entry_xinxims1'] = $this->language->get('entry_xinxims1');
		$data['entry_icon2'] = $this->language->get('entry_icon2');
		$data['entry_nametitle2'] = $this->language->get('entry_nametitle2');
		$data['entry_xinxims2'] = $this->language->get('entry_xinxims2');
		$data['entry_icon3'] = $this->language->get('entry_icon3');
		$data['entry_nametitle3'] = $this->language->get('entry_nametitle3');
		$data['entry_xinxims3'] = $this->language->get('entry_xinxims3');
		$data['entry_icon4'] = $this->language->get('entry_icon4');
		$data['entry_nametitle4'] = $this->language->get('entry_nametitle4');
		$data['entry_xinxims4'] = $this->language->get('entry_xinxims4');
		$data['entry_iconstyles'] = $this->language->get('entry_iconstyles');
		$data['entry_iconsizes'] = $this->language->get('entry_iconsizes');
		$data['entry_iconcolors'] = $this->language->get('entry_iconcolors');
		$data['entry_tncolors'] = $this->language->get('entry_tncolors');
		$data['entry_xxmscolors'] = $this->language->get('entry_xxmscolors');
		$data['entry_blog'] = $this->language->get('entry_blog');
		
		$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['navp'])) {
			$data['error_navp'] = $this->error['navp'];
		} else {
			$data['error_navp'] = '';
		}
		$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_extension'),
			'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/navmenuten', 'token=' . $this->session->data['token'], 'SSL')
		);

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

		if (isset($this->request->post['height'])) {
			$data['height'] = $this->request->post['height'];
		} elseif (!empty($module_info)) {
			$data['height'] = $module_info['height'];
		} else {
			$data['height'] = '46';
		}
		if (isset($this->request->post['bgcolor'])) {
			$data['bgcolor'] = $this->request->post['bgcolor'];
		} elseif (!empty($module_info)) {
			$data['bgcolor'] = $module_info['bgcolor'];
		} else {
			$data['bgcolor'] = '#ffffff';
		}
		if (isset($this->request->post['minheight'])) {
			$data['minheight'] = $this->request->post['minheight'];
		} elseif (!empty($module_info)) {
			$data['minheight'] = $module_info['minheight'];
		} else {
			$data['minheight'] = '10';
		}
		if (isset($this->request->post['htx'])) {
			$data['htx'] = $this->request->post['htx'];
		} elseif (!empty($module_info)) {
			$data['htx'] = $module_info['htx'];
		} else {
			$data['htx'] = '19';
		}
		if (isset($this->request->post['titlecss'])) {
			$data['titlecss'] = $this->request->post['titlecss'];
		} elseif (!empty($module_info)) {
			$data['titlecss'] = $module_info['titlecss'];
		} else {
			$data['titlecss'] = '#000000';
		}
		if (isset($this->request->post['navp'])) {
			$data['navp'] = $this->request->post['navp'];
		} elseif (!empty($module_info)) {
			$data['navp'] = $module_info['navp'];
		} else {
			$data['navp'] = 'l';
		}
		if (isset($this->request->post['icon1'])) {
			$data['icon1'] = $this->request->post['icon1'];
		} elseif (!empty($module_info)) {
			$data['icon1'] = $module_info['icon1'];
		} else {
			$data['icon1'] = '';
		}
		if (isset($this->request->post['nametitle1'])) {
			$data['nametitle1'] = $this->request->post['nametitle1'];
		} elseif (!empty($module_info)) {
			$data['nametitle1'] = $module_info['nametitle1'];
		} else {
			$data['nametitle1'] = '';
		}
		if (isset($this->request->post['xinxims1'])) {
			$data['xinxims1'] = $this->request->post['xinxims1'];
		} elseif (!empty($module_info)) {
			$data['xinxims1'] = $module_info['xinxims1'];
		} else {
			$data['xinxims1'] = '';
		}
		if (isset($this->request->post['icon2'])) {
			$data['icon2'] = $this->request->post['icon2'];
		} elseif (!empty($module_info)) {
			$data['icon2'] = $module_info['icon2'];
		} else {
			$data['icon2'] = '';
		}
		if (isset($this->request->post['nametitle2'])) {
			$data['nametitle2'] = $this->request->post['nametitle2'];
		} elseif (!empty($module_info)) {
			$data['nametitle2'] = $module_info['nametitle2'];
		} else {
			$data['nametitle2'] = '';
		}
		if (isset($this->request->post['xinxims2'])) {
			$data['xinxims2'] = $this->request->post['xinxims2'];
		} elseif (!empty($module_info)) {
			$data['xinxims2'] = $module_info['xinxims2'];
		} else {
			$data['xinxims2'] = '';
		}
		if (isset($this->request->post['icon3'])) {
			$data['icon3'] = $this->request->post['icon3'];
		} elseif (!empty($module_info)) {
			$data['icon3'] = $module_info['icon3'];
		} else {
			$data['icon3'] = '';
		}
		if (isset($this->request->post['nametitle3'])) {
			$data['nametitle3'] = $this->request->post['nametitle3'];
		} elseif (!empty($module_info)) {
			$data['nametitle3'] = $module_info['nametitle3'];
		} else {
			$data['nametitle3'] = '';
		}
		if (isset($this->request->post['xinxims3'])) {
			$data['xinxims3'] = $this->request->post['xinxims3'];
		} elseif (!empty($module_info)) {
			$data['xinxims3'] = $module_info['xinxims3'];
		} else {
			$data['xinxims3'] = '';
		}
		if (isset($this->request->post['icon4'])) {
			$data['icon4'] = $this->request->post['icon4'];
		} elseif (!empty($module_info)) {
			$data['icon4'] = $module_info['icon4'];
		} else {
			$data['icon4'] = '';
		}
		if (isset($this->request->post['nametitle4'])) {
			$data['nametitle4'] = $this->request->post['nametitle4'];
		} elseif (!empty($module_info)) {
			$data['nametitle4'] = $module_info['nametitle4'];
		} else {
			$data['nametitle4'] = '';
		}
		if (isset($this->request->post['xinxims4'])) {
			$data['xinxims4'] = $this->request->post['xinxims4'];
		} elseif (!empty($module_info)) {
			$data['xinxims4'] = $module_info['xinxims4'];
		} else {
			$data['xinxims4'] = '';
		}
		if (isset($this->request->post['iconcolors'])) {
			$data['iconcolors'] = $this->request->post['iconcolors'];
		} elseif (!empty($module_info)) {
			$data['iconcolors'] = $module_info['iconcolors'];
		} else {
			$data['iconcolors'] = '';
		}
		if (isset($this->request->post['iconsizes'])) {
			$data['iconsizes'] = $this->request->post['iconsizes'];
		} elseif (!empty($module_info)) {
			$data['iconsizes'] = $module_info['iconsizes'];
		} else {
			$data['iconsizes'] = '18';
		}
		if (isset($this->request->post['tncolors'])) {
			$data['tncolors'] = $this->request->post['tncolors'];
		} elseif (!empty($module_info)) {
			$data['tncolors'] = $module_info['tncolors'];
		} else {
			$data['tncolors'] = '';
		}
		if (isset($this->request->post['blog'])) {
			$data['blog'] = $this->request->post['blog'];
		} elseif (!empty($module_info)) {
			$data['blog'] = $module_info['blog'];
		} else {
			$data['blog'] = '';
		}
		if (isset($this->request->post['xxmscolors'])) {
			$data['xxmscolors'] = $this->request->post['xxmscolors'];
		} elseif (!empty($module_info)) {
			$data['xxmscolors'] = $module_info['xxmscolors'];
		} else {
			$data['xxmscolors'] = '';
		}
		$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['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/navmenuten.tpl', $data));
	}

	protected function validate() {
		if (!$this->user->hasPermission('modify', 'extension/module/navmenuten')) {
			$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 (!$this->request->post['navp']) {
			$this->error['navp'] = $this->language->get('error_navp');
		}
		return !$this->error;
	}
}