<?php
class ControllerExtensionModuleFeatured extends Controller {
	public function index($setting) {
		$this->load->language('extension/module/featured');

		$this->load->model('catalog/product');

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

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

		static $module = 0;
		$data['module'] = $module++;
		$this->document->addScript('catalog/view/theme/madeshop/script/slick.min.js');
		$this->load->language('madeshop/mproduct');
		$data['button_view'] = $this->language->get('button_view');
		$data['attribute_out'] = $this->config->get('theme_madeshop_attribute') ? $this->config->get('theme_madeshop_attribute') : array();
		$data['option_name'] = $this->config->get('theme_madeshop_option_name') ? $this->config->get('theme_madeshop_option_name') : '';
		$data['attribute_name'] = $this->config->get('theme_madeshop_attribute_name') ? $this->config->get('theme_madeshop_attribute_name') : '';
		$data['iwishlist'] = $this->config->get('theme_madeshop_wishlist') ? $this->config->get('theme_madeshop_wishlist') : '';
		$data['icompare'] = $this->config->get('theme_madeshop_compare') ? $this->config->get('theme_madeshop_compare') : '';
		$data['iview'] = $this->config->get('theme_madeshop_view') ? $this->config->get('theme_madeshop_view') : '';
		$data['icart'] = $this->config->get('theme_madeshop_cart') ? $this->config->get('theme_madeshop_cart') : '';
		

		if (!$setting['limit']) {
			$setting['limit'] = 4;
		}

		if (!empty($setting['product'])) {
			$products = array_slice($setting['product'], 0, (int)$setting['limit']);

			foreach ($products 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'], $setting['width'], $setting['height']);
					} else {
						$image = $this->model_tool_image->resize('placeholder.png', $setting['width'], $setting['height']);
					}

					if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {
						$price = $this->currency->format($this->tax->calculate($product_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
					} else {
						$price = false;
					}

					if (!is_null($product_info['special']) && (float)$product_info['special'] >= 0) {
						$special = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
						$tax_price = (float)$product_info['special'];
					} else {
						$special = false;
						$tax_price = (float)$product_info['price'];
					}
		
					if ($this->config->get('config_tax')) {
						$tax = $this->currency->format($tax_price, $this->session->data['currency']);
					} else {
						$tax = false;
					}

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

					
		$des = explode('@',html_entity_decode($product_info['description'], ENT_QUOTES, 'UTF-8'));				
		if(isset($des[1])){
			$min_d = $des[0];
		} else {
			$min_d = '';					
		}

        $swapimage = array();
        $swapimages = $this->model_catalog_product->getProductImages($product_info['product_id']);
        foreach ($swapimages as $swap) {  
            $swapimage[] =  $this->model_tool_image->resize($swap['image'], $setting['width'], $setting['height']);
        }
        $one = array($image);
        $swap = array_merge($one,$swapimage);

       if($this->config->get('theme_madeshop_time')){
		$date_end = $this->model_catalog_product->DateEnd($product_info['product_id']);
		} else {
        $date_end = '';
        }


		if($this->config->get('theme_madeshop_hit')){
		$hit = $this->model_catalog_product->getHit($product_info['product_id']) >= $this->config->get('theme_madeshop_hit') ? '<div class="hit">'.$this->language->get('text_hit').'</div>' : '';
        } else {
        $hit = '';
        }

		if($this->config->get('theme_madeshop_new')){
		$new = (strtotime(date("Y-m-d "))-strtotime($product_info['date_available']))/(60*60*24) <= $this->config->get('theme_madeshop_new') ? '<div class="new">'.$this->language->get('text_new').'</div>' : '';
		} else {
        $new = '';
        }		
		
		$data['products'][] = array(
		'tags'        => $product_info['tag'] ? explode(',', $product_info['tag']) : '',
		'sale'        => $product_info['special'] ? '-'.round((($product_info['price'] - $product_info['special'])/$product_info['price'])*100, 0).'% '.$date_end : '',
		'hit'         => $hit,
		'new'         => $new,
		'options'      => $this->config->get('theme_madeshop_option_status') ? $this->model_catalog_product->getProductOptions($product_info['product_id']) : '',
		'attribute_groups' => $this->config->get('theme_madeshop_attribute_status') ? $this->model_catalog_product->getProductAttributes($product_info['product_id']) : '',
		'min_d' => $this->config->get('theme_madeshop_description_status') ? $min_d : '',
        'swap'      => join(',' , $swap),
        'istock'    => $product_info['quantity'] <= 0 && $this->config->get('theme_madeshop_istock') ? $this->language->get('text_nostock') : '',
		
						'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, $this->config->get('theme_' . $this->config->get('config_theme') . '_product_description_length')) . '..',
						'price'       => $price,
						'special'     => $special,
						'tax'         => $tax,
						'rating'      => $rating,
						'href'        => $this->url->link('product/product', 'product_id=' . $product_info['product_id'])
					);
				}
			}
		}

		if ($data['products']) {
			return $this->load->view('extension/module/featured', $data);
		}
	}
}