<?php 



class ControllerCheckoutAddress extends Controller {



	var $error = array(); 



	



		function index() {



		



		$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');



		$template =& $this->locator->get('template');



		$url      =& $this->locator->get('url');



    	



		$template->set('content', $this->getForm());



		$template->set($module->fetchPublic());



    	$response->set($template->fetch('layout.tpl'));



  	} 



	



	function shipping() {



		



		$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');



		$template =& $this->locator->get('template');



		$url      =& $this->locator->get('url');



    	$language->load('controller/checkout_address.php');



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



		if (!$customer->isLogged()) {  



			$session->set('redirect', $url->ssl('checkout_shipping'));



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



    	}



    	if (($request->isPost()) && ($request->get('address_id', 'post'))) {



	  		$session->set('shipping_address_id', $request->get('address_id', 'post'));



			$response->redirect($url->ssl('checkout_shipping'));



		}



		if (($request->isPost()) && ($this->validate())) {



			$sql = "insert into address set customer_id = '?', company = '?', firstname = '?', lastname = '?', address_1 = '?',postcode = '?', city = '?', zone_id = '?', country_id = '?'";



      		$database->query($database->parse($sql, $customer->getId(), $request->get('company', 'post'), $request->get('firstname', 'post'), $request->get('lastname', 'post'), $request->get('address_1', 'post'), $request->get('postcode', 'post'), $request->get('city', 'post'), $request->get('zone_id', 'post'), $request->get('country_id', 'post')));



	  		$session->set('shipping_address_id', $database->getLastId());



	  		if ($request->get('checkout_confirm')==1){



	  			$response->redirect('https://www.beststoreitems.com/index.php?controller=checkout_confirm');



	  		}else



	  		{



	  			$response->redirect($url->ssl('checkout_shipping'));



	  		}



	  		



    	}



		$template->set('content', $this->getForm());



		$template->set($module->fetchPublic());



    	$response->set($template->fetch('layout.tpl'));



  	} 



		



  	function payment() {



		



 		$cart     =& $this->locator->get('cart');



		$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');



		$template =& $this->locator->get('template');



		$url      =& $this->locator->get('url'); 



		$language->load('controller/checkout_address.php');



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



		if (!$customer->isLogged()) {  



      		$session->set('redirect', $url->ssl('checkout_shipping'));



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



    	} 



    	if (($request->isPost()) && ($request->get('address_id', 'post'))) {



	  		$session->set('payment_address_id', $request->get('address_id', 'post'));



			$response->redirect($url->ssl('checkout_payment'));



		} 



		if (($request->isPost()) && ($this->validate())) {



  	  		$sql = "insert into address set customer_id = '?', company = '?', firstname = '?', lastname = '?', address_1 = '?', postcode = '?', city = '?', zone_id = '?', country_id = '?'";



      		$database->query($database->parse($sql, $customer->getId(), $request->get('company', 'post'), $request->get('firstname', 'post'), $request->get('lastname', 'post'), $request->get('address_1', 'post'),$request->get('postcode', 'post'), $request->get('city', 'post'), $request->get('zone_id', 'post'), $request->get('country_id', 'post')));



	  		$session->set('payment_address_id', $database->getLastId()); 	  		



	  		$response->redirect('https://www.beststoreitems.com/index.php?controller=checkout_confirm');	



    	}



    	$template->set('content', $this->getForm());



		$template->set($module->fetchPublic());



    	$response->set($template->fetch('layout.tpl'));   



  	}  



 	



