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

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

		$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('navbfooter', $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_powered'] = $this->language->get('entry_powered');
		$data['entry_poweredlink'] = $this->language->get('entry_poweredlink');
		$data['entry_miit'] = $this->language->get('entry_miit');
		$data['entry_imagelogo'] = $this->language->get('entry_imagelogo');
		$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_titlejbcss'] = $this->language->get('entry_titlejbcss');
		$data['entry_titlecss'] = $this->language->get('entry_titlecss');
		$data['entry_title'] = $this->language->get('entry_title');
		$data['entry_image'] = $this->language->get('entry_image');
		$data['entry_sort_order'] = $this->language->get('entry_sort_order');

		$data['entry_footerpy'] = $this->language->get('entry_footerpy');
		$data['entry_footerpx'] = $this->language->get('entry_footerpx');
		$data['entry_fticon'] = $this->language->get('entry_fticon');
		$data['entry_mobilestyle'] = $this->language->get('entry_mobilestyle');
		$data['entry_ftnameone'] = $this->language->get('entry_ftnameone');	
		$data['entry_ftnametwo'] = $this->language->get('entry_ftnametwo');	
		$data['entry_ftnamethree'] = $this->language->get('entry_ftnamethree');
		$data['entry_ftnamefour'] = $this->language->get('entry_ftnamefour');	
		$data['entry_ftnamefive'] = $this->language->get('entry_ftnamefive');	
		$data['entry_ftnamesix'] = $this->language->get('entry_ftnamesix');	
		$data['entry_fticonone'] = $this->language->get('entry_fticonone');	
		$data['entry_fticontwo'] = $this->language->get('entry_fticontwo');	
		$data['entry_fticonthree'] = $this->language->get('entry_fticonthree');	
		$data['entry_fticonfour'] = $this->language->get('entry_fticonfour');	
		$data['entry_fticonfive'] = $this->language->get('entry_fticonfive');	
		$data['entry_fticonsix'] = $this->language->get('entry_fticonsix');	
		$data['entry_ftlinkone'] = $this->language->get('entry_ftlinkone');	
		$data['entry_ftlinktwo'] = $this->language->get('entry_ftlinktwo');
		$data['entry_ftlinkthree'] = $this->language->get('entry_ftlinkthree');
		$data['entry_ftlinkfour'] = $this->language->get('entry_ftlinkfour');
        $data['entry_ftlinkfive'] = $this->language->get('entry_ftlinkfive');	
        $data['entry_ftlinksix'] = $this->language->get('entry_ftlinksix');
        $data['entry_ftsetone'] = $this->language->get('entry_ftsetone');	
		$data['entry_ftsettwo'] = $this->language->get('entry_ftsettwo');	
		$data['entry_ftsetthree'] = $this->language->get('entry_ftsetthree');	
		$data['entry_ftsetfour'] = $this->language->get('entry_ftsetfour');
		$data['entry_ftsetfive'] = $this->language->get('entry_ftsetfive');	
		$data['entry_ftsetsix'] = $this->language->get('entry_ftsetsix');	
		$data['entry_ftstyle'] = $this->language->get('entry_ftstyle');
		$data['entry_ftbj'] = $this->language->get('entry_ftbj');
		$data['entry_ftcolor'] = $this->language->get('entry_ftcolor');
		$data['entry_ftround'] = $this->language->get('entry_ftround');
		$data['entry_ftsize'] = $this->language->get('entry_ftsize');
		$data['entry_ftborder'] = $this->language->get('entry_ftborder');
		$data['entry_ftbc'] = $this->language->get('entry_ftbc');
		$data['entry_ftwidth'] = $this->language->get('entry_ftwidth');
		$data['entry_ftheight'] = $this->language->get('entry_ftheight');
		$data['entry_columns'] = $this->language->get('entry_columns');
		$data['help_columns'] = $this->language->get('help_columns');
		$data['entry_columnone'] = $this->language->get('entry_columnone');
		$data['entry_columntwo'] = $this->language->get('entry_columntwo');
		$data['entry_columnthree'] = $this->language->get('entry_columnthree');
		$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['columnone'])) {
			$data['error_columnone'] = $this->error['columnone'];
		} else {
			$data['error_columnone'] = '';
		}
		if (isset($this->error['columntwo'])) {
			$data['error_columntwo'] = $this->error['columntwo'];
		} else {
			$data['error_columntwo'] = '';
		}
		if (isset($this->error['columnthree'])) {
			$data['error_columnthree'] = $this->error['columnthree'];
		} else {
			$data['error_columnthree'] = '';
		}
		$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/navbfooter', 'token=' . $this->session->data['token'], 'SSL')
		);

		if (!isset($this->request->get['module_id'])) {
			$data['action'] = $this->url->link('extension/module/navbfooter', 'token=' . $this->session->data['token'], 'SSL');
		} else {
			$data['action'] = $this->url->link('extension/module/navbfooter', '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['powered'])) {
			$data['powered'] = $this->request->post['powered'];
		} elseif (!empty($module_info)) {
			$data['powered'] = $module_info['powered'];
		} else {
			$data['powered'] = '天津触网科技有限公司';
		}
		if (isset($this->request->post['poweredlink'])) {
			$data['poweredlink'] = $this->request->post['poweredlink'];
		} elseif (!empty($module_info)) {
			$data['poweredlink'] = $module_info['poweredlink'];
		} else {
			$data['poweredlink'] = 'http://wogaoding.net';
		}
		if (isset($this->request->post['miit'])) {
			$data['miit'] = $this->request->post['miit'];
		} elseif (!empty($module_info)) {
			$data['miit'] = $module_info['miit'];
		} else {
			$data['miit'] = '津ICP备16004560号-1';
		}
		if (isset($this->request->post['bgcolor'])) {
			$data['bgcolor'] = $this->request->post['bgcolor'];
		} elseif (!empty($module_info)) {
			$data['bgcolor'] = $module_info['bgcolor'];
		} else {
			$data['bgcolor'] = '#333333';
		}
		if (isset($this->request->post['titlecss'])) {
			$data['titlecss'] = $this->request->post['titlecss'];
		} elseif (!empty($module_info)) {
			$data['titlecss'] = $module_info['titlecss'];
		} else {
			$data['titlecss'] = '#999999';
		}
		if (isset($this->request->post['footerpy'])) {
			$data['footerpy'] = $this->request->post['footerpy'];
		} elseif (!empty($module_info)) {
			$data['footerpy'] = $module_info['footerpy'];
		} else {
			$data['footerpy'] = '30';
		}
		if (isset($this->request->post['mobilestyle'])) {
			$data['mobilestyle'] = $this->request->post['mobilestyle'];
		} elseif (!empty($module_info)) {
			$data['mobilestyle'] = $module_info['mobilestyle'];
		} else {
			$data['mobilestyle'] = '';
		}
		if (isset($this->request->post['footerpx'])) {
			$data['footerpx'] = $this->request->post['footerpx'];
		} elseif (!empty($module_info)) {
			$data['footerpx'] = $module_info['footerpx'];
		} else {
			$data['footerpx'] = '60';
		}
		if (isset($this->request->post['ftnameone'])) {
			$data['ftnameone'] = $this->request->post['ftnameone'];
		} elseif (!empty($module_info)) {
			$data['ftnameone'] = $module_info['ftnameone'];
		} else {
			$data['ftnameone'] = '新浪微博';
		}
		if (isset($this->request->post['ftnametwo'])) {
			$data['ftnametwo'] = $this->request->post['ftnametwo'];
		} elseif (!empty($module_info)) {
			$data['ftnametwo'] = $module_info['ftnametwo'];
		} else {
			$data['ftnametwo'] = '腾讯微博';
		}
		if (isset($this->request->post['ftnamethree'])) {
			$data['ftnamethree'] = $this->request->post['ftnamethree'];
		} elseif (!empty($module_info)) {
			$data['ftnamethree'] = $module_info['ftnamethree'];
		} else {
			$data['ftnamethree'] = '';
		}
		if (isset($this->request->post['ftnamefour'])) {
			$data['ftnamefour'] = $this->request->post['ftnamefour'];
		} elseif (!empty($module_info)) {
			$data['ftnamefour'] = $module_info['ftnamefour'];
		} else {
			$data['ftnamefour'] = '';
		}
		if (isset($this->request->post['ftnamefive'])) {
			$data['ftnamefive'] = $this->request->post['ftnamefive'];
		} elseif (!empty($module_info)) {
			$data['ftnamefive'] = $module_info['ftnamefive'];
		} else {
			$data['ftnamefive'] = '';
		}
		if (isset($this->request->post['ftnamesix'])) {
			$data['ftnamesix'] = $this->request->post['ftnamesix'];
		} elseif (!empty($module_info)) {
			$data['ftnamesix'] = $module_info['ftnamesix'];
		} else {
			$data['ftnamesix'] = '';
		}
		if (isset($this->request->post['fticonone'])) {
			$data['fticonone'] = $this->request->post['fticonone'];
		} elseif (!empty($module_info)) {
			$data['fticonone'] = $module_info['fticonone'];
		} else {
			$data['fticonone'] = 'fa fa-weibo';
		}
		if (isset($this->request->post['fticontwo'])) {
			$data['fticontwo'] = $this->request->post['fticontwo'];
		} elseif (!empty($module_info)) {
			$data['fticontwo'] = $module_info['fticontwo'];
		} else {
			$data['fticontwo'] = 'fa fa-tencent-weibo';
		}
		if (isset($this->request->post['fticonthree'])) {
			$data['fticonthree'] = $this->request->post['fticonthree'];
		} elseif (!empty($module_info)) {
			$data['fticonthree'] = $module_info['fticonthree'];
		} else {
			$data['fticonthree'] = '';
		}	
		if (isset($this->request->post['fticonfour'])) {
			$data['fticonfour'] = $this->request->post['fticonfour'];
		} elseif (!empty($module_info)) {
			$data['fticonfour'] = $module_info['fticonfour'];
		} else {
			$data['fticonfour'] = '';
		}
		if (isset($this->request->post['fticonfive'])) {
			$data['fticonfive'] = $this->request->post['fticonfive'];
		} elseif (!empty($module_info)) {
			$data['fticonfive'] = $module_info['fticonfive'];
		} else {
			$data['fticonfive'] = '';
		}
		if (isset($this->request->post['fticonsix'])) {
			$data['fticonsix'] = $this->request->post['fticonsix'];
		} elseif (!empty($module_info)) {
			$data['fticonsix'] = $module_info['fticonsix'];
		} else {
			$data['fticonsix'] = '';
		}
		if (isset($this->request->post['ftlinkone'])) {
			$data['ftlinkone'] = $this->request->post['ftlinkone'];
		} elseif (!empty($module_info)) {
			$data['ftlinkone'] = $module_info['ftlinkone'];
		} else {
			$data['ftlinkone'] = '';
		}
		if (isset($this->request->post['ftlinktwo'])) {
			$data['ftlinktwo'] = $this->request->post['ftlinktwo'];
		} elseif (!empty($module_info)) {
			$data['ftlinktwo'] = $module_info['ftlinktwo'];
		} else {
			$data['ftlinktwo'] = '';
		}
		if (isset($this->request->post['ftlinkthree'])) {
			$data['ftlinkthree'] = $this->request->post['ftlinkthree'];
		} elseif (!empty($module_info)) {
			$data['ftlinkthree'] = $module_info['ftlinkthree'];
		} else {
			$data['ftlinkthree'] = '';
		}
		if (isset($this->request->post['ftlinkfour'])) {
			$data['ftlinkfour'] = $this->request->post['ftlinkfour'];
		} elseif (!empty($module_info)) {
			$data['ftlinkfour'] = $module_info['ftlinkfour'];
		} else {
			$data['ftlinkfour'] = '';
		}
		if (isset($this->request->post['ftlinkfive'])) {
			$data['ftlinkfive'] = $this->request->post['ftlinkfive'];
		} elseif (!empty($module_info)) {
			$data['ftlinkfive'] = $module_info['ftlinkfive'];
		} else {
			$data['ftlinkfive'] = '';
		}
		if (isset($this->request->post['ftlinksix'])) {
			$data['ftlinksix'] = $this->request->post['ftlinksix'];
		} elseif (!empty($module_info)) {
			$data['ftlinksix'] = $module_info['ftlinksix'];
		} else {
			$data['ftlinksix'] = '';
		}
		if (isset($this->request->post['ftbj'])) {
			$data['ftbj'] = $this->request->post['ftbj'];
		} elseif (!empty($module_info)) {
			$data['ftbj'] = $module_info['ftbj'];
		} else {
			$data['ftbj'] = '#333333';
		}
		if (isset($this->request->post['ftcolor'])) {
			$data['ftcolor'] = $this->request->post['ftcolor'];
		} elseif (!empty($module_info)) {
			$data['ftcolor'] = $module_info['ftcolor'];
		} else {
			$data['ftcolor'] = '#ffffff';
		}
		if (isset($this->request->post['ftround'])) {
			$data['ftround'] = $this->request->post['ftround'];
		} elseif (!empty($module_info)) {
			$data['ftround'] = $module_info['ftround'];
		} else {
			$data['ftround'] = '50';
		}
		if (isset($this->request->post['ftsize'])) {
			$data['ftsize'] = $this->request->post['ftsize'];
		} elseif (!empty($module_info)) {
			$data['ftsize'] = $module_info['ftsize'];
		} else {
			$data['ftsize'] = '18';
		}
		if (isset($this->request->post['ftborder'])) {
			$data['ftborder'] = $this->request->post['ftborder'];
		} elseif (!empty($module_info)) {
			$data['ftborder'] = $module_info['ftborder'];
		} else {
			$data['ftborder'] = '1';
		}
		if (isset($this->request->post['ftbc'])) {
			$data['ftbc'] = $this->request->post['ftbc'];
		} elseif (!empty($module_info)) {
			$data['ftbc'] = $module_info['ftbc'];
		} else {
			$data['ftbc'] = '';
		}
		if (isset($this->request->post['ftwidth'])) {
			$data['ftwidth'] = $this->request->post['ftwidth'];
		} elseif (!empty($module_info)) {
			$data['ftwidth'] = $module_info['ftwidth'];
		} else {
			$data['ftwidth'] = '35';
		}
		if (isset($this->request->post['ftheight'])) {
			$data['ftheight'] = $this->request->post['ftheight'];
		} elseif (!empty($module_info)) {
			$data['ftheight'] = $module_info['ftheight'];
		} else {
			$data['ftheight'] = '35';
		}
		if (isset($this->request->post['columnone'])) {
			$data['columnone'] = $this->request->post['columnone'];
		} elseif (!empty($module_info)) {
			$data['columnone'] = $module_info['columnone'];
		} else {
			$data['columnone'] = '4';
		}	
		if (isset($this->request->post['columntwo'])) {
			$data['columntwo'] = $this->request->post['columntwo'];
		} elseif (!empty($module_info)) {
			$data['columntwo'] = $module_info['columntwo'];
		} else {
			$data['columntwo'] = '4';
		}	
		if (isset($this->request->post['columnthree'])) {
			$data['columnthree'] = $this->request->post['columnthree'];
		} elseif (!empty($module_info)) {
			$data['columnthree'] = $module_info['columnthree'];
		} else {
			$data['columnthree'] = '4';
		}			
		if (isset($this->request->post['blog'])) {
			$data['blog'] = $this->request->post['blog'];
		} elseif (!empty($module_info)) {
			$data['blog'] = $module_info['blog'];
		} else {
			$data['blog'] = '';
		}
		$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) {
			if (is_file(DIR_IMAGE . $service['image'])) {
				$image = $service['image'];
				$thumb = $service['image'];
			} else {
				$image = '';
				$thumb = 'no_image.png';
			}

			$data['services'][] = array(
				'title'       => $service['title'],
				'fticon'        => $service['fticon'],
				'image'       => $image,
				'thumb'       => $this->model_tool_image->resize($thumb, 30, 30),
				'sort_order'  => $service['sort_order']
			);
		}

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

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

	protected function validate() {
		if (!$this->user->hasPermission('modify', 'extension/module/navbfooter')) {
			$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['columnone']) {
			$this->error['columnone'] = $this->language->get('error_columnone');
		}
		if (!$this->request->post['columntwo']) {
			$this->error['columntwo'] = $this->language->get('error_columntwo');
		}
		if (!$this->request->post['columnthree']) {
			$this->error['columnthree'] = $this->language->get('error_columnthree');
		}
		return !$this->error;
	}
}