source: branches/comu-ver2/data/class/SC_CartSession.php @ 18052

Revision 18052, 12.3 KB checked in by Seasoft, 15 years ago (diff)

・店舗基本情報の取得処理にランタイムのキャッシュ機構を設け、店舗基本情報を深く渡し回す実装を改めた。
・SC_Utils 冒頭のコメントに従い、インスタンスを生成していた処理を、Helper クラスへ移す。計算処理のみ SC_Utils に残す。

  • 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-2007 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;
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            "detail_image.php",
252            "/shopping/"
253        );
254        $exclude = false;
255        // ページチェックを行う。
256        foreach($arrExclude as $val) {
257            if(ereg($val, $url)) {
258                $exclude = true;
259                break;
260            }
261        }
262        // 除外ページでない場合は、前頁として記録する。
263        if(!$exclude) {
264            $_SESSION[$this->key]['prev_url'] = $url;
265        }
266    }
267
268    // 前頁のURLを取得する
269    function getPrevURL() {
270        return isset($_SESSION[$this->key]['prev_url'])
271            ? $_SESSION[$this->key]['prev_url'] : "";
272    }
273
274    // キーが一致した商品の削除
275    function delProductKey($keyname, $val) {
276        $max = count($_SESSION[$this->key]);
277        for($i = 0; $i < $max; $i++) {
278            if($_SESSION[$this->key][$i][$keyname] == $val) {
279                unset($_SESSION[$this->key][$i]);
280            }
281        }
282    }
283
284    function setValue($key, $val) {
285        $_SESSION[$this->key][$key] = $val;
286    }
287
288    function getValue($key) {
289        return $_SESSION[$this->key][$key];
290    }
291
292    function getCartList() {
293        $max = $this->getMax();
294        $arrRet = array();
295        for($i = 0; $i <= $max; $i++) {
296            if(isset($_SESSION[$this->key][$i]['cart_no'])
297               && $_SESSION[$this->key][$i]['cart_no'] != "") {
298                $arrRet[] = $_SESSION[$this->key][$i];
299            }
300        }
301        return $arrRet;
302    }
303
304    // カート内にある商品IDを全て取得する
305    function getAllProductID() {
306        $max = $this->getMax();
307        for($i = 0; $i <= $max; $i++) {
308            if($_SESSION[$this->key][$i]['cart_no'] != "") {
309                $arrRet[] = $_SESSION[$this->key][$i]['id'][0];
310            }
311        }
312        return $arrRet;
313    }
314
315    function delAllProducts() {
316        $max = $this->getMax();
317        for($i = 0; $i <= $max; $i++) {
318            unset($_SESSION[$this->key][$i]);
319        }
320    }
321
322    // 商品の削除
323    function delProduct($cart_no) {
324        $max = $this->getMax();
325        for($i = 0; $i <= $max; $i++) {
326            if($_SESSION[$this->key][$i]['cart_no'] == $cart_no) {
327                unset($_SESSION[$this->key][$i]);
328            }
329        }
330    }
331
332    // 数量の増加
333    function upQuantity($cart_no) {
334        $max = $this->getMax();
335        for($i = 0; $i <= $max; $i++) {
336            if($_SESSION[$this->key][$i]['cart_no'] == $cart_no) {
337                if(strlen($_SESSION[$this->key][$i]['quantity'] + 1) <= INT_LEN) {
338                    $_SESSION[$this->key][$i]['quantity']++;
339                }
340            }
341        }
342    }
343
344    // 数量の減少
345    function downQuantity($cart_no) {
346        $max = $this->getMax();
347        for($i = 0; $i <= $max; $i++) {
348            if($_SESSION[$this->key][$i]['cart_no'] == $cart_no) {
349                if($_SESSION[$this->key][$i]['quantity'] > 1) {
350                    $_SESSION[$this->key][$i]['quantity']--;
351                }
352            }
353        }
354    }
355
356    // カートの中の売り切れチェック
357    function chkSoldOut($arrCartList, $is_mobile = false){
358        foreach($arrCartList as $key => $val){
359            if($val['quantity'] == 0){
360                // 売り切れ商品をカートから削除する
361                $this->delProduct($val['cart_no']);
362                SC_Utils_Ex::sfDispSiteError(SOLD_OUT, "", true, "", $is_mobile);
363            }
364        }
365    }
366
367    /**
368     * カートの中のキャンペーン商品のチェック
369     * @param integer $campaign_id キャンペーンID
370     * @return boolean True:キャンペーン商品有り False:キャンペーン商品無し
371     */
372    function chkCampaign($campaign_id){
373        $max = $this->getMax();
374        for($i = 0; $i <= $max; $i++) {
375            if($_SESSION[$this->key][$i]['is_campaign'] and $_SESSION[$this->key][$i]['campaign_id'] == $campaign_id) return true;
376        }
377
378        return false;
379    }
380
381}
382?>
Note: See TracBrowser for help on using the repository browser.