[317] | 1 | <?php |
---|
| 2 | /** |
---|
[362] | 3 | * The Strangecode Codebase - a general application development framework for PHP |
---|
| 4 | * For details visit the project site: <http://trac.strangecode.com/codebase/> |
---|
[396] | 5 | * Copyright 2001-2012 Strangecode, LLC |
---|
[611] | 6 | * |
---|
[362] | 7 | * This file is part of The Strangecode Codebase. |
---|
| 8 | * |
---|
| 9 | * The Strangecode Codebase is free software: you can redistribute it and/or |
---|
| 10 | * modify it under the terms of the GNU General Public License as published by the |
---|
| 11 | * Free Software Foundation, either version 3 of the License, or (at your option) |
---|
| 12 | * any later version. |
---|
[611] | 13 | * |
---|
[362] | 14 | * The Strangecode Codebase is distributed in the hope that it will be useful, but |
---|
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
| 16 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
---|
| 17 | * details. |
---|
[611] | 18 | * |
---|
[362] | 19 | * You should have received a copy of the GNU General Public License along with |
---|
| 20 | * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>. |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | /** |
---|
[317] | 24 | * Cart.inc.php |
---|
| 25 | * |
---|
| 26 | * Class description goes here... |
---|
| 27 | * |
---|
| 28 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 29 | * @version 1.0 |
---|
[611] | 30 | * |
---|
[317] | 31 | * Example of use: |
---|
| 32 | --------------------------------------------------------------------- |
---|
[333] | 33 | $cart = new Cart(); |
---|
| 34 | |
---|
| 35 | switch (getFormData('op')) { |
---|
| 36 | case 'add' : |
---|
| 37 | // User clicks an "Add to cart" button. |
---|
| 38 | if ($product_id = getFormData('product_id')) { |
---|
| 39 | $qid = $db->query("SELECT * FROM product_tbl WHERE product_id = '" . $db->escapeString($product_id) . "'"); |
---|
| 40 | if ($p = mysql_fetch_assoc($qid)) { |
---|
| 41 | $cart->add($product_id, $p['price'], 1, array( |
---|
| 42 | 'title' => $p['title'], |
---|
| 43 | 'shipping' => $p['shipping'], |
---|
| 44 | 'weight' => $p['weight'], |
---|
| 45 | )); |
---|
| 46 | } |
---|
| 47 | } |
---|
| 48 | break; |
---|
| 49 | |
---|
| 50 | case 'remove' : |
---|
| 51 | // User clicks a "Remove item" button. |
---|
| 52 | $cart->remove(getFormData('product_id')); |
---|
| 53 | break; |
---|
| 54 | |
---|
| 55 | case 'qty' : |
---|
| 56 | // User changes the quantity of an item. |
---|
| 57 | $cart->setQty(getFormData('product_id'), getFormData('qty')); |
---|
| 58 | break; |
---|
| 59 | |
---|
| 60 | case 'view' : |
---|
| 61 | default : |
---|
| 62 | // User views cart. |
---|
| 63 | foreach ($cart->getList() as $item) { |
---|
| 64 | printf("Item: %s\nQty: %s\nPrice: %s\n\n", $item['title'], $item['quantity'], $item['price']); |
---|
| 65 | } |
---|
| 66 | break; |
---|
| 67 | } |
---|
[317] | 68 | --------------------------------------------------------------------- |
---|
| 69 | */ |
---|
[502] | 70 | class Cart |
---|
| 71 | { |
---|
[317] | 72 | |
---|
| 73 | // Namespace of this instance. |
---|
[484] | 74 | protected $_ns; |
---|
[317] | 75 | |
---|
| 76 | // Configuration parameters for this object. |
---|
[484] | 77 | protected $_params = array( |
---|
[317] | 78 | ); |
---|
| 79 | |
---|
| 80 | /** |
---|
| 81 | * Cart constructor. |
---|
| 82 | */ |
---|
[468] | 83 | public function __construct($namespace='') |
---|
[317] | 84 | { |
---|
[479] | 85 | $app =& App::getInstance(); |
---|
[317] | 86 | |
---|
| 87 | $this->_ns = $namespace; |
---|
[611] | 88 | |
---|
[317] | 89 | // Initialize. |
---|
| 90 | if (!isset($_SESSION['_cart'][$this->_ns])) { |
---|
| 91 | $this->clear(); |
---|
| 92 | } |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | /** |
---|
| 96 | * Set the params of this object. |
---|
| 97 | * |
---|
| 98 | * @param array $params Array of param keys and values to set. |
---|
| 99 | */ |
---|
[468] | 100 | public function setParam($params=null) |
---|
[317] | 101 | { |
---|
| 102 | if (isset($params) && is_array($params)) { |
---|
| 103 | // Merge new parameters with old overriding only those passed. |
---|
| 104 | $this->_params = array_merge($this->_params, $params); |
---|
| 105 | } |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | /** |
---|
| 109 | * Return the value of a parameter, if it exists. |
---|
| 110 | * |
---|
| 111 | * @access public |
---|
| 112 | * @param string $param Which parameter to return. |
---|
| 113 | * @return mixed Configured parameter value. |
---|
| 114 | */ |
---|
[468] | 115 | public function getParam($param) |
---|
[317] | 116 | { |
---|
[479] | 117 | $app =& App::getInstance(); |
---|
[611] | 118 | |
---|
[478] | 119 | if (array_key_exists($param, $this->_params)) { |
---|
[317] | 120 | return $this->_params[$param]; |
---|
| 121 | } else { |
---|
| 122 | $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 123 | return null; |
---|
| 124 | } |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | /* |
---|
[333] | 128 | * Add an item to the cart. If called twice for one item it will add the quantities together. |
---|
[317] | 129 | * |
---|
| 130 | * @access public |
---|
[333] | 131 | * @param string $item_id A unique ID for the item |
---|
| 132 | * @param float $price Price-per-quantity-unit for the item |
---|
| 133 | * @param float $quantity Quantity of items (can be fractional unites?) |
---|
| 134 | * @param array $specs An array of additional specifications (title, weight, etc) |
---|
| 135 | * @return float The new adjusted quantity of this item. |
---|
[317] | 136 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 137 | * @version 1.0 |
---|
| 138 | * @since 11 Mar 2008 18:59:37 |
---|
| 139 | */ |
---|
[468] | 140 | public function add($item_id, $price, $quantity=1, $specs=array()) |
---|
[317] | 141 | { |
---|
[333] | 142 | $app =& App::getInstance(); |
---|
[611] | 143 | |
---|
[333] | 144 | // Include any previously added items in the total quantity. |
---|
| 145 | $quantity += isset($_SESSION['_cart'][$this->_ns]['items'][$item_id]) ? $_SESSION['_cart'][$this->_ns]['items'][$item_id]['quantity'] : 0; |
---|
[611] | 146 | |
---|
[333] | 147 | // Add item with adjusted quantity and merge in additional specs. |
---|
| 148 | $_SESSION['_cart'][$this->_ns]['items'][$item_id] = array_merge($specs, array( |
---|
| 149 | 'quantity' => $quantity, |
---|
| 150 | 'price' => $price, |
---|
| 151 | 'extended_price' => $quantity * $price, |
---|
| 152 | )); |
---|
[611] | 153 | |
---|
[333] | 154 | $app->logMsg(sprintf('Added %s %s to cart (%s)', $quantity, $item_id, truncate(getDump($specs, true), 128, 'end')), LOG_DEBUG, __FILE__, __LINE__); |
---|
[611] | 155 | |
---|
[333] | 156 | return $quantity; |
---|
[317] | 157 | } |
---|
[611] | 158 | |
---|
[317] | 159 | /* |
---|
[333] | 160 | * Set the absolute quantity value of a specified item in a cart. |
---|
[317] | 161 | * |
---|
| 162 | * @access public |
---|
[611] | 163 | * @param |
---|
| 164 | * @return |
---|
[317] | 165 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 166 | * @version 1.0 |
---|
[333] | 167 | * @since 10 May 2008 16:42:25 |
---|
| 168 | */ |
---|
[468] | 169 | public function setQty($item_id, $quantity) |
---|
[333] | 170 | { |
---|
| 171 | if ($quantity <= 0) { |
---|
| 172 | $this->remove($item_id); |
---|
| 173 | } else { |
---|
| 174 | if (isset($_SESSION['_cart'][$this->_ns]['items'][$item_id])) { |
---|
| 175 | $item = $_SESSION['_cart'][$this->_ns]['items'][$item_id]; |
---|
| 176 | $_SESSION['_cart'][$this->_ns]['items'][$item_id] = array_merge($item, array( |
---|
| 177 | 'quantity' => $quantity, |
---|
| 178 | 'extended_price' => $quantity * $item['price'], |
---|
| 179 | )); |
---|
[339] | 180 | return true; |
---|
| 181 | } else { |
---|
| 182 | return false; |
---|
| 183 | } |
---|
| 184 | return true; |
---|
[333] | 185 | } |
---|
| 186 | } |
---|
[611] | 187 | |
---|
[333] | 188 | /* |
---|
[339] | 189 | * Set the absolute price value of a specified item in a cart. |
---|
| 190 | * |
---|
| 191 | * @access public |
---|
[611] | 192 | * @param |
---|
| 193 | * @return |
---|
[339] | 194 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 195 | * @version 1.0 |
---|
| 196 | * @since 10 May 2008 16:42:25 |
---|
| 197 | */ |
---|
[468] | 198 | public function setPrice($item_id, $price) |
---|
[339] | 199 | { |
---|
| 200 | if (isset($_SESSION['_cart'][$this->_ns]['items'][$item_id])) { |
---|
| 201 | $item = $_SESSION['_cart'][$this->_ns]['items'][$item_id]; |
---|
| 202 | $_SESSION['_cart'][$this->_ns]['items'][$item_id] = array_merge($item, array( |
---|
| 203 | 'price' => $price, |
---|
| 204 | 'extended_price' => $price * $item['quantity'], |
---|
| 205 | )); |
---|
| 206 | return true; |
---|
| 207 | } else { |
---|
| 208 | return false; |
---|
| 209 | } |
---|
| 210 | } |
---|
[611] | 211 | |
---|
[339] | 212 | /* |
---|
[333] | 213 | * Remove an item from the cart. |
---|
| 214 | * |
---|
| 215 | * @access public |
---|
| 216 | * @param string $item_id Item to remove from cart. |
---|
| 217 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 218 | * @version 1.0 |
---|
[317] | 219 | * @since 11 Mar 2008 18:59:48 |
---|
| 220 | */ |
---|
[468] | 221 | public function remove($item_id) |
---|
[317] | 222 | { |
---|
[333] | 223 | $app =& App::getInstance(); |
---|
[611] | 224 | |
---|
[333] | 225 | if (isset($_SESSION['_cart'][$this->_ns]['items'][$item_id])) { |
---|
| 226 | unset($_SESSION['_cart'][$this->_ns]['items'][$item_id]); |
---|
| 227 | $app->logMsg(sprintf('Removed %s from cart', $item_id), LOG_DEBUG, __FILE__, __LINE__); |
---|
[339] | 228 | return true; |
---|
| 229 | } else { |
---|
| 230 | return false; |
---|
[333] | 231 | } |
---|
[317] | 232 | } |
---|
[611] | 233 | |
---|
[317] | 234 | /* |
---|
[333] | 235 | * Return the value matching key for a specified item. |
---|
[317] | 236 | * |
---|
| 237 | * @access public |
---|
[333] | 238 | * @param string $item_id The ID of the cart item to retrieve. |
---|
| 239 | * @param string $spec_key The key of the value to return. |
---|
| 240 | * @return mixed The value, or false if not found. |
---|
[317] | 241 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 242 | * @version 1.0 |
---|
| 243 | * @since 11 Mar 2008 18:59:55 |
---|
| 244 | */ |
---|
[468] | 245 | public function get($item_id, $spec_key) |
---|
[333] | 246 | { |
---|
| 247 | if (isset($_SESSION['_cart'][$this->_ns]['items'][$item_id][$spec_key])) { |
---|
| 248 | return $_SESSION['_cart'][$this->_ns]['items'][$item_id][$spec_key]; |
---|
| 249 | } |
---|
| 250 | return false; |
---|
| 251 | } |
---|
[611] | 252 | |
---|
[333] | 253 | /* |
---|
| 254 | * Return an array of the items in cart. |
---|
| 255 | * |
---|
| 256 | * @access public |
---|
| 257 | * @return array Array of cart items. |
---|
| 258 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 259 | * @version 1.0 |
---|
| 260 | * @since 11 Mar 2008 18:59:55 |
---|
| 261 | */ |
---|
[468] | 262 | public function getList() |
---|
[317] | 263 | { |
---|
[333] | 264 | return $_SESSION['_cart'][$this->_ns]['items']; |
---|
[317] | 265 | } |
---|
[611] | 266 | |
---|
[317] | 267 | /* |
---|
[333] | 268 | * Return the sum of the numeric values stored under the specified key for cart items. |
---|
| 269 | * 0 will be returned if key is invalid or not filled with numeric values. |
---|
[354] | 270 | * Most commonly, $cart->sum() will be called when checking out to retrieve the total |
---|
| 271 | * cost of the items in the cart. |
---|
[317] | 272 | * |
---|
| 273 | * @access public |
---|
[333] | 274 | * @param string $key The spec key to sum. |
---|
| 275 | * @return float The total added by all the found values. |
---|
[317] | 276 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 277 | * @version 1.0 |
---|
| 278 | * @since 11 Mar 2008 19:00:12 |
---|
| 279 | */ |
---|
[468] | 280 | public function sum($key='extended_price') |
---|
[317] | 281 | { |
---|
[479] | 282 | $sum = 0; |
---|
[333] | 283 | switch ($key) { |
---|
| 284 | case 'items' : |
---|
[354] | 285 | $sum = sizeof($_SESSION['_cart'][$this->_ns]['items']); |
---|
[356] | 286 | break; |
---|
[333] | 287 | |
---|
[479] | 288 | case 'extended_price' : |
---|
[353] | 289 | foreach ($_SESSION['_cart'][$this->_ns]['items'] as $item_id => $specs) { |
---|
| 290 | $sum += isset($specs[$key]) && is_numeric($specs[$key]) ? $specs[$key] : 0; |
---|
| 291 | } |
---|
[356] | 292 | break; |
---|
[354] | 293 | |
---|
[333] | 294 | default : |
---|
[390] | 295 | // Retrieve arbitrary values stored in the cart (shipping, air miles, etc). |
---|
[333] | 296 | foreach ($_SESSION['_cart'][$this->_ns]['items'] as $item_id => $specs) { |
---|
[336] | 297 | $sum += isset($specs[$key]) && is_numeric($specs[$key]) ? $specs[$key] * $specs['quantity'] : 0; |
---|
[333] | 298 | } |
---|
[356] | 299 | break; |
---|
[333] | 300 | } |
---|
[611] | 301 | |
---|
[354] | 302 | return $sum; |
---|
[317] | 303 | } |
---|
| 304 | |
---|
| 305 | /** |
---|
[611] | 306 | * Resets the $_SESSION cart array. This should be executed with the same consideration |
---|
[317] | 307 | * as $auth->clear(), such as when logging out. |
---|
| 308 | */ |
---|
[468] | 309 | public function clear() |
---|
[317] | 310 | { |
---|
[611] | 311 | $app =& App::getInstance(); |
---|
| 312 | |
---|
[317] | 313 | $_SESSION['_cart'][$this->_ns] = array( |
---|
| 314 | 'items' => array(), |
---|
| 315 | ); |
---|
[611] | 316 | |
---|
| 317 | $app->logMsg(sprintf('Cleared %s cart', $this->_ns), LOG_DEBUG, __FILE__, __LINE__); |
---|
[317] | 318 | } |
---|
| 319 | } |
---|