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

Revision 18177, 57.7 KB checked in by kajiwara, 15 years ago (diff)

2.4.1 正式版をコミット。コミット内容詳細はこちら(http://svn.ec-cube.net/open_trac/query?status=closed&milestone=EC-CUBE2.4.1

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