<?php
/*
Author: Jitendra Sharma
Email: jks0586@gmail.com, vxinfosystem@gmail.com
URL: vxinfosystem.com
Skype: jks0586
Product name: Product Short Description
Environment: Opencart
*/

class ControllerExtensionModuleProductshortdescription extends Controller {
	private $error = array();

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

		$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('product_short_description', $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'] = $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_maxlength'] = $this->language->get('entry_maxlength');
    $data['entry_category'] = $this->language->get('entry_category');
		$data['entry_search'] = $this->language->get('entry_search');

		$data['entry_html'] = $this->language->get('entry_html');
		$data['entry_featured'] = $this->language->get('entry_featured');
		$data['entry_bestseller'] = $this->language->get('entry_bestseller');
		$data['entry_latest'] = $this->language->get('entry_latest');
		$data['entry_special'] = $this->language->get('entry_special');
		$data['entry_related'] = $this->language->get('entry_related');
		$data['entry_short_detail'] = $this->language->get('entry_short_detail');

		$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'] = '';
		}

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

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

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


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

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


		$data['cancel'] = $this->url->link('extension/extension', 'token=' . $this->session->data['token'] , true);


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

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

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

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

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

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

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

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

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

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

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

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

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

		return !$this->error;
	}

  public function install() {
		$this->db->query("ALTER TABLE " . DB_PREFIX . "product_description ADD short_description VARCHAR(1024) NOT NULL AFTER description");
    //ALTER TABLE {TABLE_PREFIX}product_description ADD `short_description` VARCHAR( 255 ) NOT NULL AFTER `description`
	}

	public function uninstall() {
		$this->load->model('setting/setting');
		$this->db->query("ALTER TABLE " . DB_PREFIX . "product_description DROP COLUMN short_description");
		$this->model_setting_setting->deleteSetting('product_short_description');
	}
}
