<?php 
class ControllerAccountHistory 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');
		
    	if (!$customer->isLogged()) {
      		$session->set('redirect', $url->ssl('account_history'));

	  		$response->redirect($url->ssl('account_login'));
    	}
 
    	$language->load('controller/account_history.php');

    	$template->set('title', $language->get('heading_title'));
		
		$results = $database->getRows($database->splitQuery("select o.order_id, o.reference, o.firstname, o.lastname, os.name as status, o.date_added, o.total, o.currency, o.value from `order` o left join order_status os on o.order_status_id = os.order_status_id where customer_id = '" . (int)$customer->getId() . "' and os.language_id = '" . (int)$language->getId() . "' order by order_id desc", $session->get('account_history.page'), $config->get('config_max_rows')));
		
		if ($results) {
      		$view = $this->locator->create('template');

      		$view->set('heading_title', $language->get('heading_title'));

      		$view->set('text_order', $language->get('text_order'));
      		$view->set('text_status', $language->get('text_status'));
     		$view->set('text_date_added', $language->get('text_date_added'));
      		$view->set('text_customer', $language->get('text_customer'));
      		$view->set('text_products', $language->get('text_products'));
      		$view->set('text_total', $language->get('text_total'));
     		$view->set('text_results', $language->get('text_results', $database->getFrom(), $database->getTo(), $database->getTotal()));

      		$view->set('entry_page', $language->get('entry_page'));

      		$view->set('button_view', $language->get('button_view'));
      		$view->set('button_continue', $language->get('button_continue'));
      
	  		$view->set('action', $url->href('account_history'));
	 
      		$order_data = array();

      		foreach ($results as $result) {
        		$product_info = $database->getRow("select count(*) as products from order_product where order_id = '" . (int)$result['order_id'] . "'");

        		$order_data[] = array(
          			'reference'  => $result['reference'],
          			'href'       => $url->ssl('account_invoice', FALSE, array('order_id' => $result['order_id'])),
          			'name'       => $result['firstname'] . ' ' . $result['lastname'],
          			'status'     => $result['status'],
          			'date_added' => date('d F Y', strtotime($result['date_added'])),
          			'products'   => $product_info['products'],
          			'total'      => $currency->format($result['total'], $result['currency'], $result['value'])
        		);
      		}

      		$view->set('orders', $order_data);

      		$view->set('page', $session->get('account_history.page'));

      		$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('continue', $url->ssl('account'));
	  
	  		$template->set('content', $view->fetch('content/account_history.tpl'));
	  
	  		$template->set($module->fetch());
	
      		$response->set($template->fetch('layout.tpl'));
    	} else {
      		$view = $this->locator->create('template');

      		$view->set('heading_title', $language->get('heading_title'));

      		$view->set('text_error', $language->get('text_error'));

      		$view->set('button_continue', $language->get('button_continue'));

      		$view->set('continue', $url->ssl('account'));

	  		$template->set('content', $view->fetch('content/error.tpl'));
	  
	  		$template->set($module->fetch());
	
      		$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('account_history.page', $request->get('page', 'post'));
		}
		
		$response->redirect($url->ssl('account_history'));
	}
}
?>
