Exercise: Implementing the Componentized Templates

Một phần của tài liệu Beginning PHP and Postgre SQL E-Commerce From Novice to Professional phần 7 ppt (Trang 32 - 56)

1. Create a new template file named customer_login.tplin the presentation/templatesfolder, and add the following code to it:

{* customer_login.tpl *}

{load_customer_login assign="customer_login"}

<div class="left_box" id="login_box">

<p>Login</p>

<form method="post"

action="{$customer_login->mCustomerLoginTarget|prepare_link:"https"}">

{if $customer_login->mLoginMessage}

<span class="error_text">

{$customer_login->mLoginMessage}

</span>

<br />

{/if}

<span>E-mail address:</span><br />

<input type="text" maxlength="50" name="email"

size="25" value="{$customer_login->mEmail}" /><br />

<span>Password:</span><br />

<input type="password" maxlength="50"

name="password" size="25" />

<br />

<input type="submit" name="Login" value="Login" />

<strong>(

{strip}

<a href="{$customer_login->mRegisterUser|prepare_link:"https"}">

Register user

</a>

{/strip} )

</strong>

</form>

</div>

2. Create a new plugin file named function.load_customer_login.phpin the presentation/smarty_pluginsfolder, and add the following to it:

<?php

/* Smarty plugin function that gets called when the

load_customer_login function plugin is loaded from a template */

function smarty_function_load_customer_login($params, $smarty) {

// Create CustomerLogin object

$customer_login = new CustomerLogin();

$customer_login->init();

// Assign template variable

$smarty->assign($params['assign'], $customer_login);

}

class CustomerLogin {

// Public stuff public $mLoginMessage;

public $mCustomerLoginTarget;

public $mRegisterUser;

public $mEmail = '';

// Private stuff

private $_mHaveData = 0;

// Class constructor

public function __construct() {

// Decide if we have submitted

if (isset ($_POST['Login']))

$this->_mHaveData = 1;

}

public function init() {

$url_base = substr(getenv('REQUEST_URI'),

strrpos(getenv('REQUEST_URI'), '/') + 1, strlen(getenv('REQUEST_URI')) - 1);

$url_parameter_prefix = (count($_GET) == 0 ? '?' : '&');

$this->mCustomerLoginTarget = $url_base;

if (strpos($url_base, 'RegisterCustomer', 0) === false)

$this->mRegisterUser = $url_base . $url_parameter_prefix . 'RegisterCustomer';

else

$this->mRegisterUser = $url_base;

if ($this->_mHaveData) {

// Get login status

$login_status = Customer::IsValid($_POST['email'], $_POST['password']);

switch ($login_status) {

case 2:

$this->mLoginMessage = 'Unrecognized Email.';

$this->mEmail = $_POST['email'];

break;

case 1:

$this->mLoginMessage = 'Unrecognized password.';

$this->mEmail = $_POST['email'];

break;

case 0:

// Valid login... build redirect link and redirect if (isset($_GET['Checkout']) && USE_SSL != 'no') {

$redirect_link = 'https://' . getenv('SERVER_NAME');

} else {

$redirect_link = 'http://' . getenv('SERVER_NAME');

// If HTTP_SERVER_PORT is defined and different than default if (defined('HTTP_SERVER_PORT') && HTTP_SERVER_PORT != '80') {

// Append server port

$redirect_link .= ':' . HTTP_SERVER_PORT;

} }

$redirect_link .= VIRTUAL_LOCATION . $this->mCustomerLoginTarget;

header('Location:' . $redirect_link);

exit;

} } } }

?>

3. Create a new template file named customer_logged.tplin the presentation/templatesfolder, and add the following code to it:

{* customer_logged.tpl *}

{load_customer_logged assign="customer_logged"}

<div class="left_box" id="login_box">

<p>Welcome, {$customer_logged->mCustomerName}</p>

<ol>

<li>

<a href="{$customer_logged->mUpdateAccount|prepare_link:"https"}">

&raquo; Change Account Details

</a>