  	function getForm() {



		



		$address  =& $this->locator->get('address');



		$config   =& $this->locator->get('config');



		$customer =& $this->locator->get('customer');



		$database =& $this->locator->get('database');



		$language =& $this->locator->get('language');



		$request  =& $this->locator->get('request');



		$session  =& $this->locator->get('session');



		$url      =& $this->locator->get('url');



    	$view = $this->locator->create('template');



    	



    	if (strcasecmp($_REQUEST['action'],'shipping')==0)



    	{



    		$view->set('heading_title', 'Please choose a address below as your shipping address, or create a new shipping address via form below'); // $language->get('heading_title')



    		$view->set('text_entries', 'Please choose a address below as your shipping address, or create a new shipping address via form below'); // $language->get('text_entries')



    		$view->set('text_new_address','New Address' ); //$language->get('text_new_address')



    	}



    	if (strcasecmp($_REQUEST['action'],'payment')==0)



    	{



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



    		$view->set('text_entries', 'Please choose a address below as your billing address, or create a new billing address via form below'); // $language->get('text_entries')



    		$view->set('text_new_address','New Address' ); //$language->get('text_new_address')



    	}



    	   	 	



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



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



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



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



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



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



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



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



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



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



		$view->set('error_firstname', @$this->error['firstname']);



    	$view->set('error_lastname', @$this->error['lastname']);



    	$view->set('error_address_1', @$this->error['address_1']);



    	$view->set('error_city', @$this->error['city']);



		$view->set('error_company', @$this->error['company']);



		$view->set('error_postcode', @$this->error['postcode']);    	



		$view->set('action', $url->ssl('checkout_address', $request->get('action')));



		$view->set('save_action', $url->ssl('checkout_address', $request->get('action')));	



        $confirm_url="";



		if (strcasecmp($_SERVER['HTTP_REFERER'], 'https://www.beststoreitems.com/index.php?controller=checkout_confirm')==0) 



		{



			$view->set('action', $_SERVER['HTTP_REFERER']);		



			$confirm_url=$url->ssl('checkout_address', $request->get('action'), array('checkout_confirm' => 1));



			$view->set('save_action', $confirm_url);		



		}		



		if ($request->get('checkout_confirm')==1)



		{



			$view->set('action', 'https://www.beststoreitems.com/index.php?controller=checkout_confirm');		



			$confirm_url=$url->ssl('checkout_address', $request->get('action'), array('checkout_confirm' => 1));



			$view->set('save_action', $confirm_url);



		}		



$view->set('change_address_type', $request->get('action'));



		



$default_address_id=$session->get($request->get('action') . '_address_id');



$customer_row = $database->getRow("select address_id from customer where customer_id = '" . (int)$customer->getId(). "'");



if (empty($default_address_id)) {



	$default_address_id=$customer_row['address_id'];



}



$view->set('default', $default_address_id);



$account_address_url=$url->ssl('account_address');



//echo $account_address_url; exit;



    	$address_data = array();



    	$results = $database->getRows("select a.address_id,a.address_1,a.firstname,a.lastname,a.city, a.postcode, a.company, c.name as country, z.name as zone from address a left join country c on a.country_id = c.country_id left join zone z on a.zone_id = z.zone_id where a.customer_id = '" . (int)$customer->getId(). "'");



    	foreach ($results as $result) {



    		$update_url=$url->ssl('checkout_address', 'update', array('address_id' => $result['address_id'])).'&address_type='.$request->get('action');



    		if (!empty($confirm_url)){	$update_url.='&checkout_confirm=1'; }



    		$delete_url=$url->ssl('checkout_address', 'delete', array('address_id' => $result['address_id'])).'&address_type='.$request->get('action');



    		if (!empty($confirm_url)){	$delete_url.='&checkout_confirm=1'; }



      		$address_data[] = array(



        		'address_id' => $result['address_id'],



	    		'address'    => $result['firstname'] . ' ' . $result['lastname'] . ', ' . $result['address_1'] . ', ' . $result['city'] . ', ' . $result['zone'].' '.$result['postcode']  . ', ' . $result['country']. ', ' . $result['company'],				



				'update'     => $update_url,



				'delete'     => $delete_url



      		);



    	}



    	$view->set('addresses', $address_data);



    	



    	$view->set('firstname', $request->get('firstname', 'post'));



    	$view->set('lastname', $request->get('lastname', 'post'));



    	$view->set('company', $request->get('company', 'post'));



    	$view->set('address_1', $request->get('address_1', 'post'));



    	$view->set('city', $request->get('city', 'post'));



    	$view->set('postcode', $request->get('postcode', 'post'));



    	if ($request->has('country_id', 'post')) {



      		$view->set('country_id', $request->get('country_id', 'post'));



    	} else {



      		$view->set('country_id', $config->get('config_country_id'));



    	}



    	if ($request->has('zone_id', 'post')) {



      		$view->set('zone_id', $request->get('zone_id', 'post'));



    	} else {



      		$view->set('zone_id', $config->get('config_zone_id'));



    	}



    	$view->set('countries', $database->cache('country', "select * from country where allow_ship=1 order by name"));



    	$view->set('zones', $database->cache('zone', "select * from zone order by country_id, name"));



    	return $view->fetch('content/checkout_address.tpl'); 



  	}



  function setdefault() {



			



		$session  =& $this->locator->get('session');



		$customer =& $this->locator->get('customer');



		$database =& $this->locator->get('database');		



		$request  =& $this->locator->get('request');		



		$response =& $this->locator->get('response');		



		$template =& $this->locator->get('template');



		$url      =& $this->locator->get('url');		



//        $address_id=(int)$_POST['address_id'];



   



//     	$bResult=$database->query("update customer set address_id = '" .$address_id. "' where customer_id = '" . (int)$customer->getId() . "'"); 



//		echo $address_id."<br />".$bResult;



//		exit;



//	    $session->set('shipping_address_id', $address_id);



	  	$response->redirect($url->ssl('account_address'));    		



  	}



