<?php 
class ControllerMyCoupons extends Controller { 

	function index() {
		$customer =& $this->locator->get('customer'); 
		$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');
		$language =& $this->locator->get('language');
		$database =& $this->locator->get('database');
		$view = $this->locator->create('template');
		if (!$customer->isLogged()) {
	  		$session->set('redirect', $url->ssl('coupon'));
	  		$response->redirect($url->ssl('account_login'));
    	}

		$email = $customer->getEmail();
	
		if($request->has('page')){
		   $page=$request->get('page');
		}else{
			$page = 1;
		}    
		
		$sql = "select cp.code,cp.discount,cp.status,cp.usable_times,cp.date_added,cd.description from coupon cp left join coupon_description cd on cp.coupon_id = cd.coupon_id where cp.email = '$email' order by cp.coupon_id Desc";
		$coupon_rows = $database->getRows($database->splitQuery($sql, $page, 2));
		$view->set('coupons', $coupon_rows);
		
		$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_coupons'));	
		
		$template->set('content', $view->fetch('content/my_coupons.tpl'));
		$template->set($module->fetchAccount());	
    	$response->set($template->fetch('layout.tpl')); 
	}
	
}
?>     