</li>

<li>

<a href="{$customer_logged->mUpdateCreditCard|prepare_link:"https"}">

&raquo; {$customer_logged->mCreditCardAction} CC Details

</a>

</li>

<li>

<a href="{$customer_logged->mUpdateAddress|prepare_link:"https"}">

&raquo; {$customer_logged->mAddressAction} Address Details

</a>

</li>

<li>

<a href="{$customer_logged->mLogout|prepare_link}">

&raquo; Log Out

</a>

</li>

</ol>

</div>

4. Create a new plugin file named function.load_customer_logged.phpin the presentation/smarty_pluginsfolder, and add the following to it:

<?php

/* Smarty plugin function that gets called when the

load_customer_logged function plugin is loaded from a template */

function smarty_function_load_customer_logged($params, $smarty) {

// Create CustomerLogged object

$customer_logged = new CustomerLogged();

$customer_logged->init();

// Assign template variable

$smarty->assign($params['assign'], $customer_logged);

}

class CustomerLogged {

// Public attributes public $mCustomerName;

public $mCreditCardAction = 'Add';

public $mAddressAction = 'Add';

public $mUpdateAccount;

public $mUpdateCreditCard;

public $mUpdateAddress;

public $mLogout;

// Class constructor

public function __construct() {

}

public function init() {

$url_base = substr(getenv('REQUEST_URI'),

strrpos(getenv('REQUEST_URI'), '/') + 1, strlen(getenv('REQUEST_URI')) - 1);

$url_parameter_prefix = (count($_GET) == 1 ? '?' : '&');

if (isset($_GET['Logout']))

$url_base = str_replace($url_parameter_prefix . 'Logout', '',

$url_base);

elseif (isset($_GET['UpdateAccountDetails']))

$url_base = str_replace($url_parameter_prefix . 'UpdateAccountDetails', '', $url_base);

elseif (isset($_GET['UpdateCreditCardDetails']))

$url_base = str_replace($url_parameter_prefix .

'UpdateCreditCardDetails', '', $url_base);

elseif (isset($_GET['UpdateAddressDetails']))

$url_base = str_replace($url_parameter_prefix . 'UpdateAddressDetails', '', $url_base);

if (strpos($url_base, '?', 0) === false)

$url_parameter_prefix = '?';

else

$url_parameter_prefix = '&';

if (isset($_GET['Logout'])) {

Customer::Logout();

// Redirect

if (isset($_GET['Checkout']) && USE_SSL != 'no') {

$redirect_link = 'https://' . getenv('SERVER_NAME');

} else {

$redirect_link = 'http://' . getenv('SERVER_NAME');

// If HTTP_SERVER_PORT is defined and different than default if (defined('HTTP_SERVER_PORT') && HTTP_SERVER_PORT != '80') {

// Append server port

$redirect_link .= ':' . HTTP_SERVER_PORT;

} }

$redirect_link .= VIRTUAL_LOCATION . $url_base;

header('Location:' . $redirect_link);

exit;

}

$url_base .= $url_parameter_prefix;

$this->mUpdateAccount = $url_base . 'UpdateAccountDetails';

$this->mUpdateCreditCard = $url_base . 'UpdateCreditCardDetails';

$this->mUpdateAddress = $url_base . 'UpdateAddressDetails';

$this->mLogout = $url_base . 'Logout';

$customer_data = Customer::Get();

$this->mCustomerName = $customer_data['name'];

if (!(empty($customer_data['credit_card'])))

$this->mCreditCardAction = 'Change';

if (!(empty($customer_data['address_1'])))

$this->mAddressAction = 'Change';

} }

?>

5. Create a new template file named customer_details.tplin the presentation/templatesfolder, and add the following code to it:

{* customer_details.tpl *}

{load_customer_details assign="customer_details"}

<form method="post"

action="{$customer_details->mCustomerDetailsTarget|prepare_link:"https"}">

<span class="description">Please enter your details:</span>

{if $customer_details->mEmailAlreadyTaken}

