<?php 
class ControllerReview extends Controller {
	function index() {  
		$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');
		$session  =& $this->locator->get('session');
		$tax      =& $this->locator->get('tax');
		$template =& $this->locator->get('template');
		$url      =& $this->locator->get('url');
	
    	$language->load('controller/review.php');

    	$sql = "select r.review_id, r.author, r.rating, r.text, p.product_id, pd.name, p.price, i.filename, r.date_added from review r left join product p on(r.product_id = p.product_id) 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 p.date_available < now() and p.status = '1' and r.status = '1' and pd.language_id = '" . (int)$language->getId() . "' order by r.date_added desc";
    	$results = $database->getRows($database->splitQuery($sql, $session->get('review.page'), $config->get('config_max_rows')));

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

      		$product_info = $database->getRow("select * from product p left join product_description pd on p.product_id = pd.product_id where p.product_id = '" . (int)$request->get('product_id') . "' and pd.language_id = '" . (int)$language->getId() . "'");
	  
	  		$template->set('title', $language->get('heading_title', $product_info['name']));

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

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

      		$view->set('text_results', $language->get('text_results', $database->getFrom(), $database->getTo(), $database->getTotal()));
      		$view->set('text_review_by', $language->get('text_review_by'));
      		$view->set('text_date_added', $language->get('text_date_added'));
      		$view->set('text_rating', $language->get('text_rating'));

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

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

      		$review_data = array();

      		foreach ($results as $result) {
        		$review_data[] = array(
          			'href'       => $url->href('review_info', FALSE, array('review_id' => $result['review_id'])),
          			'name'       => $result['name'],
          			'thumb'      => $image->resize($result['filename'], $config->get('config_image_width'), $config->get('config_image_height')),
          			'text'       => trim(substr(strip_tags($result['text']), 0, 150)) . '...',
          			'rating'     => $result['rating'],
          			'out_of'     => $language->get('text_out_of', $result['rating']),
          			'author'     => $result['author'],
          			'date_added' => date($language->get('date_format_long'), strtotime($result['date_added']))
        		);
      		}

      		$view->set('reviews', $review_data);

      		$view->set('page', $session->get('review.page'));

      		$page_data = array();

      		for ($i = 1; $i <= $database->getPages(); $i++) {
        		$page_data[] = array(
          			'text'  => $language->get('text_pages', $i, $database->getPages()),
          			'value' => $i
        		);
      		}

      		$view->set('pages', $page_data);

	  		$template->set('content', $view->fetch('content/review.tpl'));
	  
	  		$template->set($module->fetch());
	
      		$response->set($template->fetch('layout.tpl'));   
    	} else {
      		$template->set('title', $language->get('text_empty'));
 
      		$view = $this->locator->create('template');

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

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

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

      		$view->set('continue', $url->href('home'));

	  		$template->set('content', $view->fetch('content/error.tpl'));
	  
	  		$template->set($module->fetch());
	
      		$response->set($template->fetch('layout.tpl'));  
    	}
  	}
	
	function page() {
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$session  =& $this->locator->get('session');
		$url      =& $this->locator->get('url');
						
		if ($request->has('page', 'post')) {
	  		$session->set('review.page', $request->get('page', 'post'));
		}
		
		$response->redirect($url->ssl('review'));
	}
}
?>