	function update() 



	{			



		$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');



		$template =& $this->locator->get('template');



		$url      =& $this->locator->get('url');			    



		



    	$language->load('controller/checkout_address.php');







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



		



    	if ( $request->isPost() ) {



      		$sql = "update address set company = '?', firstname = '?', lastname = '?', address_1 = '?', postcode = '?', city = '?', zone_id = '?', country_id = '?' where address_id  = '?' and customer_id = '?'";



      		$database->query($database->parse($sql, $request->get('company', 'post'), $request->get('firstname', 'post'), $request->get('lastname', 'post'), $request->get('address_1', 'post'), $request->get('postcode', 'post'), $request->get('city', 'post'), $request->get('zone_id', 'post'), $request->get('country_id', 'post'), $request->get('address_id'), $customer->getId()));







      		if ($request->has('default', 'post')) {



        		$database->query("update customer set address_id = '" . (int)$request->get('address_id') . "' where customer_id = '" . (int)$customer->getId() . "'");



      		}



       								



	  		$session->set('message', $language->get('text_update'));



	  		$refresh_url=$url->ssl('checkout_address');



			if ($request->get('address_type')){



	  			$refresh_url.='&action='.$request->get('address_type');



	  		}			



		



			if ($request->get('checkout_confirm')==1){



	  			$refresh_url.='&checkout_confirm=1';



	  		}	



	  		$response->redirect($refresh_url);



    	} 



	  	



		$template->set('content', $this->getEditForm());







      		$template->set($module->fetchAccount());	



    	$response->set($template->fetch('layout.tpl')); 



  	}



	



	function getEditForm() {	



	



		$config   =& $this->locator->get('config');



		$customer =& $this->locator->get('customer');



		$database =& $this->locator->get('database');



		$language =& $this->locator->get('language');



		$request  =& $this->locator->get('request');



		$session  =& $this->locator->get('session');



		$url      =& $this->locator->get('url');



			



    	$view = $this->locator->create('template');







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







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



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



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



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



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



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



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



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



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



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



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



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







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



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







    	$view->set('error_firstname', @$this->error['firstname']);



    	$view->set('error_lastname', @$this->error['lastname']);



    	$view->set('error_address_1', @$this->error['address_1']);



    	$view->set('error_city', @$this->error['city']);   	



    	



		$save_url=$url->ssl('checkout_address', $request->get('action'), array('address_id' => $request->get('address_id')));



		if ($request->get('address_type'))



		{



			$save_url.=	'&address_type='.$request->get('address_type');



		}



    	if ($request->get('checkout_confirm'))



		{



			$save_url.=	'&checkout_confirm=1';



		}



		



    	$view->set('action', $save_url);







    	if (($request->get('address_id')) && (!$request->isPost())) {



      		$address_info = $database->getRow("select distinct address_id,firstname,lastname,company,address_1,postcode,city,country_id,zone_id from address where address_id = '" . (int)$request->get('address_id') . "' and customer_id = '" . (int)$customer->getId() . "'");



		}



	



    	if ($request->has('firstname', 'post')) {



      		$view->set('firstname', $request->get('firstname', 'post'));



    	} else {



      		$view->set('firstname', @$address_info['firstname']);



    	}







    	if ($request->has('lastname', 'post')) {



      		$view->set('lastname', $request->get('lastname', 'post'));



    	} else {



      		$view->set('lastname', @$address_info['lastname']);



    	}







    	if ($request->has('company', 'post')) {



      		$view->set('company', $request->get('company', 'post'));



    	} else {



      		$view->set('company', @$address_info['company']);



    	}







    	if ($request->has('address_1', 'post')) {



      		$view->set('address_1', $request->get('address_1', 'post'));



    	} else {



      		$view->set('address_1', @$address_info['address_1']);



    	}   







    	if ($request->has('postcode', 'post')) {



      		$view->set('postcode', $request->get('postcode', 'post'));



    	} else {



      		$view->set('postcode', @$address_info['postcode']);



    	}







    	if ($request->has('city', 'post')) {



      		$view->set('city', $request->get('city', 'post'));



    	} else {



      		$view->set('city', @$address_info['city']);



    	}







    	if ($request->has('country_id', 'post')) {



      		$view->set('country_id', $request->get('country_id', 'post'));



    	}  elseif (isset($address_info['country_id'])) {



      		$view->set('country_id', $address_info['country_id']);



    	} else {



      		$view->set('country_id', $config->get('config_country_id'));



    	}







    	if ($request->has('zone_id', 'post')) {



      		$view->set('zone_id', $request->get('zone_id', 'post'));



    	}  elseif (isset($address_info['zone_id'])) {



      		$view->set('zone_id', $address_info['zone_id']);



    	} else {



      		$view->set('zone_id', $config->get('config_zone_id'));



    	}







    	$view->set('countries', $database->cache('country', "select country_id,name from country where allow_ship=1 order by name"));







    	$view->set('zones', $database->cache('zone', "select zone_id from zone order by country_id, name"));







    	if ($request->has('default', 'post')) {



      		$view->set('default', $request->get('default', 'post'));



    	} else {



      		$view->set('default', ($customer->getAddressId() == $request->get('address_id')));



    	}







 //    	$view->set('back', $url->ssl('account_address'));



		



    	return $view->fetch('content/account_address.tpl');



  	}



	



	