<br /><br />

<span class="error_text">

A user with that e-mail address already exists.

</span>

{/if}

<br /><br />

<table class="form_table">

<tr>

<td>E-mail Address:</td>

<td>

<input type="text" name="email"

value="{$customer_details->mEmail}"

{if $customer_details->mEditMode}readonly="readonly"{/if} />

</td>

<td>

{if $customer_details->mEmailError}

<span class="error_text">

You must enter an e-mail address.

</span>

{/if}

</td>

</tr>

<tr>

<td>Name:</td>

<td>

<input type="text" name="name"

value="{$customer_details->mName}" />

</td>

<td>

{if $customer_details->mNameError}

<span class="error_text">You must enter your name.</span>

{/if}

</td>

</tr>

<tr>

<td>Password:</td>

<td><input type="password" name="password" /></td>

<td>

{if $customer_details->mPasswordError}

<span class="error_text">You must enter a password.</span>

{/if}

</td>

</tr>

<tr>

<td>Re-enter Password:</td>

<td><input type="password" name="passwordConfirm" /></td>

<td>

{if $customer_details->mPasswordConfirmError}

<span class="error_text">

You must re-enter your password.

</span>

{elseif $customer_details->mPasswordMatchError}

<span class="error_text">

You must re-enter the same password.

</span>

{/if}

</td>

</tr>

{if $customer_details->mEditMode}

<tr>

<td>Day phone:</td>

<td>

<input type="text" name="dayPhone"

value="{$customer_details->mDayPhone}" />

</td>

</tr>

<tr>

<td>Eve phone:</td>

<td>

<input type="text" name="evePhone"

value="{$customer_details->mEvePhone}" />

</td>

</tr>

<tr>

<td>Mob phone:</td>

<td>

<input type="text" name="mobPhone"

value="{$customer_details->mMobPhone}" />

</td>

</tr>

{/if}

</table>

<br />

<input type="submit" name="sended" value="Confirm" />

<input type="button" value="Cancel"

onclick="window.location='{

$customer_details->mReturnLink|prepare_link:$customer_details->➥ mReturnLinkProtocol}';" />

</form>

6. Create a new plugin file named function.load_customer_details.phpin the presentation/smarty_pluginsfolder, and add the following to it:

<?php

/* Smarty plugin function that gets called when the

load_customer_details function plugin is loaded from a template */

function smarty_function_load_customer_details($params, $smarty) {

// Create CustomerDetails object

$customer_details = new CustomerDetails();

$customer_details->init();

// Assign template variable

$smarty->assign($params['assign'], $customer_details);

}

class CustomerDetails {

// Public attributes public $mEditMode = 0;

public $mCustomerDetailsTarget;

public $mReturnLink;

public $mReturnLinkProtocol = 'http';

public $mEmail;

public $mName;

public $mPassword;

public $mDayPhone = null;

public $mEvePhone = null;

public $mMobPhone = null;

public $mNameError = 0;

public $mEmailError = 0;

public $mPasswordError = 0;

public $mPasswordConfirmError = 0;

public $mPasswordMatchError = 0;

public $mEmailAlreadyTaken = 0;

// Private attributes private $_mErrors = 0;

private $_mHaveData = 0;

// Class constructor

public function __construct() {

// Check if we have new user or editing existing customer details if (Customer::IsAuthenticated())

$this->mEditMode = 1;

$url_base = substr(getenv('REQUEST_URI'),

strrpos(getenv('REQUEST_URI'), '/') + 1, strlen(getenv('REQUEST_URI')) - 1);

$url_parameter_prefix = (count($_GET) == 1 ? '?' : '&');

$this->mCustomerDetailsTarget = $url_base;

if ($this->mEditMode == 0)

$this->mReturnLink = str_replace($url_parameter_prefix . 'RegisterCustomer', '', $url_base);

else

$this->mReturnLink = str_replace($url_parameter_prefix . 'UpdateAccountDetails', '', $url_base);

if (isset($_GET['Checkout']) && USE_SSL != 'no')

$this->mReturnLinkProtocol = 'https';

// Check if we have submitted data if (isset ($_POST['sended']))

$this->_mHaveData = 1;

if ($this->_mHaveData == 1) {

// Name cannot be empty if (empty ($_POST['name'])) {

$this->mNameError = 1;

$this->_mErrors++;

} else

$this->mName = $_POST['name'];

if ($this->mEditMode == 0 && empty ($_POST['email'])) {

$this->mEmailError = 1;

$this->_mErrors++;

} else

$this->mEmail = $_POST['email'];

// Password cannot be empty if (empty ($_POST['password'])) {

$this->mPasswordError = 1;

$this->_mErrors++;

} else

$this->mPassword = $_POST['password'];

// Password confirm cannot be empty if (empty ($_POST['passwordConfirm'])) {

$this->mPasswordConfirmError = 1;

$this->_mErrors++;

} else

