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

	public function index() {
		$language = $this->language->load('module/certificate');
		$this->data = array_merge($this->data, $language);

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

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

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			$group = isset($this->request->get['group']) ? $this->request->get['group'] : 'certificate';
			$this->model_setting_setting->editSetting($group, $this->request->post);

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

			$this->redirect($this->url->linkAnchor('extension/module', 'token=' . $this->session->data['token'], 'tab-info=' . $this->request->get['type'], 'SSL'));
		}

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

    $this->breadcrumbs();

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

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

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

    $action_ebit = $this->url->link(
      'module/certificate',
      'token=' . $this->session->data['token'] . '&type=' . $this->request->get['type']. '&group=ebit',
      'SSL'
    );
		$this->data['action'] = array(
      'ebit' => $action_ebit
    );

    $this->data['cancel'] = $this->url->linkAnchor(
      'extension/module',
      'token=' . $this->session->data['token'],
      'tab-info=' . $this->request->get['type'],
      'SSL'
    );

		$this->data['modules'] = array();

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

		$this->load->dmodel('design/layout');

		$this->data['layouts'] = $this->dmodel_design_layout->getLayouts();

		$this->children = array(
			'common/header',
			'common/footer'
		);

		$this->response->setOutput($this->render('module/certificate'));
  }


  private function breadcrumbs() {
		$this->data['breadcrumbs'] = array();

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

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

    $link = $this->url->link(
      'module/certificate',
      'token=' . $this->session->data['token'] . '&type=' . $this->request->get['type'],
      'SSL'
    );
    $this->data['breadcrumbs'][] = array(
      'text' => $this->language->get('heading_title'),
      'href' => $link,
    );
  }

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

		if (!$this->error) {
			return true;
		} else {
			return false;
		}
	}
}
