1 | <?php |
---|
2 | /** |
---|
3 | * The Strangecode Codebase - a general application development framework for PHP |
---|
4 | * For details visit the project site: <http://trac.strangecode.com/codebase/> |
---|
5 | * Copyright 2001-2012 Strangecode, LLC |
---|
6 | * |
---|
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. |
---|
13 | * |
---|
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. |
---|
18 | * |
---|
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 | /** |
---|
24 | * Cart.inc.php |
---|
25 | * |
---|
26 | * Class description goes here... |
---|
27 | * |
---|
28 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
29 | * @version 1.0 |
---|
30 | * |
---|
31 | * Example of use: |
---|
32 | --------------------------------------------------------------------- |
---|
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 | 'name' => $p['name'], |
---|
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['name'], $item['quantity'], $item['price']); |
---|
65 | } |
---|
66 | break; |
---|
67 | } |
---|
68 | --------------------------------------------------------------------- |
---|
69 | */ |
---|
70 | class Cart |
---|
71 | { |
---|
72 | |
---|
73 | // Namespace of this instance. |
---|
74 | protected $_ns; |
---|
75 | |
---|
76 | // Configuration parameters for this object. |
---|
77 | protected $_params = array( |
---|
78 | ); |
---|
79 | |
---|
80 | /** |
---|
81 | * Cart constructor. |
---|
82 | */ |
---|
83 | public function __construct($namespace='') |
---|
84 | { |
---|
85 | $app =& App::getInstance(); |
---|
86 | |
---|
87 | $this->_ns = $namespace; |
---|
88 | |
---|
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 | */ |
---|
100 | public function setParam($params=null) |
---|
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 | */ |
---|
115 | public function getParam($param) |
---|
116 | { |
---|
117 | $app =& App::getInstance(); |
---|
118 | |
---|
119 | if (array_key_exists($param, $this->_params)) { |
---|
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 | /* |
---|
128 | * Add an item to the cart. If called twice for one item it will add the quantities together. |
---|
129 | * |
---|
130 | * @access public |
---|
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 (name, weight, etc) |
---|
135 | * @return float The new adjusted quantity of this item. |
---|
136 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
137 | * @version 1.0 |
---|
138 | * @since 11 Mar 2008 18:59:37 |
---|
139 | */ |
---|
140 | public function add($item_id, $price, $quantity=1, $specs=array()) |
---|
141 | { |
---|
142 | $app =& App::getInstance(); |
---|
143 | |
---|
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; |
---|
146 | |
---|
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 | )); |
---|
153 | |
---|
154 | $app->logMsg(sprintf('Added %s %s to cart (%s)', $quantity, $item_id, truncate(getDump($specs, true), 128, 'end')), LOG_DEBUG, __FILE__, __LINE__); |
---|
155 | |
---|
156 | return $quantity; |
---|
157 | } |
---|
158 | |
---|
159 | /* |
---|
160 | * Set the absolute quantity value of a specified item in a cart. |
---|
161 | * |
---|
162 | * @access public |
---|
163 | * @param |
---|
164 | * @return |
---|
165 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
166 | * @version 1.0 |
---|
167 | * @since 10 May 2008 16:42:25 |
---|
168 | */ |
---|
169 | public function setQty($item_id, $quantity) |
---|
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 | )); |
---|
180 | return true; |
---|
181 | } else { |
---|
182 | return false; |
---|
183 | } |
---|
184 | return true; |
---|
185 | } |
---|
186 | } |
---|
187 | |
---|
188 | /* |
---|
189 | * Set the absolute price value of a specified item in a cart. |
---|
190 | * |
---|
191 | * @access public |
---|
192 | * @param |
---|
193 | * @return |
---|
194 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
195 | * @version 1.0 |
---|
196 | * @since 10 May 2008 16:42:25 |
---|
197 | */ |
---|
198 | public function setPrice($item_id, $price) |
---|
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 | } |
---|
211 | |
---|
212 | /* |
---|
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 |
---|
219 | * @since 11 Mar 2008 18:59:48 |
---|
220 | */ |
---|
221 | public function remove($item_id) |
---|
222 | { |
---|
223 | $app =& App::getInstance(); |
---|
224 | |
---|
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__); |
---|
228 | return true; |
---|
229 | } else { |
---|
230 | return false; |
---|
231 | } |
---|
232 | } |
---|
233 | |
---|
234 | /* |
---|
235 | * Return the value matching key for a specified item. |
---|
236 | * |
---|
237 | * @access public |
---|
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. |
---|
241 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
242 | * @version 1.0 |
---|
243 | * @since 11 Mar 2008 18:59:55 |
---|
244 | */ |
---|
245 | public function get($item_id, $spec_key) |
---|
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 | } |
---|
252 | |
---|
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 | */ |
---|
262 | public function getList() |
---|
263 | { |
---|
264 | return $_SESSION['_cart'][$this->_ns]['items']; |
---|
265 | } |
---|
266 | |
---|
267 | /* |
---|
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. |
---|
270 | * Most commonly, $cart->sum() will be called when checking out to retrieve the total |
---|
271 | * cost of the items in the cart. |
---|
272 | * |
---|
273 | * @access public |
---|
274 | * @param string $key The spec key to sum. |
---|
275 | * @return float The total added by all the found values. |
---|
276 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
277 | * @version 1.0 |
---|
278 | * @since 11 Mar 2008 19:00:12 |
---|
279 | */ |
---|
280 | public function sum($key='extended_price') |
---|
281 | { |
---|
282 | $sum = 0; |
---|
283 | switch ($key) { |
---|
284 | case 'items' : |
---|
285 | $sum = sizeof($_SESSION['_cart'][$this->_ns]['items']); |
---|
286 | break; |
---|
287 | |
---|
288 | case 'extended_price' : |
---|
289 | foreach ($_SESSION['_cart'][$this->_ns]['items'] as $item_id => $specs) { |
---|
290 | $sum += isset($specs[$key]) && is_numeric($specs[$key]) ? $specs[$key] : 0; |
---|
291 | } |
---|
292 | break; |
---|
293 | |
---|
294 | default : |
---|
295 | // Retrieve arbitrary values stored in the cart (shipping, air miles, etc). |
---|
296 | foreach ($_SESSION['_cart'][$this->_ns]['items'] as $item_id => $specs) { |
---|
297 | $sum += isset($specs[$key]) && is_numeric($specs[$key]) ? $specs[$key] * $specs['quantity'] : 0; |
---|
298 | } |
---|
299 | break; |
---|
300 | } |
---|
301 | |
---|
302 | return $sum; |
---|
303 | } |
---|
304 | |
---|
305 | /** |
---|
306 | * Resets the $_SESSION cart array. This should be executed with the same consideration |
---|
307 | * as $auth->clear(), such as when logging out. |
---|
308 | */ |
---|
309 | public function clear() |
---|
310 | { |
---|
311 | $app =& App::getInstance(); |
---|
312 | |
---|
313 | $_SESSION['_cart'][$this->_ns] = array( |
---|
314 | 'items' => array(), |
---|
315 | ); |
---|
316 | |
---|
317 | $app->logMsg(sprintf('Cleared %s cart', $this->_ns), LOG_DEBUG, __FILE__, __LINE__); |
---|
318 | } |
---|
319 | } |
---|