Exercise: Creating the admin_orders Componentized Template

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

1. Create a new file named admin_orders.tplin the presentation/templatesfolder with the following code in it:

{* admin_orders.tpl *}

{load_admin_orders assign="admin_orders"}

{if $admin_orders->mErrorMessage neq ""}

<span class="admin_error_text">{$admin_orders->mErrorMessage}</span>

<br /><br />

{/if}

<form action="{"admin.php"|prepare_link:"https"}" method="get">

<input name="Page" type="hidden" value="Orders" />

<span class="admin_page_text">Show the most recent</span>

<input name="recordCount" type="text" value="{$admin_orders->mRecordCount}" />

<span class="admin_page_text">orders</span>

<input type="submit" name="submitMostRecent" value="Go!" />

<br /><br />

<span class="admin_page_text">Show all records created between</span>

<input name="startDate" type="text" value="{$admin_orders->mStartDate}" />

<span class="admin_page_text">and</span>

<input name="endDate" type="text" value="{$admin_orders->mEndDate}" />

<input type="submit" name="submitBetweenDates" value="Go!" />

<br /><br />

<span class="admin_page_text">Show orders by status</span>

{html_options name="status" options=$admin_orders->mOrderStatusOptions selected=$admin_orders->mSelectedStatus}

<input type="submit" name="submitOrdersByStatus" value="Go!" />

<br /><br />

</form>

<br />

{if $admin_orders->mOrders}

<table>

<tr>

<th>Order ID</th>

<th>Date Created</th>

<th>Date Shipped</th>

<th>Status</th>

<th>Customer</th>

<th>&nbsp;</th>

</tr>

{section name=cOrders loop=$admin_orders->mOrders}

{assign var=status value=$admin_orders->mOrders[cOrders].status}

<tr>

<td>{$admin_orders->mOrders[cOrders].order_id}</td>

<td>

{$admin_orders->mOrders[cOrders].created_on|date_format:"%Y-%m-%d %T"}

</td>

<td>

{$admin_orders->mOrders[cOrders].shipped_on|date_format:"%Y-%m-%d %T"}

</td>

<td>{$admin_orders->mOrderStatusOptions[$status]}</td>

<td>{$admin_orders->mOrders[cOrders].customer_name}</td>

<td align="right">

<input type="button" value="View Details"

onclick="window.location='{

$admin_orders->mOrders[cOrders].onclick|prepare_link:"https"}';" />

</td>

</tr>

{/section}

</table>

{/if}

2. Create a new file named presentation/smarty_plugins/function.load_admin_orders.php, and add the following code to it:

<?php

// Plugin functions inside plugin files must be named: smarty_type_name function smarty_function_load_admin_orders($params, $smarty)

{

// Create AdminOrders object

$admin_orders = new AdminOrders();

$admin_orders->init();

// Assign template variable

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

}

/* Presentation tier class that supports order administration functionality */

class AdminOrders {

// Public variables available in smarty template public $mOrders;

public $mStartDate;

public $mEndDate;

public $mRecordCount = 20;

public $mOrderStatusOptions;

public $mSelectedStatus = 0;

public $mErrorMessage = '';

// Class constructor

public function __construct() {

/* Save the link to the current page in the AdminOrdersPageLink session variable; it will be used to create the

"back to admin orders ..." link in admin order details pages */

$_SESSION['admin_orders_page_link'] =

str_replace(VIRTUAL_LOCATION, '', getenv('REQUEST_URI'));

$this->mOrderStatusOptions = Orders::$mOrderStatusOptions;

}

public function init() {

// If the "Show the most recent x orders" filter is in action ...

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

// If the record count value is not a valid integer, display error if ((string)(int)$_GET['recordCount'] == (string)$_GET['recordCount']) {

$this->mRecordCount = (int)$_GET['recordCount'];

$this->mOrders = Orders::GetMostRecentOrders($this->mRecordCount);

} else

$this->mErrorMessage = $_GET['recordCount'] . ' is not a number.';

}

/* If the "Show all records created between date_1 and date_2"

filter is in action ... */

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

$this->mStartDate = $_GET['startDate'];

$this->mEndDate = $_GET['endDate'];

// Check if the start date is in accepted format if (($this->mStartDate == '') ||

($timestamp = strtotime($this->mStartDate)) == -1)

$this->mErrorMessage = 'The start date is invalid. ';

else

// Transform date to YYYY/MM/DD HH:MM:SS format

$this->mStartDate =

strftime('%Y/%m/%d %H:%M:%S', strtotime($this->mStartDate));

// Check if the end date is in accepted format if (($this->mEndDate == '') ||

($timestamp = strtotime($this->mEndDate)) == -1)

$this->mErrorMessage .= 'The end date is invalid.';

else

// Transform date to YYYY/MM/DD HH:MM:SS format

$this->mEndDate =

strftime('%Y/%m/%d %H:%M:%S', strtotime($this->mEndDate));

// Check if start date is more recent than the end date if ((empty ($this->mErrorMessage)) &&

(strtotime($this->mStartDate) > strtotime($this->mEndDate)))

$this->mErrorMessage .=

'The start date should be more recent than the end date.';

// If there are no errors, get the orders between the two dates if (empty($this->mErrorMessage))

$this->mOrders = Orders::GetOrdersBetweenDates(

$this->mStartDate, $this->mEndDate);

}

// If "Show orders by status" filter is in action ...

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

$this->mSelectedStatus = $_GET['status'];

$this->mOrders = Orders::GetOrdersByStatus($this->mSelectedStatus);

}

// Build View Details link

for ($i = 0; $i < count($this->mOrders); $i++)

{

$this->mOrders[$i]['onclick'] =

'admin.php?Page=OrderDetails&OrderId=' .

$this->mOrders[$i]['order_id'];

} } }

?>

3. Load admin.phpinto the browser and introduce the username/password combination if you logged out.

Click on the ORDERS ADMINmenu link, then click one of the Go! buttons, and see the results that should be similar to those found earlier in Figure 9-4.

How It Works: The admin_orders Componentized Template

Each of the Go! buttons calls one of the business tier methods (in the Ordersclass) and populates the table with the returned orders information.

When processing the request, we test the data the visitor entered to make sure it’s valid. When the first Go! button is clicked, we verify that the entered value is a number (how many records to show). We also verify whether the dates entered in the Start Date and End Date text boxes are valid. We process them first with strtotimethat parses a string and transforms it into a Unix timestamp. This function is useful because it also accepts entries such as “now,” “tomorrow,” “last week,” and so on as input values. The resulting timestamp is then processed with the strftimefunction, which transforms it into the YYYY/MM/DD HH:MM:SSformat. Have a look at how these date/time values are parsed:

// Check if the start date is in accepted format if (($this->mStartDate == '') ||

($timestamp = strtotime($this->mStartDate)) == -1)

$this->mErrorMessage = 'The start date is invalid. ';

else

// Transform date to YYYY/MM/DD HH:MM:SS format

$this->mStartDate =

strftime('%Y/%m/%d %H:%M:%S', strtotime($this->mStartDate));

Note Check http://www.php.net/strtotimeto see what input formats are supported by the strtotimefunction and http://www.php.net/strftimefor more details about strftime.

Apart from this detail, the admin_orders.tpltemplate file is pretty simple and doesn’t introduce any new theoretical elements for you.

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

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

(63 trang)