$password_confirm = $_POST['passwordConfirm'];

// Password and password confirm should be the same if (!isset ($password_confirm) ||

$this->mPassword != $password_confirm) {

$this->mPasswordMatchError = 1;

$this->_mErrors++;

}

if ($this->mEditMode == 1) {

if (!empty ($_POST['dayPhone']))

$this->mDayPhone = $_POST['dayPhone'];

if (!empty ($_POST['evePhone']))

$this->mEvePhone = $_POST['evePhone'];

if (!empty ($_POST['mobPhone']))

$this->mMobPhone = $_POST['mobPhone'];

} } }

public function init()

{

// If we have submitted data and no errors in submitted data if (($this->_mHaveData == 1) && ($this->_mErrors == 0)) {

// Check if we have any customer with submitted email...

$customer_read = Customer::GetLoginInfo($this->mEmail);

/* ...if we have one and we are in 'new user' mode then email already taken error */

if ((!(empty ($customer_read['customer_id']))) &&

($this->mEditMode == 0)) {

$this->mEmailAlreadyTaken = 1;

return;

}

// We have a new user or we are updating an exisiting user details if ($this->mEditMode == 0)

Customer::Add($this->mName, $this->mEmail, $this->mPassword);

else

Customer::UpdateAccountDetails($this->mName, $this->mEmail,

$this->mPassword, $this->mDayPhone, $this->mEvePhone,

$this->mMobPhone);

// Redirect

if (isset($_GET['Checkout']) && USE_SSL != 'no') {

$redirect_link = 'https://' . getenv('SERVER_NAME');

} else {

$redirect_link = 'http://' . getenv('SERVER_NAME');

// If HTTP_SERVER_PORT is defined and different than default if (defined('HTTP_SERVER_PORT') && HTTP_SERVER_PORT != '80') {

// Append server port

$redirect_link .= ':' . HTTP_SERVER_PORT;

} }

$redirect_link .= VIRTUAL_LOCATION . $this->mReturnLink;

header('Location:' . $redirect_link);

exit;

}

if ($this->mEditMode == 1 && $this->_mHaveData == 0) {

// We are editing an existing customer’s details

$customer_data = Customer::Get();

$this->mName = $customer_data['name'];

$this->mEmail = $customer_data['email'];

$this->mDayPhone = $customer_data['day_phone'];

$this->mEvePhone = $customer_data['eve_phone'];

$this->mMobPhone = $customer_data['mob_phone'];

} } }

?>

7. Create a new template file named customer_address.tplin the presentation/templatesfolder, and add the following code to it:

{* customer_address.tpl *}

{load_customer_address assign="customer_address"}

<form method="post"

action="{$customer_address->mCustomerAddressTarget|prepare_link:"https"}">

<span class="description">Please enter your address details:</span>

<br /><br />

<table class="form_table">

<tr>

<td>Address 1:</td>

<td>

<input type="text" name="address1"

value="{$customer_address->mAddress1}" />

</td>

<td>

{if $customer_address->mAddress1Error}

<span class="error_text">You must enter an address.</span>

{/if}

</td>

</tr>

