1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
<?php
/*
* Session Management for PHP3
*
* Copyright (c) 1998-2000 NetUSE AG
* Boris Erdmann, Kristian Koehntopp
*
* $Id: cart.inc,v 1.3 2000/07/12 18:22:33 kk Exp $
*
*/
class Cart {
var $classname = "Cart";
var $persistent_slots = array("item", "currentItem");
var $item = array(); ## The shopping cart array($item, array("art"=>..., "num"=>..., ...))
var $currentItem = 1; ## Next item number in cart
##
## Return the position and number of units
## of an article in the cart(or false and 0,
## if it is not in there)
##
function check($art) {
if (!is_array($this->item))
return array(false, 0);
reset($this->item);
while(list($item, $attr) = each($this->item)) {
if (isset($attr["art"])
&& ($attr["art"] == $art)) {
return array($item, $attr["num"]);
}
}
return array(false, 0);
}
##
## Delete all articles from current cart
##
function reset() {
reset($this->item);
while(list($item, $attr) = each($this->item)) {
unset($this->item[$item]);
}
$this->currentItem = 1;
return true;
}
##
## Add num units of an article to the cart
## and return the item number (or false on error).
##
function add_item($art, $num) {
## Check to see if we already have some of these
list($item, $have) = $this->check($art);
## We already have them
if ($item) {
$this->item[$item]["num"] += $num;
return $item;
}
## New article
$item = $this->currentItem++;
$this->item[$item]["art"] = $art;
$this->item[$item]["num"] = $num;
return $item;
}
##
## Take num units of an article from the cart
## and return the item number (or false on error)
##
function remove_item($art, $num) {
## Check to see if we have some of these
list($item, $have) = $this->check($art);
## Can't take them out
if (!$item || ($num > $have)) {
return false;
}
## Drop the item
if ($num == $have) {
unset($this->item[$item]);
return $item;
}
## Take $num out...
$this->item[$item]["num"] -= $num;
return $item;
}
##
## Set quantity of an article in the cart to exactly $num
## and return the item number
##
function set_item($art, $num) {
## Check to see if we already have some of these
list($item, $have) = $this->check($art);
## We already have them
if ($item) {
if ($num > 0) {
$this->item[$item]["num"] = $num;
} else {
unset($this->item[$item]);
}
return $item;
}
if ($num > 0) {
## New article
$item = $this->currentItem++;
$this->item[$item]["art"] = $art;
$this->item[$item]["num"] = $num;
}
return $item;
}
#
# Return the number of articles in current cart.
#
function num_items() {
if (!is_array($this->item))
return 0;
return count($this->item);
}
function tot_arts() {
printf("Please use \$cart->num_items() instead.\n"); ## Comment out, if you want.
return $this->num_items();
}
#
# Iterator to show cart contents.
#
function show_all() {
if (!is_array($this->item) or $this->num_items() == 0) {
$this->show_empty_cart();
return false;
}
reset($this->item);
$this->show_cart_open();
while(list($item, $attr) = each($this->item)) {
$this->show_item($attr["art"], $attr["num"]);
}
$this->show_cart_close();
}
function start() {
global $sess;
$sess->register("cart");
}
##
## Dummy, to be overwritten by user.
##
function show_cart_open() {
return;
}
function show_cart_close() {
return;
}
function show_item($art, $num) {
printf("%s units of %s<br>\n", $num, $art);
}
function show_empty_cart() {
printf("Your shopping cart is empty.<br>\n");
}
}
?>
|