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

Revision 17435, 60.4 KB checked in by Seasoft, 16 years ago (diff)

ポイントシステムをON・OFF可能に。フロント機能のみの暫定対応。

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