<tr>

<td>Address 2:</td>

<td>

<input type="text" name="address2"

value="{$customer_address->mAddress2}" />

</td>

</tr>

<tr>

<td>Town/City:</td>

<td>

<input type="text" name="city"

value="{$customer_address->mCity}" />

</td>

<td>

{if $customer_address->mCityError}

<span class="error_text">You must enter a city.</span>

{/if}

</td>

</tr>

<tr>

<td>Region/State:</td>

<td>

<input type="text" name="region"

value="{$customer_address->mRegion}" />

</td>

<td>

{if $customer_address->mRegionError}

<span class="error_text">You must enter a region/state.</span>

{/if}

</td>

</tr>

<tr>

<td>Postal Code/ZIP:</td>

<td>

<input type="text" name="postalCode"

value="{$customer_address->mPostalCode}" />

</td>

<td>

{if $customer_address->mPostalCodeError}

<span class="error_text">You must enter a postal code/ZIP.</span>

{/if}

</td>

</tr>

<tr>

<td>Country:</td>

<td>

<input type="text" name="country"

value="{$customer_address->mCountry}" />

</td>

<td>

{if $customer_address->mCountryError}

<span class="error_text">You must enter a country.</span>

{/if}

</td>

</tr>

<tr>

<td>Shipping region:</td>

<td>

<select name="shippingRegion">

{html_options options=$customer_address->mShippingRegions selected=$customer_address->mShippingRegion}

</select>

</td>

<td>

{if $customer_address->mShippingRegionError}

<span class="error_text">You must select a shipping region.</span>

{/if}

</td>

</tr>

</table>

<br />

<input type="submit" name="sended" value="Confirm" />

<input type="button" value="Cancel"

onclick="window.location='{

$customer_address->mReturnLink|prepare_link:$customer_address->➥ mReturnLinkProtocol}';" />

</form>

8. Create a new plugin file named function.load_customer_address.phpin the presentation/smarty_pluginsfolder, and add the following to it:

<?php

/* Smarty plugin function that gets called when the

load_customer_address function plugin is loaded from a template */

function smarty_function_load_customer_address($params, $smarty) {

// Create CustomerAddress object

$customer_address = new CustomerAddress();

$customer_address->init();

// Assign template variable

$smarty->assign($params['assign'], $customer_address);

}

class CustomerAddress {

// Public attributes

public $mCustomerAddressTarget;

public $mReturnLink;

public $mReturnLinkProtocol = 'http';

public $mAddress1 = '';

public $mAddress2 = '';

public $mCity = '';

public $mRegion = '';

public $mPostalCode = '';

public $mCountry = '';

public $mShippingRegion = '';

public $mShippingRegions = array ();

public $mAddress1Error = 0;

public $mCityError = 0;

public $mRegionError = 0;

public $mPostalCodeError = 0;

public $mCountryError = 0;

public $mShippingRegionError = 0;

// Private attributes private $_mErrors = 0;

private $_mHaveData = 0;

// Class constructor

public function __construct() {

$url_base = substr(getenv('REQUEST_URI'),

strrpos(getenv('REQUEST_URI'), '/') + 1, strlen(getenv('REQUEST_URI')) - 1);

$url_parameter_prefix = (count($_GET) == 1 ? '?' : '&');

// Set form action target

$this->mCustomerAddressTarget = $url_base;

// Set the return page

$this->mReturnLink = str_replace($url_parameter_prefix . 'UpdateAddressDetails', '', $url_base);

if (isset($_GET['Checkout']) && USE_SSL != 'no')

$this->mReturnLinkProtocol = 'https';

if (isset ($_POST['sended']))

$this->_mHaveData = 1;

if ($this->_mHaveData == 1) {

// Address 1 cannot be empty if (empty ($_POST['address1'])) {

$this->mAddress1Error = 1;

$this->_mErrors++;

} else

$this->mAddress1 = $_POST['address1'];

if (isset ($_POST['address2']))

$this->mAddress2 = $_POST['address2'];

if (empty ($_POST['city'])) {

$this->mCityError = 1;

$this->_mErrors++;

} else

$this->mCity = $_POST['city'];

if (empty ($_POST['region'])) {

$this->mRegionError = 1;

$this->_mErrors++;

} else

$this->mRegion = $_POST['region'];

if (empty ($_POST['postalCode'])) {

$this->mPostalCodeError = 1;

$this->_mErrors++;

} else

$this->mPostalCode = $_POST['postalCode'];

if (empty ($_POST['country'])) {

$this->mCountryError = 1;

$this->_mErrors++;

} else

$this->mCountry = $_POST['country'];

if ($_POST['shippingRegion'] == 1) {

$this->mShippingRegionError = 1;

$this->_mErrors++;

} else

$this->mShippingRegion = $_POST['shippingRegion'];

} }

