<?php
/**
* 
*/
class ControllerModulePromotion extends Controller
{
	private $moduleModel;
    private $moduleName;
    private $modulePath;
    private $callModel;
    private $moduleVersion;
	
	public function __construct($registry)
	{
		parent::__construct($registry);	
		$this->config->load('isenselabs/promotion');

		$this->moduleName = $this->config->get('promotion_moduleName');
        $this->modulePath = $this->config->get('promotion_modulePath');
        $this->moduleVersion = $this->config->get('promotion_moduleVersion');

        $this->load->model($this->modulePath);
        $this->load->language($this->modulePath);

        $this->callModel = $this->config->get('promotion_callModel');
        $this->moduleModel = $this->{$this->callModel};

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

	public function index() {
		$data['promotions'] = $this->moduleModel->getPromotions();

		if(empty($data['promotions'])) {
			$this->response->redirect($this->url->link('error/not_found', '', 'SSL'));
		}

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

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

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('heading_title'),
			'href' => $this->url->link($this->modulePath, '', 'SSL')
		);

		$language_strings = $this->load->language($this->modulePath);
		foreach ($language_strings as $key => $value) {
			$data[$key] = $value;
		}

		$data['current_language'] = $this->config->get('config_language');

		$data['column_left'] = $this->load->controller('common/column_left');
		$data['column_right'] = $this->load->controller('common/column_right');
		$data['content_top'] = $this->load->controller('common/content_top');
		$data['content_bottom'] = $this->load->controller('common/content_bottom');
		$data['footer'] = $this->load->controller('common/footer');
		$data['header'] = $this->load->controller('common/header');

		if(version_compare(VERSION, '2.2.0.0', '<')) {
			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/'.$this->modulePath.'/index.tpl')) {
			$this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/'.$this->modulePath.'/index.tpl', $data));
			} else {
				$this->response->setOutput($this->load->view('default/template/'.$this->modulePath.'/index.tpl', $data));
			}
		} else {
			$this->response->setOutput($this->load->view($this->modulePath.'/index.tpl', $data));
		}
	}

	private function getImageConfigs($name) {
		if(version_compare(VERSION, '2.2.0.0', '<')) {
			return $this->config->get($name);
		} else {
			if(strpos($name, 'config_image') !== false){
				$name = str_replace('config', '', $name);
				return $this->config->get($this->config->get('config_theme') . $name);
			}
		}	
	}

	public function view() {

		$promotion_id = 0;
		if(isset($this->request->get['promotion_id'])) {
			$promotion_id = $this->request->get['promotion_id'];
		} else {
			$this->response->redirect($this->url->link('error/not_found', '', 'SSL'));
		}
		$data['promotion_id'] = $promotion_id;

		$data['text_tax'] = 'tax';

		$promotion_data = $this->moduleModel->getPromotion($promotion_id);
		$data['promotion_data'] = $promotion_data;


		$language_strings = $this->load->language($this->modulePath);
		foreach ($language_strings as $key => $value) {
			$data[$key] = $value;
		}


		$data['promotion_title'] = $promotion_data['name'];
		$data['promotion_description'] = $promotion_data['information_page']['description'][$this->config->get('config_language')];


		$image_width = 800; $image_height = 500;
		if(isset($promotion_data['information_page']['main_image_width'])) {
			$image_width = $promotion_data['information_page']['main_image_width'];
		}

		if(isset($promotion_data['information_page']['main_image_height'])) {
			$image_height = $promotion_data['information_page']['main_image_height'];
		}

		$image_width_related = $this->getImageConfigs('config_image_related_width');
		$image_height_related = $this->getImageConfigs('config_image_related_height'); 


		if(isset($promotion_data['information_page']['image']) && !empty($promotion_data['information_page']['image'])){
			$data['promotion_image'] = $this->model_tool_image->resize($promotion_data['information_page']['image'], $image_width, $image_height);
		} else {
			$data['promotion_image'] = $this->model_tool_image->resize('no_image.png', $image_width, $image_height);
		}

		if(isset($data['promotion_data']['included_products']['product_ids'])) {
			$this->load->model('catalog/product');
			foreach ($data['promotion_data']['included_products']['product_ids'] as &$product_id) {

				$product_info = $this->model_catalog_product->getProduct($product_id);

				if ($product_info) {
					if ($product_info['image']) {
						$image = $this->model_tool_image->resize($product_info['image'], $image_width_related, $image_height_related);
					} else {
						$image = $this->model_tool_image->resize('placeholder.png', $image_width_related, $image_height_related);
					}

					if ($this->config->get('config_review_status')) {
						$rating = $product_info['rating'];
					} else {
						$rating = false;
					}

					$data_temp = array(
						'product_id'  => $product_info['product_id'],
						'thumb'       => $image,
						'name'        => $product_info['name'],
						'description' => utf8_substr(strip_tags(html_entity_decode($product_info['description'], ENT_QUOTES, 'UTF-8')), 0, 100) . '..',
						'rating'      => $rating,
						'href'        => $this->url->link('product/product', 'product_id=' . $product_info['product_id'])
					);
				}

				$product_id = $data_temp;
			}
		}

		if(isset($data['promotion_data']['included_products']['category_ids'])) {
			$this->load->model('catalog/category');
			foreach ($data['promotion_data']['included_products']['category_ids'] as &$category_id) {
				$category_info = $this->model_catalog_category->getCategory($category_id);

				if ($category_info) {
					if ($category_info['image']) {
						$image = $this->model_tool_image->resize($category_info['image'], $image_width_related, $image_height_related);
					} else {
						$image = $this->model_tool_image->resize('placeholder.png', $image_width_related, $image_height_related);
					}

					$data_temp = array(
						'product_id'  => $category_info['category_id'],
						'thumb'       => $image,
						'name'        => $category_info['name'],
						'description' => utf8_substr(strip_tags(html_entity_decode($category_info['description'], ENT_QUOTES, 'UTF-8')), 0, 100) . '..',
						'href'        => $this->url->link('product/category', 'path=' . $category_info['category_id'])
					);
				}


				$category_id = $data_temp;
			}
		}

		if(isset($data['promotion_data']['included_products']['manufacturer_ids'])) {
			$this->load->model('catalog/manufacturer');
			foreach ($data['promotion_data']['included_products']['manufacturer_ids'] as &$manufacturer_id) {
				$manufacturer_info = $this->model_catalog_manufacturer->getManufacturer($manufacturer_id);

				if ($manufacturer_info) {
					if ($manufacturer_info['image']) {
						$image = $this->model_tool_image->resize($manufacturer_info['image'], $image_width_related, $image_height_related);
					} else {
						$image = $this->model_tool_image->resize('placeholder.png', $image_width_related, $image_height_related);
					}

					$data_temp = array(
						'product_id'  => $manufacturer_info['manufacturer_id'],
						'thumb'       => $image,
						'name'        => $manufacturer_info['name'],
						'href'        => $this->url->link('product/manufacturer/info', 'manufacturer_id=' . $manufacturer_info['manufacturer_id'])
					);
				}



				$manufacturer_id = $data_temp;
			}
		}



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

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

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('heading_title'),
			'href' => $this->url->link($this->modulePath, '', 'SSL')
		);

		$data['breadcrumbs'][] = array(
			'text' => $promotion_data['name'],
			'href' => $this->url->link($this->modulePath, 'promotion_id='.$promotion_id, 'SSL')
		);

		$data['button_cart'] = $this->language->get('button_cart');
		$data['button_wishlist'] = $this->language->get('button_wishlist');
		$data['button_compare'] = $this->language->get('button_compare');

		$data['current_language'] = $this->config->get('config_language');

		$data['column_left'] = $this->load->controller('common/column_left');
		$data['column_right'] = $this->load->controller('common/column_right');
		$data['content_top'] = $this->load->controller('common/content_top');
		$data['content_bottom'] = $this->load->controller('common/content_bottom');
		$data['footer'] = $this->load->controller('common/footer');
		$data['header'] = $this->load->controller('common/header');

		if(version_compare(VERSION, '2.2.0.0', '<')) {
			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/'.$this->modulePath.'/view.tpl')) {
				$this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/'.$this->modulePath.'/view.tpl', $data));
			} else {
				$this->response->setOutput($this->load->view('default/template/'.$this->modulePath.'/view.tpl', $data));
			}
		} else {
				$this->response->setOutput($this->load->view($this->modulePath.'/view.tpl', $data));
		}

	}



}