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

		$this->data['heading_title'] = $this->config->get('special_countdown_heading_title_' . $this->config->get('config_language_id') );
		
		$this->document->addScript('catalog/view/javascript/jquery/jquery.countdown.js');
		$this->document->addScript('catalog/view/javascript/jquery/jquery.bxSlider.min.js');
		$this->document->addScript('catalog/view/javascript/jquery/jquery.easing.1.3.js');
		$this->document->addStyle('catalog/view/theme/' . $this->config->get('config_template') . '/stylesheet/special_countdown.css');
		
		$this->load->model('module/special_countdown');
		
		$this->data['button_cart'] = $this->language->get('button_cart');
		
		$this->load->model('catalog/product');
		
		$this->load->model('tool/image');

		$this->data['products'] = array();
		
		$data = array(
			'sort'  => 'pd.name',
			'order' => 'ASC',
			'start' => 0,
			'limit' => $setting['limit']
		);

		$results = $this->model_catalog_product->getProductSpecials($data);

		foreach ($results as $result) {
			if ($result['image']) {
				$image = $this->model_tool_image->resize($result['image'], $setting['image_width'], $setting['image_height']);
			} else {
				$image = false;
			}

			if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
				$price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')));
			} else {
				$price = false;
			}
					
			if ((float)$result['special']) { 
				$special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')));
			} else {
				$special = false;
			}
			
			if ($this->config->get('config_review_status')) {
				$rating = $result['rating'];
			} else {
				$rating = false;
			}
			
			$show_special_countdown = 0;
			$countdown_format       = 'dd:hh:mm:ss';
			$show_days              = 1;
			$secs					= 0;
			$mins                   = 0;
			$hours	                = 0;
			$days                   = 0;
			$weeks                  = 0;
 			
			$date_stop = $this->model_module_special_countdown->getSpecialPriceEndData($result['product_id']);
			
			if ($date_stop){
				$target_split = preg_split('/\-/', $date_stop);		
				
				$now = time();
				$target = mktime(0,0,0, $target_split[1], $target_split[2], $target_split[0]);
				$diffSecs = $target - $now;
				
				if ($diffSecs <= $setting['hours_bs'] * 60 * 60 && $diffSecs > 0 ){
					$show_special_countdown = 1;
					
					if ($setting['show_days'] ==0 && $diffSecs < 24 * 60 * 60 ){
						$countdown_format = 'hh:mm:ss';
						$show_days = 0;
					}
					
					$secs  = $diffSecs % 60;
					$mins  = floor($diffSecs/60)%60;
					$hours = floor($diffSecs/60/60)%24;
					$days  = floor($diffSecs/60/60/24);//%7;
					$weeks = floor($diffSecs/60/60/24/7);
				}
			}		
			
			$this->data['products'][] = array(
				'product_id' => $result['product_id'],
				'thumb'   	 => $image,
				'name'    	 => $result['name'],
				'description' => mb_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, 500) . '..',
				'price'   	 => $price,
				'special' 	 => $special,
				'rating'     => $rating,
				'show_special_countdown' => $show_special_countdown,
				'countdown_format'       => $countdown_format,
				'show_days'              => $show_days,
				'days'                   => ($days < 10)? '0' . $days : $days,
				'hours'                  => ($hours < 10)? '0' . $hours : $hours,
				'mins'                   => ($mins < 10)? '0' . $mins : $mins,
				'secs'                   => ($secs < 10)? '0' . $secs : $secs,
				'reviews'    => sprintf($this->language->get('text_reviews'), (int)$result['reviews']),
				'href'    	 => $this->url->link('product/product', 'product_id=' . $result['product_id']),
			);
		
		}
					
		$this->data['digit_width']  = $setting['digit_width'];
		$this->data['digit_height'] = $setting['digit_height'];
		$this->data['transition']   = $setting['transition'];
		$this->data['show_once']    = $setting['show_once'];
		$this->data['slide_direction']    = $setting['slide_direction'];
		$this->data['slide_step']   = $setting['slide_step'];
		
		$countdown_image_width  = $setting['digit_width'];
		$countdown_image_height = $setting['digit_height'] *60; 
		$search_height = $setting['digit_height'];

		if ( ($setting['digit_width'] != 53 && $setting['digit_height'] != 77) || $search_height < 10 ){  // original image
			
			$calcWidth      = 53 / (4619 / ($search_height * 60));
			$roundCalcWidth = round($calcWidth);

			while( $roundCalcWidth - $calcWidth < 0){
				$search_height--;
				$calcWidth      = 53 / (4619 / ($search_height * 60));
				$roundCalcWidth = round($calcWidth);
			}

			$countdown_image_width  = $roundCalcWidth;
			$countdown_image_height = $search_height *60; 
			$this->data['digit_height'] = $search_height;
			$this->data['digit_width']  = $roundCalcWidth;
			
		}	


		$this->load->model('tool/image');
		$this->data['digit_image']  = $this->model_tool_image->resize('data/special_countdown/digits.png', $countdown_image_width, $countdown_image_height );

		$this->data['new_font_size'] = round(($setting['digit_height'] * 43 ) / 77);
			
		$this->data['theme_name'] = $this->config->get('config_template');
		
		if ($setting['position'] == "column_left" || $setting['position'] == "column_right" ){
			$template_type = "column_lr";
		} else {
			$template_type = "content_tb";
		}

		$this->data['module'] = $module++;
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/special_countdown_' . $template_type .  '.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/module/special_countdown_' . $template_type . '.tpl';
		} else {
			$this->template = 'default/template/module/special_countdown_' . $template_type . '.tpl';
		}

		$this->render();
	}
}
?>