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

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

		$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('Alerts', $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['entry_name'] = $this->language->get('entry_name');
		$data['entry_bgcolor'] = $this->language->get('entry_bgcolor');
		$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_title'] = $this->language->get('entry_title');
		$data['entry_section1'] = $this->language->get('entry_section1');
		$data['entry_status'] = $this->language->get('entry_status');

		$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_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_link'] = $this->language->get('entry_link');
		$data['entry_link1'] = $this->language->get('entry_link1');
		$data['entry_link2'] = $this->language->get('entry_link2');
		$data['entry_Alerts'] = $this->language->get('entry_Alerts');
		$data['entry_dbgcolor'] = $this->language->get('entry_dbgcolor');
		$data['entry_dbgcolorj'] = $this->language->get('entry_dbgcolorj');
		$data['entry_tcolor'] = $this->language->get('entry_tcolor');
		$data['entry_dpposition'] = $this->language->get('entry_dpposition');
		$data['help_dpposition'] = $this->language->get('help_dpposition');
		$data['entry_ttpadding'] = $this->language->get('entry_ttpadding');
		$data['entry_tbpadding'] = $this->language->get('entry_tbpadding');
		$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['entry_icons1'] = $this->language->get('entry_icons1');
		$data['entry_icons2'] = $this->language->get('entry_icons2');
		
		$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['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['dpposition'])) {
			$data['error_dpposition'] = $this->error['dpposition'];
		} else {
			$data['error_dpposition'] = '';
		}
		if (isset($this->error['title'])) {
			$data['error_title'] = $this->error['title'];
		} else {
			$data['error_title'] = '';
		}

		$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/Alerts', '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/Alerts', '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/Alerts', 'token=' . $this->session->data['token'], 'SSL');
		} else {
			$data['action'] = $this->url->link('extension/module/Alerts', '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'] = '';
		}
		if (isset($this->request->post['dbgcolor'])) {
			$data['dbgcolor'] = $this->request->post['dbgcolor'];
		} elseif (!empty($module_info)) {
			$data['dbgcolor'] = $module_info['dbgcolor'];
		} else {
			$data['dbgcolor'] = '';
		}
		$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'] = '500';
		}

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

		if (isset($this->request->post['title'])) {
			$data['title'] = $this->request->post['title'];
		} elseif (!empty($module_info)) {
			$data['title'] = $module_info['title'];
		} else {
			$data['title'] = '';
		}
		
		if (isset($this->request->post['section1'])) {
			$data['section1'] = $this->request->post['section1'];
		} elseif (!empty($module_info)) {
			$data['section1'] = $module_info['section1'];
		} else {
			$data['section1'] = '';
		}				
		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['link1'])) {
			$data['link1'] = $this->request->post['link1'];
		} elseif (!empty($module_info)) {
			$data['link1'] = $module_info['link1'];
		} else {
			$data['link1'] = 'http://wogaoding.net';
		}
		if (isset($this->request->post['link2'])) {
			$data['link2'] = $this->request->post['link2'];
		} elseif (!empty($module_info)) {
			$data['link2'] = $module_info['link2'];
		} else {
			$data['link2'] = 'http://wogaoding.net';
		}
		if (isset($this->request->post['dbgcolorj'])) {
			$data['dbgcolorj'] = $this->request->post['dbgcolorj'];
		} elseif (!empty($module_info)) {
			$data['dbgcolorj'] = $module_info['dbgcolorj'];
		} else {
			$data['dbgcolorj'] = '';
		}
		if (isset($this->request->post['tcolor'])) {
			$data['tcolor'] = $this->request->post['tcolor'];
		} elseif (!empty($module_info)) {
			$data['tcolor'] = $module_info['tcolor'];
		} else {
			$data['tcolor'] = '';
		}
		if (isset($this->request->post['dpposition'])) {
			$data['dpposition'] = $this->request->post['dpposition'];
		} elseif (!empty($module_info)) {
			$data['dpposition'] = $module_info['dpposition'];
		} else {
			$data['dpposition'] = 'center';
		}
		if (isset($this->request->post['ttpadding'])) {
			$data['ttpadding'] = $this->request->post['ttpadding'];
		} elseif (!empty($module_info)) {
			$data['ttpadding'] = $module_info['ttpadding'];
		} else {
			$data['ttpadding'] = '';
		}
		if (isset($this->request->post['tbpadding'])) {
			$data['tbpadding'] = $this->request->post['tbpadding'];
		} elseif (!empty($module_info)) {
			$data['tbpadding'] = $module_info['tbpadding'];
		} else {
			$data['tbpadding'] = 'fa fa-weixin';
		}
		if (isset($this->request->post['buttoncolor1'])) {
			$data['buttoncolor1'] = $this->request->post['buttoncolor1'];
		} elseif (!empty($module_info)) {
			$data['buttoncolor1'] = $module_info['buttoncolor1'];
		} else {
			$data['buttoncolor1'] = 'primary';
		}
		if (isset($this->request->post['buttoncolor2'])) {
			$data['buttoncolor2'] = $this->request->post['buttoncolor2'];
		} elseif (!empty($module_info)) {
			$data['buttoncolor2'] = $module_info['buttoncolor2'];
		} else {
			$data['buttoncolor2'] = 'deeporange';
		}
		if (isset($this->request->post['buttonsize'])) {
			$data['buttonsize'] = $this->request->post['buttonsize'];
		} elseif (!empty($module_info)) {
			$data['buttonsize'] = $module_info['buttonsize'];
		} else {
			$data['buttonsize'] = '';
		}		
		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'] = '';
		}
		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'] = '';
		}		
		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['icons1'])) {
			$data['icons1'] = $this->request->post['icons1'];
		} elseif (!empty($module_info)) {
			$data['icons1'] = $module_info['icons1'];
		} else {
			$data['icons1'] = 'fa fa-apple';
		}
		if (isset($this->request->post['icons2'])) {
			$data['icons2'] = $this->request->post['icons2'];
		} elseif (!empty($module_info)) {
			$data['icons2'] = $module_info['icons2'];
		} else {
			$data['icons2'] = 'fa fa-android';
		}
		$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/Alerts.tpl', $data));
	}

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