<?php  
class ControllerModuleAccessories extends Controller {
	protected function index() {
		$this->language->load('module/accessories');
		
		if (file_exists('catalog/view/theme/' . $this->config->get('config_template') . '/stylesheet/accessories.css')) {
			$this->document->addStyle('catalog/view/theme/' . $this->config->get('config_template') . '/stylesheet/accessories.css');
		} else {
			$this->document->addStyle('catalog/view/theme/default/stylesheet/accessories.css');
		}
		
		if ($this->config->get('heading_box' . $this->config->get('config_language_id'))) {
			$this->data['heading_title'] = $this->config->get('heading_box' . $this->config->get('config_language_id'));
		} else {
			$this->data['heading_title'] = $this->language->get('heading_title');
		}
		
		if ($this->config->get('text_options_product' . $this->config->get('config_language_id'))) {
			$this->data['text_accessories_product_options'] =  $this->config->get('text_options_product' . $this->config->get('config_language_id'));
		} else {
			$this->data['text_accessories_product_options'] =  $this->language->get('text_accessories_product_options');
		}
		
		$this->data['button_cart'] =  $this->language->get('button_cart');
		$this->data['text_qty'] =  $this->language->get('text_qty');
		$this->data['text_discount'] =  $this->language->get('text_discount');
		
		if (isset($this->request->get['product_id'])) {
			$product_id = (int)$this->request->get['product_id'];
		} else {
			$product_id = 0;
		}
		
		$this->load->model('catalog/product');
		
		$product_info = $this->model_catalog_product->getProduct($product_id);
			
		if ($product_info) {
		
			if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
				$this->data['price_accessory'] = $this->currency->format($this->tax->calculate($product_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax')), '', '', false);
			} else {
				$this->data['price_accessory'] = false;
			}
			
			if ((float)$product_info['special']) {
				$this->data['special_accessory'] = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax')), '', '', false);
			} else {
				$this->data['special_accessory'] = false;
			}
			
			if ($this->config->get('description_accessories_product')) {
				$description_accessories_product = $this->config->get('description_accessories_product');
			} else {
				$description_accessories_product = 200;
			}
			
			if ($this->config->get('image_width')) {
				$image_accessories_width = $this->config->get('image_width');
			} else {
				$image_accessories_width = 60;
			}
			
			if ($this->config->get('image_height')) {
				$image_accessories_height = $this->config->get('image_height');
			} else {
				$image_accessories_height = 60;
			}
			
			if ($this->config->get('popup_width')) {
				$popup_accessories_width = $this->config->get('popup_width');
			} else {
				$popup_accessories_width = 200;
			}
			
			if ($this->config->get('popup_height')) {
				$popup_accessories_height = $this->config->get('popup_height');
			} else {
				$popup_accessories_height = 200;
			}
			
			$this->data['accessories_product'] = array();
			
			$results = $this->model_catalog_product->getAccessoriesProduct($this->request->get['product_id']);

			foreach ($results as $result) {
				if ($result['image']) {
					$image = $this->model_tool_image->resize($result['image'], $image_accessories_width, $image_accessories_height);
				} else {
					$image = $this->model_tool_image->resize('no_image.jpg', $image_accessories_width, $image_accessories_height);
				}
				
				if ($result['image']) {
					$popup = $this->model_tool_image->resize($result['image'], $popup_accessories_width, $popup_accessories_height);
				} else {
					$popup = $this->model_tool_image->resize('no_image.jpg', $popup_accessories_width, $popup_accessories_height);
				}
					
				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')));
					$price_accessory = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')), '', '', false);
				} else {
					$price = false;
					$price_accessory = false;
				}
						
				if ((float)$result['special']) {
					$special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')));
					$special_accessory = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')), '', '', false);
				} else {
					$special = false;
					$special_accessory = false;
				}
				
				if ($result['minimum'] > 1) {
					$minimum = $result['minimum'];
				}
				else{
					$minimum = 1;
				}
				
				$discounts = $this->model_catalog_product->getProductDiscounts($result['product_id']);
				
				$accessories_discounts = array(); 
				
				foreach ($discounts as $discount) {
					$accessories_discounts[] = array(
						'quantity' 			 => $discount['quantity'],
						'price'    			 => $this->currency->format($this->tax->calculate($discount['price'], $product_info['tax_class_id'], $this->config->get('config_tax'))),
						'price_accessory'    => $this->currency->format($this->tax->calculate($discount['price'], $product_info['tax_class_id'], $this->config->get('config_tax')), '', '', false)
					);
				}						
				
				$options = $this->model_catalog_product->getProductOptions($result['product_id']);
				
				$description_symbols = $description_accessories_product;
					$descr_plaintext = strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8'));
				if( mb_strlen($descr_plaintext, 'UTF-8') > $description_symbols ) {
					$descr_plaintext = mb_substr($descr_plaintext, 0, $description_symbols, 'UTF-8') . '&nbsp;&hellip;';
				}
				
				$this->data['accessories_product'][] = array(
					'product_id'    	=> $result['product_id'],
					'name'    			=> $result['name'],
					'description' 		=> $descr_plaintext,
					'price'   			=> $price,
					'price_accessory'   => $price_accessory,
					'special' 			=> $special,
					'special_accessory'	=> $special_accessory,
					'minimum'			=> $minimum,
					'discounts'			=> $accessories_discounts,					
					'image'   			=> $image,
					'popup'   			=> $popup,
					'href'    			=> $this->url->link('product/product', 'product_id=' . $result['product_id']),
					'options'			=> (empty($options)) ? false : true
				);
			}
			
		$this->data['mrg'] = $image_accessories_width + 15;
			
		}
    	
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/accessories.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/module/accessories.tpl';
		} else {
			$this->template = 'default/template/module/accessories.tpl';
		}
		
		$this->render();
	}
}
?>