	function delete()



	{	



		echo "<script>alert('delete');</script>"; 



		$response =& $this->locator->get('response');



		$customer =& $this->locator->get('customer');



		$database =& $this->locator->get('database');



		$request  =& $this->locator->get('request');	



		$url      =& $this->locator->get('url');	



		$error=$this->validateDelete();



		



		if (!empty($error))



		{



			echo "<script>alert('$error');window.history.back(-1);</script>";



		}



		else



		{



			$database->query("delete from address where address_id = '" . (int)$request->get('address_id') . "' and customer_id = '" . (int)$customer->getId() . "'");



			$refresh_url=$url->ssl('checkout_address');



			if ($request->get('address_type')=="payment"){



	  			$refresh_url.='&action=payment';



	  		}else{



	  			$refresh_url.='&action=shipping';



	  		}



			



			if ($request->get('checkout_confirm')==1){



	  			$refresh_url.='&checkout_confirm=1';



	  		}	



echo "<script>alert('BACK=$refresh_url');</script>"; 



	  		$response->redirect($refresh_url);



		}



  	}



	



	 function validateDelete() {



		 



		$customer =& $this->locator->get('customer');



		$database =& $this->locator->get('database');



		$language =& $this->locator->get('language');



		$request  =& $this->locator->get('request');



		



    	$result = $database->getRow("select count(address_id) as total from address where customer_id = '" . (int)$customer->getId() . "'");



        



		$error_message="";



		



    	if ($result['total'] == 1) {



      		$error_message = 'Error: you must have at least one address!';



    	}







    	if ($customer->getAddressId() == $request->get('address_id')) {



 //     		$error_message = 'Error: you can not delete your default address!';



    	}







    	return $error_message;



  	}



	



  	function validate() {



		$language =& $this->locator->get('language');



		$request  =& $this->locator->get('request');



    	if ((strlen($request->get('firstname', 'post')) < 2) || (strlen($request->get('firstname', 'post')) > 32)) {



      		$this->error['firstname'] = $language->get('error_firstname');



    	}



    	if ((strlen($request->get('lastname', 'post')) < 2) || (strlen($request->get('lastname', 'post')) > 32)) {



      		$this->error['lastname'] = $language->get('error_lastname');



    	}



    	if ((strlen($request->get('address_1', 'post')) < 2) || (strlen($request->get('address_1', 'post')) > 64)) {



      		$this->error['address_1'] = $language->get('error_address_1');



    	}



    	if ((strlen($request->get('city', 'post')) < 2) || (strlen($request->get('city', 'post')) > 32)) {



      		$this->error['city'] = $language->get('error_city');



    	} 



	    if ((strlen($request->get('postcode', 'post')) < 2) || (strlen($request->get('postcode', 'post')) > 32)) {



      		$this->error['postcode'] = $language->get('error_postcode');



    	}



    	if ((strlen($request->get('company', 'post')) < 2) || (strlen($request->get('company', 'post')) > 32)) {



      		$this->error['company'] = $language->get('error_company');



    	}







		if (!$this->error) {



	  		return TRUE;



		} else {



	  		return FALSE;



		}  



  	}







	function zone() {



		



		$database =& $this->locator->get('database');



		$language =& $this->locator->get('language');



		$request  =& $this->locator->get('request');



		$response =& $this->locator->get('response');



    	$results = $database->cache('zone-' . (int)$request->get('country_id'), "select zone_id, name from zone where country_id = '" . (int)$request->get('country_id') . "' order by name");



    	$output = '<select name="zone_id">';  







		foreach ($results as $result) {



        	$output .= '<option value="' . $result['zone_id'] . '"';



	    	if ($request->get('zone_id') == $result['zone_id']) {



	      		$output .= ' SELECTED';



	    	}



	    	$output .= '>' . $result['name'] . '</option>';



      	}







		if (!$results) {



      		$output .= '<option value="0">' . $language->get('text_none') . '</option>';



    	}



    	$output .= '</select>';



		$response->set($output);



  	} 	



}



?>