<?php 
class ControllerAccountLogin extends Controller {
	var $error = array();
	
  	function index() {
		$customer =& $this->locator->get('customer');
		$language =& $this->locator->get('language');
		$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');
				
		if ($customer->isLogged()) {  
      		$response->redirect($url->ssl('account'));
    	}
	
    	$language->load('controller/account_login.php');

    	$template->set('title', $language->get('heading_title'));
			
		if (($request->isPost()) && ($this->validate())) {			
      		if ($request->has('redirect', 'post')) {
				$response->redirect($request->get('redirect', 'post'));
      		} else {
				$response->redirect($url->ssl('account'));
      		} 
    	}  
		
    	$view = $this->locator->create('template');

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

    	$view->set('text_new_customer', $language->get('text_new_customer'));
    	$view->set('text_i_am_new_customer', $language->get('text_i_am_new_customer'));
    	$view->set('text_returning_customer', $language->get('text_returning_customer'));
    	$view->set('text_i_am_returning_customer', $language->get('text_i_am_returning_customer'));
    	$view->set('text_create_account', $language->get('text_create_account'));
    	$view->set('text_forgotten_password', $language->get('text_forgotten_password'));
    	$view->set('text_forgotten_password', $language->get('text_forgotten_password'));

    	$view->set('entry_email', $language->get('entry_email_address'));
    	$view->set('entry_password', $language->get('entry_password'));

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

		$view->set('error', @$this->error['message']);
    
		$view->set('action', $url->ssl('account_login'));

    	if ($request->has('redirect')) {
			$view->set('redirect', $request->get('redirect', 'post'));
		} else {
      		$view->set('redirect', $session->get('redirect'));
	  		$session->delete('redirect');		  	
    	}

    	$view->set('message', $session->get('message'));
    
		$session->delete('message');

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

    	$view->set('forgotten', $url->ssl('account_forgotten'));

		$template->set('content', $view->fetch('content/account_login.tpl'));
	
		$template->set($module->fetch());
	
		$response->set($template->fetch('layout.tpl'));
  	}
  
  	function validate() {
		$customer =& $this->locator->get('customer');
		$language =& $this->locator->get('language');
		$request  =& $this->locator->get('request');
					
    	if (!$customer->login($request->get('email', 'post'), $request->get('password', 'post'))) {
      		$this->error['message'] = $language->get('error_login');
    	}
	
    	if (!$this->error) {
      		return TRUE;
    	} else {
      		return FALSE;
    	}  	
  	}
}
?>