<?php
class ControllerModuleAjaxTab extends Controller{
	protected function index($setting){
		$this->language->load('module/ajaxtab');
	
		$this->data['links'] = array();
		$links = array();
		$controls = $setting['tab_type'];
		
		foreach($controls as $control){
			$option = $setting[''.$control];
			$links[] = array(
				'type' => $control,
				'name' => $this->language->get('text_'.$control),
				'href' => $this->url->link('module/ajaxtab','type='.$control),
				'order' => $option['order']
			);

		}
		if (!function_exists('mytabsort')) {
			function mytabsort($left,$right){
			if($left['order'] == $right['order']) return 0;
			return $left['order'] < $right['order'] ? -1 : 1 ;
			} 
		}
		usort($links,'mytabsort');
		$this->data['links'] = $links;
		
		//$defaultload = $links[0]['name'];
		
			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/ajaxtab/ajaxtab.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/ajaxtab/ajaxtab.tpl';
		} else {
			$this->template = 'default/template/ajaxtab/ajaxtab.tpl';
		}

		$this->render();
		
	}
	public function ResponseAjax(){
		$this->load->model('catalog/product');
		$this->load->model('tool/image');
		$this->data['products'] = array();
		$getconfig = $this->config->get('ajaxtab_module');
		$this->data['button_cart'] = $this->language->get('button_cart');
		if(isset($this->request->get['type'])){
			$type = $this->request->get['type'];
		}else{
			$type = $this->request->get['type'];
		}
		$specials = $getconfig[0][''.$type];
		$image_w = $specials['width'];
		$image_h = $specials['height'];
		$limit = $specials['limit'];
		switch($type){
			case "specials" :
				$data = array(
					'sort'  => 'pd.name',
					'order' => 'ASC',
					'start' => 0,
					'limit' => $limit
				);
				$results = $this->model_catalog_product->getProductSpecials($data);
				break;
			case "mostviews" :
				$results = $this->Getmostview($limit);
				break;
			case "bestsellers";
				$results = $this->model_catalog_product->getBestSellerProducts($limit);
				break;
			case "latest" :
				$data = array(
					'sort'  => 'p.date_added',
					'order' => 'DESC',
					'start' => 0,
					'limit' => $limit
				);
				$results = $this->model_catalog_product->getProducts($data);
				break;
			case "featured" :
				$results = array();
				$products = explode(',', $this->config->get('featured_product'));	
				foreach ($products as $product_id) {
					$results[] = $this->model_catalog_product->getProduct($product_id);
				}
				break;
		}
		
		foreach ($results as $result) {
			if ($result['image']) {
				$image = $this->model_tool_image->resize($result['image'], $image_w, $image_h);
			} 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;
			}
			
			$this->data['products'][] = array(
				'product_id' => $result['product_id'],
				'thumb'   	 => $image,
				'name'    	 => $result['name'],
				'price'   	 => $price,
				'special' 	 => $special,
				'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/ajaxtab/ajaxtab_product.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/ajaxtab/ajaxtab_product.tpl';
		} else {
			$this->template = 'default/template/ajaxtab/ajaxtab_product.tpl';
		}
			
		$this->response->setOutput($this->render());
	}	
	
	public function Getmostview($limit){
		$product_data = $this->cache->get('product.mostview.' . $this->config->get('config_language_id') . '.' . (int)$this->config->get('config_store_id') . '.' . $limit);
		if (!$product_data) {
			$query = $this->db->query("SELECT p.product_id FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) WHERE p.status = '1' AND p.date_available <= NOW() AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "' AND p.viewed > 0 ORDER BY p.viewed DESC LIMIT " . (int)$limit);
			foreach ($query->rows as $result) {
				$product_data[$result['product_id']] = $this->model_catalog_product->getProduct($result['product_id']);
			}
			$this->cache->set('product.mostview.' . $this->config->get('config_language_id') . '.' . (int)$this->config->get('config_store_id') . '.' . $limit, $product_data);
		}
		return $product_data;
	}
}
?>