<?php 
class ControllerCompletedreview extends Controller { 

	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');
		$module   =& $this->locator->get('module');
		$response =& $this->locator->get('response');
		$session  =& $this->locator->get('session');
		$template =& $this->locator->get('template');
		$url      =& $this->locator->get('url');
		$request  =& $this->locator->get('request');	
		$tax      =& $this->locator->get('tax');
		$image    =& $this->locator->get('image');
		$view = $this->locator->create('template');		
		
    	if (!$customer->isLogged()) {
      		$session->set('redirect', $url->ssl('completed_review'));
	     	$response->redirect($url->ssl('account_login'));
    	} 
    	if($request->has('page')){
		   $page=$request->get('page');
		}else{
			$page = 1;
		}    
		
        $customer_id = $customer->getId();
		$review_rows = $database->getRows($database->splitQuery("select * from `review`  where customer_id=$customer_id order by review_id desc", $page, 2));   

		$reviews_data=array();
		
		foreach($review_rows as $k=>$review){
			$review_images_data=array();
		    $product_info=$database->getRow("select p.sold, pd.name as product_title, i.filename 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=".$review['product_id']);
		    $order_info=$database->getRow("select o.reference,o.date_added, op.price from `order` o left join `order_product` op on (o.order_id=op.order_id) where o.customer_id=$customer_id and op.itemcode=".$review['product_id']);      	
		  	
		    $review_image_rows=$database->getRows("select review_image_filename from review_image where review_id=".$review['review_id']);
		    foreach($review_image_rows as $f_image){
		    	$review_images_data[] = CDN_REVIEW_IMAGE.$f_image['review_image_filename'];  
		    }	
		    
			$reviews_data[] = array(
					'review_id'=>$review['review_id'],
                    'product_id'=>$review['product_id'],
                    'product_href' => $url->ssl('product', FALSE, array('product_id' => $review['product_id'])), 
                    'product_title'=>$product_info['product_title'],                    
        			'thumb' => $image->resize($product_info['filename'], 270, 270),
        			'price' => $order_info['price'],
        			'review_content'  => $review['content'],	
        			'reference'=>$order_info['reference'],        			
        			'review_images'=>$review_images_data,
        			'rating'=>$review['rating'],
        			'size'=>$review['product_size'],
        			'color'=>$review['product_color'],
        			'material'=>$review['product_material'],
        			'date_added'=>$order_info['date_added'],
        			'sold'=>$product_info['sold'],
        			'review_info'=>$database->getRow("select avg(rating) as avg, count(review_id) as count from `review` where product_id=".$review['product_id']),
        			'delete_href'=>$url->ssl('completed_review', 'delete', array('review_id' => $review['review_id']))
      			);	          	  
		}
	 	$view->set('reviews', $reviews_data);			
		
		$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);  	      	        		
  		$view->set('page', $page);	
  		$view->set('text_results', $language->get('text_results',$page, $database->getPages()));		
  		$view->set('action',  $url->href('completed_review'));
               
		$template->set('content', $view->fetch('content/completed_review.tpl'));
		$template->set($module->fetchAccount());	
    	$response->set($template->fetch('layout.tpl')); 
	}
	function delete() { 
		
		$database =& $this->locator->get('database');
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		
		if ($request->has('review_id')) {			
			$review_image_rows=$database->getRows("select review_image_filename from `review_image` where review_id = ".$request->get('review_id'));
			foreach($review_image_rows as $result)
			{
				 @unlink(DIR_REVIEW_IMAGE.$result['review_image_filename']);	
			}
			
			$database->query("delete from `review` where review_id = ".$request->get('review_id'));
			$database->query("delete from `review_image` where review_id = ".$request->get('review_id'));
			
			echo "<script>alert('Delete Review Successfully');window.history.back(-1);</script>";
		}
	}
	function validateForm() { 
		$request  =& $this->locator->get('request');
		$language =& $this->locator->get('language');
		$user     =& $this->locator->get('user');
		$upload   =& $this->locator->get('upload');
		$image_module    =& $this->locator->get('image');
    
		$image_list = array();
		$image_list[0]='image';
		$image_list[1]='image_1';
		$image_list[2]='image_2';
		$image_list[3]='image_3';
		$image_list[4]='image_4';
		$image_list[5]='image_5';
		$image_list[6]='image_6';
		$image_list[7]='image_7';
		$image_list[8]='image_8';
    	
		$image_posted = array();
		$i=0;
		while($i<9)
		{
			if ($upload->has($image_list[$i]) )  {			
				if ( $upload->getName($image_list[$i])=='' )
				{
					$i = $i + 1;
					continue;
				}				
				$image_name = '';
				$image_path = ''; 
				if ($request->get('OriginalFileName','post'))
				{
					$image_name = $upload->getName($image_list[$i]);
				}else
				{
					$image_name = $upload->getSize($image_list[$i]).'-'.time().'-'.$upload->getName($image_list[$i]);
				}				
				$image_path = DIR_REVIEW_IMAGE. $image_name;				
				if (!$upload->save($image_list[$i], $image_path)) {
					$this->error['file'] = $language->get('error_upload');
					$i = $i + 1;
					continue;					
				}
				if ((strlen($upload->getName($image_list[$i])) < 3) || (strlen($upload->getName($image_list[$i])) > 128)) {
					$this->error['file'] = $language->get('error_filename');
					$i = $i + 1;
					continue;					
				}				
				$allowed = array(
					'image/jpeg',
					'image/pjpeg',
					'image/gif', 
					'image/png',
					'image/x-png'
				);		  
				if (!in_array($upload->getType($image_list[$i]), $allowed)) {
					$this->error['file'] = $language->get('error_filetype');
					$i = $i + 1;
					continue;					
				}		  
				if ($upload->hasError($image_list[$i])) {
					$this->error['message'] = $upload->getError('image');
					$i = $i + 1;
					continue;
				}				
				if ( $request->has('image_title_'.$i,'post') )
				{
				$image_posted[] = array( 'name' => $image_name,
				                         'path'=>$image_path,
										 'id'=>$image_path,
										 'title'=>$request->get('image_title_'.$i,'post') );
				}else
				{
				$image_posted[] = array( 'name' => $image_name,
				                         'path'=>$image_path,
										 'id'=>$image_path );				
				}
			}			
			$i = $i + 1;
    	}			
	  		return $image_posted;
  	}
   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('completed_review.page', $request->get('page', 'post'));
		}

		$response->redirect($url->ssl('completed_review', FALSE));
	}
}
?>