<?php
class ControllerModuleDealOfTheDay extends Controller 
{
	/*
	 * Version: 1.5.1.3.1
	 * Updated: 01/03/2012
	 */
	protected function index($setting) 
	{
		$this->language->load('module/dealoftheday');
		
		$this->load->model('tool/image');
		$this->load->model('module/dealoftheday');
		
		$this->data['heading_title'] 			= $this->language->get('heading_title');
						
		/** Deal Of The Day module parameters **/
		
		$this->data['entry_api_key'] 		= $this->language->get('entry_product');
		$this->data['entry_image_width'] 	= $this->language->get('entry_image_width');
		$this->data['entry_image_height'] 	= $this->language->get('entry_image_height');
		
		$this->data['text_price'] 	= $this->language->get('text_price');
		$this->data['text_discount'] 	= $this->language->get('text_discount');
		$this->data['text_buy_at'] 	= $this->language->get('text_buy_at');
		$this->data['text_you_save'] 	= $this->language->get('text_you_save');
		$this->data['text_add_to_cart'] 	= $this->language->get('text_add_to_cart');
		$this->data['text_years'] 	= $this->language->get('text_years');
		$this->data['text_months'] 	= $this->language->get('text_months');
		$this->data['text_weeks'] 	= $this->language->get('text_weeks');
		$this->data['text_days'] 	= $this->language->get('text_days');
		$this->data['text_hours'] 	= $this->language->get('text_hours');
		$this->data['text_minutes'] 	= $this->language->get('text_minutes');
		$this->data['text_seconds'] 	= $this->language->get('text_seconds');
		$this->data['text_year'] 	= $this->language->get('text_year');
		$this->data['text_month'] 	= $this->language->get('text_month');
		$this->data['text_week'] 	= $this->language->get('text_week');
		$this->data['text_day'] 	= $this->language->get('text_day');
		$this->data['text_hour'] 	= $this->language->get('text_hour');
		$this->data['text_minute'] 	= $this->language->get('text_minute');
		$this->data['text_second'] 	= $this->language->get('text_second');
		$this->data['text_availability'] 	= $this->language->get('text_availability');
		$this->data['text_quantity'] 	= $this->language->get('text_quantity');
		
		$this->data['text_no_special_product'] 	= $this->language->get('text_no_special_product');
		
		// Product
		$this->data['dealoftheday_product'] = $this->config->get('dealoftheday_product');
		
		$product = array();
		if($this->data['dealoftheday_product']) {
			$product = $this->model_module_dealoftheday->getProduct($this->data['dealoftheday_product']);
		}

		if(empty($product)) {
			$this->data['av_product'] 		= false;
		}
		else {
			$this->data['av_product'] 		= true;
			
			$product_id = $product['product_id'];
			$product_name = $product['name'];
			if($product['image']) {
				$setting['image_width'] = (int)$setting['image_width'] > 0 ? (int)$setting['image_width'] : 360;
				$setting['image_height'] = (int)$setting['image_height'] > 0 ? (int)$setting['image_height'] : 230;
				$image = $this->model_tool_image->resize($product['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($product['price'], $product['tax_class_id'], $this->config->get('config_tax')));
			}
			else {
				$price = false;
			}
			
			if((float)$product['special']) { 
				$special = $this->currency->format($this->tax->calculate($product['special'], $product['tax_class_id'], $this->config->get('config_tax')));
			}
			else {
				$special = false;
			}
			$clean_price = $this->tax->calculate($product['price'], $product['tax_class_id'], $this->config->get('config_tax'));
			$clean_special = $this->tax->calculate($product['special'], $product['tax_class_id'], $this->config->get('config_tax'));
			$you_save = 0;
			if($product['special'] && $clean_special && $clean_price>$clean_special) {
				$you_save = $clean_price - $clean_special;
			}
			
			$discount = '0';
			if($you_save>0) {
				$discount = ( $you_save / $clean_price ) * 100;
				$discount = round( $discount, 2 );
				$discount = $discount.'%';
			}
			$seconds = 0;
			if($product['date_end'] && $product['date_end']!= '0000-00-00' && strtotime($product['date_end'])>time()) {
				$seconds = strtotime($product['date_end'])-time();
			}
			if(!$seconds>0) {
				$this->data['av_product'] 	= false;
			}
			
			if(!$product['quantity']>0) {
				$this->data['av_product'] 	= false;
			}
							
			$href = $this->url->link('product/product', 'product_id=' . $product_id);
			
			$this->data['product_id'] = $product_id;
			$this->data['product_name'] = $product_name;
			$this->data['product_image'] = $image;
			$this->data['product_url'] = $href;
			$this->data['product_price'] = $price;
			$this->data['product_special'] = $special;
			$this->data['product_discount'] = $discount;
			$this->data['product_save'] = $this->currency->format($you_save);
			$this->data['expire_url'] = HTTP_SERVER;
			$this->data['seconds'] = $seconds;
			$this->data['product_availability'] = $product['stock_status'];
			$this->data['product_quantity'] = $product['quantity'];
			$this->data['product_description'] = substr(strip_tags(html_entity_decode($product['description'], ENT_QUOTES, 'UTF-8')), 0, 100) . '..';
			
			if ($product['quantity'] <= 0) {
				$this->data['product_availability'] = $product['stock'];
			}
			else {
				$this->data['product_availability'] = $this->language->get('text_instock');
			}
			
		}
		
		$this->data['assets'] = HTTP_SERVER.'catalog/view/javascript/dealoftheday/';		
		// Apply image width-height
		$this->data['image_width'] 	= (int)$setting['image_width'] > 0 ? (int)$setting['image_width'] : 360;
		$this->data['image_height'] = (int)$setting['image_height'] > 0 ? (int)$setting['image_height'] : 230;
		$this->data['image_width_px'] 	= 'width:'.$this->data['image_width'].'px;';
		$this->data['image_height_px']	= 'height:'.$this->data['image_height'].'px;';
		
		$this->data['image_style']	= $this->data['image_width_px'].$this->data['image_height_px'];
				
		$this->id = 'dealoftheday';
		
		if(file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/dealoftheday.tpl')) 
		{
			$this->template = $this->config->get('config_template') . '/template/module/dealoftheday.tpl';
		} 
		else 
		{
			$this->template = 'default/template/module/dealoftheday.tpl';
		}
				
		$this->render();
	}
}
?>