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

Revision 17785, 56.9 KB checked in by zeniya, 15 years ago (diff)

EC-CUBE2.4改修 #376

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