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

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

		$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('pricecss', $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_status'] = $this->language->get('entry_status');
		$data['entry_name'] = $this->language->get('entry_name');
		$data['entry_titlename'] = $this->language->get('entry_titlename');
		$data['entry_describe'] = $this->language->get('entry_describe');
		$data['entry_title'] = $this->language->get('entry_title');
		$data['entry_taocan'] = $this->language->get('entry_taocan');
		$data['entry_taocan1'] = $this->language->get('entry_taocan1');
		$data['entry_taocan2'] = $this->language->get('entry_taocan2');
		$data['entry_taocan3'] = $this->language->get('entry_taocan3');
		$data['entry_taocan4'] = $this->language->get('entry_taocan4');
		$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_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_borderstyle'] = $this->language->get('entry_borderstyle');
		$data['entry_boxbs'] = $this->language->get('entry_boxbs');
		$data['entry_boxline'] = $this->language->get('entry_boxline');
		$data['entry_boxlinewidth'] = $this->language->get('entry_boxlinewidth');
		$data['entry_iccolor'] = $this->language->get('entry_iccolor');
		$data['entry_tccolor'] = $this->language->get('entry_tccolor');

		$data['entry_taocanname'] = $this->language->get('entry_taocanname');
		$data['entry_tcone'] = $this->language->get('entry_tcone');
		$data['entry_tctwo'] = $this->language->get('entry_tctwo');
		$data['entry_tcthree'] = $this->language->get('entry_tcthree');
		$data['entry_tcfour'] = $this->language->get('entry_tcfour');
		
		$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_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/pricecss', 'token=' . $this->session->data['token'], 'SSL')
		);

		if (!isset($this->request->get['module_id'])) {
			$data['action'] = $this->url->link('extension/module/pricecss', 'token=' . $this->session->data['token'], 'SSL');
		} else {
			$data['action'] = $this->url->link('extension/module/pricecss', '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['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['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['boxlinewidth'])) {
			$data['boxlinewidth'] = $this->request->post['boxlinewidth'];
		} elseif (!empty($module_info)) {
			$data['boxlinewidth'] = $module_info['boxlinewidth'];
		} else {
			$data['boxlinewidth'] = '1';
		}
		if (isset($this->request->post['boxbs'])) {
			$data['boxbs'] = $this->request->post['boxbs'];
		} elseif (!empty($module_info)) {
			$data['boxbs'] = $module_info['boxbs'];
		} else {
			$data['boxbs'] = '';
		}
		if (isset($this->request->post['iccolor'])) {
			$data['iccolor'] = $this->request->post['iccolor'];
		} elseif (!empty($module_info)) {
			$data['iccolor'] = $module_info['iccolor'];
		} else {
			$data['iccolor'] = '';
		}
		if (isset($this->request->post['tccolor'])) {
			$data['tccolor'] = $this->request->post['tccolor'];
		} elseif (!empty($module_info)) {
			$data['tccolor'] = $module_info['tccolor'];
		} else {
			$data['tccolor'] = '';
		}
		if (isset($this->request->post['boxline'])) {
			$data['boxline'] = $this->request->post['boxline'];
		} elseif (!empty($module_info)) {
			$data['boxline'] = $module_info['boxline'];
		} else {
			$data['boxline'] = '';
		}
		if (isset($this->request->post['tcone'])) {
			$data['tcone'] = $this->request->post['tcone'];
		} elseif (!empty($module_info)) {
			$data['tcone'] = $module_info['tcone'];
		} else {
			$data['tcone'] = '';
		}
		if (isset($this->request->post['tctwo'])) {
			$data['tctwo'] = $this->request->post['tctwo'];
		} elseif (!empty($module_info)) {
			$data['tctwo'] = $module_info['tctwo'];
		} else {
			$data['tctwo'] = '';
		}
		if (isset($this->request->post['tcthree'])) {
			$data['tcthree'] = $this->request->post['tcthree'];
		} elseif (!empty($module_info)) {
			$data['tcthree'] = $module_info['tcthree'];
		} else {
			$data['tcthree'] = '';
		}
		if (isset($this->request->post['tcfour'])) {
			$data['tcfour'] = $this->request->post['tcfour'];
		} elseif (!empty($module_info)) {
			$data['tcfour'] = $module_info['tcfour'];
		} else {
			$data['tcfour'] = '';
		}		
		$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['describe'])) {
			$data['describe'] = $this->request->post['name'];
		} elseif (!empty($module_info)) {
			$data['describe'] = $module_info['describe'];
		} else {
			$data['describe'] = '';
		}
		$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'],
				'taocan1'        => $service['taocan1'],
				'taocan2'        => $service['taocan2'],
				'taocan3'        => $service['taocan3'],
				'taocan4'        => $service['taocan4'],
				'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/pricecss.tpl', $data));
	}

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