<?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()&&!isset($_POST)) {
	        
			$session->set('redirect', $url->ssl('checkout_shipping'));

	  		$response->redirect($url->ssl('account_login'));
    	} 
    	
    	 elseif(isset($_POST)&&isset($_POST['cancel_return'])){
    	    

        	echo '

        	       <form action="https://www.beststoreitems.com/index.php?controller=checkout_payment" method="post"  id="form" name="form">

        	       

        	          <input type="hidden" name="cmd" value="'.$_POST['cmd'].'" />

        	          <input type="hidden" name="business" value="wutingjing@hotmail.com" />

        	          <input type="hidden" name="item_name" value="'.$_POST['item_name'].'" />

        	          <input type="hidden" name="currency_code" value="'.$_POST['currency_code'].'" />

        	          <input type="hidden" name="amount" value="'.$_POST['amount'].'" />

        	          <input type="hidden" name="first_name" value="'.$_POST['first_name'].'" />

        	          <input type="hidden" name="last_name" value="'.$_POST['last_name'].'" />

        	          <input type="hidden" name="address1" value="'.$_POST['address1'].'" />
        	          
        	           <input type="hidden" name="charset" value="utf-8" />
        	           
        	           <input type="hidden" name="lc" value="US" />

        	          <input type="hidden" name="city" value="'.$_POST['city'].'" />

        	          <input type="hidden" name="zip" value="'.$_POST['zip'].'" />

        	          <input type="hidden" name="country" value="'.$_POST['country'].'" />

        	          <input type="hidden" name="address_override" value="0" />

        	          <input type="hidden" name="notify_url" value="https://www.beststoreitems.com/payment/paypal/paypal_notify.php" />

        	          <input type="hidden" name="email" value="'.$_POST['email'].'" />

        	          <input type="hidden" name="invoice" value="'.$_POST['invoice'].'" />

        	          <input type="hidden" name="return" value="https://www.beststoreitems.com/paypal_sucess.html" />

        	          <input type="hidden" name="rm" value="2" />

        	          <input type="hidden" name="no_note" value="1" />

        	          <input type="hidden" name="cancel_return" value="https://www.beststoreitems.com/paypal_cancel.html" />

        	          

        	          <script language="javascript">

						   document.form.submit();

					  </script>

				   </form>	 

        	';

        	exit;

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

       	 if ($request->isPost() && isset($_REQUEST['address_id'])) {			
			 $session->set('shipping_address_id', $_POST['address_id']);
		 }
         
    	if (!$address->has($session->get('shipping_address_id'))) {    
	  		$session->set('shipping_address_id', $customer->getAddressId());
    	}
        $sql = "select address_1,address_2  from address where customer_id='?'";
        $profile=$database->getRows($database->parse($sql, (int)$customer->getId())); 
  
    	if (!$address->has($session->get('shipping_address_id'))||(empty($profile[0]['address_1'])&&empty($profile[0]['address_2'])&&empty($profile[1]['address_1'])&&empty($profile[1]['address_2']))) {
	  		$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_name', 'post'));
			
    		$check_comment=$request->get('comment', 'post');
    		if($check_comment=='')
    		{
    			$session->set('comment', '');
    		}
    		else 
    		{
    			$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'));
					
	 

		//Free ShippingҲҪ���÷����ַ
		if ($cart->hasShipping()) {
    		$view->set('methods', $shipping->getQuotes());	
		}

		$view->set('weight', $cart->getWeight());

    	$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->fetchPublic());
	
    	$response->set($template->fetch('layout.tpl'));
  	}
  
  	function validate() {
  		$session  =& $this->locator->get('session');  		
		$language =& $this->locator->get('language');
		$request  =& $this->locator->get('request');
		$cart     =& $this->locator->get('cart');	
		$database =& $this->locator->get('database');
		if ($cart->hasShipping()) {    
			if (!$request->get('shipping', 'post')) {
				$this->error['message'] = $language->get('error_shipping');
			}
		}
	    $checkout=$database->getRow("select value from setting where `key` = 'config_checkout'");
     
  	    if ($cart->getTotal()<$checkout['value'] && $cart->hasShipping()==FALSE)
		{
			$this->error['message'] = "Sorry, We can't accept the order less than $".$checkout['value']." USD. Please make sure your order is over $".$checkout['value']." USD.";
		}
            
		$check_allow_ship=$database->getRow("select a.address_id,a.country_id,c.name,c.allow_ship from address As a join country As c on a.country_id=c.country_id where a.address_id='".$session->get('shipping_address_id')."'");	
		if($check_allow_ship['allow_ship']==0){
			$this->error['message'] = "Sorry, We can't ship to the country you filled in, as the custom of this country recently check replica very strickly.  ";
		}
		if (!$this->error) {
	  		return TRUE;
		} else {
	  		return FALSE;
		}
  	}
}
?>
