日期:2014-05-17 浏览次数:20610 次
<?php
/**
* 购物车程序 Modified by CodeIgniter
*
*/
class cart {
// 对产品ID和产品名称进行正则验证属性
var $product_id_rules = '\.a-z0-9_-';
var $product_name_rules = '\.\:\-_ a-z0-9'; // 考虑到汉字,该功能暂不使用
// 私有变量
var $_cart_contents = array();
/**
* 构造方法
*
*/
public function __construct()
{
if ($this->session('cart_contents') !== FALSE)
{
$this->_cart_contents = $this->session('cart_contents');
}
else
{
// 初始化数据
$this->_cart_contents['cart_total'] = 0;
$this->_cart_contents['total_items'] = 0;
}
}
// --------------------------------
/**
* 添加到购物车
*
* @access public
* @param array
* @return bool
*/
function insert($items = array())
{
// 检测数据是否正确
if ( ! is_array($items) OR count($items) == 0)
{
return FALSE;
}
// 可以添加一个商品(一维数组),也可以添加多个商品(二维数组)
$save_cart = FALSE;
if (isset($items['id']))
{
if ($this->_insert($items) == TRUE)
{
$save_cart = TRUE;
}
}
else
{
foreach ($items as $val)
{
if (is_array($val) AND isset($val['id']))
{
if ($this->_insert($val) == TRUE)
{
$save_cart = TRUE;
}
}
}
}
// 更新数据
if ($save_cart == TRUE)
{
$this->_save_cart();
return TRUE;
}
return FALSE;
}
// --------------------------------
/**
* 处理插入购物车数据
*
* @access private
* @param array
* @return bool
*/
function _insert($items = array())
{
// 检查购物车
if ( ! is_array($items) OR count($items) == 0)
{
return FALSE;
}
// --------------------------------
/* 前四个数组索引 (id, qty, price 和name) 是 必需的。
如果缺少其中的任何一个,数据将不会被保存到购物车中。
第5个索引 (options) 是可选的。当你的商品包含一些相关的选项信息时,你就可以使用它。
请使用一个数组来保存选项信息。注意:$data['price'] 的值必须大于0
如:$data = array(
'id' => 'sku_123ABC',
'qty' => 1,
'price' => 39.95,
'name' => 'T-Shirt',
'options' => array('Size' => 'L', 'Color' => 'Red')
);
*/
if ( ! isset($items['id']) OR ! isset($items['qty']) OR ! isset($items['price']) OR ! isset($items['name']))
{
return FALSE;
}
// --------------------------------
// 数量验证,不是数字替换为空
$items['qty'] = trim(preg_replace('/([^0-9])/i', '', $items['qty']));
// 数量验证
$items['qty'] = trim(preg_replace('/(^[0]+)/i', '', $items['qty']));
// 数量必须是数字或不为0
if ( ! is_numeric($items['qty']) OR $items['qty'] == 0)
{
return FALSE;
}
// --------------------------------
// 产品ID验证
if ( ! preg_match("/^[".$this->product_id_rules."]+$/i", $items['id']))
{
return FALSE;
}
// --------------------------------
// 验证产品名称,考虑到汉字,暂不使用
/*
if ( ! preg_match("/^[".$this->product_name_rules."]+$/i", $items['name']))
{
return FALSE;
}
*/
// --------------------------------
// 价格验证
$items['price'] = trim(preg_replace('/([^0-9\.])/i', '', $items['price']));
$items['price'] = trim(preg_replace('/(^[0]+)/i', '', $items['price']));
// 验证价格是否是数值
if ( ! is_numeric($items['price']))
{
return FALSE;
}
// --------------------------------
// 属性验证,如果属性存在,属性值+产品ID进行加密保存在$rowid中
if (isset($items['options']) AND count($items['options']) > 0)
{
$rowid = md5($items['id'].implode('', $items['options']));
}
else
{
// 没有属性时直接对产品ID加密
$rowid = md5($items['id']);
}
// 检测购物车中是否有该产品,如果有,在原来的基础上加上本次新增的商品数量
$_contents = $this->_cart_contents;
$_tmp_contents = array();
foreach ($_contents as $val)
{
if (is_array($val) AND isset($val['rowid']) AND isset($val['qty']) AND $val['rowid']==$rowid)
{
$_tmp_contents[$val['rowid']]['qty'] = $val['qty'];
} else {
$_tmp_contents[$val['rowid']]['qty'] = 0;
}
}
// --------------------------------
// 清除原来的数据
unset($this->_cart_contents[$rowid]);
// 重新赋值
$this->_cart_contents[$rowid]['rowid'] = $rowid;
// 添加新项目
foreach ($items as $key => $val)
{