| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | * This file is part of EC-CUBE |
|---|
| 4 | * |
|---|
| 5 | * Copyright(c) 2000-2010 LOCKON CO.,LTD. All Rights Reserved. |
|---|
| 6 | * |
|---|
| 7 | * http://www.lockon.co.jp/ |
|---|
| 8 | * |
|---|
| 9 | * This program is free software; you can redistribute it and/or |
|---|
| 10 | * modify it under the terms of the GNU General Public License |
|---|
| 11 | * as published by the Free Software Foundation; either version 2 |
|---|
| 12 | * of the License, or (at your option) any later version. |
|---|
| 13 | * |
|---|
| 14 | * This program is distributed in the hope that it will be useful, |
|---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 17 | * GNU General Public License for more details. |
|---|
| 18 | * |
|---|
| 19 | * You should have received a copy of the GNU General Public License |
|---|
| 20 | * along with this program; if not, write to the Free Software |
|---|
| 21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 22 | */ |
|---|
| 23 | |
|---|
| 24 | /* カートセッション管理クラス */ |
|---|
| 25 | class SC_CartSession { |
|---|
| 26 | var $key; |
|---|
| 27 | var $key_tmp; // ユニークIDを指定する。 |
|---|
| 28 | |
|---|
| 29 | /* コンストラクタ */ |
|---|
| 30 | function SC_CartSession($key = 'cart') { |
|---|
| 31 | SC_Utils::sfDomainSessionStart(); |
|---|
| 32 | |
|---|
| 33 | if($key == "") $key = "cart"; |
|---|
| 34 | $this->key = $key; |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | // 商品購入処理中のロック |
|---|
| 38 | function saveCurrentCart($key_tmp) { |
|---|
| 39 | $this->key_tmp = "savecart_" . $key_tmp; |
|---|
| 40 | // すでに情報がなければ現状のカート情報を記録しておく |
|---|
| 41 | if(count($_SESSION[$this->key_tmp]) == 0) { |
|---|
| 42 | $_SESSION[$this->key_tmp] = $_SESSION[$this->key]; |
|---|
| 43 | } |
|---|
| 44 | // 1世代古いコピー情報は、削除しておく |
|---|
| 45 | foreach($_SESSION as $key => $val) { |
|---|
| 46 | if($key != $this->key_tmp && ereg("^savecart_", $key)) { |
|---|
| 47 | unset($_SESSION[$key]); |
|---|
| 48 | } |
|---|
| 49 | } |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | // 商品購入中の変更があったかをチェックする。 |
|---|
| 53 | function getCancelPurchase() { |
|---|
| 54 | $ret = isset($_SESSION[$this->key]['cancel_purchase']) |
|---|
| 55 | ? $_SESSION[$this->key]['cancel_purchase'] : ""; |
|---|
| 56 | $_SESSION[$this->key]['cancel_purchase'] = false; |
|---|
| 57 | return $ret; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | // 購入処理中に商品に変更がなかったかを判定 |
|---|
| 61 | function checkChangeCart() { |
|---|
| 62 | $change = false; |
|---|
| 63 | $max = $this->getMax(); |
|---|
| 64 | for($i = 1; $i <= $max; $i++) { |
|---|
| 65 | if ($_SESSION[$this->key][$i]['quantity'] != $_SESSION[$this->key_tmp][$i]['quantity']) { |
|---|
| 66 | $change = true; |
|---|
| 67 | break; |
|---|
| 68 | } |
|---|
| 69 | if ($_SESSION[$this->key][$i]['id'] != $_SESSION[$this->key_tmp][$i]['id']) { |
|---|
| 70 | $change = true; |
|---|
| 71 | break; |
|---|
| 72 | } |
|---|
| 73 | } |
|---|
| 74 | if ($change) { |
|---|
| 75 | // 一時カートのクリア |
|---|
| 76 | unset($_SESSION[$this->key_tmp]); |
|---|
| 77 | $_SESSION[$this->key]['cancel_purchase'] = true; |
|---|
| 78 | } else { |
|---|
| 79 | $_SESSION[$this->key]['cancel_purchase'] = false; |
|---|
| 80 | } |
|---|
| 81 | return $_SESSION[$this->key]['cancel_purchase']; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | // 次に割り当てるカートのIDを取得する |
|---|
| 85 | function getNextCartID() { |
|---|
| 86 | foreach($_SESSION[$this->key] as $key => $val){ |
|---|
| 87 | $arrRet[] = $_SESSION[$this->key][$key]['cart_no']; |
|---|
| 88 | } |
|---|
| 89 | return (max($arrRet) + 1); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | /** |
|---|
| 93 | * 商品ごとの合計価格 |
|---|
| 94 | * XXX 実際には、「商品」ではなく、「カートの明細行(≒商品規格)」のような気がします。 |
|---|
| 95 | * |
|---|
| 96 | * @param integer $id |
|---|
| 97 | * @return string 商品ごとの合計価格(税込み) |
|---|
| 98 | */ |
|---|
| 99 | function getProductTotal($id) { |
|---|
| 100 | $max = $this->getMax(); |
|---|
| 101 | for($i = 0; $i <= $max; $i++) { |
|---|
| 102 | if(isset($_SESSION[$this->key][$i]['id']) |
|---|
| 103 | && $_SESSION[$this->key][$i]['id'] == $id) { |
|---|
| 104 | |
|---|
| 105 | // 税込み合計 |
|---|
| 106 | $price = $_SESSION[$this->key][$i]['price']; |
|---|
| 107 | $quantity = $_SESSION[$this->key][$i]['quantity']; |
|---|
| 108 | $pre_tax = SC_Helper_DB_Ex::sfPreTax($price); |
|---|
| 109 | $total = $pre_tax * $quantity; |
|---|
| 110 | return $total; |
|---|
| 111 | } |
|---|
| 112 | } |
|---|
| 113 | return 0; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | // 値のセット |
|---|
| 117 | function setProductValue($id, $key, $val) { |
|---|
| 118 | $max = $this->getMax(); |
|---|
| 119 | for($i = 0; $i <= $max; $i++) { |
|---|
| 120 | if(isset($_SESSION[$this->key][$i]['id']) |
|---|
| 121 | && $_SESSION[$this->key][$i]['id'] == $id) { |
|---|
| 122 | $_SESSION[$this->key][$i][$key] = $val; |
|---|
| 123 | } |
|---|
| 124 | } |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | // カート内商品の最大要素番号を取得する。 |
|---|
| 128 | function getMax() { |
|---|
| 129 | $cnt = 0; |
|---|
| 130 | $pos = 0; |
|---|
| 131 | $max = 0; |
|---|
| 132 | if (count($_SESSION[$this->key]) > 0){ |
|---|
| 133 | foreach($_SESSION[$this->key] as $key => $val) { |
|---|
| 134 | if (is_numeric($key)) { |
|---|
| 135 | if($max < $key) { |
|---|
| 136 | $max = $key; |
|---|
| 137 | } |
|---|
| 138 | } |
|---|
| 139 | } |
|---|
| 140 | } |
|---|
| 141 | return ($max); |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | // カート内商品数の合計 |
|---|
| 145 | function getTotalQuantity() { |
|---|
| 146 | $total = 0; |
|---|
| 147 | $max = $this->getMax(); |
|---|
| 148 | for($i = 0; $i <= $max; $i++) { |
|---|
| 149 | $total+= $_SESSION[$this->key][$i]['quantity']; |
|---|
| 150 | } |
|---|
| 151 | return $total; |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | // 全商品の合計価格 |
|---|
| 156 | function getAllProductsTotal() { |
|---|
| 157 | // 税込み合計 |
|---|
| 158 | $total = 0; |
|---|
| 159 | $max = $this->getMax(); |
|---|
| 160 | for($i = 0; $i <= $max; $i++) { |
|---|
| 161 | |
|---|
| 162 | if (!isset($_SESSION[$this->key][$i]['price'])) { |
|---|
| 163 | $_SESSION[$this->key][$i]['price'] = ""; |
|---|
| 164 | } |
|---|
| 165 | $price = $_SESSION[$this->key][$i]['price']; |
|---|
| 166 | |
|---|
| 167 | if (!isset($_SESSION[$this->key][$i]['quantity'])) { |
|---|
| 168 | $_SESSION[$this->key][$i]['quantity'] = ""; |
|---|
| 169 | } |
|---|
| 170 | $quantity = $_SESSION[$this->key][$i]['quantity']; |
|---|
| 171 | |
|---|
| 172 | $pre_tax = SC_Helper_DB_Ex::sfPreTax($price); |
|---|
| 173 | $total+= ($pre_tax * $quantity); |
|---|
| 174 | } |
|---|
| 175 | return $total; |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | // 全商品の合計税金 |
|---|
| 179 | function getAllProductsTax() { |
|---|
| 180 | // 税合計 |
|---|
| 181 | $total = 0; |
|---|
| 182 | $max = $this->getMax(); |
|---|
| 183 | for($i = 0; $i <= $max; $i++) { |
|---|
| 184 | $price = $_SESSION[$this->key][$i]['price']; |
|---|
| 185 | $quantity = $_SESSION[$this->key][$i]['quantity']; |
|---|
| 186 | $tax = SC_Helper_DB_Ex::sfTax($price); |
|---|
| 187 | $total+= ($tax * $quantity); |
|---|
| 188 | } |
|---|
| 189 | return $total; |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | // 全商品の合計ポイント |
|---|
| 193 | function getAllProductsPoint() { |
|---|
| 194 | // ポイント合計 |
|---|
| 195 | $total = 0; |
|---|
| 196 | if (USE_POINT !== false) { |
|---|
| 197 | $max = $this->getMax(); |
|---|
| 198 | for($i = 0; $i <= $max; $i++) { |
|---|
| 199 | $price = $_SESSION[$this->key][$i]['price']; |
|---|
| 200 | $quantity = $_SESSION[$this->key][$i]['quantity']; |
|---|
| 201 | |
|---|
| 202 | if (!isset($_SESSION[$this->key][$i]['point_rate'])) { |
|---|
| 203 | $_SESSION[$this->key][$i]['point_rate'] = ""; |
|---|
| 204 | } |
|---|
| 205 | $point_rate = $_SESSION[$this->key][$i]['point_rate']; |
|---|
| 206 | |
|---|
| 207 | if (!isset($_SESSION[$this->key][$i]['id'][0])) { |
|---|
| 208 | $_SESSION[$this->key][$i]['id'][0] = ""; |
|---|
| 209 | } |
|---|
| 210 | $id = $_SESSION[$this->key][$i]['id'][0]; |
|---|
| 211 | $point = SC_Utils_Ex::sfPrePoint($price, $point_rate, POINT_RULE, $id); |
|---|
| 212 | $total+= ($point * $quantity); |
|---|
| 213 | } |
|---|
| 214 | } |
|---|
| 215 | return $total; |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | // カートへの商品追加 |
|---|
| 219 | function addProduct($id, $quantity, $campaign_id = "") { |
|---|
| 220 | $find = false; |
|---|
| 221 | $max = $this->getMax(); |
|---|
| 222 | for($i = 0; $i <= $max; $i++) { |
|---|
| 223 | |
|---|
| 224 | if($_SESSION[$this->key][$i]['id'] == $id) { |
|---|
| 225 | $val = $_SESSION[$this->key][$i]['quantity'] + $quantity; |
|---|
| 226 | if(strlen($val) <= INT_LEN) { |
|---|
| 227 | $_SESSION[$this->key][$i]['quantity']+= $quantity; |
|---|
| 228 | if(!empty($campaign_id)){ |
|---|
| 229 | $_SESSION[$this->key][$i]['campaign_id'] = $campaign_id; |
|---|
| 230 | $_SESSION[$this->key][$i]['is_campaign'] = true; |
|---|
| 231 | } |
|---|
| 232 | } |
|---|
| 233 | $find = true; |
|---|
| 234 | } |
|---|
| 235 | } |
|---|
| 236 | if(!$find) { |
|---|
| 237 | $_SESSION[$this->key][$max+1]['id'] = $id; |
|---|
| 238 | $_SESSION[$this->key][$max+1]['quantity'] = $quantity; |
|---|
| 239 | $_SESSION[$this->key][$max+1]['cart_no'] = $this->getNextCartID(); |
|---|
| 240 | if(!empty($campaign_id)){ |
|---|
| 241 | $_SESSION[$this->key][$max+1]['campaign_id'] = $campaign_id; |
|---|
| 242 | $_SESSION[$this->key][$max+1]['is_campaign'] = true; |
|---|
| 243 | } |
|---|
| 244 | } |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | // 前頁のURLを記録しておく |
|---|
| 248 | function setPrevURL($url) { |
|---|
| 249 | // 前頁として記録しないページを指定する。 |
|---|
| 250 | $arrExclude = array( |
|---|
| 251 | "/shopping/" |
|---|
| 252 | ); |
|---|
| 253 | $exclude = false; |
|---|
| 254 | // ページチェックを行う。 |
|---|
| 255 | foreach($arrExclude as $val) { |
|---|
| 256 | if(ereg($val, $url)) { |
|---|
| 257 | $exclude = true; |
|---|
| 258 | break; |
|---|
| 259 | } |
|---|
| 260 | } |
|---|
| 261 | // 除外ページでない場合は、前頁として記録する。 |
|---|
| 262 | if(!$exclude) { |
|---|
| 263 | $_SESSION[$this->key]['prev_url'] = $url; |
|---|
| 264 | } |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | // 前頁のURLを取得する |
|---|
| 268 | function getPrevURL() { |
|---|
| 269 | return isset($_SESSION[$this->key]['prev_url']) |
|---|
| 270 | ? $_SESSION[$this->key]['prev_url'] : ""; |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | // キーが一致した商品の削除 |
|---|
| 274 | function delProductKey($keyname, $val) { |
|---|
| 275 | $max = count($_SESSION[$this->key]); |
|---|
| 276 | for($i = 0; $i < $max; $i++) { |
|---|
| 277 | if($_SESSION[$this->key][$i][$keyname] == $val) { |
|---|
| 278 | unset($_SESSION[$this->key][$i]); |
|---|
| 279 | } |
|---|
| 280 | } |
|---|
| 281 | } |
|---|
| 282 | |
|---|
| 283 | function setValue($key, $val) { |
|---|
| 284 | $_SESSION[$this->key][$key] = $val; |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | function getValue($key) { |
|---|
| 288 | return $_SESSION[$this->key][$key]; |
|---|
| 289 | } |
|---|
| 290 | |
|---|
| 291 | function getCartList() { |
|---|
| 292 | $max = $this->getMax(); |
|---|
| 293 | $arrRet = array(); |
|---|
| 294 | for($i = 0; $i <= $max; $i++) { |
|---|
| 295 | if(isset($_SESSION[$this->key][$i]['cart_no']) |
|---|
| 296 | && $_SESSION[$this->key][$i]['cart_no'] != "") { |
|---|
| 297 | $arrRet[] = $_SESSION[$this->key][$i]; |
|---|
| 298 | } |
|---|
| 299 | } |
|---|
| 300 | return $arrRet; |
|---|
| 301 | } |
|---|
| 302 | |
|---|
| 303 | // カート内にある商品IDを全て取得する |
|---|
| 304 | function getAllProductID() { |
|---|
| 305 | $max = $this->getMax(); |
|---|
| 306 | for($i = 0; $i <= $max; $i++) { |
|---|
| 307 | if($_SESSION[$this->key][$i]['cart_no'] != "") { |
|---|
| 308 | $arrRet[] = $_SESSION[$this->key][$i]['id'][0]; |
|---|
| 309 | } |
|---|
| 310 | } |
|---|
| 311 | return $arrRet; |
|---|
| 312 | } |
|---|
| 313 | |
|---|
| 314 | function delAllProducts() { |
|---|
| 315 | $max = $this->getMax(); |
|---|
| 316 | for($i = 0; $i <= $max; $i++) { |
|---|
| 317 | unset($_SESSION[$this->key][$i]); |
|---|
| 318 | } |
|---|
| 319 | } |
|---|
| 320 | |
|---|
| 321 | // 商品の削除 |
|---|
| 322 | function delProduct($cart_no) { |
|---|
| 323 | $max = $this->getMax(); |
|---|
| 324 | for($i = 0; $i <= $max; $i++) { |
|---|
| 325 | if($_SESSION[$this->key][$i]['cart_no'] == $cart_no) { |
|---|
| 326 | unset($_SESSION[$this->key][$i]); |
|---|
| 327 | } |
|---|
| 328 | } |
|---|
| 329 | } |
|---|
| 330 | |
|---|
| 331 | // 数量の増加 |
|---|
| 332 | function upQuantity($cart_no) { |
|---|
| 333 | $max = $this->getMax(); |
|---|
| 334 | for($i = 0; $i <= $max; $i++) { |
|---|
| 335 | if($_SESSION[$this->key][$i]['cart_no'] == $cart_no) { |
|---|
| 336 | if(strlen($_SESSION[$this->key][$i]['quantity'] + 1) <= INT_LEN) { |
|---|
| 337 | $_SESSION[$this->key][$i]['quantity']++; |
|---|
| 338 | } |
|---|
| 339 | } |
|---|
| 340 | } |
|---|
| 341 | } |
|---|
| 342 | |
|---|
| 343 | // 数量の減少 |
|---|
| 344 | function downQuantity($cart_no) { |
|---|
| 345 | $max = $this->getMax(); |
|---|
| 346 | for($i = 0; $i <= $max; $i++) { |
|---|
| 347 | if($_SESSION[$this->key][$i]['cart_no'] == $cart_no) { |
|---|
| 348 | if($_SESSION[$this->key][$i]['quantity'] > 1) { |
|---|
| 349 | $_SESSION[$this->key][$i]['quantity']--; |
|---|
| 350 | } |
|---|
| 351 | } |
|---|
| 352 | } |
|---|
| 353 | } |
|---|
| 354 | |
|---|
| 355 | /** |
|---|
| 356 | * カートの中のキャンペーン商品のチェック |
|---|
| 357 | * @param integer $campaign_id キャンペーンID |
|---|
| 358 | * @return boolean True:キャンペーン商品有り False:キャンペーン商品無し |
|---|
| 359 | */ |
|---|
| 360 | function chkCampaign($campaign_id){ |
|---|
| 361 | $max = $this->getMax(); |
|---|
| 362 | for($i = 0; $i <= $max; $i++) { |
|---|
| 363 | if($_SESSION[$this->key][$i]['is_campaign'] and $_SESSION[$this->key][$i]['campaign_id'] == $campaign_id) return true; |
|---|
| 364 | } |
|---|
| 365 | |
|---|
| 366 | return false; |
|---|
| 367 | } |
|---|
| 368 | |
|---|
| 369 | } |
|---|
| 370 | ?> |
|---|