1. Create a new template file named admin_order_details.tplin the presentation/templates folder, and add the following code to it:
{* admin_order_details.tpl *}
{load_admin_order_details assign="admin_order_details"}
<span class="admin_page_text">
Editing details for order ID:
{$admin_order_details->mOrderInfo.order_id} [ {strip}
<a href="{$admin_order_details->mAdminOrdersPageLink|prepare_link:"https"}">
back to admin orders...
</a>
{/strip}
]
</span>
<br /><br />
<form action="{"admin.php"|prepare_link:"https"}" method="get">
<input type="hidden" name="Page" value="OrderDetails" />
<input type="hidden" name="OrderId"
value="{$admin_order_details->mOrderInfo.order_id}" />
<table class="edit">
<tr>
<td class="admin_page_text">Total Amount: </td>
<td class="price">
${$admin_order_details->mOrderInfo.total_amount}
</td>
</tr>
<tr>
<td class="admin_page_text">Date Created: </td>
<td>
{$admin_order_details->mOrderInfo.created_on|date_format:"%Y-%m-%d %T"}
</td>
</tr>
<tr>
<td class="admin_page_text">Date Shipped: </td>
<td>
{$admin_order_details->mOrderInfo.shipped_on|date_format:"%Y-%m-%d %T"}
</td>
</tr>
<tr>
<td class="admin_page_text">Status: </td>
<td>
<select name="status"
{if ! $admin_order_details->mEditEnabled}
disabled="disabled"
{/if} >
{html_options options=$admin_order_details->mOrderStatusOptions selected=$admin_order_details->mOrderInfo.status}
</select>
</td>
</tr>
<tr>
<td class="admin_page_text">Comments: </td>
<td>
<input name="comments" type="text" size="50"
value="{$admin_order_details->mOrderInfo.comments}"
{if ! $admin_order_details->mEditEnabled}
disabled="disabled"
{/if} />
<td>
</tr>
<tr>
<td class="admin_page_text">Customer Name: </td>
<td>
<input name="customerName" type="text" size="50"
value="{$admin_order_details->mOrderInfo.customer_name}"
{if ! $admin_order_details->mEditEnabled}
disabled="disabled"
{/if} />
<td>
</tr>
<tr>
<td class="admin_page_text">Shipping Address: </td>
<td>
<input name="shippingAddress" type="text" size="50"
value="{$admin_order_details->mOrderInfo.shipping_address}"
{if ! $admin_order_details->mEditEnabled}
disabled="disabled"
{/if} />
</td>
</tr>
<tr>
<td class="admin_page_text">Customer Email: </td>
<td>
<input name="customerEmail" type="text" size="50"
value="{$admin_order_details->mOrderInfo.customer_email}"
{if ! $admin_order_details->mEditEnabled}
disabled="disabled"
{/if} />
</td>
</tr>
</table>
<br />
<input type="submit" name="submitEdit" value="Edit"
{if $admin_order_details->mEditEnabled}
disabled="disabled"
{/if} />
<input type="submit" name="submitUpdate" value="Update"
{if ! $admin_order_details->mEditEnabled}
disabled="disabled"
{/if} />
<input type="submit" name="submitCancel" value="Cancel"
{if ! $admin_order_details->mEditEnabled}
disabled="disabled"
{/if} />
<br /><br />
<span class="admin_page_text">Order contains these products:</span>
<br /><br />
<table>
<tr>
<th>Product ID</th>
<th>Product Name</th>
<th>Quantity</th>
<th>Unit Cost</th>
<th>Subtotal</th>
</tr>
{section name=cOrder loop=$admin_order_details->mOrderDetails}
<tr>
<td>{$admin_order_details->mOrderDetails[cOrder].product_id}</td>
<td>{$admin_order_details->mOrderDetails[cOrder].product_name}</td>
<td>{$admin_order_details->mOrderDetails[cOrder].quantity}</td>
<td>${$admin_order_details->mOrderDetails[cOrder].unit_cost}</td>
<td>${$admin_order_details->mOrderDetails[cOrder].subtotal}</td>
</tr>
{/section}
</table>
</form>
2. Create a new file named function.load_admin_order_details.phpin the presentation/smarty_pluginsfolder, and write the following code in it:
<?php
// Plugin functions inside plugin files must be named: smarty_type_name function smarty_function_load_admin_order_details($params, $smarty) {
// Create AdminOrderDetils object
$admin_order_details = new AdminOrderDetails();
$admin_order_details->init();
// Assign the template variable
$smarty->assign($params['assign'], $admin_order_details);
}
// Presentation tier class that deals with administering order details class AdminOrderDetails
{
// Public variables available in smarty template public $mOrderId;
public $mOrderInfo;
public $mOrderDetails;
public $mEditEnabled;
public $mOrderStatusOptions;
public $mAdminOrdersPageLink;
// Class constructor
public function __construct() {
// Get the back link from session
$this->mAdminOrdersPageLink = $_SESSION['admin_orders_page_link'];
// We receive the order ID in the query string if (isset ($_GET['OrderId']))
$this->mOrderId = (int) $_GET['OrderId'];
else
trigger_error('OrderId paramater is required');
$this->mOrderStatusOptions = Orders::$mOrderStatusOptions;
}
// Initializes class members public function init() {
if (isset ($_GET['submitUpdate'])) {
Orders::UpdateOrder($this->mOrderId, $_GET['status'],
$_GET['comments'], $_GET['customerName'], $_GET['shippingAddress'],
$_GET['customerEmail']);
}
$this->mOrderInfo = Orders::GetOrderInfo($this->mOrderId);
$this->mOrderDetails = Orders::GetOrderDetails($this->mOrderId);
// Value which specifies whether to enable or disable edit mode if (isset ($_GET['submitEdit']))
$this->mEditEnabled = true;
else
$this->mEditEnabled = false;
} }
?>
3. Open hatshop.css, and add the following styles:
.edit tr td {
background: #ffffff;
border: none;
}
4. Add some fictional orders to the database, and then load the admin.phpfile in your browser. Click on the ORDERS ADMINmenu link, click on a Go! button to show some orders, and click the View Details button for one of the orders. The order details admin page will show up allowing you to edit the order’s details, as advertised earlier in this chapter.
How It Works: The admin_order_details Componentized Template
The three files you just wrote,admin_order_details.tpl,function.load_admin_order_details.php, and admin_order_details.php, allow you to view and update the details of a particular order.
The function plugin is loaded from the template file using the usual mechanism. The constructor of the AdminOrderDetailsclass (the __constructmethod) ensures that there’s an OrderIdparameter in the query string because without it this componentized template doesn’t make sense:
// Class constructor
public function __construct() {
// Get the back link from session
$this->mAdminOrdersPageLink = $_SESSION['admin_orders_page_link'];
// We receive the order ID in the query string if (isset ($_GET['OrderId']))
$this->mOrderId = (int) $_GET['OrderId'];
else
trigger_error('OrderId paramater is required');
$this->mOrderStatusOptions = Orders::$mOrderStatusOptions;
}
The init()method reacts to user’s actions and calls various business tier methods to accomplish the user’s requests.
It populates the form with data it gets from the Orders::GetOrderInfoand the Orders::GetOrderDetailsbusiness tier methods.
The mEditEnabledclass member enters or exits edit mode depending on whether the submitEditparameter from the query string is set or not. When entering edit mode, all text boxes and the Update and Cancel buttons become enabled, but the Edit button is disabled. The reverse happens when exiting edit mode, which happens when either the Cancel or Update button is clicked.