Page Templates:Creating a Address Book Management Template

From Whirlwind eCommerce Wiki
Jump to: navigation, search
Page Template Managers

Create address book template

Below is an example address book template:

// kick if logged out 
// recommended to place in 'pre process' code of template
// no reason to waste processing power if we just gonna kick to home.
authorizeIsLoggedIn("/index.php?message=You must be logged in to manage your address book");
 
// this function will actually post the form
$addressFormArr = eV::userAddressAddEdit('addressId,emailAddress,firstName/min:1:You must enter a first name,lastName/min:1:You must enter a last name,street1/min:1:You must enter a street address,street2,city/min:1:You must enter a city,state,state_alt,zipCode/min:1:You must enter a postal code,country,phone1/min:1:You must enter a phone number,company');
 
// shortcut the returned arrays
$addVals = $addressFormArr['values'];
$addMsg = $addressFormArr['message'];
 
// get addresses for display and for arrays to fill in fields on selection
$addressesArr = eV::userGetAddresses('addressId,emailAddress,firstName,lastName,street1,street2,city,state,state_alt,zipCode,country,phone1,company'); // returns multidimarray, first dim = address index, 2nd dim is keys for each field. can pass fieldList to limit fields returned
 
// quick and ez state and country options
$stateOptions = eV::addressStateOptions($addVals['state']); // returns string of option tags for states
$countryOptions= eV::addressCountryOptions($addVals['country']); // returns string of option tags for countries
 
 
// output page controlled content
echo "$title
<br/>$subTitle
<br/>$copy";
 
// global message
if(!empty($message)) echo $message;
 
// userAddressAddEdit error message
if(!empty($addMsg)) echo $addMsg;
 
 
// loop thru addresses and display
foreach($addressesArr AS $address){
	echo "<br>" . $address['firstName'] . " " . $address['lastName'];
    echo "<br>" . $address['street1'];
	echo "<br>" . $address['street2'];
	echo "<br>" . $address['city'] . " ";
	echo (!empty($address['state_alt'])) ? $address['state_alt'] : $address['state'];
	echo " " . $address['zipCode'];
    echo "<br>" . $address['country'];
 
	// edit function
	// using some handy javascript to populate the form without having to reload the page.
	// javascript functions at end of this script
    echo "<br><a href=\"javascript:editAddress(" . $address['addressId'] . ");\">Edit</a> "; 
    echo "<a href=\"javascript:deleteAddress(" . $address['addressId'] . ");\">Delete</a>";
 
// end looping thru addresses	  
};
 
// start address form
echo "<hr><form name=\"addressForm\" action=\"index.php\" method=\"post\">";
// use htmlspecial chars to make sure characters that could adversely effect the form field are converted (>"'<...etc)
echo "<br>Email Address: <input name=\"emailAddress\" maxlength=100 value=\"" . htmlspecialchars($addVals['emailAddress']) . "\" type=\"text\" />";
echo "<br>First Name: <input name=\"firstName\" value=\"" . htmlspecialchars($addVals['firstName']) . "\" type=\"text\" maxlength=50/>";
echo "<br>Last Name: <input name=\"lastName\" value=\"" . htmlspecialchars($addVals['lastName']) . "\" type=\"text\" maxlength=50/>";
echo "<br>Company: <input name=\"company\" value=\"" . htmlspecialchars($addVals['company']) . "\" type=\"text\" maxlength=50/>";
echo "<br>Street 1: <input name=\"street1\" value=\"" . htmlspecialchars($addVals['street1']) . "\" type=\"text\" maxlength=100/>";
echo "<br>Street 2: <input name=\"street2\" value=\"" . htmlspecialchars($addVals['street2']) . "\" type=\"text\" maxlength=100/>";
echo "<br>City: <input name=\"city\" value=\"" . htmlspecialchars($addVals['city']) . "\" type=\"text\" maxlength=50/>";
echo "<br>State: <select name=\"state\" >" . $stateOptions . "</select>";  
 
