Latest and greatest.

git-svn-id: http://svn.cleancode.org/svn/pickles@85 4d10bc64-7434-11dc-a737-d2d0f8310089
This commit is contained in:
Josh Sherman 2008-12-31 02:45:40 +00:00
parent 8fc09876cf
commit c6af785100
18 changed files with 451 additions and 46 deletions

View file

@ -94,7 +94,8 @@ class Controller extends Object {
}
// Grab the passed in module or use the default
$module_name = isset($_REQUEST['module']) ? strtr($_REQUEST['module'], '-', '_') : $config->getDefaultModule();
#$module_name = isset($_REQUEST['module']) ? strtr($_REQUEST['module'], '-', '_') : $config->getDefaultModule();
$module_name = isset($_REQUEST['module']) ? $_REQUEST['module'] : $config->getDefaultModule();
/**
* @todo Maybe the logout shouldn't be an internal thing, what if the

View file

@ -57,7 +57,7 @@ abstract class Display_Common extends Object {
ini_set('arg_separator.output', '&');
ini_set('url_rewriter.tags', 'a=href,area=href,frame=src,input=src,fieldset=');
header('Content-type: text/html; charset=UTF-8');
//header('Content-type: text/html; charset=UTF-8');
if ($this->config->getDebug() === true) {
?>

View file

@ -54,7 +54,7 @@ class Display_PHP extends Display_Common {
//$this->smarty->cache_lifetime = $this->caching;
}
}
$this->template = $this->template_path . $this->module_name . '.php';
$this->shared_template = PICKLES_PATH . 'templates/' . $this->shared_name . '.php';
}

View file

@ -102,7 +102,7 @@ class Display_Smarty extends Display_Common {
}
$this->template = $template;
$cache_id = isset($this->cache_id) ? $this->cache_id : $this->module_filename;
$template = $this->smarty->template_exists('index.tpl') ? 'index.tpl' : $this->template;
@ -116,7 +116,7 @@ class Display_Smarty extends Display_Common {
// Only assign the template if it's not the index, this avoids an infinite loop.
if ($this->template != 'index.tpl') {
$this->smarty->assign('template', $this->template);
$this->smarty->assign('template', strtr($this->template, '-', '_'));
}
// Loads the data from the config

View file

@ -9,7 +9,7 @@ class store extends Module {
parent::__construct($config, $db, $mailer, $error);
// Loads up the cart in case we need it
if (!isset($_SESSION['cart'])) {
if (!isset($_SESSION['cart'], $_SESSION['cart']['count'], $_SESSION['cart']['products'])) {
$_SESSION['cart'] = array();
$_SESSION['cart'] = array('count' => 0, 'products' => null);
}

View file

@ -18,7 +18,75 @@ class store_cart extends store {
protected $display = DISPLAY_SMARTY;
public function __default() {
$this->cart = $_SESSION['cart'];
if (isset($_SESSION['cart'])) {
$this->cart = $_SESSION['cart'];
}
$discounts = null;
if (isset($_SESSION['cart']['discounts']) && is_array($_SESSION['cart']['discounts'])) {
foreach ($_SESSION['cart']['discounts'] as $code => $discount) {
if (isset($discount['rules']) && is_array($discount['rules'])) {
foreach ($discount['rules'] as $rule) {
switch ($rule['applied_to']) {
case 'ORDER':
// Takes the discount from the subtotal
break;
case 'PRODUCT':
// Takes the discount from the product
if (isset($discount['xref']) && is_array($discount['xref'])) {
foreach ($discount['xref'] as $xref) {
switch ($xref['type']) {
case 'CATEGORY':
break;
case 'CUSTOMER':
break;
case 'PRODUCT':
// Checks if the product referenced is in the cart
if (array_key_exists($xref['xref_id'], $_SESSION['cart']['products'])) {
$quantity = $_SESSION['cart']['products'][$xref['xref_id']]['quantity'];
$total = $_SESSION['cart']['products'][$xref['xref_id']]['total'];
$price = $_SESSION['cart']['products'][$xref['xref_id']]['price'];
switch ($rule['amount_type']) {
case 'FLAT':
break;
case 'PERCENT':
$discounts[$xref['xref_id']]['price'] = round($price * ($rule['amount'] * 0.01), 2);
$discounts[$xref['xref_id']]['total'] = $discounts[$xref['xref_id']]['price'] * $quantity;
break;
}
var_dump($discounts);
}
break;
}
}
}
break;
case 'SHIPPING':
// Takes the discount from the shipping
break;
}
}
}
}
}
$this->discounts = $discounts;
//var_dump($_SESSION['cart']);
//var_dump($_SESSION['cart']['discounts']['inPink']);
}
}

View file

@ -13,14 +13,12 @@
class store_cart_add extends store {
/**
* @todo Add handling for an invalid product
*/
public function __default() {
var_dump($_SESSION);
if ($this->db->getField('SELECT COUNT(id) FROM products WHERE id = "' . $_REQUEST['id'] . '";') != 1) {
/**
* @todo Add handling for an invalid product
*/
}
else {
// References the product in the cart
@ -37,7 +35,7 @@ class store_cart_add extends store {
// Increment the quantity and update the total
$product['quantity']++;
$product['total'] = number_format($product['price'] * $product['quantity'], 2);
$product['total'] = round($product['price'] * $product['quantity'], 2);
unset($product);
// References the cart as a whole
@ -52,7 +50,7 @@ class store_cart_add extends store {
}
// Set the subtotal in the cart
$cart['subtotal'] = $subtotal;
$cart['subtotal'] = round($subtotal, 2);
unset($cart);
// Redirect to the cart
@ -60,7 +58,6 @@ class store_cart_add extends store {
exit();
}
}
}
?>

View file

@ -0,0 +1,113 @@
<?php
/**
* Applies a discount to the cart
*
* @package PICKLES
* @subpackage store
* @author Joshua Sherman <josh@phpwithpickles.org>
* @copyright 2007-2008 Joshua Sherman
*/
class store_cart_discount_apply extends store {
public function __default() {
$error = false;
$coupon = $_POST['coupon'];
try {
if (trim($coupon) == '') {
throw new Exception('is blank');
}
// Check if the discount is already in the cart
if (in_array($coupon, $_SESSION['cart']['discounts'])) {
throw new Exception('has already been applied');
}
// Pulls the discount from the database (null if it's not there)
$discount = $this->db->getRow("
SELECT
id,
name,
all_products,
all_categories,
all_customers,
combinable,
valid_from,
valid_through,
max_customer_usage,
max_order_usage,
usage_count,
remaining_usages,
disabled
FROM
discounts
WHERE
coupon = '{$coupon}';
");
if ($discount == null) {
throw new Exception('was not found.');
}
// Check combinability against if another discount is in the session
// Checks if the discount is being used during the right time
if (isset($valid_from) || isset($valid_through)) {
$today = date('Y-m-d');
if ($today < $valid_from) {
throw new Exception('is associated with a promotion that has not yet started.');
}
else if ($today > $valid_through) {
throw new Exception('has expired.');
}
}
// @todo
// check if the customer already used the coupon on a previous order
// check if the customer already used the coupon this order
// Checks if the discount still has remaining usages
if ($discount['remaining_usages'] <= 0) {
throw new Exception('has no more remaining usages');
}
// Checks if the discount is disabled
if ($discount['disabled'] == 'Y') {
throw new Exception('is currently disabled');
}
// Pulls any associated discount rules
$discount['rules'] = $this->db->getArray("
SELECT applied_to, amount, amount_type, min_subtotal, min_items, max_discount
FROM discount_rules
WHERE discount_id = '{$discount['id']}';
");
// Pulls any associated discount cross-references
$discount['xref'] = $this->db->getArray("
SELECT type, xref_id, eligible, exclusion
FROM discount_xref
WHERE discount_id = '{$discount['id']}';
");
// Adds the discount to the session
// Calculations aren't done here, they are done on the cart view module
$_SESSION['cart']['discounts'][$coupon] = $discount;
}
catch (Exception $e) {
// @todo Get the error message to the user.
var_dump('The specified discount code ' . $e->getMessage());
}
// Redirect to the cart
header('Location: /store/cart');
exit();
}
}
?>

View file

@ -0,0 +1,30 @@
<?php
/**
* Empties the cart
*
* Removes all the items from the cart completely
*
* @package PICKLES
* @subpackage store
* @author Joshua Sherman <josh@phpwithpickles.org>
* @copyright 2008 Joshua Sherman
*/
class store_cart_empty extends store {
public function __default() {
// Unsets the products array if products are there.
if (isset($_SESSION['cart'])) {
unset($_SESSION['cart']);
}
// Redirect to the cart
header('Location: /store/cart');
exit();
}
}
?>

View file

@ -0,0 +1,55 @@
<?php
/**
* Removes an item from the cart
*
* Removes the passed item from the cart completely
*
* @package PICKLES
* @subpackage store
* @author Joshua Sherman <josh@phpwithpickles.org>
* @copyright 2008 Joshua Sherman
*/
class store_cart_remove extends store {
/**
* @todo Add handling for an invalid product
*/
public function __default() {
if ($this->db->getField('SELECT COUNT(id) FROM products WHERE id = "' . $_REQUEST['id'] . '";') != 1) {
}
else {
// Unsets the product from the cart
unset($_SESSION['cart']['products'][$_REQUEST['id']]);
if (count($_SESSION['cart']['products']) == 0) {
unset($_SESSION['cart']['products']);
}
// References the cart as a whole
$cart =& $_SESSION['cart'];
$subtotal = 0;
// Loops through the products and totals them up
if (is_array($cart['products'])) {
foreach ($cart['products'] as $product) {
$subtotal += $product['total'];
}
}
// Set the subtotal in the cart
$cart['subtotal'] = $subtotal;
unset($cart);
// Redirect to the cart
header('Location: /store/cart');
exit();
}
}
}
?>

View file

@ -0,0 +1,68 @@
<?php
/**
* Updates the cart
*
* Updates the submitted data for the cart
*
* @package PICKLES
* @subpackage store
* @author Joshua Sherman <josh@phpwithpickles.org>
* @copyright 2008 Joshua Sherman
*/
class store_cart_update extends store {
/**
* @todo Add handling for an invalid product
*/
public function __default() {
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['quantity']) && is_array($_POST['quantity'])) {
// Updates the quantities
foreach ($_POST['quantity'] as $id => $quantity) {
// References the product in the cart
$product = $_SESSION['cart']['products'][$id];
if ($quantity == 0) {
unset($_SESSION['cart']['products'][$id]);
}
else {
$product['quantity'] = $quantity;
$product['total'] = round($product['price'] * $product['quantity'], 2);
$_SESSION['cart']['products'][$id] = $product;
}
}
if (count($_SESSION['cart']['products']) == 0) {
unset($_SESSION['cart']['products']);
}
// References the cart as a whole
$cart =& $_SESSION['cart'];
$subtotal = 0;
// Loops through the products and totals them up
if (is_array($cart['products'])) {
foreach ($cart['products'] as $product) {
$subtotal += $product['total'];
}
}
// Set the subtotal in the cart
$cart['subtotal'] = round($subtotal, 2);
unset($cart);
}
}
// Redirect to the cart
header('Location: /store/cart');
exit();
}
}
?>

View file

@ -5,13 +5,21 @@ class store_category extends store {
protected $display = DISPLAY_SMARTY;
public function __default() {
$category = $this->db->getRow('
$category = $this->db->getRow("
SELECT id, name, permalink, description
FROM categories
WHERE permalink = "' . $_REQUEST['permalink'] . '";
');
WHERE permalink = '{$_REQUEST['permalink']}';
");
$this->category = $category;
$this->products = $this->db->getArray("
SELECT p.*
FROM products AS p
INNER JOIN category_xref as c
ON p.id = c.product_id
WHERE c.category_id = '{$category['id']}';
");
}
}

View file

@ -9,14 +9,15 @@ class store_home extends store {
}
public function __default() {
$this->featured = $this->db->getRow('SELECT id, name, teaser FROM products WHERE featured = "Y" AND id = 30 ORDER BY RAND() LIMIT 1;');
$featured = $this->db->getRow('SELECT id, name, teaser FROM products WHERE featured = "Y" AND id = 30 ORDER BY RAND() LIMIT 1;');
foreach (array('gif', 'jpg', 'png') as $extension) {
if (file_exists(getcwd() . '/images/products/' . $this->featured['id'] . '/medium.' . $extension)) {
$this->featured['image'] = $extension;
if (file_exists(getcwd() . '/images/products/' . $featured['id'] . '/medium.' . $extension)) {
$featured['image'] = $extension;
}
}
$this->featured = $featured;
$this->top_sellers = $this->db->getArray('SELECT id, name FROM products ORDER BY RAND() LIMIT 10;');
}
}

View file

@ -6,30 +6,73 @@
<div class="your-cart">
<h1>Your Cart</h1>
</div>
{if is_array($cart.products)}
<table class="product-list">
<tr>
<th class="product-quantity">Qty.</th>
<th class="product-sku">SKU</th>
<th class="product-description">Product Description</th>
<th class="product-price">Price</th>
<th class="product-total">Total</th>
</tr>
{foreach from=$cart.products item=product}
{if is_array($cart.products) && count($cart.products) > 0}
<form method="POST" action="/store/cart/" name="cart">
<table class="product-list">
<tr>
<td class="product-quantity"><input type="text" class="product-quantity" value="{$product.quantity}" /></td>
<td class="product-sku">{$product.sku}</td>
<td class="product-description">{$product.name}</td>
<td class="product-price">${$product.price}</td>
<td class="product-total">${$product.total}</td>
<th class="product-quantity">Qty.</th>
<th class="product-sku">SKU</th>
<th class="product-description">Product Description</th>
<th class="product-price">Price</th>
<th class="product-total">Total</th>
</tr>
{/foreach}
<tr>
<td colspan="5">
</td>
</tr>
</table>
{foreach from=$cart.products key=id item=product}
<tr>
<td class="product-quantity">
<input type="text" class="product-quantity" value="{$product.quantity}" name="quantity[{$id}]" /><br />
<span style="font-size: 7pt"><a href="/store/cart/remove/{$id}">Remove</a></span>
</td>
<td class="product-sku">{$product.sku}</td>
<td class="product-description">{$product.name}</td>
<td class="product-price">
${$product.price|number_format:2}
{if is_array($discounts) && array_key_exists($id, $discounts)}
<div style="color: #090">
-${$discounts.$id.price|number_format:2}
</div>
{/if}
</td>
<td class="product-total">
${$product.total|number_format:2}
{if is_array($discounts) && array_key_exists($id, $discounts)}
<div style="color: #090">
-${$discounts.$id.total|number_format:2}
</div>
{/if}
</td>
</tr>
{/foreach}
<tr>
<td colspan="3">
<br />
<b>Discount Code:</b> <input type="text" size="12" name="coupon" /> <input type="submit" value="Apply" onclick="document.cart.action += 'discount/apply';" />
</td>
<td class="right">
<b>Subtotal:</b><br />
{if $cart.discount}{/if}
<b>Shipping:</b><br />
<b>Total:</b>
</td>
<td class="right">
${$cart.subtotal|number_format:2}<br />
$4.99<br />
${$cart.subtotal+4.99|number_format:2}
</td>
</tr>
<tr>
<td colspan="3">
<br />
<input type="submit" value="Update Cart" onclick="document.cart.action += 'update';" />
<input type="submit" value="Empty Cart" onclick="document.cart.action += 'empty'" />
<input type="button" value="Continue Shopping" onclick="location.href='/store';" />
</td>
<td colspan="2" class="right">
<br />
<input type="button" value="Checkout" onclick="location.href='/store/cart/checkout';" />
</td>
</tr>
</table>
</form>
{else}
You have no items in your shopping cart.
{/if}

View file

@ -10,6 +10,21 @@
{$category.description}
</div>
<div class="breadcrumbs">
<a href="/store">Store Home</a> > <a href="/store/category/{$category.permalink}">{$category.name}</a>
<a href="/store">Shopping Home</a> &gt; <a href="/store/category/{$category.permalink}">{$category.name}</a>
</div>
<div>
{foreach from=$products item=product name=products}
<div class="float-left" style="width: 200px; margin: 3px">
<img src="/images/products/{$product.id}/small.jpg" class="float-left" style="padding-right: 5px" />
<div class="float-left" style="width: 120px">
<a href="/store/product/{$product.id}">{$product.name}</a><br /><br />
{$product.teaser}<br /><br />
<b>${$product.price}</b><br /><br />
<ul><li><a href="/store/cart/add/{$product.id}" class="add-to-cart"><span>Add to Cart</span></a></li></ul>
<br /><br />
</div>
</div>
{if $smarty.foreach.products.iteration % 3 == 0}<br class="clear" />{/if}
{/foreach}
</div>
</div>

View file

@ -1,4 +1,4 @@
<img src="/images/products/{$featured.id}/medium.{$featured.image}" class="float-right" style="padding-right: 20px; height: 155px;" />
<img src="/images/products/{$featured.id}/featured.jpg" class="float-right" style="padding-right: 20px" />
<div class="store-featured-product">
<h1>Featured Product</h1>
<h2>{$featured.name}</h2><br />

View file

@ -2,7 +2,7 @@
<ul class="subnav">
{foreach from=$config.store key=link item=label}
<li {if $module.0 == 'store/'|cat:$link}class="selected"{/if}>
<a href="/{$section}/{$link}" class="{$link}">
<a href="/{$module.1}/{$link}" class="{$link}">
{$label}{if $link == 'cart' && $cart.count != 0} ({$cart.count}){/if}
</a>
</li>

View file

@ -10,4 +10,10 @@
<a href="/store/product/{$top_sellers[product].id}"><b>{$smarty.section.product.index+1}.</b> {$top_sellers[product].name}</a><br />
{/section}
</div>
<!-- Single Column -->
<!--div>
{section name=product loop=$top_sellers start=0 loop=10 step=1}
<a href="/store/product/{$top_sellers[product].id}"><b>{$smarty.section.product.index+1}.</b> {$top_sellers[product].name}</a><br />
{/section}
</div-->
</div>