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

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

		$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('ytabs', $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_bgcolortitle'] = $this->language->get('entry_bgcolortitle');
		$data['entry_bgcolor'] = $this->language->get('entry_bgcolor');
		$data['entry_bgcolorcss'] = $this->language->get('entry_bgcolorcss');
		$data['entry_titlecss'] = $this->language->get('entry_titlecss');
		$data['entry_describe'] = $this->language->get('entry_describe');
		$data['entry_tab1'] = $this->language->get('entry_tab1');
		$data['entry_tab2'] = $this->language->get('entry_tab2');
		$data['entry_tab3'] = $this->language->get('entry_tab3');
		$data['entry_tab4'] = $this->language->get('entry_tab4');
		$data['entry_tab5'] = $this->language->get('entry_tab5');
		$data['entry_tab6'] = $this->language->get('entry_tab6');		
		$data['entry_title'] = $this->language->get('entry_title');
		$data['entry_text'] = $this->language->get('entry_text');
		$data['entry_tabs'] = $this->language->get('entry_tabs');
		$data['entry_buttonname'] = $this->language->get('entry_buttonname');
		$data['entry_link'] = $this->language->get('entry_link');
		$data['entry_image'] = $this->language->get('entry_image');
		$data['entry_sort_order'] = $this->language->get('entry_sort_order');

		$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['tab1'])) {
			$data['error_tab1'] = $this->error['tab1'];
		} else {
			$data['error_tab1'] = '';
		}
		if (isset($this->error['tab2'])) {
			$data['error_tab2'] = $this->error['tab2'];
		} else {
			$data['error_tab2'] = '';
		}
		if (isset($this->error['tab3'])) {
			$data['error_tab3'] = $this->error['tab3'];
		} else {
			$data['error_tab3'] = '';
		}
		if (isset($this->error['tab4'])) {
			$data['error_tab4'] = $this->error['tab4'];
		} else {
			$data['error_tab4'] = '';
		}
		if (isset($this->error['tab5'])) {
			$data['error_tab5'] = $this->error['tab5'];
		} else {
			$data['error_tab5'] = '';
		}
		if (isset($this->error['tab6'])) {
			$data['error_tab6'] = $this->error['tab6'];
		} else {
			$data['error_tab6'] = '';
		}
		$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/ytabs', 'token=' . $this->session->data['token'], 'SSL')
		);

		if (!isset($this->request->get['module_id'])) {
			$data['action'] = $this->url->link('extension/module/ytabs', 'token=' . $this->session->data['token'], 'SSL');
		} else {
			$data['action'] = $this->url->link('extension/module/ytabs', '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['describe'])) {
			$data['describe'] = $this->request->post['describe'];
		} elseif (!empty($module_info)) {
			$data['describe'] = $module_info['describe'];
		} else {
			$data['describe'] = '';
		}
		if (isset($this->request->post['bgcolor'])) {
			$data['bgcolor'] = $this->request->post['bgcolor'];
		} elseif (!empty($module_info)) {
			$data['bgcolor'] = $module_info['bgcolor'];
		} else {
			$data['bgcolor'] = '';
		}
		if (isset($this->request->post['bgcolorcss'])) {
			$data['bgcolorcss'] = $this->request->post['bgcolorcss'];
		} elseif (!empty($module_info)) {
			$data['bgcolorcss'] = $module_info['bgcolorcss'];
		} else {
			$data['bgcolorcss'] = '';
		}
		if (isset($this->request->post['titlecss'])) {
			$data['titlecss'] = $this->request->post['titlecss'];
		} elseif (!empty($module_info)) {
			$data['titlecss'] = $module_info['titlecss'];
		} else {
			$data['titlecss'] = '';
		}
		if (isset($this->request->post['tab1'])) {
			$data['tab1'] = $this->request->post['tab1'];
		} elseif (!empty($module_info)) {
			$data['tab1'] = $module_info['tab1'];
		} else {
			$data['tab1'] = '';
		}
		if (isset($this->request->post['tab2'])) {
			$data['tab2'] = $this->request->post['tab2'];
		} elseif (!empty($module_info)) {
			$data['tab2'] = $module_info['tab2'];
		} else {
			$data['tab2'] = '';
		}
		if (isset($this->request->post['tab3'])) {
			$data['tab3'] = $this->request->post['tab3'];
		} elseif (!empty($module_info)) {
			$data['tab3'] = $module_info['tab3'];
		} else {
			$data['tab3'] = '';
		}
		if (isset($this->request->post['tab4'])) {
			$data['tab4'] = $this->request->post['tab4'];
		} elseif (!empty($module_info)) {
			$data['tab4'] = $module_info['tab4'];
		} else {
			$data['tab4'] = '';
		}
		if (isset($this->request->post['tab5'])) {
			$data['tab5'] = $this->request->post['tab5'];
		} elseif (!empty($module_info)) {
			$data['tab5'] = $module_info['tab5'];
		} else {
			$data['tab5'] = '';
		}
		if (isset($this->request->post['tab6'])) {
			$data['tab6'] = $this->request->post['tab6'];
		} elseif (!empty($module_info)) {
			$data['tab6'] = $module_info['tab6'];
		} else {
			$data['tab6'] = '';
		}
		$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'],
				'text'        => $service['text'],
				'tabs'        => $service['tabs'],
				'buttonname'        => $service['buttonname'],
				'link'        => $service['link'],
				'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/ytabs.tpl', $data));
	}

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