summaryrefslogtreecommitdiffstats
path: root/system/libraries
diff options
context:
space:
mode:
authorAndrey Andreev <narf@bofh.bg>2011-12-21 15:42:51 +0100
committerAndrey Andreev <narf@bofh.bg>2011-12-21 15:42:51 +0100
commitbb2488305194e50881df0971bf4f33f30d974d36 (patch)
tree31d476a7589812a61a7f8837ff29971f41330a6f /system/libraries
parentfec2f59721af5721ede0d2b60075910a6b646c03 (diff)
Improved the Cart library
Diffstat (limited to 'system/libraries')
-rw-r--r--system/libraries/Cart.php196
1 files changed, 61 insertions, 135 deletions
diff --git a/system/libraries/Cart.php b/system/libraries/Cart.php
index 717ccd9fb..b2cc2081e 100644
--- a/system/libraries/Cart.php
+++ b/system/libraries/Cart.php
@@ -1,13 +1,13 @@
-<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
+<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter
*
* An open source application development framework for PHP 5.1.6 or newer
*
* NOTICE OF LICENSE
- *
+ *
* Licensed under the Open Software License version 3.0
- *
+ *
* This source file is subject to the Open Software License (OSL 3.0) that is
* bundled with this package in the files license.txt / license.rst. It is
* also available through the world wide web at this URL:
@@ -39,13 +39,13 @@
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
- var $product_name_safe = true; // only allow safe product names
+ public $product_id_rules = '\.a-z0-9_-'; // alpha-numeric, dashes, underscores, or periods
+ public $product_name_rules = '\.\:\-_ a-z0-9'; // alpha-numeric, dashes, underscores, colons or periods
+ public $product_name_safe = true; // only allow safe product names
// Private variables. Do not change!
- var $CI;
- var $_cart_contents = array();
+ private $CI;
+ private $_cart_contents = array();
/**
@@ -59,28 +59,17 @@ class CI_Cart {
$this->CI =& get_instance();
// Are any config settings being passed manually? If so, set them
- $config = array();
- if (count($params) > 0)
- {
- foreach ($params as $key => $val)
- {
- $config[$key] = $val;
- }
- }
+ $config = is_array($params) ? $params : array();
// 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)
- {
- $this->_cart_contents = $this->CI->session->userdata('cart_contents');
- }
- else
+ // Grab the shopping cart array from the session table
+ $this->_cart_contents = $this->CI->session->userdata('cart_contents');
+ if ($this->_cart_contents === FALSE)
{
// 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 = array('cart_total' => 0, 'total_items' => 0);
}
log_message('debug', "Cart Class Initialized");
@@ -95,10 +84,10 @@ class CI_Cart {
* @param array
* @return bool
*/
- function insert($items = array())
+ public function insert($items = array())
{
// Was any cart data passed? No? Bah...
- if ( ! is_array($items) OR count($items) == 0)
+ if ( ! is_array($items) OR count($items) === 0)
{
log_message('error', 'The insert method must be passed an array containing data.');
return FALSE;
@@ -132,7 +121,7 @@ class CI_Cart {
}
// Save the cart data if the insert was successful
- if ($save_cart == TRUE)
+ if ($save_cart === TRUE)
{
$this->_save_cart();
return isset($rowid) ? $rowid : TRUE;
@@ -150,10 +139,10 @@ class CI_Cart {
* @param array
* @return bool
*/
- function _insert($items = array())
+ private function _insert($items = array())
{
// Was any cart data passed? No? Bah...
- if ( ! is_array($items) OR count($items) == 0)
+ if ( ! is_array($items) OR count($items) === 0)
{
log_message('error', 'The insert method must be passed an array containing data.');
return FALSE;
@@ -170,10 +159,8 @@ 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
- $items['qty'] = trim(preg_replace('/(^[0]+)/i', '', $items['qty']));
+ // Prep the quantity. It can only be a number. Duh... also trim any leading zeros
+ $items['qty'] = ltrim(trim(preg_replace('/([^0-9])/i', '', $items['qty'])), '0');
// If the quantity is zero or blank there's nothing for us to do
if ( ! is_numeric($items['qty']) OR $items['qty'] == 0)
@@ -186,7 +173,7 @@ class CI_Cart {
// 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.
- if ( ! preg_match("/^[".$this->product_id_rules."]+$/i", $items['id']))
+ if ( ! preg_match('/^['.$this->product_id_rules.']+$/i', $items['id']))
{
log_message('error', 'Invalid product ID. The product ID can only contain alpha-numeric characters, dashes, and underscores');
return FALSE;
@@ -196,7 +183,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 ( $this->product_name_safe && ! preg_match("/^[".$this->product_name_rules."]+$/i", $items['name']))
+ if ($this->product_name_safe && ! preg_match('/^['.$this->product_name_rules.']+$/i', $items['name']))
{
log_message('error', 'An invalid name was submitted as the product name: '.$items['name'].' The name can only contain alpha-numeric characters, dashes, underscores, colons, and spaces');
return FALSE;
@@ -204,10 +191,8 @@ class CI_Cart {
// --------------------------------------------------------------------
- // Prep the price. Remove anything that isn't a number or decimal point.
- $items['price'] = trim(preg_replace('/([^0-9\.])/i', '', $items['price']));
- // Trim any leading zeros
- $items['price'] = trim(preg_replace('/(^[0]+)/i', '', $items['price']));
+ // Prep the price. Remove leading zeros and anything that isn't a number or decimal point.
+ $items['price'] = lrtrim(trim(preg_replace('/([^0-9\.])/i', '', $items['price'])), '0');
// Is the price a valid number?
if ( ! is_numeric($items['price']))
@@ -244,33 +229,13 @@ class CI_Cart {
// Now that we have our unique "row ID", we'll add our cart items to the master array
// grab quantity if it's already there and add it on
- if (isset($this->_cart_contents[$rowid]['qty']))
- {
- // set our old quantity
- $old_quantity = (int)$this->_cart_contents[$rowid]['qty'];
- }
- else
- {
- // we have no old quantity but - we don't want to throw an error
- $old_quantity = 0;
- }
-
- // let's unset this first, just to make sure our index contains only the data from this submission
- unset($this->_cart_contents[$rowid]);
+ $old_quantity = isset($this->_cart_contents[$rowid]['qty']) ? (int) $this->_cart_contents[$rowid]['qty'] : 0;
- // Create a new index with our new row ID
- $this->_cart_contents[$rowid]['rowid'] = $rowid;
+ // Re-create the entry, just to make sure our index contains only the data from this submission
+ $items['rowid'] = $rowid;
+ $items['qty'] += $old_quantity;
+ $this->_cart_contents[$rowid] = $items;
- // And add the new items to the cart array
- foreach ($items as $key => $val)
- {
- $this->_cart_contents[$rowid][$key] = $val;
- }
-
- // add old quantity back in
- $this->_cart_contents[$rowid]['qty'] = ($this->_cart_contents[$rowid]['qty'] + $old_quantity);
-
- // Woot!
return $rowid;
}
@@ -289,10 +254,10 @@ class CI_Cart {
* @param string
* @return bool
*/
- function update($items = array())
+ public function update($items = array())
{
// Was any cart data passed?
- if ( ! is_array($items) OR count($items) == 0)
+ if ( ! is_array($items) OR count($items) === 0)
{
return FALSE;
}
@@ -302,9 +267,9 @@ class CI_Cart {
// 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
$save_cart = FALSE;
- if (isset($items['rowid']) AND isset($items['qty']))
+ if (isset($items['rowid'], $items['qty']))
{
- if ($this->_update($items) == TRUE)
+ if ($this->_update($items) === TRUE)
{
$save_cart = TRUE;
}
@@ -313,9 +278,9 @@ class CI_Cart {
{
foreach ($items as $val)
{
- if (is_array($val) AND isset($val['rowid']) AND isset($val['qty']))
+ if (is_array($val) && isset($val['rowid'], $val['qty']))
{
- if ($this->_update($val) == TRUE)
+ if ($this->_update($val) === TRUE)
{
$save_cart = TRUE;
}
@@ -324,7 +289,7 @@ class CI_Cart {
}
// Save the cart data if the insert was successful
- if ($save_cart == TRUE)
+ if ($save_cart === TRUE)
{
$this->_save_cart();
return TRUE;
@@ -347,7 +312,7 @@ class CI_Cart {
* @param array
* @return bool
*/
- function _update($items = array())
+ private function _update($items = array())
{
// Without these array indexes there is nothing we can do
if ( ! isset($items['qty']) OR ! isset($items['rowid']) OR ! isset($this->_cart_contents[$items['rowid']]))
@@ -393,15 +358,10 @@ class CI_Cart {
* @access private
* @return bool
*/
- function _save_cart()
+ private function _save_cart()
{
- // Unset these so our total can be calculated correctly below
- unset($this->_cart_contents['total_items']);
- unset($this->_cart_contents['cart_total']);
-
// Lets add up the individual prices and set the cart sub-total
- $total = 0;
- $items = 0;
+ $this->_cart_contents['total_items'] = $this->_cart_contents['cart_total'] = 0;
foreach ($this->_cart_contents as $key => $val)
{
// We make sure the array contains the proper indexes
@@ -410,17 +370,11 @@ class CI_Cart {
continue;
}
- $total += ($val['price'] * $val['qty']);
- $items += $val['qty'];
-
- // Set the subtotal
+ $this->_cart_contents['cart_total'] += ($val['price'] * $val['qty']);
+ $this->_cart_contents['total_items'] += $val['qty'];
$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'] = $items;
- $this->_cart_contents['cart_total'] = $total;
-
// Is our cart empty? If so we delete it from the session
if (count($this->_cart_contents) <= 2)
{
@@ -434,7 +388,6 @@ class CI_Cart {
// Let's pass it to the Session class so it can be stored
$this->CI->session->set_userdata(array('cart_contents' => $this->_cart_contents));
- // Woot!
return TRUE;
}
@@ -446,13 +399,13 @@ class CI_Cart {
* @access public
* @return integer
*/
- function total()
+ public function total()
{
return $this->_cart_contents['cart_total'];
}
-
+
// --------------------------------------------------------------------
-
+
/**
* Remove Item
*
@@ -463,16 +416,12 @@ class CI_Cart {
*/
public function remove($rowid)
{
- // just do an unset
+ // unset & save
unset($this->_cart_contents[$rowid]);
-
- // we need to save the cart now we've made our changes
$this->_save_cart();
-
- // completed
- return true;
+ return TRUE;
}
-
+
// --------------------------------------------------------------------
/**
@@ -483,7 +432,7 @@ class CI_Cart {
* @access public
* @return integer
*/
- function total_items()
+ public function total_items()
{
return $this->_cart_contents['total_items'];
}
@@ -498,19 +447,10 @@ class CI_Cart {
* @access public
* @return array
*/
- function contents($newest_first = false)
+ public function contents($newest_first = FALSE)
{
// do we want the newest first?
- if($newest_first)
- {
- // reverse the array
- $cart = array_reverse($this->_cart_contents);
- }
- else
- {
- // just added first to last
- $cart = $this->_cast_contents;
- }
+ $cart = ($newest_first) ? array_reverse($this->_cart_contents) : $this->_cart_contents;
// Remove these so they don't create a problem when showing the cart table
unset($cart['total_items']);
@@ -528,16 +468,11 @@ class CI_Cart {
* that has options associated with it.
*
* @access public
- * @return array
+ * @return bool
*/
- function has_options($rowid = '')
+ public function has_options($rowid = '')
{
- if ( ! isset($this->_cart_contents[$rowid]['options']) OR count($this->_cart_contents[$rowid]['options']) === 0)
- {
- return FALSE;
- }
-
- return TRUE;
+ return (isset($this->_cart_contents[$rowid]['options']) && count($this->_cart_contents[$rowid]['options']) !== 0) ? TRUE : FALSE;
}
// --------------------------------------------------------------------
@@ -550,14 +485,9 @@ class CI_Cart {
* @access public
* @return array
*/
- function product_options($rowid = '')
+ public function product_options($rowid = '')
{
- if ( ! isset($this->_cart_contents[$rowid]['options']))
- {
- return array();
- }
-
- return $this->_cart_contents[$rowid]['options'];
+ return isset($this->_cart_contents[$rowid]['options']) ? $this->_cart_contents[$rowid]['options'] : array();
}
// --------------------------------------------------------------------
@@ -568,9 +498,9 @@ class CI_Cart {
* Returns the supplied number with commas and a decimal point.
*
* @access public
- * @return integer
+ * @return string
*/
- function format_number($n = '')
+ public function format_number($n = '')
{
if ($n == '')
{
@@ -591,15 +521,11 @@ class CI_Cart {
* Empties the cart and kills the session
*
* @access public
- * @return null
+ * @return void
*/
- function destroy()
+ public function destroy()
{
- unset($this->_cart_contents);
-
- $this->_cart_contents['cart_total'] = 0;
- $this->_cart_contents['total_items'] = 0;
-
+ $this->_cart_contents = array('cart_total' => 0, 'total_items' => 0);
$this->CI->session->unset_userdata('cart_contents');
}
@@ -608,4 +534,4 @@ class CI_Cart {
// END Cart Class
/* End of file Cart.php */
-/* Location: ./system/libraries/Cart.php */ \ No newline at end of file
+/* Location: ./system/libraries/Cart.php */