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

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

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