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

	public function index() {
				
		$this->load->model('madeshop/review');
		$this->model_madeshop_review->install();
		
		$yes = $this->db->query("SHOW COLUMNS FROM " . DB_PREFIX . "review LIKE 'yes'");
        if (!$yes->num_rows) {
            $this->db->query("ALTER TABLE " . DB_PREFIX . "review ADD yes int(11) NOT NULL");
			$this->db->query("ALTER TABLE " . DB_PREFIX . "review ADD unlike int(11) NOT NULL");
		}
		
		$mtext = $this->db->query("SHOW COLUMNS FROM " . DB_PREFIX . "review LIKE 'mtext'");
        if (!$mtext->num_rows) {
			$this->db->query("ALTER TABLE " . DB_PREFIX . "review ADD mtext text NOT NULL");
		}
		
		$this->load->language('extension/module/madeshop_review');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('setting/setting');

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			$this->model_setting_setting->editSetting('module_madeshop_review', $this->request->post);

			$this->session->data['success'] = $this->language->get('text_success');

			$this->response->redirect($this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=module', true));
		}
		

		$data['heading_title'] = $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_status'] = $this->language->get('entry_status');

		$data['button_save'] = $this->language->get('button_save');
		$data['button_cancel'] = $this->language->get('button_cancel');

		
		$data['user_token'] = $this->session->data['user_token'];

		if (isset($this->error['warning'])) {
			$data['error_warning'] = $this->error['warning'];
		} else {
			$data['error_warning'] = '';
		}

		$data['breadcrumbs'] = array();

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_home'),
			'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true)
		);

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_extension'),
			'href' => $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=module', true)
		);

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('heading_title'),
			'href' => $this->url->link('extension/module/madeshop_review', 'user_token=' . $this->session->data['user_token'], true)
		);

		$data['action'] = $this->url->link('extension/module/madeshop_review', 'user_token=' . $this->session->data['user_token'], true);

		$data['cancel'] = $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=module', true);
		
		$this->load->model("localisation/language");
        $data['languages'] = $this->model_localisation_language->getLanguages();

		if (isset($this->request->post['module_madeshop_review_title'])) {
			$data['module_madeshop_review_title'] = $this->request->post['module_madeshop_review_title'];
		} else {
			$data['module_madeshop_review_title'] = $this->config->get('module_madeshop_review_title');
		}
		
		if (isset($this->request->post['module_madeshop_review_status'])) {
			$data['module_madeshop_review_status'] = $this->request->post['module_madeshop_review_status'];
		} else {
			$data['module_madeshop_review_status'] = $this->config->get('module_madeshop_review_status');
		}


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

	protected function validate() {
		if (!$this->user->hasPermission('modify', 'extension/module/madeshop_review')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		return !$this->error;
	}
}