// state alt only necessary if using countried other than USA, to support other country states, provences etc.      	
echo "<br>State Alt: <input name=\"state_alt\" value=\"" . htmlspecialchars($addVals['state_alt']) . "\" type=\"text\" maxlength=50/>";
 
echo "<br>Zip Code: <input name=\"zipCode\" value=\"" . htmlspecialchars($addVals['zipCode']) . "\" type=\"text\" size=\"10\" maxlength=\"10\"/>";
echo "<br>Country: <select name=\"country\">" . $countryOptions; . "</select>";
echo "<br>Phone: <input name=\"phone1\" value=\"" . htmlspecialchars($addVals['phone1']) . "\" type=\"text\" />";
 
// reset javascript function - see end of form
echo "<br><a href=\"javascript:clearAddress();\">Reset</a>";
// submit button
echo "<br><input type=\"submit\" value=\"Post Address\">";
 
// required hidden fields
echo "<input type=\"hidden\" name=\"addressId\" value=\"" . htmlspecialchars($addVals['addressId']) . "\">
	<input type=\"hidden\" name=\"formAction\" value=\"form\">
	<input type=\"hidden\" name=\"pageId\" value=\"$pageId\">";
 
// exitLocation field tells system where to go after form is succesfully submitted
// usu just want to go back into the address management form
echo "<input type=\"hidden\" name=\"exitLocation\" value=\"index.php?pageId=$pageId&message=Changes to your address book have been posted\">";
 
// close out form
echo "</form>";
 
// javascript functions to:
// populate form to edit an address, delete address, reset form
echo " 
<script language=\"javascript\">
<!--
// set up arrays
// these arrays are used to populate the form when an address is selected to edit
addressArr = new Array();
";
 
foreach($addressesArr as $addressArr){
	echo "
	addressArr[" . $addressArr['addressId'] . "] = {";
	foreach($addressArr as $key => $value){
		echo "$key : unescape('" . rawurlencode($value) . "'),";
	};
	echo "dummy : 0};";
};
 
echo "
textFields = new Array('addressId','emailAddress','firstName','lastName','street1','street2','city','state_alt','zipCode','phone1','company');
selectFields = new Array('state','country');
formObj = window.document.addressForm;
 
function editAddress(addressId){
 
var addObj = addressArr[addressId];
	for(var i=0;i < textFields.length;i++) formObj[textFields[i]].value = addObj[textFields[i]];
	for(var i=0;i < selectFields.length;i++) {
		formObj[selectFields[i]].selectedIndex = 0;
		for(var ii=0;ii < formObj[selectFields[i]].options.length;ii++) if(formObj[selectFields[i]].options[ii].value == addObj[selectFields[i]]) formObj[selectFields[i]].selectedIndex = ii;
	};
	window.document.submitButton.src = '/files/KYSimages/global/account/account-savechanges.gif';
	var elem = document.getElementById('addressBoxTab');
	elem.innerHTML = '<a href=\"#\">Modifying Address</a>';
};
function deleteAddress(addressId){
	if(confirm('Are you sure you want to delete this address')) window.location.href='index.php?fa=USER.addressDelete&addressId=' + addressId +'&redirect=' + escape('index.php?pageId=130&message=Address has been deleted');
};
function clearAddress(){
	for(var i=0;i < textFields.length;i++) formObj[textFields[i]].value = '';
	for(var i=0;i < selectFields.length;i++) formObj[selectFields[i]].selectedIndex = 0;
	window.document.submitButton.src = '/files/KYSimages/global/account/account-createbutton.gif';
	var elem = document.getElementById('addressBoxTab');
	elem.innerHTML = '<a href=\"#\">Adding a New Address</a>';
};
// -->
</script>
";

Create address book management page

assign the new address book template to this page
check the 'hide this page from in-site searches'
post this page and take note of the page Id