<?php 
class ControllerCheckoutShipping extends Controller {
	var $error = array(); 
 	
  	function index() {
		$address  =& $this->locator->get('address');
		$cart     =& $this->locator->get('cart');
		$config   =& $this->locator->get('config');
		$customer =& $this->locator->get('customer');
		$database =& $this->locator->get('database');
		$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');
		$shipping =& $this->locator->get('shipping');
		$template =& $this->locator->get('template');
		$url      =& $this->locator->get('url');
				    		
		if (!$customer->isLogged()) {
			$session->set('redirect', $url->ssl('checkout_shipping'));

	  		$response->redirect($url->ssl('account_login'));
    	} 

    	if ((!$cart->hasProducts()) || ((!$cart->hasStock()) && (!$config->get('config_stock_checkout')))) {
	  		$response->redirect($url->ssl('cart'));
    	}

    	if (!$cart->hasShipping()) {
			$session->delete('shipping_address_id');
			$session->delete('shipping_method');
			
			$response->redirect($url->ssl('checkout_payment'));
    	}

    	if (!$address->has($session->get('shipping_address_id'))) {
	  		$session->set('shipping_address_id', $customer->getAddressId());
    	}

    	if (!$address->has($session->get('shipping_address_id'))) {
	  		$response->redirect($url->ssl('checkout_address', 'shipping'));
		}
		
    	$language->load('controller/checkout_shipping.php');

		$template->set('title', $language->get('heading_title')); 
  
    	if (($request->isPost()) && ($this->validate())) {
			$session->set('shipping_method', $request->get('shipping', 'post'));
			
			$session->set('comment', strip_tags($request->get('comment', 'post')));

	  		$response->redirect($url->ssl('checkout_payment'));
    	}
		
    	$view = $this->locator->create('template');

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

    	$view->set('text_shipping', $language->get('text_shipping'));
    	$view->set('text_shipping_to', $language->get('text_shipping_to'));
    	$view->set('text_shipping_address', $language->get('text_shipping_address'));
    	$view->set('text_shipping_method', $language->get('text_shipping_method'));
    	$view->set('text_shipping_methods', $language->get('text_shipping_methods'));
    	$view->set('text_comments', $language->get('text_comments'));
    
		$view->set('button_change_address', $language->get('button_change_address'));
    	$view->set('button_back', $language->get('button_back'));
    	$view->set('button_continue', $language->get('button_continue'));
    
		$view->set('error', @$this->error['message']);
    
		$view->set('action', $url->ssl('checkout_shipping'));
    		
    	$view->set('address', $address->getFormatted($session->get('shipping_address_id'), '<br />'));
    
    	$view->set('change_address', $url->ssl('checkout_address', 'shipping'));
					
    	$view->set('methods', $shipping->getQuotes());

    	$view->set('default', $session->get('shipping_method'));
		
    	$view->set('comment', $session->get('comment'));

    	$view->set('back', $url->href('cart'));
		
		$template->set('content', $view->fetch('content/checkout_shipping.tpl'));

		$template->set($module->fetch());
	
    	$response->set($template->fetch('layout.tpl'));
  	}
  
  	function validate() {
		$language =& $this->locator->get('language');
		$request  =& $this->locator->get('request');
			    
    	if (!$request->get('shipping', 'post')) {
	  		$this->error['message'] = $language->get('error_shipping');
		}
	
		if (!$this->error) {
	  		return TRUE;
		} else {
	  		return FALSE;
		}
  	}
}
?>
