<?php
class ControllerModuleDresscodeLatest extends Controller {
	public function index($setting) {
		$this->load->language('module/dresscode_latest');
		
		$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();

		$filter_data = array(
			'sort'  => 'p.date_added',
			'order' => 'DESC',
			'start' => 0,
			'limit' => $setting['limit']
		);

		$results = $this->model_catalog_product->getProducts($filter_data);

		if ($results) {
		foreach ($results as $result) {
			if ($result['image']) {
				$image = $this->model_tool_image->resize($result['image'], $setting['width'], $setting['height']);
			} else {
                $image = $this->model_tool_image->resize('placeholder.png', $setting['width'], $setting['height']);
            }
            //addit image
            $results_images = $this->model_catalog_product->getProductImages($result['product_id']);
            if ($results_images) {
                $first = true;
                foreach ($results_images as $results_image) {
                    if ( $first ) {
                        $popup = $this->model_tool_image->resize($results_image['image'], $setting['width'], $setting['height']);
                        $first = false;
                    }
                }
            } else {
                $popup = false;
            }
            //addit image

            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_tax')) {
					$tax = $this->currency->format((float)$result['special'] ? $result['special'] : $result['price']);
				} else {
					$tax = false;
				}
			if ($this->config->get('config_review_status')) {
				$rating = $result['rating'];
			} else {
				$rating = false;
			}

            //youtube
            $this->load->model('custom/general');
            $youtube = $this->model_custom_general->getProductOption($result['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' => $result['product_id'],
				'thumb'   	 => $image,

                //addit image
                'popup'   	 => $popup,
                //addit image

                'name-main'        => strlen($result['name']) > 43 ? utf8_substr(strip_tags(html_entity_decode($result['name'], ENT_QUOTES, 'UTF-8')), 0, 43) . '..' : $result['name'],
                'name'        => $result['name'],
					'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get('config_product_description_length')) . '..',
                //youtube video
                'youtube_video' => $youtube,

                'price'   	 => $price,
				'special' 	 => $special,
					'tax'         => $tax,
				'rating'     => $rating,
				'reviews'    => sprintf($this->language->get('text_reviews'), (int)$result['reviews']),
				'href'    	 => $this->url->link('product/product', 'product_id=' . $result['product_id']),
			);
		}

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