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

		$data['heading_title'] = $this->language->get('heading_title');
		
		$data['text_tax'] = $this->language->get('text_tax');

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

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

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

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

		if (!$setting['limit']) {
			$setting['limit'] = 1;
		}
		
		$products = array_slice($setting['dresscode_featured_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->config->get('config_customer_price') && $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')));
				} else {
					$price = false;
				}
						
				if ((float)$product_info['special']) {
					$special = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax')));
				} else {
					$special = false;
				}

				if ($this->config->get('config_tax')) {
					$tax = $this->currency->format((float)$product_info['special'] ? $product_info['special'] : $product_info['price']);
				} else {
					$tax = false;
				}

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

                //youtube
                $this->load->model('custom/general');
                $youtube = $this->model_custom_general->getProductOption($product_id, 'youtube');
                //youtube

                /* vars from admin panel labels settings */
                $store_id = $this->config->get('config_store_id');

                $data['buyshop_general'] = $this->config->get('buyshop_general');
                if (isset($data['buyshop_general']['ajaxcart'][$store_id])) {
                    $data['ajaxcart'] = $data['buyshop_general']['ajaxcart'][$store_id];
                } else {
                    $data['ajaxcart'] = '';
                }
                $data['buyshop_productlabels'] = $this->config->get('buyshop_productlabels');
                if (isset($data['buyshop_productlabels']['sale'][$store_id])) {
                    $data['sale'] = $data['buyshop_productlabels']['sale'][$store_id];
                } else {
                    $data['sale'] = '';
                }
                if (isset($data['buyshop_productlabels']['video'][$store_id])) {
                    $data['video'] = $data['buyshop_productlabels']['video'][$store_id];
                } else {
                    $data['video'] = '';
                }
                if (isset($data['buyshop_productlabels']['saleposition'][$store_id])) {
                    $data['saleposition'] = $data['buyshop_productlabels']['saleposition'][$store_id];
                } else {
                    $data['saleposition'] = '';
                }
                if (isset($data['buyshop_productlabels']['videoposition'][$store_id])) {
                    $data['videoposition'] = $data['buyshop_productlabels']['videoposition'][$store_id];
                } else {
                    $data['videoposition'] = '';
                }
                /* end vars from admin panel labels settings */

                $data['products'][] = array(
					'product_id' => $product_info['product_id'],
					'thumb'   	 => $image,

                    //youtube video
                    'youtube_video' => $youtube,

                    'name-main'        => strlen($product_info['name']) > 43 ? utf8_substr(strip_tags(html_entity_decode($product_info['name'], ENT_QUOTES, 'UTF-8')), 0, 43) . '..' : $product_info['name'],
                    'name'        => $product_info['name'],
					'description' => utf8_substr(strip_tags(html_entity_decode($product_info['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get('config_product_description_length')) . '..',
                    'price'   	 => $price,
					'special' 	 => $special,
					'tax'         => $tax,
					'rating'     => $rating,
					'reviews'    => sprintf($this->language->get('text_reviews'), (int)$product_info['reviews']),
					'href'    	 => $this->url->link('product/product', 'product_id=' . $product_info['product_id'])
				);
			}
		}

		if ($data['products']) {
			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/dresscode_featured.tpl')) {
				return $this->load->view($this->config->get('config_template') . '/template/module/dresscode_featured.tpl', $data);
			} else {
				return $this->load->view('default/template/module/dresscode_featured.tpl', $data);
			}
		}
	}
}
