<?php  
class ModuleReview extends Controller {
	function fetch() {
		$config   =& $this->locator->get('config');
		$database =& $this->locator->get('database');
		$image    =& $this->locator->get('image');
		$language =& $this->locator->get('language');
		$request  =& $this->locator->get('request');
		$url      =& $this->locator->get('url');
		
		if ($config->get('review_status')) {	
    		if (($request->get('controller') == 'product') && ($request->get('product_id'))) {
      			$review_info = $database->getRow("select r.review_id, r.rating, r.text, p.product_id, pd.name, i.filename 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.status = '1' and pd.language_id = '" . (int)$language->getId() . "' and p.date_available < now() and p.status = '1' and r.status = '1' order by rand() limit 1");
    		} else {
     			$review_info = $database->getRow("select r.review_id, r.rating, r.text, p.product_id, pd.name, i.filename 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.status = '1' and pd.language_id = '" . (int)$language->getId() . "' and p.date_available < now() and p.status = '1' and r.status = '1' order by rand() limit 1");
    		}	
		
			$language->load('extension/module/review.php');
    		 
			$view = $this->locator->create('template');
		
			if ($review_info) {
      			$view->set('heading_title', $language->get('heading_title'));
	
     			$view->set('text_rating', $language->get('text_rating', $review_info['rating']));
	
     			$view->set('name', $review_info['name']);

      			$view->set('rating', $review_info['rating']);

      			$view->set('desciption', trim(substr(strip_tags($review_info['text']), 0, 40)) . '...');
		
     			$view->set('image', $image->resize($review_info['filename'], $config->get('config_image_width'), $config->get('config_image_height')));

      			$view->set('review', $url->href('review_info', false, array('review_id' => $review_info['review_id'])));

   	   			$view->set('reviews', $url->href('review'));
	  			
				return $view->fetch('module/review.tpl');
    		} elseif ($request->get('product_id')) {
      			$view->set('heading_title', $language->get('heading_title'));
	  
				$view->set('text_write', $language->get('text_write'));
 
      			$view->set('write', $url->href('review_write', false, array('product_id' => $request->get('product_id'))));
				
				return $view->fetch('module/review.tpl');
    		}
		}
	}
}
?>
