Warning: Can't use blame annotator:
svn blame failed on branches/comu-ver2/data/class/helper/SC_Helper_DB.php: バイナリファイル 'file:///home/svn/open/branches/comu-ver2/data/class/helper/SC_Helper_DB.php' に対しては blame で各行の最終変更者を計算できません 195004

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

Revision 17509, 61.7 KB checked in by Seasoft, 16 years ago (diff)

パラメータ設定により、在庫無し商品を一覧に非表示しないように設定可能とする。
※ カテゴリ別おすすめ商品( SC_Utils::sfGetBestProducts() )はテスト方法が分からず、未テスト。
※ dtb_maker_count の更新は未対応。

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