source: branches/comu-ver2/data/class/helper/SC_Helper_DB.php @ 18044

Revision 18044, 64.5 KB checked in by Seasoft, 14 years ago (diff)

merge r17785
・取得元: version-2_4
・一部のロジックは改訂を行う
【取得元のログメッセージ】

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