<?php
class ControllerModuleBanner extends Controller {

	private $error = array();

	public function index() {

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

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

    $language = $this->load->language('module/banner');

    $this->data = array_merge($this->data, $language);

    $this->data['languageForJS'] = array(
      'module/banner' => $language
    );

    $this->document->addScript(array(
      'view/javascript/module.banner.js'
    ));

    $this->breadcrumbs();

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


    $this->document->addTypesForJs(array(
      Type_Tamanho::getTypeForJs(),
      Type_DsimDnao::getTypeForJs(),
      TypeProduct_Status::getTypeForJs(),
      Type_ModulePosition::getTypeForJs(),
    ));

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

    $this->data['cancel'] = $link_anchor;

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

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

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

			$this->redirect($link_anchor);
    }


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

		if (isset($this->error['dimension'])) {
			$this->data['error_dimension'] = $this->error['dimension'];
		} else {
			$this->data['error_dimension'] = array();
		}


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

    $this->changeTypeToTwig();

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

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



  private function changeTypeToTwig() {
    $banner_position = Type_ModulePosition::getEnumList(true);

    foreach ($this->data['modules'] as $key => $module) {

     $this->data['modules'][$key]['size'] = ftr_geral::setTypeInArray([
        'type'         => Type_Tamanho::getEnumList(),
        'compare'      => isset($module['tamanho']) ? $module['tamanho'] : null,
        'compare_type' => 'selected',
        'default'      => Type_Tamanho::Tp_Pequeno
      ]);

     $this->data['modules'][$key]['type'] = ftr_geral::setTypeInArray([
        'type'         => Type_DsimDnao::getEnumList(),
        'compare'      => isset($module['type']) ? $module['type'] : null,
        'compare_type' => 'selected',
        'default'      => Type_DsimDnao::Tp_Nao
      ]);

     $this->data['modules'][$key]['status'] = ftr_geral::setTypeInArray([
        'type'         => TypeProduct_Status::getEnumList(),
        'compare'      => $module['status'],
        'compare_type' => 'selected',
        'default'      => TypeProduct_Status::Tp_Desabilitado
      ]);

     $this->data['modules'][$key]['position'] = ftr_geral::setTypeInArray([
        'type'         => $banner_position,
        'compare'      => $module['position'],
        'compare_type' => 'selected',
      ]);
    }
  }




  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/banner',
      '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/banner')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

    $banner_module = $this->request->post['banner_module'];
		if (isset($banner_module)) {
			foreach ($banner_module as $key => $value) {
				if (!$value['width'] || !$value['height']) {
					$this->error['dimension'][$key] = $this->language->get('error_dimension');
				}
			}
    }

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

}
