<?php 
class ControllerMyQuestions extends Controller { 
	function index(){
		$customer =& $this->locator->get('customer');
		$database =& $this->locator->get('database');
		$module   =& $this->locator->get('module');
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$session  =& $this->locator->get('session');
		$template =& $this->locator->get('template');
		$url      =& $this->locator->get('url');
		$language =& $this->locator->get('language');
		$view = $this->locator->create('template');
		$image    =& $this->locator->get('image');
		$config   =& $this->locator->get('config');
		$currency =& $this->locator->get('currency');
		$tax      =& $this->locator->get('tax');
		
		if (!$customer->isLogged()) {
	  		$session->set('redirect', $url->ssl('my_questions'));
	  		$response->redirect($url->ssl('account_login'));
    	} 
    	
    	if($request->has('page')){
		   $page=$request->get('page');
		}else{
			$page = 1;
		}    
			
		$question_data = array();
		$sql = "select q.*,pd.name,p.product_id,p.quantity,p.sold,p.tax_class_id,price from question as q join product_description pd on (q.product_id = pd.product_id) join product p on (p.product_id=pd.product_id) where q.email like '".$customer->getEmail()."'";
		
		$question_list = $database->getRows($database->splitQuery($sql, $page, 2));
			
		
    	foreach ($question_list as $v) {
           $image_info=$database->getRow("select i.filename from product p left join image i on p.image_id=i.image_id where p.product_id='".$v['product_id']."'");  
           $review_sql = "select avg(rating) as avg, count(review_id) as count from `review` where product_id=".$v['product_id'];
//           echo $review_sql."<br />";
		   $review_info=$database->getRow($review_sql);
//		   print_r($review_info);exit;
		   $review_avg = $review_count = 0;
		   if($review_info){
		   	 $review_avg = intval(($review_info['avg'])*20);
		   	 $review_count = $review_info['count'];
		   }
    		$question_data[] = array(
				'product_id'       => $v['product_id'],		
				'product_href' => $url->ssl('product', FALSE, array('product_id' => $v['product_id'])),    				
      			'name'     => $v['name'],                 		
      		    'thumb' => $image->resize($image_info['filename'], $config->get('config_image_width'), $config->get('config_image_height')),
				'price' => $currency->format($tax->calculate($v['price'], $v['tax_class_id'])),               
      			'question_id' => $v['question_id'],
      			'quantity' => $v['quantity'],
      			'sold' => $v['sold'],
      			'text'  =>$v['text'],
                'reply_text'  =>$v['reply_text'],             
                'status'  =>$v['status'],
                'date_added'  =>$v['date_added'],
                'date_modified'  =>$v['date_modified'],
                'author'  =>$v['author'],
                'review_avg'=> $review_avg,
                'review_count'=> $review_count
    		);
  		}
		
		$view->set('question_data', $question_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('my_questions'));	
  		
		$template->set('content', $view->fetch('content/my_questions.tpl'));
		$template->set($module->fetchAccount());	
    	$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(($request->has('path') ? 'my_questions.' . $request->get('path') . '.page' : 'my_questions.page'), $request->get('page', 'post'));
		}

		$response->redirect($url->ssl('my_questions', FALSE, array('path' => $request->get('path'))));
	}
}
?>