summaryrefslogtreecommitdiffstats
path: root/system/libraries/Cart.php
diff options
context:
space:
mode:
Diffstat (limited to 'system/libraries/Cart.php')
-rwxr-xr-x[-rw-r--r--]system/libraries/Cart.php146
1 files changed, 73 insertions, 73 deletions
diff --git a/system/libraries/Cart.php b/system/libraries/Cart.php
index 643d2eca4..7f65b48b9 100644..100755
--- a/system/libraries/Cart.php
+++ b/system/libraries/Cart.php
@@ -2,11 +2,11 @@
/**
* CodeIgniter
*
- * An open source application development framework for PHP 4.3.2 or newer
+ * An open source application development framework for PHP 5.1.6 or newer
*
* @package CodeIgniter
* @author ExpressionEngine Dev Team
- * @copyright Copyright (c) 2006 - 2010, EllisLab, Inc.
+ * @copyright Copyright (c) 2006 - 2011, EllisLab, Inc.
* @license http://codeigniter.com/user_guide/license.html
* @link http://codeigniter.com
* @since Version 1.0
@@ -29,7 +29,7 @@ class CI_Cart {
// These are the regular expression rules that we use to validate the product ID and product name
var $product_id_rules = '\.a-z0-9_-'; // alpha-numeric, dashes, underscores, or periods
var $product_name_rules = '\.\:\-_ a-z0-9'; // alpha-numeric, dashes, underscores, colons or periods
-
+
// Private variables. Do not change!
var $CI;
var $_cart_contents = array();
@@ -39,12 +39,12 @@ class CI_Cart {
* Shopping Class Constructor
*
* The constructor loads the Session class, used to store the shopping cart contents.
- */
- function CI_Cart($params = array())
- {
+ */
+ public function __construct($params = array())
+ {
// Set the super object to a local variable for use later
$this->CI =& get_instance();
-
+
// Are any config settings being passed manually? If so, set them
$config = array();
if (count($params) > 0)
@@ -54,10 +54,10 @@ class CI_Cart {
$config[$key] = $val;
}
}
-
+
// Load the Sessions class
$this->CI->load->library('session', $config);
-
+
// Grab the shopping cart array from the session table, if it exists
if ($this->CI->session->userdata('cart_contents') !== FALSE)
{
@@ -66,15 +66,15 @@ class CI_Cart {
else
{
// No cart exists so we'll set some base values
- $this->_cart_contents['cart_total'] = 0;
- $this->_cart_contents['total_items'] = 0;
+ $this->_cart_contents['cart_total'] = 0;
+ $this->_cart_contents['total_items'] = 0;
}
-
+
log_message('debug', "Cart Class Initialized");
}
// --------------------------------------------------------------------
-
+
/**
* Insert items into the cart and save it to the session table
*
@@ -90,15 +90,15 @@ class CI_Cart {
log_message('error', 'The insert method must be passed an array containing data.');
return FALSE;
}
-
- // You can either insert a single product using a one-dimensional array,
+
+ // You can either insert a single product using a one-dimensional array,
// or multiple products using a multi-dimensional one. The way we
// determine the array type is by looking for a required array key named "id"
// at the top level. If it's not found, we will assume it's a multi-dimensional array.
-
- $save_cart = FALSE;
+
+ $save_cart = FALSE;
if (isset($items['id']))
- {
+ {
if ($this->_insert($items) == TRUE)
{
$save_cart = TRUE;
@@ -114,7 +114,7 @@ class CI_Cart {
{
$save_cart = TRUE;
}
- }
+ }
}
}
@@ -129,7 +129,7 @@ class CI_Cart {
}
// --------------------------------------------------------------------
-
+
/**
* Insert
*
@@ -145,9 +145,9 @@ class CI_Cart {
log_message('error', 'The insert method must be passed an array containing data.');
return FALSE;
}
-
+
// --------------------------------------------------------------------
-
+
// Does the $items array contain an id, quantity, price, and name? These are required
if ( ! isset($items['id']) OR ! isset($items['qty']) OR ! isset($items['price']) OR ! isset($items['name']))
{
@@ -156,7 +156,7 @@ class CI_Cart {
}
// --------------------------------------------------------------------
-
+
// Prep the quantity. It can only be a number. Duh...
$items['qty'] = trim(preg_replace('/([^0-9])/i', '', $items['qty']));
// Trim any leading zeros
@@ -167,9 +167,9 @@ class CI_Cart {
{
return FALSE;
}
-
+
// --------------------------------------------------------------------
-
+
// Validate the product ID. It can only be alpha-numeric, dashes, underscores or periods
// Not totally sure we should impose this rule, but it seems prudent to standardize IDs.
// Note: These can be user-specified by setting the $this->product_id_rules variable.
@@ -180,7 +180,7 @@ class CI_Cart {
}
// --------------------------------------------------------------------
-
+
// Validate the product name. It can only be alpha-numeric, dashes, underscores, colons or periods.
// Note: These can be user-specified by setting the $this->product_name_rules variable.
if ( ! preg_match("/^[".$this->product_name_rules."]+$/i", $items['name']))
@@ -195,7 +195,7 @@ class CI_Cart {
$items['price'] = trim(preg_replace('/([^0-9\.])/i', '', $items['price']));
// Trim any leading zeros
$items['price'] = trim(preg_replace('/(^[0]+)/i', '', $items['price']));
-
+
// Is the price a valid number?
if ( ! is_numeric($items['price']))
{
@@ -204,13 +204,13 @@ class CI_Cart {
}
// --------------------------------------------------------------------
-
+
// We now need to create a unique identifier for the item being inserted into the cart.
- // Every time something is added to the cart it is stored in the master cart array.
- // Each row in the cart array, however, must have a unique index that identifies not only
- // a particular product, but makes it possible to store identical products with different options.
- // For example, what if someone buys two identical t-shirts (same product ID), but in
- // different sizes? The product ID (and other attributes, like the name) will be identical for
+ // Every time something is added to the cart it is stored in the master cart array.
+ // Each row in the cart array, however, must have a unique index that identifies not only
+ // a particular product, but makes it possible to store identical products with different options.
+ // For example, what if someone buys two identical t-shirts (same product ID), but in
+ // different sizes? The product ID (and other attributes, like the name) will be identical for
// both sizes because it's the same shirt. The only difference will be the size.
// Internally, we need to treat identical submissions, but with different options, as a unique product.
// Our solution is to convert the options array to a string and MD5 it along with the product ID.
@@ -225,19 +225,19 @@ class CI_Cart {
// Technically, we don't need to MD5 the ID in this case, but it makes
// sense to standardize the format of array indexes for both conditions
$rowid = md5($items['id']);
- }
+ }
// --------------------------------------------------------------------
// Now that we have our unique "row ID", we'll add our cart items to the master array
-
+
// let's unset this first, just to make sure our index contains only the data from this submission
- unset($this->_cart_contents[$rowid]);
-
+ unset($this->_cart_contents[$rowid]);
+
// Create a new index with our new row ID
$this->_cart_contents[$rowid]['rowid'] = $rowid;
-
- // And add the new items to the cart array
+
+ // And add the new items to the cart array
foreach ($items as $key => $val)
{
$this->_cart_contents[$rowid][$key] = $val;
@@ -248,11 +248,11 @@ class CI_Cart {
}
// --------------------------------------------------------------------
-
+
/**
* Update the cart
*
- * This function permits the quantity of a given item to be changed.
+ * This function permits the quantity of a given item to be changed.
* Typically it is called from the "view cart" page if a user makes
* changes to the quantity before checkout. That array must contain the
* product ID and quantity for each item.
@@ -269,8 +269,8 @@ class CI_Cart {
{
return FALSE;
}
-
- // You can either update a single product using a one-dimensional array,
+
+ // You can either update a single product using a one-dimensional array,
// or multiple products using a multi-dimensional one. The way we
// determine the array type is by looking for a required array key named "id".
// If it's not found we assume it's a multi-dimensional array
@@ -292,7 +292,7 @@ class CI_Cart {
{
$save_cart = TRUE;
}
- }
+ }
}
}
@@ -307,11 +307,11 @@ class CI_Cart {
}
// --------------------------------------------------------------------
-
+
/**
* Update the cart
*
- * This function permits the quantity of a given item to be changed.
+ * This function permits the quantity of a given item to be changed.
* Typically it is called from the "view cart" page if a user makes
* changes to the quantity before checkout. That array must contain the
* product ID and quantity for each item.
@@ -319,7 +319,7 @@ class CI_Cart {
* @access private
* @param array
* @return bool
- */
+ */
function _update($items = array())
{
// Without these array indexes there is nothing we can do
@@ -327,7 +327,7 @@ class CI_Cart {
{
return FALSE;
}
-
+
// Prep the quantity
$items['qty'] = preg_replace('/([^0-9])/i', '', $items['qty']);
@@ -336,7 +336,7 @@ class CI_Cart {
{
return FALSE;
}
-
+
// Is the new quantity different than what is already saved in the cart?
// If it's the same there's nothing to do
if ($this->_cart_contents[$items['rowid']]['qty'] == $items['qty'])
@@ -348,18 +348,18 @@ class CI_Cart {
// If the quantity is greater than zero we are updating
if ($items['qty'] == 0)
{
- unset($this->_cart_contents[$items['rowid']]);
+ unset($this->_cart_contents[$items['rowid']]);
}
else
{
$this->_cart_contents[$items['rowid']]['qty'] = $items['qty'];
}
-
+
return TRUE;
}
// --------------------------------------------------------------------
-
+
/**
* Save the cart array to the session DB
*
@@ -383,20 +383,20 @@ class CI_Cart {
}
$total += ($val['price'] * $val['qty']);
-
+
// Set the subtotal
$this->_cart_contents[$key]['subtotal'] = ($this->_cart_contents[$key]['price'] * $this->_cart_contents[$key]['qty']);
}
// Set the cart total and total items.
- $this->_cart_contents['total_items'] = count($this->_cart_contents);
+ $this->_cart_contents['total_items'] = count($this->_cart_contents);
$this->_cart_contents['cart_total'] = $total;
-
+
// Is our cart empty? If so we delete it from the session
if (count($this->_cart_contents) <= 2)
{
$this->CI->session->unset_userdata('cart_contents');
-
+
// Nothing more to do... coffee time!
return FALSE;
}
@@ -406,11 +406,11 @@ class CI_Cart {
$this->CI->session->set_userdata(array('cart_contents' => $this->_cart_contents));
// Woot!
- return TRUE;
+ return TRUE;
}
// --------------------------------------------------------------------
-
+
/**
* Cart Total
*
@@ -423,7 +423,7 @@ class CI_Cart {
}
// --------------------------------------------------------------------
-
+
/**
* Total Items
*
@@ -438,7 +438,7 @@ class CI_Cart {
}
// --------------------------------------------------------------------
-
+
/**
* Cart Contents
*
@@ -450,16 +450,16 @@ class CI_Cart {
function contents()
{
$cart = $this->_cart_contents;
-
+
// Remove these so they don't create a problem when showing the cart table
unset($cart['total_items']);
unset($cart['cart_total']);
-
+
return $cart;
}
// --------------------------------------------------------------------
-
+
/**
* Has options
*
@@ -475,12 +475,12 @@ class CI_Cart {
{
return FALSE;
}
-
+
return TRUE;
}
// --------------------------------------------------------------------
-
+
/**
* Product options
*
@@ -500,7 +500,7 @@ class CI_Cart {
}
// --------------------------------------------------------------------
-
+
/**
* Format Number
*
@@ -515,15 +515,15 @@ class CI_Cart {
{
return '';
}
-
+
// Remove anything that isn't a number or decimal point.
$n = trim(preg_replace('/([^0-9\.])/i', '', $n));
-
+
return number_format($n, 2, '.', ',');
}
-
+
// --------------------------------------------------------------------
-
+
/**
* Destroy the cart
*
@@ -535,9 +535,9 @@ class CI_Cart {
function destroy()
{
unset($this->_cart_contents);
-
- $this->_cart_contents['cart_total'] = 0;
- $this->_cart_contents['total_items'] = 0;
+
+ $this->_cart_contents['cart_total'] = 0;
+ $this->_cart_contents['total_items'] = 0;
$this->CI->session->unset_userdata('cart_contents');
}