<?php  
class ControllerProduct extends Controller {
	function index() { 
		$cart     =& $this->locator->get('cart');
		$config   =& $this->locator->get('config');
		$currency =& $this->locator->get('currency');
		$database =& $this->locator->get('database');
		$language =& $this->locator->get('language');
		$image    =& $this->locator->get('image');
		$module   =& $this->locator->get('module');
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$shipping =& $this->locator->get('shipping');
		$tax      =& $this->locator->get('tax');
		$template =& $this->locator->get('template');
		$url      =& $this->locator->get('url');
			
    	if ($request->isPost()) {
      		$cart->add($request->get('product_id', 'post'), '1', $request->get('option', 'post'));
 
      		$response->redirect($url->href('cart'));
    	}

    	$language->load('controller/product.php');
		
		$product_info = $database->getRow("select * from product p left join product_description pd on (p.product_id = pd.product_id) left join image i on (p.image_id = i.image_id) where p.product_id = '" . (int)$request->get('product_id') . "' and pd.language_id = '" . (int)$language->getId() . "' and p.date_available < now() and p.status = '1'");
    	
		if ($product_info) {
      		$database->query("update product set viewed = viewed + 1 where product_id = '" . (int)$request->get('product_id') . "'");

      		$breadcrumb = array();

      		$breadcrumb[] = array(
        		'href'      => $url->href('home'),
        		'text'      => $language->get('text_home'),
        		'separator' => FALSE
      		);

      		if ($request->get('path')) {
        		foreach (explode('_', $request->get('path')) as $category_id) {
          			$category_info = $database->getRow("select cd.name, c.path from category c left join category_description cd on c.category_id = cd.category_id where c.category_id = '" . (int)$category_id . "' and cd.language_id = '" . (int)$language->getId() . "'");

          			$breadcrumb[] = array(
						'href'      => $url->href('category', FALSE, array('path' => $category_info['path'])),
            			'text'      => $category_info['name'],
            			'separator' => $language->get('text_separator')
          			);
        		}
      		}

      		$query = array(
        		'path'       => $request->get('path'),
        		'product_id' => $request->get('product_id')
      		);

      		$breadcrumb[] = array(
        		'href'      => $url->href('product', FALSE, $query),
        		'text'      => $product_info['name'],
        		'separator' => $language->get('text_separator')
      		);

	  		$template->set('title', $product_info['name']);

      		$view = $this->locator->create('template');

      		$view->set('heading_title', $product_info['name']);

      		$view->set('text_enlarge', $language->get('text_enlarge'));
      		$view->set('text_images', $language->get('text_images'));
      		$view->set('text_options', $language->get('text_options'));

      		$view->set('button_reviews', $language->get('button_reviews'));
      		$view->set('button_add_to_cart', $language->get('button_add_to_cart'));

      		$view->set('breadcrumbs', $breadcrumb);

      		$query = array(
        		'path'       => $request->get('path'),
        		'product_id' => $request->get('product_id')
      		);

     		$view->set('action', $url->href('product', FALSE, $query));

      		$view->set('popup', $image->href($product_info['filename']));
      
	  		$view->set('thumb', $image->resize($product_info['filename'], $config->get('config_image_width'), $config->get('config_image_height')));

      		$view->set('price', $currency->format($tax->calculate($product_info['price'], $product_info['tax_class_id'], $config->get('config_tax'))));

      		$view->set('description', $product_info['description']);

      		$view->set('product_id', $request->get('product_id'));

      		$view->set('review', $url->href('review', FALSE, array('product_id' => $request->get('product_id'))));

      		$options = array();

      		$results = $database->getRows("select * from product_to_option where product_id = '" . (int)$request->get('product_id') . "' order by sort_order");

      		foreach ($results as $result) {
        		$options[$result['option_id']][] = array(
          			'product_to_option_id' => $result['product_to_option_id'],
          			'option_value_id'      => $result['option_value_id'],
          			'price'                => $result['price'],
          			'prefix'               => $result['prefix']
        		);
      		}

      		$option_data = array();

      		foreach ($options as $key => $values) {
        		$option_value_data = array();

        		foreach ($values as $value) {
          			$option_value_info = $database->getRow("select * from option_value where option_value_id = '" . (int)$value['option_value_id'] . "' and option_id = '" . (int)$key . "' and language_id = '" . (int)$language->getId() . "'");

          			$option_value_data[] = array(
            			'product_to_option_id' => $value['product_to_option_id'],
            			'option_value_id'      => $value['option_value_id'],
            			'name'                 => $option_value_info['name'],
            			'price'                => (($value['price'] != '0.00') ? $currency->format($tax->calculate($value['price'], $product_info['tax_class_id'], $config->get('config_tax'))) : null),
            			'prefix'               => $value['prefix']
          			);
        		}

        		$option = $database->getRow("select * from `option` where option_id = '" . (int)$key . "' and language_id = '" . (int)$language->getId() . "'");

        		$option_data[] = array(
          			'option_id' => $key,
          			'name'      => $option['name'],
          			'value'     => $option_value_data
        		);
      		}

      		$view->set('options', $option_data);

      		$image_data = array();

      		$results = $database->getRows("select i.image_id, i.filename, id.title from image i left join image_description id on (i.image_id = id.image_id) left join product_to_image p2i on (i.image_id = p2i.image_id) where p2i.product_id = '" . (int)$request->get('product_id') . "' and id.language_id = '" . (int)$language->getId() . "'");

      		foreach ($results as $result) {
        		$image_data[] = array(
          			'title' => $result['title'],
          			'popup' => $image->href($result['filename']),
          			'thumb' => $image->resize($result['filename'], $config->get('config_image_width'), $config->get('config_image_height')),
        		);
      		}

      		$view->set('images', $image_data);
	  
	  		$template->set($module->fetch());
	  	  
	  		$template->set('content', $view->fetch('content/product.tpl'));
	  
	  		$response->set($template->fetch('layout.tpl'));
    	} else {
      		$template->set('title', $language->get('text_error'));

      		$view = $this->locator->create('template');

      		$view->set('heading_title', $language->get('text_error'));

      		$view->set('text_error', $language->get('text_error'));

      		$view->set('button_continue', $language->get('button_continue'));

      		$view->set('continue', $url->href('home'));
      
	  		$template->set($module->fetch());
	  	  
	  		$template->set('content', $view->fetch('content/error.tpl'));
	  
	  		$response->set($template->fetch('layout.tpl'));	
    	}
  	}
}
?>
