From 5390e376127a6224db59303ff278a4b6f5e40717 Mon Sep 17 00:00:00 2001 From: Josh Sherman Date: Tue, 23 Sep 2008 02:21:55 +0000 Subject: [PATCH] Ditched the POS Session class in favor of just interacting with the session directly. Added more to the store / cart. git-svn-id: http://svn.cleancode.org/svn/pickles@56 4d10bc64-7434-11dc-a737-d2d0f8310089 --- classes/Config.php | 5 +- classes/Controller.php | 10 +- classes/DB.php | 5 +- classes/Session.php | 136 --------------------------- classes/Singleton.php | 6 +- models/store.php | 19 ++++ models/store/cart.php | 23 +++++ models/store/cart/add.php | 64 +++++++++++++ templates/store/cart.tpl | 35 +++++++ templates/store/featured_product.tpl | 7 +- templates/store/home.tpl | 4 +- templates/store/navigation.tpl | 6 +- 12 files changed, 165 insertions(+), 155 deletions(-) delete mode 100755 classes/Session.php create mode 100644 models/store/cart.php create mode 100644 models/store/cart/add.php create mode 100644 templates/store/cart.tpl diff --git a/classes/Config.php b/classes/Config.php index a475793..ec86587 100755 --- a/classes/Config.php +++ b/classes/Config.php @@ -41,11 +41,9 @@ class Config extends Singleton { * @return An instace of the Config class */ public static function getInstance() { - $session = Session::getInstance(); - $class = __CLASS__; - if (isset($session->$class)) { + if (isset($_SESSION['objects'][$class])) { self::$instance = Singleton::thaw($class); } else if (!self::$instance instanceof $class) { @@ -75,7 +73,6 @@ class Config extends Singleton { } } - $load = true; if ($load) { if (file_exists($file)) { $this->file = $file; diff --git a/classes/Controller.php b/classes/Controller.php index 25107da..774ce58 100755 --- a/classes/Controller.php +++ b/classes/Controller.php @@ -21,7 +21,6 @@ class Controller extends Object { */ private $model = null; private $viewer = null; - private $session = null; /* protected $config = null; @@ -41,8 +40,13 @@ class Controller extends Object { public function __construct($file = '../config.xml', $controller = 'Web') { parent::__construct(); - // Establish the session - $this->session = Session::getInstance(); + // Start the session if it's not started already + /** + * @todo Need to make the session not so mandatory. + */ + if (ini_get('session.auto_start') == 0) { + session_start(); + } // Load the config for the site passed in $this->config = Config::getInstance(); diff --git a/classes/DB.php b/classes/DB.php index 6f135f1..0384ad2 100755 --- a/classes/DB.php +++ b/classes/DB.php @@ -53,11 +53,9 @@ class DB extends Singleton { * @return object An instace of the DB class */ public static function getInstance() { - $session = Session::getInstance(); - $class = __CLASS__; - if (isset($session->$class)) { + if (isset($_SESSION['objects'][$class])) { self::$instance = Singleton::thaw($class); } else if (!self::$instance instanceof $class) { @@ -75,6 +73,7 @@ class DB extends Singleton { * * @return boolean Based on the success or failure of mysql_connect() * @todo Remove the error supressing @ from mysql_connect() + * @todo Currently the DBO is not being cached in the object, since it stores the password, it's a security issue. */ public function open() { if (!is_resource($this->connection)) { diff --git a/classes/Session.php b/classes/Session.php deleted file mode 100755 index ab3b048..0000000 --- a/classes/Session.php +++ /dev/null @@ -1,136 +0,0 @@ - - * @copyright 2007-2008 Joshua Sherman - * @todo Make the SQL less specific, right now you have to use a table - * named users, and use the email as the username. I will need to - * move this to the configuration and allow the user to specify which - * table to authenticate against, and what column names to use for the - * username and password. - */ -class Session extends Singleton { - - /** - * Private instance of the Session class - */ - private static $instance; - - /** - * Session ID - */ - public static $id = null; - - /** - * Private constructor - * - * Checks if session.auto_start is not enabled and if not, it will start the - * session and then grabs the session ID. - * - * @todo Need to look into whether or not sessions can be disabled - * indefinately. If so, this may need to be rethought. - */ - private function __construct() { - if (ini_get('session.auto_start') == 0) { - session_start(); - } - - $this->id = session_id(); - } - - /** - * Gets an instance of the Session class - */ - public static function getInstance() { - if (!self::$instance instanceof Session) { - self::$instance = new Session(); - } - - return self::$instance; - } - - /** - * Destroys the session - * - * Loops through all the $_SESSION variables and session_unregister()s them. - * After the unregistering session_destory() is called - */ - public function destroy() { - // foreach ($_SESSION as $variable => $value) - foreach (array_keys($_SESSION) as $variable) { - session_unregister($variable); - } - - session_destroy(); - } - - /** - * Clone function - * - * Triggers a PHP error as the Session class cannot be cloned because it - * extends the Singleton class. - * - * @todo The Config and Session (and any other Singleton classes) need to - * have this function. I think it can be added to the Singleton class - * directly, then the child classes will have it via inheritance. Wait - * just remembered that class inheritance with static classes is a bit - * screwy so every class may need the function directly. - */ - public function __clone() { - trigger_error('Clone is not allowed for ' . __CLASS__, E_USER_ERROR); - } - - /** - * Gets a session variable - * - * @param string $var Name of the variable to be returned - * @return Value of the requested variable - * @todo Returns null if the variable isn't set. Not sure if this is as - * drastic as returning false, we'll see. - */ - public function __get($var) { - if (!isset($_SESSION[$var])) { - $_SESSION[$var] = null; - } - - return $_SESSION[$var]; - } - - /** - * Sets a session variable - * - * @param string $var Name of the variable to set - * @param mixed $val Value to be assigned to the passed variable - * @return boolean The returned status of assigning the variable. - */ - function __set($var, $val) { - return ($_SESSION[$var] = $val); - } - - /** - * Checks if a session variable is set - * - * @param string $var Name of the variable to be checked - * @return boolean Whether or not the passed variable is set - */ - public function __isset($var) { - return isset($_SESSION[$var]) || isset($this->$var); - } - - /** - * Destructor - * - * Closes out the session. - */ - public function __destruct() { - session_write_close(); - } -} - -?> diff --git a/classes/Singleton.php b/classes/Singleton.php index 0ad1238..07d0a28 100644 --- a/classes/Singleton.php +++ b/classes/Singleton.php @@ -68,10 +68,9 @@ class Singleton { * @todo Needs to return the status for error tracking. */ public function freeze() { - $session = Session::getInstance(); $this->timestamp = time(); $class = get_class($this); - $session->$class = serialize($this); + $_SESSION['objects'][$class] = serialize($this); } /** @@ -88,8 +87,7 @@ class Singleton { public static function thaw($class) { __autoload($class); - $session = Session::getInstance(); - return unserialize($session->$class); + return unserialize($_SESSION['objects'][$class]); } } diff --git a/models/store.php b/models/store.php index 8d0b32f..4cd181c 100644 --- a/models/store.php +++ b/models/store.php @@ -5,6 +5,25 @@ class store extends Model { public function __construct() { parent::__construct(); + // Loads up the cart in case we need it + if (!isset($_SESSION['cart'])) { + $_SESSION['cart'] = array(); + $_SESSION['cart'] = array('count' => 0, 'products' => null); + } + else { + $count = 0; + + if (is_array($_SESSION['cart']['products'])) { + foreach ($_SESSION['cart']['products'] as $product_id => $product_info) { + $count += $product_info['quantity']; + } + } + + $_SESSION['cart']['count'] = $count; + } + + $this->data['cart'] = $_SESSION['cart']; + // Loads the navigation $config = Config::getInstance(); $this->data['subnav'] = $config->get('store', 'sections'); diff --git a/models/store/cart.php b/models/store/cart.php new file mode 100644 index 0000000..31042de --- /dev/null +++ b/models/store/cart.php @@ -0,0 +1,23 @@ + + * @copyright 2007-2008 Joshua Sherman + */ + +class store_cart extends store { + + public function __default() { + //session_destroy(); + } +} + +?> diff --git a/models/store/cart/add.php b/models/store/cart/add.php new file mode 100644 index 0000000..46025d3 --- /dev/null +++ b/models/store/cart/add.php @@ -0,0 +1,64 @@ + + * @copyright 2007-2008 Joshua Sherman + */ + +class store_cart_add 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 { + // References the product in the cart + $product =& $_SESSION['cart']['products'][$_REQUEST['id']]; + + // If the data is not set then grab it and set it + if (!isset($product['name'], $product['sku'], $product['price'])) { + $data = $this->db->getRow('SELECT name, sku, price FROM products WHERE id ="' . $_REQUEST['id'] . '";'); + + $product['name'] = $data['name']; + $product['sku'] = $data['sku']; + $product['price'] = $data['price']; + } + + // Increment the quantity and update the total + $product['quantity']++; + $product['total'] = number_format($product['price'] * $product['quantity'], 2); + unset($product); + + // 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(); + } + } + +} + +?> diff --git a/templates/store/cart.tpl b/templates/store/cart.tpl new file mode 100644 index 0000000..49c2b29 --- /dev/null +++ b/templates/store/cart.tpl @@ -0,0 +1,35 @@ +
+ {include file="../../pickles/templates/store/navigation.tpl"}

+ {include file="../../pickles/templates/store/categories.tpl"} +
+
+
+

Your Cart

+
+ {if is_array($cart.products)} + + + + + + + + + {foreach from=$cart.products item=product} + + + + + + + {/foreach} + + + +
Qty.SKUProduct DescriptionPriceTotal
{$product.sku}{$product.name}${$product.price}${$product.total}
+ +
+ {else} + You have no items in your shopping cart. + {/if} +
diff --git a/templates/store/featured_product.tpl b/templates/store/featured_product.tpl index d009da5..405bd6b 100644 --- a/templates/store/featured_product.tpl +++ b/templates/store/featured_product.tpl @@ -1,7 +1,10 @@ -