source: branches/version-2_5-dev/data/class/SC_CartSession.php @ 18851

Revision 18851, 11.6 KB checked in by eccuore, 13 years ago (diff)

#792(ダウンロード販売機能) 大容量ファイル対策、Safari対応、オンライン決済時のお届け先非表示

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
Line 
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/* カートセッション管理クラス */
25class SC_CartSession {
26    var $key_tmp;   // ユニークIDを指定する。
27
28    /* コンストラクタ */
29    function SC_CartSession() {
30    }
31
32    // 商品購入処理中のロック
33    function saveCurrentCart($key_tmp, $key) {
34        $this->key_tmp = "savecart_" . $key_tmp;
35        // すでに情報がなければ現状のカート情報を記録しておく
36        if(count($_SESSION[$this->key_tmp]) == 0) {
37            $_SESSION[$this->key_tmp] = $_SESSION[$key];
38        }
39        // 1世代古いコピー情報は、削除しておく
40        foreach($_SESSION as $k => $val) {
41            if($k != $this->key_tmp && preg_match("/^savecart_/", $k)) {
42                unset($_SESSION[$key][$k]);
43            }
44        }
45        $this->registerKey($key);
46    }
47
48    // 商品購入中の変更があったかをチェックする。
49    function getCancelPurchase($key) {
50        $this->addKey($key);
51        $ret = isset($_SESSION[$key]['cancel_purchase'])
52            ? $_SESSION[$key]['cancel_purchase'] : "";
53        $_SESSION[$key]['cancel_purchase'] = false;
54        return $ret;
55    }
56
57    // 購入処理中に商品に変更がなかったかを判定
58    function checkChangeCart($key) {
59        $this->addKey($key);
60        $change = false;
61        $max = $this->getMax();
62        for($i = 1; $i <= $max; $i++) {
63            if ($_SESSION[$key][$i]['quantity'] != $_SESSION[$this->key_tmp][$i]['quantity']) {
64                $change = true;
65                break;
66            }
67            if ($_SESSION[$key][$i]['id'] != $_SESSION[$this->key_tmp][$i]['id']) {
68                $change = true;
69                break;
70            }
71        }
72        if ($change) {
73            // 一時カートのクリア
74            unset($_SESSION[$this->key_tmp]);
75            $_SESSION[$key]['cancel_purchase'] = true;
76        } else {
77            $_SESSION[$key]['cancel_purchase'] = false;
78        }
79        return $_SESSION[$key]['cancel_purchase'];
80    }
81
82    // 次に割り当てるカートのIDを取得する
83    function getNextCartID($key) {
84        $this->addKey($key);
85        foreach($_SESSION[$key] as $k => $val){
86            $arrRet[] = $_SESSION[$key][$k]['cart_no'];
87        }
88        return (max($arrRet) + 1);
89    }
90
91    /**
92     * 商品ごとの合計価格
93     * XXX 実際には、「商品」ではなく、「カートの明細行(≒商品規格)」のような気がします。
94     *
95     * @param integer $id
96     * @return string 商品ごとの合計価格(税込み)
97     */
98    function getProductTotal($id, $key) {
99        $this->addKey($key);
100        $max = $this->getMax();
101        for($i = 0; $i <= $max; $i++) {
102            if(isset($_SESSION[$key][$i]['id'])
103               && $_SESSION[$key][$i]['id'] == $id) {
104
105                // 税込み合計
106                $price = $_SESSION[$key][$i]['price'];
107                $quantity = $_SESSION[$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, $k, $val, $key) {
118        $this->addKey($key);
119        $max = $this->getMax($key);
120        for($i = 0; $i <= $max; $i++) {
121            if(isset($_SESSION[$key][$i]['id'])
122               && $_SESSION[$key][$i]['id'] == $id) {
123                $_SESSION[$key][$i][$k] = $val;
124            }
125        }
126    }
127
128    // カート内商品の最大要素番号を取得する。
129    function getMax($key) {
130        $this->addKey($key);
131        $cnt = 0;
132        $pos = 0;
133        $max = 0;
134        if (count($_SESSION[$key]) > 0){
135            foreach($_SESSION[$key] as $k => $val) {
136                if (is_numeric($k)) {
137                    if($max < $k) {
138                        $max = $k;
139                    }
140                }
141            }
142        }
143        return ($max);
144    }
145
146    // カート内商品数の合計
147    function getTotalQuantity($key) {
148        $this->addKey($key);
149        $total = 0;
150        $max = $this->getMax($key);
151        for($i = 0; $i <= $max; $i++) {
152            $total+= $_SESSION[$key][$i]['quantity'];
153        }
154        return $total;
155    }
156
157
158    // 全商品の合計価格
159    function getAllProductsTotal($key) {
160        $this->addKey($key);
161        // 税込み合計
162        $total = 0;
163        $max = $this->getMax($key);
164        for($i = 0; $i <= $max; $i++) {
165
166            if (!isset($_SESSION[$key][$i]['price'])) {
167                $_SESSION[$key][$i]['price'] = "";
168            }
169            $price = $_SESSION[$key][$i]['price'];
170
171            if (!isset($_SESSION[$key][$i]['quantity'])) {
172                $_SESSION[$key][$i]['quantity'] = "";
173            }
174            $quantity = $_SESSION[$key][$i]['quantity'];
175
176            $pre_tax = SC_Helper_DB_Ex::sfPreTax($price);
177            $total+= ($pre_tax * $quantity);
178        }
179        return $total;
180    }
181
182    // 全商品の合計税金
183    function getAllProductsTax($key) {
184        $this->addKey($key);
185        // 税合計
186        $total = 0;
187        $max = $this->getMax($key);
188        for($i = 0; $i <= $max; $i++) {
189            $price = $_SESSION[$key][$i]['price'];
190            $quantity = $_SESSION[$key][$i]['quantity'];
191            $tax = SC_Helper_DB_Ex::sfTax($price);
192            $total+= ($tax * $quantity);
193        }
194        return $total;
195    }
196
197    // 全商品の合計ポイント
198    function getAllProductsPoint($key) {
199        $this->addKey($key);
200        // ポイント合計
201        $total = 0;
202        if (USE_POINT !== false) {
203            $max = $this->getMax($key);
204            for($i = 0; $i <= $max; $i++) {
205                $price = $_SESSION[$key][$i]['price'];
206                $quantity = $_SESSION[$key][$i]['quantity'];
207
208                if (!isset($_SESSION[$key][$i]['point_rate'])) {
209                    $_SESSION[$key][$i]['point_rate'] = "";
210                }
211                $point_rate = $_SESSION[$key][$i]['point_rate'];
212
213                if (!isset($_SESSION[$key][$i]['id'][0])) {
214                    $_SESSION[$key][$i]['id'][0] = "";
215                }
216                $id = $_SESSION[$key][$i]['id'][0];
217                $point = SC_Utils_Ex::sfPrePoint($price, $point_rate, POINT_RULE, $id);
218                $total+= ($point * $quantity);
219            }
220        }
221        return $total;
222    }
223
224    // カートへの商品追加
225    function addProduct($id, $quantity, $key) {
226        $this->addKey($key);
227        $find = false;
228        $max = $this->getMax($key);
229        for($i = 0; $i <= $max; $i++) {
230
231            if($_SESSION[$key][$i]['id'] == $id) {
232                $val = $_SESSION[$key][$i]['quantity'] + $quantity;
233                if(strlen($val) <= INT_LEN) {
234                    $_SESSION[$key][$i]['quantity']+= $quantity;
235                }
236                $find = true;
237            }
238        }
239        if(!$find) {
240            $_SESSION[$key][$max+1]['id'] = $id;
241            $_SESSION[$key][$max+1]['quantity'] = $quantity;
242            $_SESSION[$key][$max+1]['cart_no'] = $this->getNextCartID($key);
243        }
244    }
245
246    // 前頁のURLを記録しておく
247    function setPrevURL($url, $key) {
248        $this->addKey($key);
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[$key]['prev_url'] = $url;
264        }
265    }
266
267    // 前頁のURLを取得する
268    function getPrevURL($key) {
269        $this->addKey($key);
270        return isset($_SESSION[$key]['prev_url'])
271            ? $_SESSION[$key]['prev_url'] : "";
272    }
273
274    // キーが一致した商品の削除
275    function delProductKey($keyname, $val, $key) {
276        $this->addKey($key);
277        $max = count($_SESSION[$key]);
278        for($i = 0; $i < $max; $i++) {
279            if($_SESSION[$key][$i][$keyname] == $val) {
280                unset($_SESSION[$key][$i]);
281            }
282        }
283    }
284
285    function setValue($k, $val, $key) {
286        $this->addKey($key);
287        $_SESSION[$key][$k] = $val;
288    }
289
290    function getValue($k, $key) {
291        $this->addKey($key);
292        return $_SESSION[$key][$k];
293    }
294
295    function getCartList($key) {
296        $this->addKey($key);
297        $max = $this->getMax($key);
298        $arrRet = array();
299        for($i = 0; $i <= $max; $i++) {
300            if(isset($_SESSION[$key][$i]['cart_no'])
301               && $_SESSION[$key][$i]['cart_no'] != "") {
302                $arrRet[] = $_SESSION[$key][$i];
303            }
304        }
305        return $arrRet;
306    }
307
308    // カート内にある商品IDを全て取得する
309    function getAllProductID($key) {
310        $this->addKey($key);
311        $max = $this->getMax($key);
312        for($i = 0; $i <= $max; $i++) {
313            if($_SESSION[$key][$i]['cart_no'] != "") {
314                $arrRet[] = $_SESSION[$key][$i]['id'][0];
315            }
316        }
317        return $arrRet;
318    }
319
320    function delAllProducts($key) {
321        $this->addKey($key);
322        $max = $this->getMax($key);
323        for($i = 0; $i <= $max; $i++) {
324            unset($_SESSION[$key][$i]);
325        }
326    }
327
328    // 商品の削除
329    function delProduct($cart_no, $key) {
330        $this->addKey($key);
331        $max = $this->getMax($key);
332        for($i = 0; $i <= $max; $i++) {
333            if($_SESSION[$key][$i]['cart_no'] == $cart_no) {
334                unset($_SESSION[$key][$i]);
335            }
336        }
337    }
338
339    // 数量の増加
340    function upQuantity($cart_no, $key) {
341        $this->addKey($key);
342        $max = $this->getMax();
343        for($i = 0; $i <= $max; $i++) {
344            if($_SESSION[$key][$i]['cart_no'] == $cart_no) {
345                if(strlen($_SESSION[$key][$i]['quantity'] + 1) <= INT_LEN) {
346                    $_SESSION[$key][$i]['quantity']++;
347                }
348            }
349        }
350    }
351
352    // 数量の減少
353    function downQuantity($cart_no, $key) {
354        $this->addKey($key);
355        $max = $this->getMax();
356        for($i = 0; $i <= $max; $i++) {
357            if($_SESSION[$key][$i]['cart_no'] == $cart_no) {
358                if($_SESSION[$key][$i]['quantity'] > 1) {
359                    $_SESSION[$key][$i]['quantity']--;
360                }
361            }
362        }
363    }
364
365    function addKey($key) {
366        if (!in_array($this->keys, $key)) {
367            $this->keys[] = $key;
368        }
369    }
370
371    function getKeys() {
372        return $this->keys;
373    }
374
375    function registerKey($key) {
376        $_SESSION['cartKey'] = $key;
377    }
378
379    function unsetKey() {
380        unset($_SESSION['cartKey']);
381    }
382}
383?>
Note: See TracBrowser for help on using the repository browser.