<?php
class ControllerExtensionModuleWDBanners extends Controller {
	public function index($setting) {
		static $module = 0;

		$this->load->model('tool/image');
		$this->load->model('design/banner');
		$this->load->model('localisation/zone');

		if (isset($setting['include'])) {

			$includes = explode("\r\n", $setting['include']);

			foreach ($includes as $include) {
				$include = trim($include, "/");
				if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/' . $include)) {
					$path_parts = pathinfo($_SERVER['DOCUMENT_ROOT'] . '/' . $include);
					switch ($path_parts['extension']) {
						case "css":
							$this->document->addStyle($include);
							break;
						case "js":
							$this->document->addScript($include);
							break;
					}
				}
			}
		}

		$data['random'] = token(32);
		$data['title'] = isset($setting['hide']) ? $setting['name'] : false;

		$data['sizes'] = $setting['sizes'];

		$data['favicon'] = $this->model_tool_image->resize($this->config->get("config_icon"), 114, 114, true);

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

		if (!empty($setting['banner_id'])) {
			$results = $this->model_design_banner->getBanner($setting['banner_id']);

			foreach ($results as $result) {
				if (is_file(DIR_IMAGE . $result['image'])) {
					$data['banners'][] = array(
						'image'      => $this->model_tool_image->resize($result['image'], $setting['sizes']['desktop']['width'], $setting['sizes']['desktop']['height']),
						'image_mob'  => $result['image_mobile'] && is_file(DIR_IMAGE . $result['image_mobile']) ? $this->model_tool_image->resize($result['image_mobile'], $setting['sizes']['mob']['width'], $setting['sizes']['mob']['height']) : '',
						'image_orig' => HTTPS_SERVER . 'image/' . $result['image'],
						'title'      => html_entity_decode($result['title'], ENT_QUOTES, 'UTF-8'),
						'text'       => html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8'),
						'link'       => $this->getRegionLink($result['link']),
						'button'     => $result['button_text'],
					);
				}
			}
		}

		$data['module'] = $module++;

		return $this->load->view('extension/module/' . ((isset($setting['twig']) && $setting['twig'] != "") ? trim($setting['twig']) : "wd_banners"), $data);
	}

	private function getRegionLink($link) {
		$link = trim((string)$link);

		if ($link === '' || $link[0] === '#' || strpos($link, '//') === 0 || preg_match('#^[a-z][a-z0-9+.-]*:#i', $link)) {
			return $link;
		}

		$zone_id = isset($this->session->data['city_id']) ? (int)$this->session->data['city_id'] : (int)$this->config->get('config_zone_id');
		$store_url = $this->config->get('config_url');
		$regional_link = $this->model_localisation_zone->getZoneUrl($zone_id, rtrim($store_url, '/') . '/' . ltrim($link, '/'));
		$url_info = parse_url($regional_link);

		if (!$url_info) {
			return $link;
		}

		$link = isset($url_info['path']) ? $url_info['path'] : '/';

		if (isset($url_info['query'])) {
			$link .= '?' . $url_info['query'];
		}

		if (isset($url_info['fragment'])) {
			$link .= '#' . $url_info['fragment'];
		}

		return $link;
	}
}