public function init() {

$shipping_regions = Customer::GetShippingRegions();

foreach ($shipping_regions as $item)

$this->mShippingRegions[$item['shipping_region_id']] =

$item['shipping_region'];

if ($this->_mHaveData == 0) {

$customer_data = Customer::Get();

if (!(empty ($customer_data))) {

$this->mAddress1 = $customer_data['address_1'];

$this->mAddress2 = $customer_data['address_2'];

$this->mCity = $customer_data['city'];

$this->mRegion = $customer_data['region'];

$this->mPostalCode = $customer_data['postal_code'];

$this->mCountry = $customer_data['country'];

$this->mShippingRegion = $customer_data['shipping_region_id'];

} }

elseif ($this->_mErrors == 0) {

Customer::UpdateAddressDetails($this->mAddress1, $this->mAddress2,

$this->mCity, $this->mRegion, $this->mPostalCode,

$this->mCountry, $this->mShippingRegion);

if (isset($_GET['Checkout']) && USE_SSL != 'no') {

$redirect_link = 'https://' . getenv('SERVER_NAME');

} else {

$redirect_link = 'http://' . getenv('SERVER_NAME');

// If HTTP_SERVER_PORT is defined and different than default if (defined('HTTP_SERVER_PORT') && HTTP_SERVER_PORT != '80') {

// Append server port

$redirect_link .= ':' . HTTP_SERVER_PORT;

} }

$redirect_link .= VIRTUAL_LOCATION . $this->mReturnLink;

header('Location:' . $redirect_link);

exit;

} } }

?>

9. Create a new template file named customer_credit_card.tplin the presentation/templates folder, and add the following code to it:

{* customer_credit_card.tpl *}

{load_customer_credit_card assign="customer_credit_card"}

<form method="post"

action="{$customer_credit_card-

>mCustomerCreditCardTarget|prepare_link:"https"}">

<span class="description">

Please enter your credit card details:

</span>

<br /><br />

<table class="form_table">

<tr>

<td>Card Holder:</td>

<td>

<input type="text" name="cardHolder"

value="{$customer_credit_card->mPlainCreditCard.card_holder}" />

</td>

<td>

{if $customer_credit_card->mCardHolderError}

<span class="error_text">You must enter a card holder.</span>

{/if}

</td>

</tr>

<tr>

<td>Card Number (digits only):</td>

<td>

<input type="text" name="cardNumber"

value="{$customer_credit_card->mPlainCreditCard.card_number}" />

</td>

<td>

{if $customer_credit_card->mCardNumberError}

<span class="error_text">You must enter a card number.</span>

{/if}

</td>

</tr>

<tr>

<td>Expiry Date (MM/YY):</td>

<td>

<input type="text" name="expDate"

value="{$customer_credit_card->mPlainCreditCard.expiry_date}" />

</td>

<td>

{if $customer_credit_card->mExpDateError}

<span class="error_text">You must enter an expiry date</span>

{/if}

</td>

Một phần của tài liệu Beginning PHP and Postgre SQL E-Commerce From Novice to Professional phần 7 ppt (Trang 32 - 56)

Tải bản đầy đủ (PDF)

(63 trang)