source: branches/version-2_4/data/class/helper/SC_Helper_DB.php @ 18705

Revision 18705, 58.5 KB checked in by nanasess, 14 years ago (diff)

merged r18583, r18601, r18704

  • SALE_LIMIT_MAX が無視されているのを修正(#545)
  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id Revision Date
  • 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/**
25 * DB関連のヘルパークラス.
26 *
27 * @package Helper
28 * @author LOCKON CO.,LTD.
29 * @version $Id:SC_Helper_DB.php 15532 2007-08-31 14:39:46Z nanasess $
30 */
31class SC_Helper_DB {
32
33    // {{{ properties
34
35    /** ルートカテゴリ取得フラグ */
36    var $g_root_on;
37
38    /** ルートカテゴリID */
39    var $g_root_id;
40
41    /** 選択中カテゴリ取得フラグ */
42    var $g_category_on;
43
44    /** 選択中カテゴリID */
45    var $g_category_id;
46
47    // }}}
48    // {{{ functions
49
50    /**
51     * データベースのバージョンを所得する.
52     *
53     * @param string $dsn データソース名
54     * @return string データベースのバージョン
55     */
56    function sfGetDBVersion($dsn = "") {
57        $dbFactory = SC_DB_DBFactory_Ex::getInstance();
58        return $dbFactory->sfGetDBVersion($dsn);
59    }
60
61    /**
62     * テーブルの存在をチェックする.
63     *
64     * @param string $table_name チェック対象のテーブル名
65     * @param string $dsn データソース名
66     * @return テーブルが存在する場合 true
67     */
68    function sfTabaleExists($table_name, $dsn = "") {
69        $dbFactory = SC_DB_DBFactory_Ex::getInstance();
70        $dsn = $dbFactory->getDSN($dsn);
71
72        $objQuery = new SC_Query($dsn, true, true);
73        // 正常に接続されている場合
74        if(!$objQuery->isError()) {
75            list($db_type) = split(":", $dsn);
76            $sql = $dbFactory->getTableExistsSql();
77            $arrRet = $objQuery->getAll($sql, array($table_name));
78            if(count($arrRet) > 0) {
79                return true;
80            }
81        }
82        return false;
83    }
84
85    /**
86     * カラムの存在チェックと作成を行う.
87     *
88     * チェック対象のテーブルに, 該当のカラムが存在するかチェックする.
89     * 引数 $add が true の場合, 該当のカラムが存在しない場合は, カラムの生成を行う.
90     * カラムの生成も行う場合は, $col_type も必須となる.
91     *
92     * @param string $table_name テーブル名
93     * @param string $column_name カラム名
94     * @param string $col_type カラムのデータ型
95     * @param string $dsn データソース名
96     * @param bool $add カラムの作成も行う場合 true
97     * @return bool カラムが存在する場合とカラムの生成に成功した場合 true,
98     *               テーブルが存在しない場合 false,
99     *               引数 $add == false でカラムが存在しない場合 false
100     */
101    function sfColumnExists($table_name, $col_name, $col_type = "", $dsn = "", $add = false) {
102        $dbFactory = SC_DB_DBFactory_Ex::getInstance();
103        $dsn = $dbFactory->getDSN($dsn);
104
105        // テーブルが無ければエラー
106        if(!$this->sfTabaleExists($table_name, $dsn)) return false;
107
108        $objQuery = new SC_Query($dsn, true, true);
109        // 正常に接続されている場合
110        if(!$objQuery->isError()) {
111            list($db_type) = split(":", $dsn);
112
113            // カラムリストを取得
114            $arrRet = $dbFactory->sfGetColumnList($table_name);
115            if(count($arrRet) > 0) {
116                if(in_array($col_name, $arrRet)){
117                    return true;
118                }
119            }
120        }
121
122        // カラムを追加する
123        if($add){
124            $objQuery->query("ALTER TABLE $table_name ADD $col_name $col_type ");
125            return true;
126        }
127        return false;
128    }
129
130    /**
131     * インデックスの存在チェックと作成を行う.
132     *
133     * チェック対象のテーブルに, 該当のインデックスが存在するかチェックする.
134     * 引数 $add が true の場合, 該当のインデックスが存在しない場合は, インデックスの生成を行う.
135     * インデックスの生成も行う場合で, DB_TYPE が mysql の場合は, $length も必須となる.
136     *
137     * @param string $table_name テーブル名
138     * @param string $column_name カラム名
139     * @param string $index_name インデックス名
140     * @param integer|string $length インデックスを作成するデータ長
141     * @param string $dsn データソース名
142     * @param bool $add インデックスの生成もする場合 true
143     * @return bool インデックスが存在する場合とインデックスの生成に成功した場合 true,
144     *               テーブルが存在しない場合 false,
145     *               引数 $add == false でインデックスが存在しない場合 false
146     */
147    function sfIndexExists($table_name, $col_name, $index_name, $length = "", $dsn = "", $add = false) {
148        $dbFactory = SC_DB_DBFactory_Ex::getInstance();
149        $dsn = $dbFactory->getDSN($dsn);
150
151        // テーブルが無ければエラー
152        if (!$this->sfTabaleExists($table_name, $dsn)) return false;
153
154        $objQuery = new SC_Query($dsn, true, true);
155        $arrRet = $dbFactory->getTableIndex($index_name, $table_name);
156
157        // すでにインデックスが存在する場合
158        if(count($arrRet) > 0) {
159            return true;
160        }
161
162        // インデックスを作成する
163        if($add){
164            $dbFactory->createTableIndex($index_name, $table_name, $col_name, $length());
165            return true;
166        }
167        return false;
168    }
169
170    /**
171     * データの存在チェックを行う.
172     *
173     * @param string $table_name テーブル名
174     * @param string $where データを検索する WHERE 句
175     * @param string $dsn データソース名
176     * @param string $sql データの追加を行う場合の SQL文
177     * @param bool $add データの追加も行う場合 true
178     * @return bool データが存在する場合 true, データの追加に成功した場合 true,
179     *               $add == false で, データが存在しない場合 false
180     */
181    function sfDataExists($table_name, $where, $arrval, $dsn = "", $sql = "", $add = false) {
182        $dbFactory = SC_DB_DBFactory_Ex::getInstance();
183        $dsn = $dbFactory->getDSN($dsn);
184
185        $objQuery = new SC_Query($dsn, true, true);
186        $count = $objQuery->count($table_name, $where, $arrval);
187
188        if($count > 0) {
189            $ret = true;
190        } else {
191            $ret = false;
192        }
193        // データを追加する
194        if(!$ret && $add) {
195            $objQuery->exec($sql);
196        }
197        return $ret;
198    }
199
200    /**
201     * 店舗基本情報を取得する.
202     *
203     * @return array 店舗基本情報の配列
204     */
205    function sf_getBasisData() {
206        $objQuery = new SC_Query();
207        $arrRet = $objQuery->select('*', 'dtb_baseinfo');
208
209        if (isset($arrRet[0])) return $arrRet[0];
210
211        return array();
212    }
213
214    /* 選択中のアイテムのルートカテゴリIDを取得する */
215    function sfGetRootId() {
216
217        if(!$this->g_root_on)   {
218            $this->g_root_on = true;
219            $objQuery = new SC_Query();
220
221            if (!isset($_GET['product_id'])) $_GET['product_id'] = "";
222            if (!isset($_GET['category_id'])) $_GET['category_id'] = "";
223
224            if(!empty($_GET['product_id']) || !empty($_GET['category_id'])) {
225                // 選択中のカテゴリIDを判定する
226                $category_id = $this->sfGetCategoryId($_GET['product_id'], $_GET['category_id']);
227                // ROOTカテゴリIDの取得
228                $arrRet = $this->sfGetParents($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $category_id);
229                $root_id = isset($arrRet[0]) ? $arrRet[0] : "";
230            } else {
231                // ROOTカテゴリIDをなしに設定する
232                $root_id = "";
233            }
234            $this->g_root_id = $root_id;
235        }
236        return $this->g_root_id;
237    }
238
239    /**
240     * 商品規格情報を取得する.
241     *
242     * @param array $arrID 規格ID
243     * @return array 規格情報の配列
244     */
245    function sfGetProductsClass($arrID) {
246        list($product_id, $classcategory_id1, $classcategory_id2) = $arrID;
247
248        if($classcategory_id1 == "") {
249            $classcategory_id1 = '0';
250        }
251        if($classcategory_id2 == "") {
252            $classcategory_id2 = '0';
253        }
254
255        // 商品規格取得
256        $objQuery = new SC_Query();
257        $col = "product_id, deliv_fee, name, product_code, main_list_image, main_image, price01, price02, point_rate, product_class_id, classcategory_id1, classcategory_id2, class_id1, class_id2, stock, stock_unlimited, sale_limit, sale_unlimited";
258        $table = "vw_product_class AS prdcls";
259        $where = "product_id = ? AND classcategory_id1 = ? AND classcategory_id2 = ? AND status = 1";
260        $objQuery->setorder("rank1 DESC, rank2 DESC");
261        $arrRet = $objQuery->select($col, $table, $where, array($product_id, $classcategory_id1, $classcategory_id2));
262        return $arrRet[0];
263    }
264
265    /**
266     * 支払い方法を取得する.
267     *
268     * @return void
269     */
270    function sfGetPayment() {
271        $objQuery = new SC_Query();
272        // 購入金額が条件額以下の項目を取得
273        $where = "del_flg = 0";
274        $objQuery->setorder("fix, rank DESC");
275        $arrRet = $objQuery->select("payment_id, payment_method, rule", "dtb_payment", $where);
276        return $arrRet;
277    }
278
279    /**
280     * カート内商品の集計処理を行う.
281     *
282     * @param LC_Page $objPage ページクラスのインスタンス
283     * @param SC_CartSession $objCartSess カートセッションのインスタンス
284     * @param array $arrInfo 商品情報の配列
285     * @return LC_Page 集計処理後のページクラスインスタンス
286     */
287    function sfTotalCart(&$objPage, $objCartSess, $arrInfo) {
288
289        // 規格名一覧
290        $arrClassName = $this->sfGetIDValueList("dtb_class", "class_id", "name");
291        // 規格分類名一覧
292        $arrClassCatName = $this->sfGetIDValueList("dtb_classcategory", "classcategory_id", "name");
293
294        $objPage->tpl_total_pretax = 0;     // 費用合計(税込み)
295        $objPage->tpl_total_tax = 0;        // 消費税合計
296        if (USE_POINT === true) {
297            $objPage->tpl_total_point = 0;      // ポイント合計
298        }
299
300        // カート内情報の取得
301        $arrCart = $objCartSess->getCartList();
302        $max = count($arrCart);
303        $cnt = 0;
304
305        for ($i = 0; $i < $max; $i++) {
306            // 商品規格情報の取得
307            $arrData = $this->sfGetProductsClass($arrCart[$i]['id']);
308            $limit = "";
309            // DBに存在する商品
310            if (count($arrData) > 0) {
311
312                // 購入制限数を求める。
313                if ($arrData['stock_unlimited'] != '1' && $arrData['sale_unlimited'] != '1') {
314                    $limit = min($arrData['sale_limit'], $arrData['stock']);
315                } elseif ($arrData['sale_unlimited'] != '1') {
316                    $limit = $arrData['sale_limit'];
317                } else {
318                    // 購入制限なしの場合は、SALE_LIMIT_MAXが最大購入個数
319                    // 但し、SALE_LIMIT_MAXは、有効な整数値でないと機能しない
320
321                    if ($arrData['stock_unlimited'] != '1') {
322                        // 在庫制限がある場合は、SALE_LIMIT_MAXと在庫数の小さい方が購入可能最大数
323                        if (SALE_LIMIT_MAX > 0) {
324                            $limit = min(SALE_LIMIT_MAX, $arrData['stock']);
325                        } else {
326                            $limit = $arrData['stock'];
327                        }
328                    } else {
329                        if (SALE_LIMIT_MAX > 0) {
330                            $limit = SALE_LIMIT_MAX;
331                        }
332                    }
333                }
334
335                if($limit != "" && $limit < $arrCart[$i]['quantity']) {
336                    // カート内商品数を制限に合わせる
337                    $objCartSess->setProductValue($arrCart[$i]['id'], 'quantity', $limit);
338                    $quantity = $limit;
339                    $objPage->tpl_message = "※「" . $arrData['name'] . "」は販売制限しております、一度にこれ以上の購入はできません。";
340                } else {
341                    $quantity = $arrCart[$i]['quantity'];
342                }
343
344                $objPage->arrProductsClass[$cnt] = $arrData;
345                $objPage->arrProductsClass[$cnt]['quantity'] = $quantity;
346                $objPage->arrProductsClass[$cnt]['cart_no'] = $arrCart[$i]['cart_no'];
347                $objPage->arrProductsClass[$cnt]['class_name1'] =
348                    isset($arrClassName[$arrData['class_id1']])
349                        ? $arrClassName[$arrData['class_id1']] : "";
350
351                $objPage->arrProductsClass[$cnt]['class_name2'] =
352                    isset($arrClassName[$arrData['class_id2']])
353                        ? $arrClassName[$arrData['class_id2']] : "";
354
355                $objPage->arrProductsClass[$cnt]['classcategory_name1'] =
356                    $arrClassCatName[$arrData['classcategory_id1']];
357
358                $objPage->arrProductsClass[$cnt]['classcategory_name2'] =
359                    $arrClassCatName[$arrData['classcategory_id2']];
360
361                // 画像サイズ
362                $main_image_path = IMAGE_SAVE_DIR . basename($objPage->arrProductsClass[$cnt]["main_image"]);
363                if(file_exists($main_image_path)) {
364                    list($image_width, $image_height) = getimagesize($main_image_path);
365                } else {
366                    $image_width = 0;
367                    $image_height = 0;
368                }
369
370                $objPage->arrProductsClass[$cnt]["tpl_image_width"] = $image_width + 60;
371                $objPage->arrProductsClass[$cnt]["tpl_image_height"] = $image_height + 80;
372                // 価格の登録
373                if ($arrData['price02'] != "") {
374                    $objCartSess->setProductValue($arrCart[$i]['id'], 'price', $arrData['price02']);
375                    $objPage->arrProductsClass[$cnt]['uniq_price'] = $arrData['price02'];
376                } else {
377                    $objCartSess->setProductValue($arrCart[$i]['id'], 'price', $arrData['price01']);
378                    $objPage->arrProductsClass[$cnt]['uniq_price'] = $arrData['price01'];
379                }
380                // ポイント付与率の登録
381                if (USE_POINT === true) {
382                    $objCartSess->setProductValue($arrCart[$i]['id'], 'point_rate', $arrData['point_rate']);
383                }
384                // 商品ごとの合計金額
385                $objPage->arrProductsClass[$cnt]['total_pretax'] = $objCartSess->getProductTotal($arrInfo, $arrCart[$i]['id']);
386                // 送料の合計を計算する
387                $objPage->tpl_total_deliv_fee+= ($arrData['deliv_fee'] * $arrCart[$i]['quantity']);
388                $cnt++;
389            } else { // DBに商品が見つからない場合はカート商品の削除
390                $objPage->tpl_message .= "※申し訳ございませんが、ご購入の直前で売り切れた商品があります。該当商品をカートから削除いたしました。\n";
391                // カート商品の削除
392                $objCartSess->delProductKey('id', $arrCart[$i]['id']);
393            }
394        }
395
396        // 全商品合計金額(税込み)
397        $objPage->tpl_total_pretax = $objCartSess->getAllProductsTotal($arrInfo);
398        // 全商品合計消費税
399        $objPage->tpl_total_tax = $objCartSess->getAllProductsTax($arrInfo);
400        // 全商品合計ポイント
401        if (USE_POINT === true) {
402            $objPage->tpl_total_point = $objCartSess->getAllProductsPoint();
403        }
404
405        return $objPage;
406    }
407
408    /**
409     * 受注一時テーブルへの書き込み処理を行う.
410     *
411     * @param string $uniqid ユニークID
412     * @param array $sqlval SQLの値の配列
413     * @return void
414     */
415    function sfRegistTempOrder($uniqid, $sqlval) {
416        if($uniqid != "") {
417            // 既存データのチェック
418            $objQuery = new SC_Query();
419            $where = "order_temp_id = ?";
420            $cnt = $objQuery->count("dtb_order_temp", $where, array($uniqid));
421            // 既存データがない場合
422            if ($cnt == 0) {
423                // 初回書き込み時に会員の登録済み情報を取り込む
424                $sqlval = $this->sfGetCustomerSqlVal($uniqid, $sqlval);
425                $sqlval['create_date'] = "now()";
426                $objQuery->insert("dtb_order_temp", $sqlval);
427            } else {
428                $objQuery->update("dtb_order_temp", $sqlval, $where, array($uniqid));
429            }
430        }
431    }
432
433    /**
434     * 会員情報から SQL文の値を生成する.
435     *
436     * @param string $uniqid ユニークID
437     * @param array $sqlval SQL の値の配列
438     * @return array 会員情報を含んだ SQL の値の配列
439     */
440    function sfGetCustomerSqlVal($uniqid, $sqlval) {
441        $objCustomer = new SC_Customer();
442        // 会員情報登録処理
443        if ($objCustomer->isLoginSuccess(true)) {
444            // 登録データの作成
445            $sqlval['order_temp_id'] = $uniqid;
446            $sqlval['update_date'] = 'Now()';
447            $sqlval['customer_id'] = $objCustomer->getValue('customer_id');
448            $sqlval['order_name01'] = $objCustomer->getValue('name01');
449            $sqlval['order_name02'] = $objCustomer->getValue('name02');
450            $sqlval['order_kana01'] = $objCustomer->getValue('kana01');
451            $sqlval['order_kana02'] = $objCustomer->getValue('kana02');
452            $sqlval['order_sex'] = $objCustomer->getValue('sex');
453            $sqlval['order_zip01'] = $objCustomer->getValue('zip01');
454            $sqlval['order_zip02'] = $objCustomer->getValue('zip02');
455            $sqlval['order_pref'] = $objCustomer->getValue('pref');
456            $sqlval['order_addr01'] = $objCustomer->getValue('addr01');
457            $sqlval['order_addr02'] = $objCustomer->getValue('addr02');
458            $sqlval['order_tel01'] = $objCustomer->getValue('tel01');
459            $sqlval['order_tel02'] = $objCustomer->getValue('tel02');
460            $sqlval['order_tel03'] = $objCustomer->getValue('tel03');
461            if (defined('MOBILE_SITE')) {
462                $email_mobile = $objCustomer->getValue('email_mobile');
463                if (empty($email_mobile)) {
464                    $sqlval['order_email'] = $objCustomer->getValue('email');
465                } else {
466                    $sqlval['order_email'] = $email_mobile;
467                }
468            } else {
469                $sqlval['order_email'] = $objCustomer->getValue('email');
470            }
471            $sqlval['order_job'] = $objCustomer->getValue('job');
472            $sqlval['order_birth'] = $objCustomer->getValue('birth');
473        }
474        return $sqlval;
475    }
476
477    /**
478     * 会員編集登録処理を行う.
479     *
480     * @param array $array パラメータの配列
481     * @param array $arrRegistColumn 登録するカラムの配列
482     * @return void
483     */
484    function sfEditCustomerData($array, $arrRegistColumn) {
485        $objQuery = new SC_Query();
486
487        foreach ($arrRegistColumn as $data) {
488            if ($data["column"] != "password") {
489                if($array[ $data['column'] ] != "") {
490                    $arrRegist[ $data["column"] ] = $array[ $data["column"] ];
491                } else {
492                    $arrRegist[ $data['column'] ] = NULL;
493                }
494            }
495        }
496        if (strlen($array["year"]) > 0 && strlen($array["month"]) > 0 && strlen($array["day"]) > 0) {
497            $arrRegist["birth"] = $array["year"] ."/". $array["month"] ."/". $array["day"] ." 00:00:00";
498        } else {
499            $arrRegist["birth"] = NULL;
500        }
501
502        //-- パスワードの更新がある場合は暗号化。(更新がない場合はUPDATE文を構成しない)
503        if ($array["password"] != DEFAULT_PASSWORD) $arrRegist["password"] = sha1($array["password"] . ":" . AUTH_MAGIC);
504        $arrRegist["update_date"] = "NOW()";
505
506        //-- 編集登録実行
507        $objQuery->begin();
508        $objQuery->update("dtb_customer", $arrRegist, "customer_id = ? ", array($array['customer_id']));
509        $objQuery->commit();
510    }
511
512    /**
513     * 注文番号、利用ポイント、加算ポイントから最終ポイントを取得する.
514     *
515     * @param integer $order_id 注文番号
516     * @param integer $use_point 利用ポイント
517     * @param integer $add_point 加算ポイント
518     * @return array 最終ポイントの配列
519     */
520    function sfGetCustomerPoint($order_id, $use_point, $add_point) {
521        $objQuery = new SC_Query();
522        $arrRet = $objQuery->select("customer_id", "dtb_order", "order_id = ?", array($order_id));
523        $customer_id = $arrRet[0]['customer_id'];
524        if($customer_id != "" && $customer_id >= 1) {
525            if (USE_POINT === true) {
526                $arrRet = $objQuery->select("point", "dtb_customer", "customer_id = ?", array($customer_id));
527                $point = $arrRet[0]['point'];
528                $total_point = $arrRet[0]['point'] - $use_point + $add_point;
529            } else {
530                $total_point = "";
531                $point = "";
532            }
533        } else {
534            $total_point = 0;
535            $point = 0;
536        }
537        return array($point, $total_point);
538    }
539
540    /**
541     * 顧客番号、利用ポイント、加算ポイントから最終ポイントを取得する.
542     *
543     * @param integer $customer_id 顧客番号
544     * @param integer $use_point 利用ポイント
545     * @param integer $add_point 加算ポイント
546     * @return array 最終ポイントの配列
547     */
548    function sfGetCustomerPointFromCid($customer_id, $use_point, $add_point) {
549        $objQuery = new SC_Query();
550        if (USE_POINT === true) {
551                $arrRet = $objQuery->select("point", "dtb_customer", "customer_id = ?", array($customer_id));
552                $point = $arrRet[0]['point'];
553                $total_point = $arrRet[0]['point'] - $use_point + $add_point;
554        } else {
555            $total_point = 0;
556            $point = 0;
557        }
558        return array($point, $total_point);
559    }
560    /**
561     * カテゴリツリーの取得を行う.
562     *
563     * @param integer $parent_category_id 親カテゴリID
564     * @param bool $count_check 登録商品数のチェックを行う場合 true
565     * @return array カテゴリツリーの配列
566     */
567    function sfGetCatTree($parent_category_id, $count_check = false) {
568        $objQuery = new SC_Query();
569        $col = "";
570        $col .= " cat.category_id,";
571        $col .= " cat.category_name,";
572        $col .= " cat.parent_category_id,";
573        $col .= " cat.level,";
574        $col .= " cat.rank,";
575        $col .= " cat.creator_id,";
576        $col .= " cat.create_date,";
577        $col .= " cat.update_date,";
578        $col .= " cat.del_flg, ";
579        $col .= " ttl.product_count";
580        $from = "dtb_category as cat left join dtb_category_total_count as ttl on ttl.category_id = cat.category_id";
581        // 登録商品数のチェック
582        if($count_check) {
583            $where = "del_flg = 0 AND product_count > 0";
584        } else {
585            $where = "del_flg = 0";
586        }
587        $objQuery->setoption("ORDER BY rank DESC");
588        $arrRet = $objQuery->select($col, $from, $where);
589
590        $arrParentID = $this->sfGetParents($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $parent_category_id);
591
592        foreach($arrRet as $key => $array) {
593            foreach($arrParentID as $val) {
594                if($array['category_id'] == $val) {
595                    $arrRet[$key]['display'] = 1;
596                    break;
597                }
598            }
599        }
600
601        return $arrRet;
602    }
603
604    /**
605     * カテゴリツリーの取得を複数カテゴリーで行う.
606     *
607     * @param integer $product_id 商品ID
608     * @param bool $count_check 登録商品数のチェックを行う場合 true
609     * @return array カテゴリツリーの配列
610     */
611    function sfGetMultiCatTree($product_id, $count_check = false) {
612        $objQuery = new SC_Query();
613        $col = "";
614        $col .= " cat.category_id,";
615        $col .= " cat.category_name,";
616        $col .= " cat.parent_category_id,";
617        $col .= " cat.level,";
618        $col .= " cat.rank,";
619        $col .= " cat.creator_id,";
620        $col .= " cat.create_date,";
621        $col .= " cat.update_date,";
622        $col .= " cat.del_flg, ";
623        $col .= " ttl.product_count";
624        $from = "dtb_category as cat left join dtb_category_total_count as ttl on ttl.category_id = cat.category_id";
625        // 登録商品数のチェック
626        if($count_check) {
627            $where = "del_flg = 0 AND product_count > 0";
628        } else {
629            $where = "del_flg = 0";
630        }
631        $objQuery->setoption("ORDER BY rank DESC");
632        $arrRet = $objQuery->select($col, $from, $where);
633
634        $arrCategory_id = $this->sfGetCategoryId($product_id);
635
636        $arrCatTree = array();
637        foreach ($arrCategory_id as $pkey => $parent_category_id) {
638            $arrParentID = $this->sfGetParents($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $parent_category_id);
639
640            foreach($arrParentID as $pid) {
641                foreach($arrRet as $key => $array) {
642                    if($array['category_id'] == $pid) {
643                        $arrCatTree[$pkey][] = $arrRet[$key];
644                        break;
645                    }
646                }
647            }
648        }
649
650        return $arrCatTree;
651    }
652
653    /**
654     * 親カテゴリーを連結した文字列を取得する.
655     *
656     * @param integer $category_id カテゴリID
657     * @return string 親カテゴリーを連結した文字列
658     */
659    function sfGetCatCombName($category_id){
660        // 商品が属するカテゴリIDを縦に取得
661        $objQuery = new SC_Query();
662        $arrCatID = $this->sfGetParents($objQuery, "dtb_category", "parent_category_id", "category_id", $category_id);
663        $ConbName = "";
664
665        // カテゴリー名称を取得する
666        foreach($arrCatID as $key => $val){
667            $sql = "SELECT category_name FROM dtb_category WHERE category_id = ?";
668            $arrVal = array($val);
669            $CatName = $objQuery->getOne($sql,$arrVal);
670            $ConbName .= $CatName . ' | ';
671        }
672        // 最後の | をカットする
673        $ConbName = substr_replace($ConbName, "", strlen($ConbName) - 2, 2);
674
675        return $ConbName;
676    }
677
678    /**
679     * 指定したカテゴリーIDの大カテゴリーを取得する.
680     *
681     * @param integer $category_id カテゴリID
682     * @return array 指定したカテゴリーIDの大カテゴリー
683     */
684    function sfGetFirstCat($category_id){
685        // 商品が属するカテゴリIDを縦に取得
686        $objQuery = new SC_Query();
687        $arrRet = array();
688        $arrCatID = $this->sfGetParents($objQuery, "dtb_category", "parent_category_id", "category_id", $category_id);
689        $arrRet['id'] = $arrCatID[0];
690
691        // カテゴリー名称を取得する
692        $sql = "SELECT category_name FROM dtb_category WHERE category_id = ?";
693        $arrVal = array($arrRet['id']);
694        $arrRet['name'] = $objQuery->getOne($sql,$arrVal);
695
696        return $arrRet;
697    }
698
699    /**
700     * カテゴリツリーの取得を行う.
701     *
702     * $products_check:true商品登録済みのものだけ取得する
703     *
704     * @param string $addwhere 追加する WHERE 句
705     * @param bool $products_check 商品の存在するカテゴリのみ取得する場合 true
706     * @param string $head カテゴリ名のプレフィックス文字列
707     * @return array カテゴリツリーの配列
708     */
709    function sfGetCategoryList($addwhere = "", $products_check = false, $head = CATEGORY_HEAD) {
710        $objQuery = new SC_Query();
711        $where = "del_flg = 0";
712
713        if($addwhere != "") {
714            $where.= " AND $addwhere";
715        }
716
717        $objQuery->setoption("ORDER BY rank DESC");
718
719        if($products_check) {
720            $col = "T1.category_id, category_name, level";
721            $from = "dtb_category AS T1 LEFT JOIN dtb_category_total_count AS T2 ON T1.category_id = T2.category_id";
722            $where .= " AND product_count > 0";
723        } else {
724            $col = "category_id, category_name, level";
725            $from = "dtb_category";
726        }
727
728        $arrRet = $objQuery->select($col, $from, $where);
729
730        $max = count($arrRet);
731        for($cnt = 0; $cnt < $max; $cnt++) {
732            $id = $arrRet[$cnt]['category_id'];
733            $name = $arrRet[$cnt]['category_name'];
734            $arrList[$id] = str_repeat($head, $arrRet[$cnt]['level']) . $name;
735        }
736        return $arrList;
737    }
738
739    /**
740     * カテゴリーツリーの取得を行う.
741     *
742     * 親カテゴリの Value=0 を対象とする
743     *
744     * @param bool $parent_zero 親カテゴリの Value=0 の場合 true
745     * @return array カテゴリツリーの配列
746     */
747    function sfGetLevelCatList($parent_zero = true) {
748        $objQuery = new SC_Query();
749        $col = "category_id, parent_category_id, category_name, level";
750        $where = "del_flg = 0";
751        $objQuery->setoption("ORDER BY rank DESC");
752        $arrRet = $objQuery->select($col, "dtb_category", $where);
753        $max = count($arrRet);
754
755        for($cnt = 0; $cnt < $max; $cnt++) {
756            if($parent_zero) {
757                if($arrRet[$cnt]['level'] == LEVEL_MAX) {
758                    $arrValue[$cnt] = $arrRet[$cnt]['category_id'];
759                } else {
760                    $arrValue[$cnt] = "";
761                }
762            } else {
763                $arrValue[$cnt] = $arrRet[$cnt]['category_id'];
764            }
765
766            $arrOutput[$cnt] = "";
767
768            // 子カテゴリから親カテゴリを検索
769            $parent_category_id = $arrRet[$cnt]['parent_category_id'];
770            for($cat_cnt = $arrRet[$cnt]['level']; $cat_cnt > 1; $cat_cnt--) {
771
772                foreach ($arrRet as $arrCat) {
773                    // 親が見つかったら順番に代入
774                    if ($arrCat['category_id'] == $parent_category_id) {
775
776                        $arrOutput[$cnt] = CATEGORY_HEAD
777                            . $arrCat['category_name'] . $arrOutput[$cnt];
778                        $parent_category_id = $arrCat['parent_category_id'];
779                    }
780                }
781            }
782            $arrOutput[$cnt].= CATEGORY_HEAD . $arrRet[$cnt]['category_name'];
783        }
784
785        return array($arrValue, $arrOutput);
786    }
787
788    /**
789     * 選択中の商品のカテゴリを取得する.
790     *
791     * @param integer $product_id プロダクトID
792     * @param integer $category_id カテゴリID
793     * @return array 選択中の商品のカテゴリIDの配列
794     *
795     */
796    function sfGetCategoryId($product_id, $category_id = 0, $closed = false) {
797        if ($closed) {
798            $status = "";
799        } else {
800            $status = "status = 1";
801        }
802
803        if(!$this->g_category_on) {
804            $this->g_category_on = true;
805            $category_id = (int) $category_id;
806            $product_id = (int) $product_id;
807            if(SC_Utils_Ex::sfIsInt($category_id) && $this->sfIsRecord("dtb_category","category_id", $category_id)) {
808                $this->g_category_id = array($category_id);
809            } else if (SC_Utils_Ex::sfIsInt($product_id) && $this->sfIsRecord("dtb_products","product_id", $product_id, $status)) {
810                $objQuery = new SC_Query();
811                $where = "product_id = ?";
812                $category_id = $objQuery->getCol("dtb_product_categories", "category_id", "product_id = ?", array($product_id));
813                $this->g_category_id = $category_id;
814            } else {
815                // 不正な場合は、空の配列を返す。
816                $this->g_category_id = array();
817            }
818        }
819        return $this->g_category_id;
820    }
821
822    /**
823     * 商品をカテゴリの先頭に追加する.
824     *
825     * @param integer $category_id カテゴリID
826     * @param integer $product_id プロダクトID
827     * @return void
828     */
829    function addProductBeforCategories($category_id, $product_id) {
830
831        $sqlval = array("category_id" => $category_id,
832                        "product_id" => $product_id);
833
834        $objQuery = new SC_Query();
835
836        // 現在の商品カテゴリを取得
837        $arrCat = $objQuery->select("product_id, category_id, rank",
838                                    "dtb_product_categories",
839                                    "category_id = ?",
840                                    array($category_id));
841
842        $max = "0";
843        foreach ($arrCat as $val) {
844            // 同一商品が存在する場合は登録しない
845            if ($val["product_id"] == $product_id) {
846                return;
847            }
848            // 最上位ランクを取得
849            $max = ($max < $val["rank"]) ? $val["rank"] : $max;
850        }
851        $sqlval["rank"] = $max + 1;
852        $objQuery->insert("dtb_product_categories", $sqlval);
853    }
854
855    /**
856     * 商品をカテゴリの末尾に追加する.
857     *
858     * @param integer $category_id カテゴリID
859     * @param integer $product_id プロダクトID
860     * @return void
861     */
862    function addProductAfterCategories($category_id, $product_id) {
863        $sqlval = array("category_id" => $category_id,
864                        "product_id" => $product_id);
865
866        $objQuery = new SC_Query();
867
868        // 現在の商品カテゴリを取得
869        $arrCat = $objQuery->select("product_id, category_id, rank",
870                                    "dtb_product_categories",
871                                    "category_id = ?",
872                                    array($category_id));
873
874        $min = 0;
875        foreach ($arrCat as $val) {
876            // 同一商品が存在する場合は登録しない
877            if ($val["product_id"] == $product_id) {
878                return;
879            }
880            // 最下位ランクを取得
881            $min = ($min < $val["rank"]) ? $val["rank"] : $min;
882        }
883        $sqlval["rank"] = $min;
884        $objQuery->insert("dtb_product_categories", $sqlval);
885    }
886
887    /**
888     * 商品をカテゴリから削除する.
889     *
890     * @param integer $category_id カテゴリID
891     * @param integer $product_id プロダクトID
892     * @return void
893     */
894    function removeProductByCategories($category_id, $product_id) {
895        $sqlval = array("category_id" => $category_id,
896                        "product_id" => $product_id);
897        $objQuery = new SC_Query();
898        $objQuery->delete("dtb_product_categories",
899                          "category_id = ? AND product_id = ?", $sqlval);
900    }
901
902    /**
903     * 商品カテゴリを更新する.
904     *
905     * @param array $arrCategory_id 登録するカテゴリIDの配列
906     * @param integer $product_id プロダクトID
907     * @return void
908     */
909    function updateProductCategories($arrCategory_id, $product_id) {
910        $objQuery = new SC_Query();
911
912        // 現在のカテゴリ情報を取得
913        $arrCurrentCat = $objQuery->select("product_id, category_id, rank",
914                                           "dtb_product_categories",
915                                           "product_id = ?",
916                                           array($product_id));
917
918        // 登録するカテゴリ情報と比較
919        foreach ($arrCurrentCat as $val) {
920
921            // 登録しないカテゴリを削除
922            if (!in_array($val["category_id"], $arrCategory_id)) {
923                $this->removeProductByCategories($val["category_id"], $product_id);
924            }
925        }
926
927        // カテゴリを登録
928        foreach ($arrCategory_id as $category_id) {
929            $this->addProductBeforCategories($category_id, $product_id);
930        }
931    }
932
933    /**
934     * カテゴリ数の登録を行う.
935     *
936     * @param SC_Query $objQuery SC_Query インスタンス
937     * @return void
938     */
939    function sfCategory_Count($objQuery){
940        $sql = "";
941
942        //テーブル内容の削除
943        $objQuery->query("DELETE FROM dtb_category_count");
944        $objQuery->query("DELETE FROM dtb_category_total_count");
945
946        //各カテゴリ内の商品数を数えて格納
947        $sql = " INSERT INTO dtb_category_count(category_id, product_count, create_date) ";
948        $sql .= " SELECT T1.category_id, count(T2.category_id), now() ";
949        $sql .= " FROM dtb_category AS T1 LEFT JOIN dtb_product_categories AS T2";
950        $sql .= " ON T1.category_id = T2.category_id ";
951        $sql .= " LEFT JOIN dtb_products AS T3";
952        $sql .= " ON T2.product_id = T3.product_id";
953        $sql .= " WHERE T3.del_flg = 0 AND T3.status = 1 ";
954        $sql .= " GROUP BY T1.category_id, T2.category_id ";
955        $objQuery->query($sql);
956
957        //子カテゴリ内の商品数を集計する
958        $arrCat = $objQuery->getAll("SELECT * FROM dtb_category");
959
960        $sql = "";
961        foreach($arrCat as $key => $val){
962
963            // 子ID一覧を取得
964            $arrRet = $this->sfGetChildrenArray('dtb_category', 'parent_category_id', 'category_id', $val['category_id']);
965            $line = SC_Utils_Ex::sfGetCommaList($arrRet);
966
967            $sql = " INSERT INTO dtb_category_total_count(category_id, product_count, create_date) ";
968            $sql .= " SELECT ?, SUM(product_count), now() FROM dtb_category_count ";
969            $sql .= " WHERE category_id IN (" . $line . ")";
970
971            $objQuery->query($sql, array($val['category_id']));
972        }
973    }
974
975    /**
976     * 子IDの配列を返す.
977     *
978     * @param string $table テーブル名
979     * @param string $pid_name 親ID名
980     * @param string $id_name ID名
981     * @param integer $id ID
982     * @param array 子ID の配列
983     */
984    function sfGetChildsID($table, $pid_name, $id_name, $id) {
985        $arrRet = $this->sfGetChildrenArray($table, $pid_name, $id_name, $id);
986        return $arrRet;
987    }
988
989    /**
990     * 階層構造のテーブルから子ID配列を取得する.
991     *
992     * @param string $table テーブル名
993     * @param string $pid_name 親ID名
994     * @param string $id_name ID名
995     * @param integer $id ID番号
996     * @return array 子IDの配列
997     */
998    function sfGetChildrenArray($table, $pid_name, $id_name, $id) {
999        $objQuery = new SC_Query();
1000        $col = $pid_name . "," . $id_name;
1001         $arrData = $objQuery->select($col, $table);
1002
1003        $arrPID = array();
1004        $arrPID[] = $id;
1005        $arrChildren = array();
1006        $arrChildren[] = $id;
1007
1008        $arrRet = $this->sfGetChildrenArraySub($arrData, $pid_name, $id_name, $arrPID);
1009
1010        while(count($arrRet) > 0) {
1011            $arrChildren = array_merge($arrChildren, $arrRet);
1012            $arrRet = $this->sfGetChildrenArraySub($arrData, $pid_name, $id_name, $arrRet);
1013        }
1014
1015        return $arrChildren;
1016    }
1017
1018    /**
1019     * 親ID直下の子IDをすべて取得する.
1020     *
1021     * @param array $arrData 親カテゴリの配列
1022     * @param string $pid_name 親ID名
1023     * @param string $id_name ID名
1024     * @param array $arrPID 親IDの配列
1025     * @return array 子IDの配列
1026     */
1027    function sfGetChildrenArraySub($arrData, $pid_name, $id_name, $arrPID) {
1028        $arrChildren = array();
1029        $max = count($arrData);
1030
1031        for($i = 0; $i < $max; $i++) {
1032            foreach($arrPID as $val) {
1033                if($arrData[$i][$pid_name] == $val) {
1034                    $arrChildren[] = $arrData[$i][$id_name];
1035                }
1036            }
1037        }
1038        return $arrChildren;
1039    }
1040
1041    /**
1042     * 所属するすべての階層の親IDを配列で返す.
1043     *
1044     * @param SC_Query $objQuery SC_Query インスタンス
1045     * @param string $table テーブル名
1046     * @param string $pid_name 親ID名
1047     * @param string $id_name ID名
1048     * @param integer $id ID
1049     * @return array 親IDの配列
1050     */
1051    function sfGetParents($objQuery, $table, $pid_name, $id_name, $id) {
1052        $arrRet = $this->sfGetParentsArray($table, $pid_name, $id_name, $id);
1053        // 配列の先頭1つを削除する。
1054        array_shift($arrRet);
1055        return $arrRet;
1056    }
1057
1058    /**
1059     * 階層構造のテーブルから親ID配列を取得する.
1060     *
1061     * @param string $table テーブル名
1062     * @param string $pid_name 親ID名
1063     * @param string $id_name ID名
1064     * @param integer $id ID
1065     * @return array 親IDの配列
1066     */
1067    function sfGetParentsArray($table, $pid_name, $id_name, $id) {
1068        $objQuery = new SC_Query();
1069        $col = $pid_name . "," . $id_name;
1070        $arrData = $objQuery->select($col, $table);
1071
1072        $arrParents = array();
1073        $arrParents[] = $id;
1074        $child = $id;
1075
1076        $ret = SC_Utils::sfGetParentsArraySub($arrData, $pid_name, $id_name, $child);
1077
1078        while($ret != "") {
1079            $arrParents[] = $ret;
1080            $ret = SC_Utils::sfGetParentsArraySub($arrData, $pid_name, $id_name, $ret);
1081        }
1082
1083        $arrParents = array_reverse($arrParents);
1084
1085        return $arrParents;
1086    }
1087
1088    /**
1089     * カテゴリから商品を検索する場合のWHERE文と値を返す.
1090     *
1091     * @param integer $category_id カテゴリID
1092     * @return array 商品を検索する場合の配列
1093     */
1094    function sfGetCatWhere($category_id) {
1095        // 子カテゴリIDの取得
1096        $arrRet = $this->sfGetChildsID("dtb_category", "parent_category_id", "category_id", $category_id);
1097        $tmp_where = "";
1098        foreach ($arrRet as $val) {
1099            if($tmp_where == "") {
1100                $tmp_where.= " category_id IN ( ?";
1101            } else {
1102                $tmp_where.= ",? ";
1103            }
1104            $arrval[] = $val;
1105        }
1106        $tmp_where.= " ) ";
1107        return array($tmp_where, $arrval);
1108    }
1109
1110    /**
1111     * 受注一時テーブルから情報を取得する.
1112     *
1113     * @param integer $order_temp_id 受注一時ID
1114     * @return array 受注一時情報の配列
1115     */
1116    function sfGetOrderTemp($order_temp_id) {
1117        $objQuery = new SC_Query();
1118        $where = "order_temp_id = ?";
1119        $arrRet = $objQuery->select("*", "dtb_order_temp", $where, array($order_temp_id));
1120        return $arrRet[0];
1121    }
1122
1123    /**
1124     * SELECTボックス用リストを作成する.
1125     *
1126     * @param string $table テーブル名
1127     * @param string $keyname プライマリーキーのカラム名
1128     * @param string $valname データ内容のカラム名
1129     * @return array SELECT ボックス用リストの配列
1130     */
1131    function sfGetIDValueList($table, $keyname, $valname) {
1132        $objQuery = new SC_Query();
1133        $col = "$keyname, $valname";
1134        $objQuery->setwhere("del_flg = 0");
1135        $objQuery->setorder("rank DESC");
1136        $arrList = $objQuery->select($col, $table);
1137        $count = count($arrList);
1138        for($cnt = 0; $cnt < $count; $cnt++) {
1139            $key = $arrList[$cnt][$keyname];
1140            $val = $arrList[$cnt][$valname];
1141            $arrRet[$key] = $val;
1142        }
1143        return $arrRet;
1144    }
1145
1146    /**
1147     * ランキングを上げる.
1148     *
1149     * @param string $table テーブル名
1150     * @param string $colname カラム名
1151     * @param string|integer $id テーブルのキー
1152     * @param string $andwhere SQL の AND 条件である WHERE 句
1153     * @return void
1154     */
1155    function sfRankUp($table, $colname, $id, $andwhere = "") {
1156        $objQuery = new SC_Query();
1157        $objQuery->begin();
1158        $where = "$colname = ?";
1159        if($andwhere != "") {
1160            $where.= " AND $andwhere";
1161        }
1162        // 対象項目のランクを取得
1163        $rank = $objQuery->get($table, "rank", $where, array($id));
1164        // ランクの最大値を取得
1165        $maxrank = $objQuery->max($table, "rank", $andwhere);
1166        // ランクが最大値よりも小さい場合に実行する。
1167        if($rank < $maxrank) {
1168            // ランクが一つ上のIDを取得する。
1169            $where = "rank = ?";
1170            if($andwhere != "") {
1171                $where.= " AND $andwhere";
1172            }
1173            $uprank = $rank + 1;
1174            $up_id = $objQuery->get($table, $colname, $where, array($uprank));
1175            // ランク入れ替えの実行
1176            $sqlup = "UPDATE $table SET rank = ? WHERE $colname = ?";
1177            if($andwhere != "") {
1178                $sqlup.= " AND $andwhere";
1179            }
1180            $objQuery->exec($sqlup, array($rank + 1, $id));
1181            $objQuery->exec($sqlup, array($rank, $up_id));
1182        }
1183        $objQuery->commit();
1184    }
1185
1186    /**
1187     * ランキングを下げる.
1188     *
1189     * @param string $table テーブル名
1190     * @param string $colname カラム名
1191     * @param string|integer $id テーブルのキー
1192     * @param string $andwhere SQL の AND 条件である WHERE 句
1193     * @return void
1194     */
1195    function sfRankDown($table, $colname, $id, $andwhere = "") {
1196        $objQuery = new SC_Query();
1197        $objQuery->begin();
1198        $where = "$colname = ?";
1199        if($andwhere != "") {
1200            $where.= " AND $andwhere";
1201        }
1202        // 対象項目のランクを取得
1203        $rank = $objQuery->get($table, "rank", $where, array($id));
1204
1205        // ランクが1(最小値)よりも大きい場合に実行する。
1206        if($rank > 1) {
1207            // ランクが一つ下のIDを取得する。
1208            $where = "rank = ?";
1209            if($andwhere != "") {
1210                $where.= " AND $andwhere";
1211            }
1212            $downrank = $rank - 1;
1213            $down_id = $objQuery->get($table, $colname, $where, array($downrank));
1214            // ランク入れ替えの実行
1215            $sqlup = "UPDATE $table SET rank = ? WHERE $colname = ?";
1216            if($andwhere != "") {
1217                $sqlup.= " AND $andwhere";
1218            }
1219            $objQuery->exec($sqlup, array($rank - 1, $id));
1220            $objQuery->exec($sqlup, array($rank, $down_id));
1221        }
1222        $objQuery->commit();
1223    }
1224
1225    /**
1226     * 指定順位へ移動する.
1227     *
1228     * @param string $tableName テーブル名
1229     * @param string $keyIdColumn キーを保持するカラム名
1230     * @param string|integer $keyId キーの値
1231     * @param integer $pos 指定順位
1232     * @param string $where SQL の AND 条件である WHERE 句
1233     * @return void
1234     */
1235    function sfMoveRank($tableName, $keyIdColumn, $keyId, $pos, $where = "") {
1236        $objQuery = new SC_Query();
1237        $objQuery->begin();
1238
1239        // 自身のランクを取得する
1240        if($where != "") {
1241            $getWhere = "$keyIdColumn = ? AND " . $where;
1242        } else {
1243            $getWhere = "$keyIdColumn = ?";
1244        }
1245        $rank = $objQuery->get($tableName, "rank", $getWhere, array($keyId));
1246
1247        $max = $objQuery->max($tableName, "rank", $where);
1248        // 値の調整(逆順)
1249        if($pos > $max) {
1250            $position = 1;
1251        } else if($pos < 1) {
1252            $position = $max;
1253        } else {
1254            $position = $max - $pos + 1;
1255        }
1256
1257        //入れ替え先の順位が入れ換え元の順位より大きい場合
1258        if( $position > $rank ) $term = "rank - 1";
1259
1260        //入れ替え先の順位が入れ換え元の順位より小さい場合
1261        if( $position < $rank ) $term = "rank + 1";
1262
1263        // XXX 入れ替え先の順位が入れ替え元の順位と同じ場合
1264        if (!isset($term)) $term = "rank";
1265
1266        // 指定した順位の商品から移動させる商品までのrankを1つずらす
1267        $sql = "UPDATE $tableName SET rank = $term WHERE rank BETWEEN ? AND ?";
1268        if($where != "") {
1269            $sql.= " AND $where";
1270        }
1271        if( $position > $rank ) $objQuery->exec( $sql, array($rank, $position));
1272        if( $position < $rank ) $objQuery->exec( $sql, array($position, $rank));
1273           // 指定した順位へrankを書き換える。
1274        $sql  = "UPDATE $tableName SET rank = ? WHERE $keyIdColumn = ? ";
1275        if($where != "") {
1276            $sql.= " AND $where";
1277        }
1278        $objQuery->exec( $sql, array( $position, $keyId ) );
1279        $objQuery->commit();
1280    }
1281
1282    /**
1283     * ランクを含むレコードを削除する.
1284     *
1285     * レコードごと削除する場合は、$deleteをtrueにする
1286     *
1287     * @param string $table テーブル名
1288     * @param string $colname カラム名
1289     * @param string|integer $id テーブルのキー
1290     * @param string $andwhere SQL の AND 条件である WHERE 句
1291     * @param bool $delete レコードごと削除する場合 true,
1292     *                     レコードごと削除しない場合 false
1293     * @return void
1294     */
1295    function sfDeleteRankRecord($table, $colname, $id, $andwhere = "",
1296                                $delete = false) {
1297        $objQuery = new SC_Query();
1298        $objQuery->begin();
1299        // 削除レコードのランクを取得する。
1300        $where = "$colname = ?";
1301        if($andwhere != "") {
1302            $where.= " AND $andwhere";
1303        }
1304        $rank = $objQuery->get($table, "rank", $where, array($id));
1305
1306        if(!$delete) {
1307            // ランクを最下位にする、DELフラグON
1308            $sqlup = "UPDATE $table SET rank = 0, del_flg = 1 ";
1309            $sqlup.= "WHERE $colname = ?";
1310            // UPDATEの実行
1311            $objQuery->exec($sqlup, array($id));
1312        } else {
1313            $objQuery->delete($table, "$colname = ?", array($id));
1314        }
1315
1316        // 追加レコードのランクより上のレコードを一つずらす。
1317        $where = "rank > ?";
1318        if($andwhere != "") {
1319            $where.= " AND $andwhere";
1320        }
1321        $sqlup = "UPDATE $table SET rank = (rank - 1) WHERE $where";
1322        $objQuery->exec($sqlup, array($rank));
1323        $objQuery->commit();
1324    }
1325
1326    /**
1327     * 親IDの配列を元に特定のカラムを取得する.
1328     *
1329     * @param SC_Query $objQuery SC_Query インスタンス
1330     * @param string $table テーブル名
1331     * @param string $id_name ID名
1332     * @param string $col_name カラム名
1333     * @param array $arrId IDの配列
1334     * @return array 特定のカラムの配列
1335     */
1336    function sfGetParentsCol($objQuery, $table, $id_name, $col_name, $arrId ) {
1337        $col = $col_name;
1338        $len = count($arrId);
1339        $where = "";
1340
1341        for($cnt = 0; $cnt < $len; $cnt++) {
1342            if($where == "") {
1343                $where = "$id_name = ?";
1344            } else {
1345                $where.= " OR $id_name = ?";
1346            }
1347        }
1348
1349        $objQuery->setorder("level");
1350        $arrRet = $objQuery->select($col, $table, $where, $arrId);
1351        return $arrRet;
1352    }
1353
1354    /**
1355     * カテゴリ変更時の移動処理を行う.
1356     *
1357     * @param SC_Query $objQuery SC_Query インスタンス
1358     * @param string $table テーブル名
1359     * @param string $id_name ID名
1360     * @param string $cat_name カテゴリ名
1361     * @param integer $old_catid 旧カテゴリID
1362     * @param integer $new_catid 新カテゴリID
1363     * @param integer $id ID
1364     * @return void
1365     */
1366    function sfMoveCatRank($objQuery, $table, $id_name, $cat_name, $old_catid, $new_catid, $id) {
1367        if ($old_catid == $new_catid) {
1368            return;
1369        }
1370        // 旧カテゴリでのランク削除処理
1371        // 移動レコードのランクを取得する。
1372        $where = "$id_name = ?";
1373        $rank = $objQuery->get($table, "rank", $where, array($id));
1374        // 削除レコードのランクより上のレコードを一つ下にずらす。
1375        $where = "rank > ? AND $cat_name = ?";
1376        $sqlup = "UPDATE $table SET rank = (rank - 1) WHERE $where";
1377        $objQuery->exec($sqlup, array($rank, $old_catid));
1378        // 新カテゴリでの登録処理
1379        // 新カテゴリの最大ランクを取得する。
1380        $max_rank = $objQuery->max($table, "rank", "$cat_name = ?", array($new_catid)) + 1;
1381        $where = "$id_name = ?";
1382        $sqlup = "UPDATE $table SET rank = ? WHERE $where";
1383        $objQuery->exec($sqlup, array($max_rank, $id));
1384    }
1385
1386    /**
1387     * お届け時間を取得する.
1388     *
1389     * @param integer $payment_id 支払い方法ID
1390     * @return array お届け時間の配列
1391     */
1392    function sfGetDelivTime($payment_id = "") {
1393        $objQuery = new SC_Query();
1394
1395        $deliv_id = "";
1396        $arrRet = array();
1397
1398        if($payment_id != "") {
1399            $where = "del_flg = 0 AND payment_id = ?";
1400            $arrRet = $objQuery->select("deliv_id", "dtb_payment", $where, array($payment_id));
1401            $deliv_id = $arrRet[0]['deliv_id'];
1402        }
1403
1404        if($deliv_id != "") {
1405            $objQuery->setorder("time_id");
1406            $where = "deliv_id = ?";
1407            $arrRet= $objQuery->select("time_id, deliv_time", "dtb_delivtime", $where, array($deliv_id));
1408        }
1409
1410        return $arrRet;
1411    }
1412
1413    /**
1414     * 都道府県、支払い方法から配送料金を取得する.
1415     *
1416     * @param integer $pref 都道府県ID
1417     * @param integer $payment_id 支払い方法ID
1418     * @return string 指定の都道府県, 支払い方法の配送料金
1419     */
1420    function sfGetDelivFee($arrData) {
1421        $pref = $arrData['deliv_pref'];
1422        $payment_id = isset($arrData['payment_id']) ? $arrData['payment_id'] : "";
1423
1424        $objQuery = new SC_Query();
1425
1426        $deliv_id = "";
1427
1428        // 支払い方法が指定されている場合は、対応した配送業者を取得する
1429        if($payment_id != "") {
1430            $where = "del_flg = 0 AND payment_id = ?";
1431            $arrRet = $objQuery->select("deliv_id", "dtb_payment", $where, array($payment_id));
1432            $deliv_id = $arrRet[0]['deliv_id'];
1433        // 支払い方法が指定されていない場合は、先頭の配送業者を取得する
1434        } else {
1435            $where = "del_flg = 0";
1436            $objQuery->setOrder("rank DESC");
1437            $objQuery->setLimitOffset(1);
1438            $arrRet = $objQuery->select("deliv_id", "dtb_deliv", $where);
1439            $deliv_id = $arrRet[0]['deliv_id'];
1440        }
1441
1442        // 配送業者から配送料を取得
1443        if($deliv_id != "") {
1444
1445            // 都道府県が指定されていない場合は、東京都の番号を指定しておく
1446            if($pref == "") {
1447                $pref = 13;
1448            }
1449
1450            $objQuery = new SC_Query();
1451            $where = "deliv_id = ? AND pref = ?";
1452            $arrRet= $objQuery->select("fee", "dtb_delivfee", $where, array($deliv_id, $pref));
1453        }
1454        return $arrRet[0]['fee'];
1455    }
1456
1457    /**
1458     * 集計情報を元に最終計算を行う.
1459     *
1460     * @param array $arrData 各種情報
1461     * @param LC_Page $objPage LC_Page インスタンス
1462     * @param SC_CartSession $objCartSess SC_CartSession インスタンス
1463     * @param array $arrInfo 店舗情報の配列
1464     * @param SC_Customer $objCustomer SC_Customer インスタンス
1465     * @return array 最終計算後の配列
1466     */
1467    function sfTotalConfirm($arrData, &$objPage, &$objCartSess, $arrInfo, $objCustomer = "") {
1468        // 未定義変数を定義
1469        if (!isset($arrData['deliv_pref'])) $arrData['deliv_pref'] = "";
1470        if (!isset($arrData['payment_id'])) $arrData['payment_id'] = "";
1471        if (!isset($arrData['charge'])) $arrData['charge'] = "";
1472        if (!isset($arrData['use_point'])) $arrData['use_point'] = "";
1473
1474        // 商品の合計個数
1475        $total_quantity = $objCartSess->getTotalQuantity(true);
1476
1477        // 税金の取得
1478        $arrData['tax'] = $objPage->tpl_total_tax;
1479        // 小計の取得
1480        $arrData['subtotal'] = $objPage->tpl_total_pretax;
1481
1482        // 合計送料の取得
1483        $arrData['deliv_fee'] = 0;
1484
1485        // 商品ごとの送料が有効の場合
1486        if (OPTION_PRODUCT_DELIV_FEE == 1) {
1487            $arrData['deliv_fee']+= $objCartSess->getAllProductsDelivFee();
1488        }
1489
1490        // 配送業者の送料が有効の場合
1491        if (OPTION_DELIV_FEE == 1) {
1492            // 送料の合計を計算する
1493            $arrData['deliv_fee'] += $this->sfGetDelivFee($arrData);
1494        }
1495
1496        // 送料無料の購入数が設定されている場合
1497        if(DELIV_FREE_AMOUNT > 0) {
1498            if($total_quantity >= DELIV_FREE_AMOUNT) {
1499                $arrData['deliv_fee'] = 0;
1500            }
1501        }
1502
1503        // 送料無料条件が設定されている場合
1504        if($arrInfo['free_rule'] > 0) {
1505            // 小計が無料条件を超えている場合
1506            if($arrData['subtotal'] >= $arrInfo['free_rule']) {
1507                $arrData['deliv_fee'] = 0;
1508            }
1509        }
1510
1511        // 合計の計算
1512        $arrData['total'] = $objPage->tpl_total_pretax; // 商品合計
1513        $arrData['total']+= $arrData['deliv_fee'];      // 送料
1514        $arrData['total']+= $arrData['charge'];         // 手数料
1515        // お支払い合計
1516        $arrData['payment_total'] = $arrData['total'] - ($arrData['use_point'] * POINT_VALUE);
1517        // 加算ポイントの計算
1518        if (USE_POINT === false) {
1519            $arrData['add_point'] = 0;
1520        } else {
1521            $arrData['add_point'] = SC_Utils::sfGetAddPoint($objPage->tpl_total_point, $arrData['use_point'], $arrInfo);
1522
1523            if($objCustomer != "") {
1524                // 誕生日月であった場合
1525                if($objCustomer->isBirthMonth()) {
1526                    $arrData['birth_point'] = BIRTH_MONTH_POINT;
1527                    $arrData['add_point'] += $arrData['birth_point'];
1528                }
1529            }
1530        }
1531
1532        if($arrData['add_point'] < 0) {
1533            $arrData['add_point'] = 0;
1534        }
1535        return $arrData;
1536    }
1537
1538    /**
1539     * レコードの存在チェックを行う.
1540     *
1541     * @param string $table テーブル名
1542     * @param string $col カラム名
1543     * @param array $arrval 要素の配列
1544     * @param array $addwhere SQL の AND 条件である WHERE 句
1545     * @return bool レコードが存在する場合 true
1546     */
1547    function sfIsRecord($table, $col, $arrval, $addwhere = "") {
1548        $objQuery = new SC_Query();
1549        $arrCol = split("[, ]", $col);
1550
1551        $where = "del_flg = 0";
1552
1553        if($addwhere != "") {
1554            $where.= " AND $addwhere";
1555        }
1556
1557        foreach($arrCol as $val) {
1558            if($val != "") {
1559                if($where == "") {
1560                    $where = "$val = ?";
1561                } else {
1562                    $where.= " AND $val = ?";
1563                }
1564            }
1565        }
1566        $ret = $objQuery->get($table, $col, $where, $arrval);
1567
1568        if($ret != "") {
1569            return true;
1570        }
1571        return false;
1572    }
1573
1574}
1575?>
Note: See TracBrowser for help on using the repository browser.