<?php
class ControllerModuleBuyshop extends Controller {

    private $error = array();

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

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

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


        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
            $this->model_setting_setting->editSetting('buyshop', $this->request->post);
            $this->session->data['success'] = $this->language->get('text_success');

            if ($this->request->post['buttonForm'] == 'apply') {
                $this->response->redirect($this->url->link('module/buyshop', 'token=' . $this->session->data['token'], 'SSL'));
            } else {
                $this->response->redirect($this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'));
            }
        }

        $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['success'] = $this->language->get('text_success');
        $data['entry_name'] = $this->language->get('entry_name');
        $data['entry_title'] = $this->language->get('entry_title');
        $data['entry_description'] = $this->language->get('entry_description');
        $data['text_yes'] = $this->language->get('text_yes');
        $data['text_no'] = $this->language->get('text_no');

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

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

        $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'], 'SSL')
        );

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

        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('heading_title_normal'),
            'href' => $this->url->link('module/buyshop', 'token=' . $this->session->data['token'], 'SSL')
        );

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

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

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

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

        $data['config_url'] = $this->config->get('config_url');
        $data['bproduct'] = $this->url->link('catalog/bproduct', 'token=' . $this->session->data['token'], 'SSL');
        $data['bcategory'] = $this->url->link('catalog/bcategory', 'token=' . $this->session->data['token'], 'SSL');
        $data['mcategory'] = $this->url->link('catalog/mcategory', 'token=' . $this->session->data['token'], 'SSL');

        /* stores adding */
        $this->load->model('setting/store');
        $data['stores'] = $this->model_setting_store->getStores();
        /* end stores adding */
        $this->load->model('localisation/language');

        $data['languages'] = $this->model_localisation_language->getLanguages();


        $this->document->addScript($this->config->get('config_url').'catalog/view/bs-colorpicker/js/colorpicker.js');
        $this->document->addStyle($this->config->get('config_url').'catalog/view/bs-colorpicker/css/colorpicker.css');
        $this->document->addStyle($this->config->get('config_url').'view/stylesheet/theme.css');

        $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('module/buyshop.tpl', $data));
    }

    public function install(){
        $this->load->model('catalog/bproduct');
        $this->model_catalog_bproduct->addColumnsProducts();

        $this->load->model('catalog/mcategory');
        $this->model_catalog_mcategory->createColumnsInCategories();


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

    }


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