<?php 
class ControllerReviewWrite extends controller { 
	var $error = array();
		  
  	function index() {
		$config   =& $this->locator->get('config');
		$currency =& $this->locator->get('currency');
		$customer =& $this->locator->get('customer');
		$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');
		
    	if (!$customer->isLogged()) {
	  		$query = array(
	    		'product_id' => $request->get('product_id'),
				'review_id'  => $request->get('review_id')
	  		);
	
      		$session->set('redirect', $url->ssl('review_write', FALSE, $query));

      		$response->redirect($url->ssl('account_login'));
    	} 
	
    	$language->load('controller/review_write.php');
	
		if (($request->isPost()) && ($this->validate())) {
      		$sql = "insert into review set author = '?', customer_id = '?', product_id = '?', text = '?', rating = '?', date_added = now()";
      		$database->query($database->parse($sql, $customer->getFirstName() . ' ' . $customer->getLastName(), $customer->getId(), $request->get('product_id'), strip_tags($request->get('text', 'post')), $request->get('rating', 'post')));

	  		$response->redirect($url->ssl('review_success'));
    	}
    	
		$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) {	  
	  		$template->set('title', $language->get('heading_title'));
	  
	  		$view = $this->locator->create('template');
    
	  		$view->set('heading_title', $language->get('heading_title'));

      		$view->set('text_enlarge', $language->get('text_enlarge'));
      		$view->set('text_author', $language->get('text_author'));
      		$view->set('text_note', $language->get('text_note'));
      		$view->set('text_product', $language->get('text_product'));

      		$view->set('entry_review', $language->get('entry_review'));
      		$view->set('entry_rating', $language->get('entry_rating'));
      		$view->set('entry_good', $language->get('entry_good'));
      		$view->set('entry_bad', $language->get('entry_bad'));

      		$view->set('button_continue', $language->get('button_continue'));
      		$view->set('button_back', $language->get('button_back'));
    
	  		$view->set('error', @$this->error['message']);

	  		$query = array(
	    		'product_id' => $request->get('product_id'),
				'review_id'  => $request->get('review_id')
	  		);
	      
	  		$view->set('action', $url->ssl('review_write', FALSE, $query));
      
	  		$view->set('product', $product_info['name']);
      
	  		$view->set('price', $currency->format($tax->calculate($product_info['price'], $product_info['tax_class_id'], $config->get('config_tax'))));
      
	  		$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('author', $customer->getFirstName() . ' ' . $customer->getLastName());
      
	  		$view->set('text', strip_tags($request->get('text', 'post')));
      
	  		$view->set('rating', $request->get('rating', 'post'));
      
	  		$query = array(
	    		'product_id' => $request->get('product_id'),
				'review_id'  => $request->get('review_id')
	  		);
	  
	  		$view->set('back', $url->href('review_info', FALSE, $query));

	  		$template->set('content', $view->fetch('content/review_write.tpl'));
	
	  		$template->set($module->fetch());
	
      		$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('content', $view->fetch('content/error.tpl'));
	  
	  		$template->set($module->fetch());
	
      		$response->set($template->fetch('layout.tpl'));  	
		}
  	}
		
  	function validate() {
		$request  =& $this->locator->get('request');
		$language =& $this->locator->get('language');
			
    	if ((strlen($request->get('text', 'post')) < 25) || (strlen($request->get('text', 'post')) > 1000)) {
      		$this->error['message'] = $language->get('error_text');
    	}

    	if (!$request->get('rating', 'post')) {
      		$this->error['message'] = $language->get('error_rating');
    	}
	
    	if (!$this->error) {
      		return TRUE;
    	} else {
      		return FALSE;
    	}	
	}
}
?>
