source: branches/version-2_5-dev/data/class/helper/SC_Helper_DB.php @ 18790

Revision 18790, 74.6 KB checked in by nanasess, 14 years ago (diff)

#801 の改善に伴い MDB2 の関数に置き替えと, 未使用関数の削除

  • SC_Query
    • get_auto_increment() を削除
    • listSequences() を追加
    • listTables() を追加
    • listTableFields() を追加
    • listTableIndexes() を追加
  • SC_DB_DBFactory
    • getTableExistsSql() を削除
    • getTableIndex() を削除
    • createTableIndex() を削除
    • sfGetColumnList(), findTableNames() に @deprecated コメント追加
  • SC_Helper_DB
    • sfTabaleExists() を削除
    • sfIndexExists() を削除
    • 削除した関数の使用箇所を修正
  • SC_Helper_Session
    • sfTabaleExists() を使用していた箇所の修正
  • html/install/index.php
    • sfTabaleExists() を使用していた箇所の修正
  • 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-2010 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     * チェック対象のテーブルに, 該当のカラムが存在するかチェックする.
65     * 引数 $add が true の場合, 該当のカラムが存在しない場合は, カラムの生成を行う.
66     * カラムの生成も行う場合は, $col_type も必須となる.
67     *
68     * @param string $table_name テーブル名
69     * @param string $column_name カラム名
70     * @param string $col_type カラムのデータ型
71     * @param string $dsn データソース名
72     * @param bool $add カラムの作成も行う場合 true
73     * @return bool カラムが存在する場合とカラムの生成に成功した場合 true,
74     *               テーブルが存在しない場合 false,
75     *               引数 $add == false でカラムが存在しない場合 false
76     */
77    function sfColumnExists($table_name, $col_name, $col_type = "", $dsn = "", $add = false) {
78        $dbFactory = SC_DB_DBFactory_Ex::getInstance();
79        $dsn = $dbFactory->getDSN($dsn);
80
81        $objQuery =& SC_Query::getSingletonInstance($dsn);
82
83        // テーブルが無ければエラー
84        if(!in_array($table_name, $objQuery->listTables())) return false;
85
86        // 正常に接続されている場合
87        if(!$objQuery->isError()) {
88            list($db_type) = split(":", $dsn);
89
90            // カラムリストを取得
91            $columns = $objQuery->listTableFields($table_name);
92
93            if(in_array($col_name, $columns)){
94                return true;
95            }
96        }
97
98        // カラムを追加する
99        if($add){
100            $objQuery->query("ALTER TABLE $table_name ADD $col_name $col_type ");
101            return true;
102        }
103        return false;
104    }
105
106    /**
107     * データの存在チェックを行う.
108     *
109     * @param string $table_name テーブル名
110     * @param string $where データを検索する WHERE 句
111     * @param string $dsn データソース名
112     * @param string $sql データの追加を行う場合の SQL文
113     * @param bool $add データの追加も行う場合 true
114     * @return bool データが存在する場合 true, データの追加に成功した場合 true,
115     *               $add == false で, データが存在しない場合 false
116     */
117    function sfDataExists($table_name, $where, $arrval, $dsn = "", $sql = "", $add = false) {
118        $dbFactory = SC_DB_DBFactory_Ex::getInstance();
119        $dsn = $dbFactory->getDSN($dsn);
120
121        $objQuery =& SC_Query::getSingletonInstance();
122        $count = $objQuery->count($table_name, $where, $arrval);
123
124        if($count > 0) {
125            $ret = true;
126        } else {
127            $ret = false;
128        }
129        // データを追加する
130        if(!$ret && $add) {
131            $objQuery->exec($sql);
132        }
133        return $ret;
134    }
135
136    /**
137     * 店舗基本情報を取得する.
138     *
139     * @param boolean $force 強制的にDB取得するか
140     * @return array 店舗基本情報の配列
141     */
142    function sf_getBasisData($force = false) {
143        static $data;
144
145        if ($force || !isset($data)) {
146            $objQuery =& SC_Query::getSingletonInstance();
147            $arrRet = $objQuery->select('*', 'dtb_baseinfo');
148
149            if (isset($arrRet[0])) {
150                $data = $arrRet[0];
151            } else {
152                $data = array();
153            }
154        }
155
156        return $data;
157    }
158
159    /* 選択中のアイテムのルートカテゴリIDを取得する */
160    function sfGetRootId() {
161
162        if(!$this->g_root_on)   {
163            $this->g_root_on = true;
164            $objQuery =& SC_Query::getSingletonInstance();
165
166            if (!isset($_GET['product_id'])) $_GET['product_id'] = "";
167            if (!isset($_GET['category_id'])) $_GET['category_id'] = "";
168
169            if(!empty($_GET['product_id']) || !empty($_GET['category_id'])) {
170                // 選択中のカテゴリIDを判定する
171                $category_id = $this->sfGetCategoryId($_GET['product_id'], $_GET['category_id']);
172                // ROOTカテゴリIDの取得
173                $arrRet = $this->sfGetParents($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $category_id);
174                $root_id = isset($arrRet[0]) ? $arrRet[0] : "";
175            } else {
176                // ROOTカテゴリIDをなしに設定する
177                $root_id = "";
178            }
179            $this->g_root_id = $root_id;
180        }
181        return $this->g_root_id;
182    }
183
184    /**
185     * 商品規格情報を取得する.
186     *
187     * @param array $arrID 規格ID
188     * @param boolean $includePrivateProducts 非公開商品を含むか
189     * @return array 規格情報の配列
190     */
191    function sfGetProductsClass($arrID, $includePrivateProducts = false) {
192        list($product_id, $classcategory_id1, $classcategory_id2) = $arrID;
193
194        if (strlen($classcategory_id1) == 0) {
195            $classcategory_id1 = '0';
196        }
197        if (strlen($classcategory_id2) == 0) {
198            $classcategory_id2 = '0';
199        }
200
201        // 商品規格取得
202        $objQuery =& SC_Query::getSingletonInstance();
203        $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';
204        $table = 'vw_product_class AS prdcls';
205        $where = 'product_id = ? AND classcategory_id1 = ? AND classcategory_id2 = ?';
206        if (!$includePrivateProducts) {
207             $where .= ' AND status = 1';
208        }
209        $arrRet = $objQuery->select($col, $table, $where, array($product_id, $classcategory_id1, $classcategory_id2));
210        return $arrRet[0];
211    }
212
213    /**
214     * 支払い方法を取得する.
215     *
216     * @return void
217     */
218    function sfGetPayment() {
219        $objQuery =& SC_Query::getSingletonInstance();
220        // 購入金額が条件額以下の項目を取得
221        $where = "del_flg = 0";
222        $objQuery->setOrder("fix, rank DESC");
223        $arrRet = $objQuery->select("payment_id, payment_method, rule", "dtb_payment", $where);
224        return $arrRet;
225    }
226
227    /**
228     * カート内商品の集計処理を行う.
229     *
230     * 管理機能での利用は想定していないので注意。(非公開商品は除外される。)
231     *
232     * @param LC_Page $objPage ページクラスのインスタンス
233     * @param SC_CartSession $objCartSess カートセッションのインスタンス
234     * @param null $dummy1 互換性確保用(決済モジュール互換のため)
235     * @return LC_Page 集計処理後のページクラスインスタンス
236     */
237    function sfTotalCart(&$objPage, $objCartSess, $dummy1 = null) {
238
239        // 規格名一覧
240        $arrClassName = $this->sfGetIDValueList("dtb_class", "class_id", "name");
241        // 規格分類名一覧
242        $arrClassCatName = $this->sfGetIDValueList("dtb_classcategory", "classcategory_id", "name");
243
244        $objPage->tpl_total_pretax = 0;     // 費用合計(税込み)
245        $objPage->tpl_total_tax = 0;        // 消費税合計
246        $objPage->tpl_total_point = 0;      // ポイント合計
247
248        // カート内情報の取得
249        $arrQuantityInfo_by_product = array();
250        $cnt = 0;
251        foreach ($objCartSess->getCartList() as $arrCart) {
252            // 商品規格情報の取得
253            $arrData = $this->sfGetProductsClass($arrCart['id']);
254            $limit = null;
255            // DBに存在する商品
256            if (count($arrData) > 0) {
257
258                // 購入制限数を求める。
259                if ($arrData['stock_unlimited'] != '1' && SC_Utils_Ex::sfIsInt($arrData['sale_limit'])) {
260                    $limit = min($arrData['sale_limit'], $arrData['stock']);
261                } elseif (SC_Utils_Ex::sfIsInt($arrData['sale_limit'])) {
262                    $limit = $arrData['sale_limit'];
263                } elseif ($arrData['stock_unlimited'] != '1') {
264                    $limit = $arrData['stock'];
265                }
266
267                if (!is_null($limit) && $arrCart['quantity'] > $limit) {
268                    if ($limit > 0) {
269                        // カート内商品数を制限に合わせる
270                        $objCartSess->setProductValue($arrCart['id'], 'quantity', $limit);
271                        $quantity = $limit;
272                        $objPage->tpl_message .= "※「" . $arrData['name'] . "」は販売制限(または在庫が不足)しております。一度に数量{$limit}以上の購入はできません。\n";
273                    } else {
274                        // 売り切れ商品をカートから削除する
275                        $objCartSess->delProduct($arrCart['cart_no']);
276                        $objPage->tpl_message .= "※「" . $arrData['name'] . "」は売り切れました。\n";
277                        continue;
278                    }
279                } else {
280                    $quantity = $arrCart['quantity'];
281                }
282
283                // (商品規格単位でなく)商品単位での評価のための準備
284                $product_id = $arrCart['id'][0];
285                $arrQuantityInfo_by_product[$product_id]['quantity'] += $quantity;
286                $arrQuantityInfo_by_product[$product_id]['sale_limit'] = $arrData['sale_limit'];
287                $arrQuantityInfo_by_product[$product_id]['name'] = $arrData['name'];
288
289                $objPage->arrProductsClass[$cnt] = $arrData;
290                $objPage->arrProductsClass[$cnt]['quantity'] = $quantity;
291                $objPage->arrProductsClass[$cnt]['cart_no'] = $arrCart['cart_no'];
292                $objPage->arrProductsClass[$cnt]['class_name1'] =
293                    isset($arrClassName[$arrData['class_id1']])
294                        ? $arrClassName[$arrData['class_id1']] : "";
295
296                $objPage->arrProductsClass[$cnt]['class_name2'] =
297                    isset($arrClassName[$arrData['class_id2']])
298                        ? $arrClassName[$arrData['class_id2']] : "";
299
300                $objPage->arrProductsClass[$cnt]['classcategory_name1'] =
301                    $arrClassCatName[$arrData['classcategory_id1']];
302
303                $objPage->arrProductsClass[$cnt]['classcategory_name2'] =
304                    $arrClassCatName[$arrData['classcategory_id2']];
305
306                // 価格の登録
307                if ($arrData['price02'] != "") {
308                    $objCartSess->setProductValue($arrCart['id'], 'price', $arrData['price02']);
309                    $objPage->arrProductsClass[$cnt]['uniq_price'] = $arrData['price02'];
310                } else {
311                    $objCartSess->setProductValue($arrCart['id'], 'price', $arrData['price01']);
312                    $objPage->arrProductsClass[$cnt]['uniq_price'] = $arrData['price01'];
313                }
314                // ポイント付与率の登録
315                if (USE_POINT !== false) {
316                    $objCartSess->setProductValue($arrCart['id'], 'point_rate', $arrData['point_rate']);
317                }
318                // 商品ごとの合計金額
319                $objPage->arrProductsClass[$cnt]['total_pretax'] = $objCartSess->getProductTotal($arrCart['id']);
320                // 送料の合計を計算する
321                $objPage->tpl_total_deliv_fee+= ($arrData['deliv_fee'] * $arrCart['quantity']);
322                $cnt++;
323            } else { // DBに商品が見つからない場合、
324                $objPage->tpl_message .= "※ 現時点で販売していない商品が含まれておりました。該当商品をカートから削除しました。\n";
325                // カート商品の削除
326                $objCartSess->delProduct($arrCart['cart_no']);
327            }
328        }
329
330        foreach ($arrQuantityInfo_by_product as $product_id => $quantityInfo) {
331            if (SC_Utils_Ex::sfIsInt($quantityInfo['sale_limit']) && $quantityInfo['quantity'] > $quantityInfo['sale_limit']) {
332                $objPage->tpl_error = "※「{$quantityInfo['name']}」は数量「{$quantityInfo['sale_limit']}」以下に販売制限しております。一度にこれ以上の購入はできません。\n";
333                // 販売制限に引っかかった商品をマークする
334                foreach (array_keys($objPage->arrProductsClass) as $key) {
335                    $ProductsClass =& $objPage->arrProductsClass[$key];
336                    if ($ProductsClass['product_id'] == $product_id) {
337                        $ProductsClass['error'] = true;
338                    }
339                }
340            }
341        }
342
343        // 全商品合計金額(税込み)
344        $objPage->tpl_total_pretax = $objCartSess->getAllProductsTotal();
345        // 全商品合計消費税
346        $objPage->tpl_total_tax = $objCartSess->getAllProductsTax();
347        // 全商品合計ポイント
348        if (USE_POINT !== false) {
349            $objPage->tpl_total_point = $objCartSess->getAllProductsPoint();
350        }
351
352        return $objPage;
353    }
354
355    /**
356     * 受注一時テーブルへの書き込み処理を行う.
357     *
358     * @param string $uniqid ユニークID
359     * @param array $sqlval SQLの値の配列
360     * @return void
361     */
362    function sfRegistTempOrder($uniqid, $sqlval) {
363        if($uniqid != "") {
364            // 既存データのチェック
365            $objQuery =& SC_Query::getSingletonInstance();
366            $where = "order_temp_id = ?";
367            $cnt = $objQuery->count("dtb_order_temp", $where, array($uniqid));
368            // 既存データがない場合
369            if ($cnt == 0) {
370                // 初回書き込み時に会員の登録済み情報を取り込む
371                $sqlval = $this->sfGetCustomerSqlVal($uniqid, $sqlval);
372                $sqlval['create_date'] = "now()";
373                $objQuery->insert("dtb_order_temp", $sqlval);
374            } else {
375                $objQuery->update("dtb_order_temp", $sqlval, $where, array($uniqid));
376            }
377
378            // 受注_Tempテーブルの名称列を更新
379            // ・決済モジュールに対応するため、static メソッドとして扱う
380            SC_Helper_DB_Ex::sfUpdateOrderNameCol($uniqid, true);
381        }
382    }
383
384    /**
385     * 会員情報から SQL文の値を生成する.
386     *
387     * @param string $uniqid ユニークID
388     * @param array $sqlval SQL の値の配列
389     * @return array 会員情報を含んだ SQL の値の配列
390     */
391    function sfGetCustomerSqlVal($uniqid, $sqlval) {
392        $objCustomer = new SC_Customer();
393        // 会員情報登録処理
394        if ($objCustomer->isLoginSuccess(true)) {
395            // 登録データの作成
396            $sqlval['order_temp_id'] = $uniqid;
397            $sqlval['update_date'] = 'Now()';
398            $sqlval['customer_id'] = $objCustomer->getValue('customer_id');
399            $sqlval['order_name01'] = $objCustomer->getValue('name01');
400            $sqlval['order_name02'] = $objCustomer->getValue('name02');
401            $sqlval['order_kana01'] = $objCustomer->getValue('kana01');
402            $sqlval['order_kana02'] = $objCustomer->getValue('kana02');
403            $sqlval['order_sex'] = $objCustomer->getValue('sex');
404            $sqlval['order_zip01'] = $objCustomer->getValue('zip01');
405            $sqlval['order_zip02'] = $objCustomer->getValue('zip02');
406            $sqlval['order_pref'] = $objCustomer->getValue('pref');
407            $sqlval['order_addr01'] = $objCustomer->getValue('addr01');
408            $sqlval['order_addr02'] = $objCustomer->getValue('addr02');
409            $sqlval['order_tel01'] = $objCustomer->getValue('tel01');
410            $sqlval['order_tel02'] = $objCustomer->getValue('tel02');
411            $sqlval['order_tel03'] = $objCustomer->getValue('tel03');
412            if (defined('MOBILE_SITE')) {
413                $email_mobile = $objCustomer->getValue('email_mobile');
414                if (empty($email_mobile)) {
415                    $sqlval['order_email'] = $objCustomer->getValue('email');
416                } else {
417                    $sqlval['order_email'] = $email_mobile;
418                }
419            } else {
420                $sqlval['order_email'] = $objCustomer->getValue('email');
421            }
422            $sqlval['order_job'] = $objCustomer->getValue('job');
423            $sqlval['order_birth'] = $objCustomer->getValue('birth');
424        }
425        return $sqlval;
426    }
427
428    /**
429     * 会員編集登録処理を行う.
430     *
431     * @param array $array パラメータの配列
432     * @param array $arrRegistColumn 登録するカラムの配列
433     * @return void
434     */
435    function sfEditCustomerData($array, $arrRegistColumn) {
436        $objQuery =& SC_Query::getSingletonInstance();
437
438        foreach ($arrRegistColumn as $data) {
439            if ($data["column"] != "password") {
440                if($array[ $data['column'] ] != "") {
441                    $arrRegist[ $data["column"] ] = $array[ $data["column"] ];
442                } else {
443                    $arrRegist[ $data['column'] ] = NULL;
444                }
445            }
446        }
447        if (strlen($array["year"]) > 0 && strlen($array["month"]) > 0 && strlen($array["day"]) > 0) {
448            $arrRegist["birth"] = $array["year"] ."/". $array["month"] ."/". $array["day"] ." 00:00:00";
449        } else {
450            $arrRegist["birth"] = NULL;
451        }
452
453        //-- パスワードの更新がある場合は暗号化。(更新がない場合はUPDATE文を構成しない)
454        if ($array["password"] != DEFAULT_PASSWORD) $arrRegist["password"] = sha1($array["password"] . ":" . AUTH_MAGIC);
455        $arrRegist["update_date"] = "NOW()";
456
457        //-- 編集登録実行
458        $objQuery->update("dtb_customer", $arrRegist, "customer_id = ? ", array($array['customer_id']));
459    }
460
461    /**
462     * 注文番号、利用ポイント、加算ポイントから最終ポイントを取得する.
463     *
464     * @param integer $order_id 注文番号
465     * @param integer $use_point 利用ポイント
466     * @param integer $add_point 加算ポイント
467     * @return array 最終ポイントの配列
468     */
469    function sfGetCustomerPoint($order_id, $use_point, $add_point) {
470        $objQuery =& SC_Query::getSingletonInstance();
471        $arrRet = $objQuery->select("customer_id", "dtb_order", "order_id = ?", array($order_id));
472        $customer_id = $arrRet[0]['customer_id'];
473        if ($customer_id != "" && $customer_id >= 1) {
474            if (USE_POINT !== false) {
475                $arrRet = $objQuery->select("point", "dtb_customer", "customer_id = ?", array($customer_id));
476                $point = $arrRet[0]['point'];
477                $total_point = $arrRet[0]['point'] - $use_point + $add_point;
478            } else {
479                $total_point = 0;
480                $point = 0;
481            }
482        } else {
483            $total_point = "";
484            $point = "";
485        }
486        return array($point, $total_point);
487    }
488
489    /**
490     * 顧客番号、利用ポイント、加算ポイントから最終ポイントを取得する.
491     *
492     * @param integer $customer_id 顧客番号
493     * @param integer $use_point 利用ポイント
494     * @param integer $add_point 加算ポイント
495     * @return array 最終ポイントの配列
496     */
497    function sfGetCustomerPointFromCid($customer_id, $use_point, $add_point) {
498        $objQuery =& SC_Query::getSingletonInstance();
499        if (USE_POINT !== false) {
500            $arrRet = $objQuery->select("point", "dtb_customer", "customer_id = ?", array($customer_id));
501            $point = $arrRet[0]['point'];
502            $total_point = $arrRet[0]['point'] - $use_point + $add_point;
503        } else {
504            $total_point = 0;
505            $point = 0;
506        }
507        return array($point, $total_point);
508    }
509    /**
510     * カテゴリツリーの取得を行う.
511     *
512     * @param integer $parent_category_id 親カテゴリID
513     * @param bool $count_check 登録商品数のチェックを行う場合 true
514     * @return array カテゴリツリーの配列
515     */
516    function sfGetCatTree($parent_category_id, $count_check = false) {
517        $objQuery =& SC_Query::getSingletonInstance();
518        $col = "";
519        $col .= " cat.category_id,";
520        $col .= " cat.category_name,";
521        $col .= " cat.parent_category_id,";
522        $col .= " cat.level,";
523        $col .= " cat.rank,";
524        $col .= " cat.creator_id,";
525        $col .= " cat.create_date,";
526        $col .= " cat.update_date,";
527        $col .= " cat.del_flg, ";
528        $col .= " ttl.product_count";
529        $from = "dtb_category as cat left join dtb_category_total_count as ttl on ttl.category_id = cat.category_id";
530        // 登録商品数のチェック
531        if($count_check) {
532            $where = "del_flg = 0 AND product_count > 0";
533        } else {
534            $where = "del_flg = 0";
535        }
536        $objQuery->setOption("ORDER BY rank DESC");
537        $arrRet = $objQuery->select($col, $from, $where);
538
539        $arrParentID = $this->sfGetParents($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $parent_category_id);
540
541        foreach($arrRet as $key => $array) {
542            foreach($arrParentID as $val) {
543                if($array['category_id'] == $val) {
544                    $arrRet[$key]['display'] = 1;
545                    break;
546                }
547            }
548        }
549
550        return $arrRet;
551    }
552
553    /**
554     * カテゴリツリーの取得を複数カテゴリーで行う.
555     *
556     * @param integer $product_id 商品ID
557     * @param bool $count_check 登録商品数のチェックを行う場合 true
558     * @return array カテゴリツリーの配列
559     */
560    function sfGetMultiCatTree($product_id, $count_check = false) {
561        $objQuery =& SC_Query::getSingletonInstance();
562        $col = "";
563        $col .= " cat.category_id,";
564        $col .= " cat.category_name,";
565        $col .= " cat.parent_category_id,";
566        $col .= " cat.level,";
567        $col .= " cat.rank,";
568        $col .= " cat.creator_id,";
569        $col .= " cat.create_date,";
570        $col .= " cat.update_date,";
571        $col .= " cat.del_flg, ";
572        $col .= " ttl.product_count";
573        $from = "dtb_category as cat left join dtb_category_total_count as ttl on ttl.category_id = cat.category_id";
574        // 登録商品数のチェック
575        if($count_check) {
576            $where = "del_flg = 0 AND product_count > 0";
577        } else {
578            $where = "del_flg = 0";
579        }
580        $objQuery->setOption("ORDER BY rank DESC");
581        $arrRet = $objQuery->select($col, $from, $where);
582
583        $arrCategory_id = $this->sfGetCategoryId($product_id);
584
585        $arrCatTree = array();
586        foreach ($arrCategory_id as $pkey => $parent_category_id) {
587            $arrParentID = $this->sfGetParents($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $parent_category_id);
588
589            foreach($arrParentID as $pid) {
590                foreach($arrRet as $key => $array) {
591                    if($array['category_id'] == $pid) {
592                        $arrCatTree[$pkey][] = $arrRet[$key];
593                        break;
594                    }
595                }
596            }
597        }
598
599        return $arrCatTree;
600    }
601
602    /**
603     * 親カテゴリーを連結した文字列を取得する.
604     *
605     * @param integer $category_id カテゴリID
606     * @return string 親カテゴリーを連結した文字列
607     */
608    function sfGetCatCombName($category_id){
609        // 商品が属するカテゴリIDを縦に取得
610        $objQuery =& SC_Query::getSingletonInstance();
611        $arrCatID = $this->sfGetParents($objQuery, "dtb_category", "parent_category_id", "category_id", $category_id);
612        $ConbName = "";
613
614        // カテゴリー名称を取得する
615        foreach($arrCatID as $key => $val){
616            $sql = "SELECT category_name FROM dtb_category WHERE category_id = ?";
617            $arrVal = array($val);
618            $CatName = $objQuery->getOne($sql,$arrVal);
619            $ConbName .= $CatName . ' | ';
620        }
621        // 最後の | をカットする
622        $ConbName = substr_replace($ConbName, "", strlen($ConbName) - 2, 2);
623
624        return $ConbName;
625    }
626
627    /**
628     * 指定したカテゴリーIDのカテゴリーを取得する.
629     *
630     * @param integer $category_id カテゴリID
631     * @return array 指定したカテゴリーIDのカテゴリー
632     */
633    function sfGetCat($category_id){
634        $objQuery =& SC_Query::getSingletonInstance();
635
636        // カテゴリーを取得する
637        $arrVal = array($category_id);
638        $res = $objQuery->select('category_id AS id, category_name AS name', 'dtb_category', 'category_id = ?', $arrVal);
639
640        return $res[0];
641    }
642
643    /**
644     * 指定したカテゴリーIDの大カテゴリーを取得する.
645     *
646     * @param integer $category_id カテゴリID
647     * @return array 指定したカテゴリーIDの大カテゴリー
648     */
649    function sfGetFirstCat($category_id){
650        // 商品が属するカテゴリIDを縦に取得
651        $objQuery =& SC_Query::getSingletonInstance();
652        $arrRet = array();
653        $arrCatID = $this->sfGetParents($objQuery, "dtb_category", "parent_category_id", "category_id", $category_id);
654        $arrRet['id'] = $arrCatID[0];
655
656        // カテゴリー名称を取得する
657        $sql = "SELECT category_name FROM dtb_category WHERE category_id = ?";
658        $arrVal = array($arrRet['id']);
659        $arrRet['name'] = $objQuery->getOne($sql,$arrVal);
660
661        return $arrRet;
662    }
663
664    /**
665     * カテゴリツリーの取得を行う.
666     *
667     * $products_check:true商品登録済みのものだけ取得する
668     *
669     * @param string $addwhere 追加する WHERE 句
670     * @param bool $products_check 商品の存在するカテゴリのみ取得する場合 true
671     * @param string $head カテゴリ名のプレフィックス文字列
672     * @return array カテゴリツリーの配列
673     */
674    function sfGetCategoryList($addwhere = "", $products_check = false, $head = CATEGORY_HEAD) {
675        $objQuery =& SC_Query::getSingletonInstance();
676        $where = "del_flg = 0";
677
678        if($addwhere != "") {
679            $where.= " AND $addwhere";
680        }
681
682        $objQuery->setOption("ORDER BY rank DESC");
683
684        if($products_check) {
685            $col = "T1.category_id, category_name, level";
686            $from = "dtb_category AS T1 LEFT JOIN dtb_category_total_count AS T2 ON T1.category_id = T2.category_id";
687            $where .= " AND product_count > 0";
688        } else {
689            $col = "category_id, category_name, level";
690            $from = "dtb_category";
691        }
692
693        $arrRet = $objQuery->select($col, $from, $where);
694
695        $max = count($arrRet);
696        for($cnt = 0; $cnt < $max; $cnt++) {
697            $id = $arrRet[$cnt]['category_id'];
698            $name = $arrRet[$cnt]['category_name'];
699            $arrList[$id] = str_repeat($head, $arrRet[$cnt]['level']) . $name;
700        }
701        return $arrList;
702    }
703
704    /**
705     * カテゴリーツリーの取得を行う.
706     *
707     * 親カテゴリの Value=0 を対象とする
708     *
709     * @param bool $parent_zero 親カテゴリの Value=0 の場合 true
710     * @return array カテゴリツリーの配列
711     */
712    function sfGetLevelCatList($parent_zero = true) {
713        $objQuery =& SC_Query::getSingletonInstance();
714
715        // カテゴリ名リストを取得
716        $col = "category_id, parent_category_id, category_name";
717        $where = "del_flg = 0";
718        $objQuery->setOption("ORDER BY level");
719        $arrRet = $objQuery->select($col, "dtb_category", $where);
720        $arrCatName = array();
721        foreach ($arrRet as $arrTmp) {
722            $arrCatName[$arrTmp['category_id']] =
723                (($arrTmp['parent_category_id'] > 0)?
724                    $arrCatName[$arrTmp['parent_category_id']] : "")
725                . CATEGORY_HEAD . $arrTmp['category_name'];
726        }
727
728        $col = "category_id, parent_category_id, category_name, level";
729        $where = "del_flg = 0";
730        $objQuery->setOption("ORDER BY rank DESC");
731        $arrRet = $objQuery->select($col, "dtb_category", $where);
732        $max = count($arrRet);
733
734        for($cnt = 0; $cnt < $max; $cnt++) {
735            if($parent_zero) {
736                if($arrRet[$cnt]['level'] == LEVEL_MAX) {
737                    $arrValue[$cnt] = $arrRet[$cnt]['category_id'];
738                } else {
739                    $arrValue[$cnt] = "";
740                }
741            } else {
742                $arrValue[$cnt] = $arrRet[$cnt]['category_id'];
743            }
744
745            $arrOutput[$cnt] = $arrCatName[$arrRet[$cnt]['category_id']];
746        }
747
748        return array($arrValue, $arrOutput);
749    }
750
751    /**
752     * 選択中の商品のカテゴリを取得する.
753     *
754     * @param integer $product_id プロダクトID
755     * @param integer $category_id カテゴリID
756     * @return array 選択中の商品のカテゴリIDの配列
757     *
758     */
759    function sfGetCategoryId($product_id, $category_id = 0, $closed = false) {
760        if ($closed) {
761            $status = "";
762        } else {
763            $status = "status = 1";
764        }
765
766        if(!$this->g_category_on) {
767            $this->g_category_on = true;
768            $category_id = (int) $category_id;
769            $product_id = (int) $product_id;
770            if (SC_Utils_Ex::sfIsInt($category_id) && $category_id != 0 && $this->sfIsRecord("dtb_category","category_id", $category_id)) {
771                $this->g_category_id = array($category_id);
772            } else if (SC_Utils_Ex::sfIsInt($product_id) && $product_id != 0 && $this->sfIsRecord("dtb_products","product_id", $product_id, $status)) {
773                $objQuery =& SC_Query::getSingletonInstance();
774                $where = "product_id = ?";
775                $category_id = $objQuery->getCol("dtb_product_categories", "category_id", "product_id = ?", array($product_id));
776                $this->g_category_id = $category_id;
777            } else {
778                // 不正な場合は、空の配列を返す。
779                $this->g_category_id = array();
780            }
781        }
782        return $this->g_category_id;
783    }
784
785    /**
786     * 商品をカテゴリの先頭に追加する.
787     *
788     * @param integer $category_id カテゴリID
789     * @param integer $product_id プロダクトID
790     * @return void
791     */
792    function addProductBeforCategories($category_id, $product_id) {
793
794        $sqlval = array("category_id" => $category_id,
795                        "product_id" => $product_id);
796
797        $objQuery =& SC_Query::getSingletonInstance();
798
799        // 現在の商品カテゴリを取得
800        $arrCat = $objQuery->select("product_id, category_id, rank",
801                                    "dtb_product_categories",
802                                    "category_id = ?",
803                                    array($category_id));
804
805        $max = "0";
806        foreach ($arrCat as $val) {
807            // 同一商品が存在する場合は登録しない
808            if ($val["product_id"] == $product_id) {
809                return;
810            }
811            // 最上位ランクを取得
812            $max = ($max < $val["rank"]) ? $val["rank"] : $max;
813        }
814        $sqlval["rank"] = $max + 1;
815        $objQuery->insert("dtb_product_categories", $sqlval);
816    }
817
818    /**
819     * 商品をカテゴリの末尾に追加する.
820     *
821     * @param integer $category_id カテゴリID
822     * @param integer $product_id プロダクトID
823     * @return void
824     */
825    function addProductAfterCategories($category_id, $product_id) {
826        $sqlval = array("category_id" => $category_id,
827                        "product_id" => $product_id);
828
829        $objQuery =& SC_Query::getSingletonInstance();
830
831        // 現在の商品カテゴリを取得
832        $arrCat = $objQuery->select("product_id, category_id, rank",
833                                    "dtb_product_categories",
834                                    "category_id = ?",
835                                    array($category_id));
836
837        $min = 0;
838        foreach ($arrCat as $val) {
839            // 同一商品が存在する場合は登録しない
840            if ($val["product_id"] == $product_id) {
841                return;
842            }
843            // 最下位ランクを取得
844            $min = ($min < $val["rank"]) ? $val["rank"] : $min;
845        }
846        $sqlval["rank"] = $min;
847        $objQuery->insert("dtb_product_categories", $sqlval);
848    }
849
850    /**
851     * 商品をカテゴリから削除する.
852     *
853     * @param integer $category_id カテゴリID
854     * @param integer $product_id プロダクトID
855     * @return void
856     */
857    function removeProductByCategories($category_id, $product_id) {
858        $sqlval = array("category_id" => $category_id,
859                        "product_id" => $product_id);
860        $objQuery =& SC_Query::getSingletonInstance();
861        $objQuery->delete("dtb_product_categories",
862                          "category_id = ? AND product_id = ?", $sqlval);
863    }
864
865    /**
866     * 商品カテゴリを更新する.
867     *
868     * @param array $arrCategory_id 登録するカテゴリIDの配列
869     * @param integer $product_id プロダクトID
870     * @return void
871     */
872    function updateProductCategories($arrCategory_id, $product_id) {
873        $objQuery =& SC_Query::getSingletonInstance();
874
875        // 現在のカテゴリ情報を取得
876        $arrCurrentCat = $objQuery->select("product_id, category_id, rank",
877                                           "dtb_product_categories",
878                                           "product_id = ?",
879                                           array($product_id));
880
881        // 登録するカテゴリ情報と比較
882        foreach ($arrCurrentCat as $val) {
883
884            // 登録しないカテゴリを削除
885            if (!in_array($val["category_id"], $arrCategory_id)) {
886                $this->removeProductByCategories($val["category_id"], $product_id);
887            }
888        }
889
890        // カテゴリを登録
891        foreach ($arrCategory_id as $category_id) {
892            $this->addProductBeforCategories($category_id, $product_id);
893        }
894    }
895
896    /**
897     * カテゴリ数の登録を行う.
898     *
899     * @param SC_Query $objQuery SC_Query インスタンス
900     * @return void
901     */
902    function sfCategory_Count($objQuery){
903
904        //テーブル内容の削除
905        $objQuery->query("DELETE FROM dtb_category_count");
906        $objQuery->query("DELETE FROM dtb_category_total_count");
907
908        $sql_where .= 'alldtl.del_flg = 0 AND alldtl.status = 1';
909        // 在庫無し商品の非表示
910        if (NOSTOCK_HIDDEN === true) {
911            $sql_where .= ' AND (alldtl.stock_max >= 1 OR alldtl.stock_unlimited_max = 1)';
912        }
913
914        //各カテゴリ内の商品数を数えて格納
915        $sql = <<< __EOS__
916            INSERT INTO dtb_category_count(category_id, product_count, create_date)
917            SELECT T1.category_id, count(T2.category_id), now()
918            FROM dtb_category AS T1
919                LEFT JOIN dtb_product_categories AS T2
920                    ON T1.category_id = T2.category_id
921                LEFT JOIN vw_products_allclass_detail AS alldtl
922                    ON T2.product_id = alldtl.product_id
923            WHERE $sql_where
924            GROUP BY T1.category_id, T2.category_id
925__EOS__;
926
927        $objQuery->query($sql);
928
929        //子カテゴリ内の商品数を集計する
930
931        // カテゴリ情報を取得
932        $arrCat = $objQuery->select('category_id', 'dtb_category');
933
934        foreach ($arrCat as $row) {
935            $category_id = $row['category_id'];
936            $arrval = array();
937
938            $arrval[] = $category_id;
939
940            list($tmp_where, $tmp_arrval) = $this->sfGetCatWhere($category_id);
941            if ($tmp_where != "") {
942                $sql_where_product_ids = "alldtl.product_id IN (SELECT product_id FROM dtb_product_categories WHERE " . $tmp_where . ")";
943                $arrval = array_merge((array)$arrval, (array)$tmp_arrval);
944            } else {
945                $sql_where_product_ids = '0<>0'; // 一致させない
946            }
947
948            $sql = <<< __EOS__
949                INSERT INTO dtb_category_total_count (category_id, product_count, create_date)
950                SELECT
951                    ?
952                    ,count(*)
953                    ,now()
954                FROM vw_products_allclass_detail AS alldtl
955                WHERE ($sql_where) AND ($sql_where_product_ids)
956__EOS__;
957
958            $objQuery->query($sql, $arrval);
959        }
960    }
961
962    /**
963     * 子IDの配列を返す.
964     *
965     * @param string $table テーブル名
966     * @param string $pid_name 親ID名
967     * @param string $id_name ID名
968     * @param integer $id ID
969     * @param array 子ID の配列
970     */
971    function sfGetChildsID($table, $pid_name, $id_name, $id) {
972        $arrRet = $this->sfGetChildrenArray($table, $pid_name, $id_name, $id);
973        return $arrRet;
974    }
975
976    /**
977     * 階層構造のテーブルから子ID配列を取得する.
978     *
979     * @param string $table テーブル名
980     * @param string $pid_name 親ID名
981     * @param string $id_name ID名
982     * @param integer $id ID番号
983     * @return array 子IDの配列
984     */
985    function sfGetChildrenArray($table, $pid_name, $id_name, $id) {
986        $objQuery =& SC_Query::getSingletonInstance();
987        $col = $pid_name . "," . $id_name;
988         $arrData = $objQuery->select($col, $table);
989
990        $arrPID = array();
991        $arrPID[] = $id;
992        $arrChildren = array();
993        $arrChildren[] = $id;
994
995        $arrRet = $this->sfGetChildrenArraySub($arrData, $pid_name, $id_name, $arrPID);
996
997        while(count($arrRet) > 0) {
998            $arrChildren = array_merge($arrChildren, $arrRet);
999            $arrRet = $this->sfGetChildrenArraySub($arrData, $pid_name, $id_name, $arrRet);
1000        }
1001
1002        return $arrChildren;
1003    }
1004
1005    /**
1006     * 親ID直下の子IDをすべて取得する.
1007     *
1008     * @param array $arrData 親カテゴリの配列
1009     * @param string $pid_name 親ID名
1010     * @param string $id_name ID名
1011     * @param array $arrPID 親IDの配列
1012     * @return array 子IDの配列
1013     */
1014    function sfGetChildrenArraySub($arrData, $pid_name, $id_name, $arrPID) {
1015        $arrChildren = array();
1016        $max = count($arrData);
1017
1018        for($i = 0; $i < $max; $i++) {
1019            foreach($arrPID as $val) {
1020                if($arrData[$i][$pid_name] == $val) {
1021                    $arrChildren[] = $arrData[$i][$id_name];
1022                }
1023            }
1024        }
1025        return $arrChildren;
1026    }
1027
1028    /**
1029     * 所属するすべての階層の親IDを配列で返す.
1030     *
1031     * @param SC_Query $objQuery SC_Query インスタンス
1032     * @param string $table テーブル名
1033     * @param string $pid_name 親ID名
1034     * @param string $id_name ID名
1035     * @param integer $id ID
1036     * @return array 親IDの配列
1037     */
1038    function sfGetParents($objQuery, $table, $pid_name, $id_name, $id) {
1039        $arrRet = $this->sfGetParentsArray($table, $pid_name, $id_name, $id);
1040        // 配列の先頭1つを削除する。
1041        array_shift($arrRet);
1042        return $arrRet;
1043    }
1044
1045    /**
1046     * 階層構造のテーブルから親ID配列を取得する.
1047     *
1048     * @param string $table テーブル名
1049     * @param string $pid_name 親ID名
1050     * @param string $id_name ID名
1051     * @param integer $id ID
1052     * @return array 親IDの配列
1053     */
1054    function sfGetParentsArray($table, $pid_name, $id_name, $id) {
1055        $objQuery =& SC_Query::getSingletonInstance();
1056        $col = $pid_name . "," . $id_name;
1057        $arrData = $objQuery->select($col, $table);
1058
1059        $arrParents = array();
1060        $arrParents[] = $id;
1061        $child = $id;
1062
1063        $ret = SC_Utils::sfGetParentsArraySub($arrData, $pid_name, $id_name, $child);
1064
1065        while($ret != "") {
1066            $arrParents[] = $ret;
1067            $ret = SC_Utils::sfGetParentsArraySub($arrData, $pid_name, $id_name, $ret);
1068        }
1069
1070        $arrParents = array_reverse($arrParents);
1071
1072        return $arrParents;
1073    }
1074
1075    /**
1076     * カテゴリから商品を検索する場合のWHERE文と値を返す.
1077     *
1078     * @param integer $category_id カテゴリID
1079     * @return array 商品を検索する場合の配列
1080     */
1081    function sfGetCatWhere($category_id) {
1082        // 子カテゴリIDの取得
1083        $arrRet = $this->sfGetChildsID("dtb_category", "parent_category_id", "category_id", $category_id);
1084        $tmp_where = "";
1085        foreach ($arrRet as $val) {
1086            if($tmp_where == "") {
1087                $tmp_where.= "category_id IN ( ?";
1088            } else {
1089                $tmp_where.= ",? ";
1090            }
1091            $arrval[] = $val;
1092        }
1093        $tmp_where.= " ) ";
1094        return array($tmp_where, $arrval);
1095    }
1096
1097    /**
1098     * 受注一時テーブルから情報を取得する.
1099     *
1100     * @param integer $order_temp_id 受注一時ID
1101     * @return array 受注一時情報の配列
1102     */
1103    function sfGetOrderTemp($order_temp_id) {
1104        $objQuery =& SC_Query::getSingletonInstance();
1105        $where = "order_temp_id = ?";
1106        $arrRet = $objQuery->select("*", "dtb_order_temp", $where, array($order_temp_id));
1107        return $arrRet[0];
1108    }
1109
1110    /**
1111     * SELECTボックス用リストを作成する.
1112     *
1113     * @param string $table テーブル名
1114     * @param string $keyname プライマリーキーのカラム名
1115     * @param string $valname データ内容のカラム名
1116     * @return array SELECT ボックス用リストの配列
1117     */
1118    function sfGetIDValueList($table, $keyname, $valname) {
1119        $objQuery =& SC_Query::getSingletonInstance();
1120        $col = "$keyname, $valname";
1121        $objQuery->setWhere("del_flg = 0");
1122        $objQuery->setOrder("rank DESC");
1123        $arrList = $objQuery->select($col, $table);
1124        $count = count($arrList);
1125        for($cnt = 0; $cnt < $count; $cnt++) {
1126            $key = $arrList[$cnt][$keyname];
1127            $val = $arrList[$cnt][$valname];
1128            $arrRet[$key] = $val;
1129        }
1130        return $arrRet;
1131    }
1132
1133    /**
1134     * ランキングを上げる.
1135     *
1136     * @param string $table テーブル名
1137     * @param string $colname カラム名
1138     * @param string|integer $id テーブルのキー
1139     * @param string $andwhere SQL の AND 条件である WHERE 句
1140     * @return void
1141     */
1142    function sfRankUp($table, $colname, $id, $andwhere = "") {
1143        $objQuery =& SC_Query::getSingletonInstance();
1144        $objQuery->begin();
1145        $where = "$colname = ?";
1146        if($andwhere != "") {
1147            $where.= " AND $andwhere";
1148        }
1149        // 対象項目のランクを取得
1150        $rank = $objQuery->get($table, "rank", $where, array($id));
1151        // ランクの最大値を取得
1152        $maxrank = $objQuery->max($table, "rank", $andwhere);
1153        // ランクが最大値よりも小さい場合に実行する。
1154        if($rank < $maxrank) {
1155            // ランクが一つ上のIDを取得する。
1156            $where = "rank = ?";
1157            if($andwhere != "") {
1158                $where.= " AND $andwhere";
1159            }
1160            $uprank = $rank + 1;
1161            $up_id = $objQuery->get($table, $colname, $where, array($uprank));
1162            // ランク入れ替えの実行
1163            $sqlup = "UPDATE $table SET rank = ? WHERE $colname = ?";
1164            if($andwhere != "") {
1165                $sqlup.= " AND $andwhere";
1166            }
1167            $objQuery->exec($sqlup, array($rank + 1, $id));
1168            $objQuery->exec($sqlup, array($rank, $up_id));
1169        }
1170        $objQuery->commit();
1171    }
1172
1173    /**
1174     * ランキングを下げる.
1175     *
1176     * @param string $table テーブル名
1177     * @param string $colname カラム名
1178     * @param string|integer $id テーブルのキー
1179     * @param string $andwhere SQL の AND 条件である WHERE 句
1180     * @return void
1181     */
1182    function sfRankDown($table, $colname, $id, $andwhere = "") {
1183        $objQuery =& SC_Query::getSingletonInstance();
1184        $objQuery->begin();
1185        $where = "$colname = ?";
1186        if($andwhere != "") {
1187            $where.= " AND $andwhere";
1188        }
1189        // 対象項目のランクを取得
1190        $rank = $objQuery->get($table, "rank", $where, array($id));
1191
1192        // ランクが1(最小値)よりも大きい場合に実行する。
1193        if($rank > 1) {
1194            // ランクが一つ下のIDを取得する。
1195            $where = "rank = ?";
1196            if($andwhere != "") {
1197                $where.= " AND $andwhere";
1198            }
1199            $downrank = $rank - 1;
1200            $down_id = $objQuery->get($table, $colname, $where, array($downrank));
1201            // ランク入れ替えの実行
1202            $sqlup = "UPDATE $table SET rank = ? WHERE $colname = ?";
1203            if($andwhere != "") {
1204                $sqlup.= " AND $andwhere";
1205            }
1206            $objQuery->exec($sqlup, array($rank - 1, $id));
1207            $objQuery->exec($sqlup, array($rank, $down_id));
1208        }
1209        $objQuery->commit();
1210    }
1211
1212    /**
1213     * 指定順位へ移動する.
1214     *
1215     * @param string $tableName テーブル名
1216     * @param string $keyIdColumn キーを保持するカラム名
1217     * @param string|integer $keyId キーの値
1218     * @param integer $pos 指定順位
1219     * @param string $where SQL の AND 条件である WHERE 句
1220     * @return void
1221     */
1222    function sfMoveRank($tableName, $keyIdColumn, $keyId, $pos, $where = "") {
1223        $objQuery =& SC_Query::getSingletonInstance();
1224        $objQuery->begin();
1225
1226        // 自身のランクを取得する
1227        if($where != "") {
1228            $getWhere = "$keyIdColumn = ? AND " . $where;
1229        } else {
1230            $getWhere = "$keyIdColumn = ?";
1231        }
1232        $rank = $objQuery->get($tableName, "rank", $getWhere, array($keyId));
1233
1234        $max = $objQuery->max($tableName, "rank", $where);
1235
1236        // 値の調整(逆順)
1237        if($pos > $max) {
1238            $position = 1;
1239        } else if($pos < 1) {
1240            $position = $max;
1241        } else {
1242            $position = $max - $pos + 1;
1243        }
1244
1245        //入れ替え先の順位が入れ換え元の順位より大きい場合
1246        if( $position > $rank ) $term = "rank - 1";
1247
1248        //入れ替え先の順位が入れ換え元の順位より小さい場合
1249        if( $position < $rank ) $term = "rank + 1";
1250
1251        // XXX 入れ替え先の順位が入れ替え元の順位と同じ場合
1252        if (!isset($term)) $term = "rank";
1253
1254        // 指定した順位の商品から移動させる商品までのrankを1つずらす
1255        $sql = "UPDATE $tableName SET rank = $term WHERE rank BETWEEN ? AND ?";
1256        if($where != "") {
1257            $sql.= " AND $where";
1258        }
1259
1260        if( $position > $rank ) $objQuery->exec( $sql, array( $rank + 1, $position ));
1261        if( $position < $rank ) $objQuery->exec( $sql, array( $position, $rank - 1 ));
1262
1263        // 指定した順位へrankを書き換える。
1264        $sql  = "UPDATE $tableName SET rank = ? WHERE $keyIdColumn = ? ";
1265        if($where != "") {
1266            $sql.= " AND $where";
1267        }
1268
1269        $objQuery->exec( $sql, array( $position, $keyId ) );
1270        $objQuery->commit();
1271    }
1272
1273    /**
1274     * ランクを含むレコードを削除する.
1275     *
1276     * レコードごと削除する場合は、$deleteをtrueにする
1277     *
1278     * @param string $table テーブル名
1279     * @param string $colname カラム名
1280     * @param string|integer $id テーブルのキー
1281     * @param string $andwhere SQL の AND 条件である WHERE 句
1282     * @param bool $delete レコードごと削除する場合 true,
1283     *                     レコードごと削除しない場合 false
1284     * @return void
1285     */
1286    function sfDeleteRankRecord($table, $colname, $id, $andwhere = "",
1287                                $delete = false) {
1288        $objQuery =& SC_Query::getSingletonInstance();
1289        $objQuery->begin();
1290        // 削除レコードのランクを取得する。
1291        $where = "$colname = ?";
1292        if($andwhere != "") {
1293            $where.= " AND $andwhere";
1294        }
1295        $rank = $objQuery->get($table, "rank", $where, array($id));
1296
1297        if(!$delete) {
1298            // ランクを最下位にする、DELフラグON
1299            $sqlup = "UPDATE $table SET rank = 0, del_flg = 1 ";
1300            $sqlup.= "WHERE $colname = ?";
1301            // UPDATEの実行
1302            $objQuery->exec($sqlup, array($id));
1303        } else {
1304            $objQuery->delete($table, "$colname = ?", array($id));
1305        }
1306
1307        // 追加レコードのランクより上のレコードを一つずらす。
1308        $where = "rank > ?";
1309        if($andwhere != "") {
1310            $where.= " AND $andwhere";
1311        }
1312        $sqlup = "UPDATE $table SET rank = (rank - 1) WHERE $where";
1313        $objQuery->exec($sqlup, array($rank));
1314        $objQuery->commit();
1315    }
1316
1317    /**
1318     * 親IDの配列を元に特定のカラムを取得する.
1319     *
1320     * @param SC_Query $objQuery SC_Query インスタンス
1321     * @param string $table テーブル名
1322     * @param string $id_name ID名
1323     * @param string $col_name カラム名
1324     * @param array $arrId IDの配列
1325     * @return array 特定のカラムの配列
1326     */
1327    function sfGetParentsCol($objQuery, $table, $id_name, $col_name, $arrId ) {
1328        $col = $col_name;
1329        $len = count($arrId);
1330        $where = "";
1331
1332        for($cnt = 0; $cnt < $len; $cnt++) {
1333            if($where == "") {
1334                $where = "$id_name = ?";
1335            } else {
1336                $where.= " OR $id_name = ?";
1337            }
1338        }
1339
1340        $objQuery->setOrder("level");
1341        $arrRet = $objQuery->select($col, $table, $where, $arrId);
1342        return $arrRet;
1343    }
1344
1345    /**
1346     * カテゴリ変更時の移動処理を行う.
1347     *
1348     * @param SC_Query $objQuery SC_Query インスタンス
1349     * @param string $table テーブル名
1350     * @param string $id_name ID名
1351     * @param string $cat_name カテゴリ名
1352     * @param integer $old_catid 旧カテゴリID
1353     * @param integer $new_catid 新カテゴリID
1354     * @param integer $id ID
1355     * @return void
1356     */
1357    function sfMoveCatRank($objQuery, $table, $id_name, $cat_name, $old_catid, $new_catid, $id) {
1358        if ($old_catid == $new_catid) {
1359            return;
1360        }
1361        // 旧カテゴリでのランク削除処理
1362        // 移動レコードのランクを取得する。
1363        $where = "$id_name = ?";
1364        $rank = $objQuery->get($table, "rank", $where, array($id));
1365        // 削除レコードのランクより上のレコードを一つ下にずらす。
1366        $where = "rank > ? AND $cat_name = ?";
1367        $sqlup = "UPDATE $table SET rank = (rank - 1) WHERE $where";
1368        $objQuery->exec($sqlup, array($rank, $old_catid));
1369        // 新カテゴリでの登録処理
1370        // 新カテゴリの最大ランクを取得する。
1371        $max_rank = $objQuery->max($table, "rank", "$cat_name = ?", array($new_catid)) + 1;
1372        $where = "$id_name = ?";
1373        $sqlup = "UPDATE $table SET rank = ? WHERE $where";
1374        $objQuery->exec($sqlup, array($max_rank, $id));
1375    }
1376
1377    /**
1378     * お届け時間を取得する.
1379     *
1380     * @param integer $payment_id 支払い方法ID
1381     * @return array お届け時間の配列
1382     */
1383    function sfGetDelivTime($payment_id = "") {
1384        $objQuery =& SC_Query::getSingletonInstance();
1385
1386        $deliv_id = "";
1387        $arrRet = array();
1388
1389        if($payment_id != "") {
1390            $where = "del_flg = 0 AND payment_id = ?";
1391            $arrRet = $objQuery->select("deliv_id", "dtb_payment", $where, array($payment_id));
1392            $deliv_id = $arrRet[0]['deliv_id'];
1393        }
1394
1395        if($deliv_id != "") {
1396            $objQuery->setOrder("time_id");
1397            $where = "deliv_id = ?";
1398            $arrRet= $objQuery->select("time_id, deliv_time", "dtb_delivtime", $where, array($deliv_id));
1399        }
1400
1401        return $arrRet;
1402    }
1403
1404    /**
1405     * 都道府県、支払い方法から配送料金を取得する.
1406     *
1407     * @param array $arrData 各種情報
1408     * @return string 指定の都道府県, 支払い方法の配送料金
1409     */
1410    function sfGetDelivFee($arrData) {
1411        $pref = $arrData['deliv_pref'];
1412        $payment_id = isset($arrData['payment_id']) ? $arrData['payment_id'] : "";
1413
1414        $objQuery =& SC_Query::getSingletonInstance();
1415
1416        $deliv_id = "";
1417
1418        // 支払い方法が指定されている場合は、対応した配送業者を取得する
1419        if($payment_id != "") {
1420            $where = "del_flg = 0 AND payment_id = ?";
1421            $arrRet = $objQuery->select("deliv_id", "dtb_payment", $where, array($payment_id));
1422            $deliv_id = $arrRet[0]['deliv_id'];
1423        // 支払い方法が指定されていない場合は、先頭の配送業者を取得する
1424        } else {
1425            $where = "del_flg = 0";
1426            $objQuery->setOrder("rank DESC");
1427            $objQuery->setLimitOffset(1);
1428            $arrRet = $objQuery->select("deliv_id", "dtb_deliv", $where);
1429            $deliv_id = $arrRet[0]['deliv_id'];
1430        }
1431
1432        // 配送業者から配送料を取得
1433        if($deliv_id != "") {
1434
1435            // 都道府県が指定されていない場合は、東京都の番号を指定しておく
1436            if($pref == "") {
1437                $pref = 13;
1438            }
1439
1440            $objQuery =& SC_Query::getSingletonInstance();
1441            $where = "deliv_id = ? AND pref = ?";
1442            $arrRet= $objQuery->select("fee", "dtb_delivfee", $where, array($deliv_id, $pref));
1443        }
1444        return $arrRet[0]['fee'];
1445    }
1446
1447    /**
1448     * 集計情報を元に最終計算を行う.
1449     *
1450     * @param array $arrData 各種情報
1451     * @param LC_Page $objPage LC_Page インスタンス
1452     * @param SC_CartSession $objCartSess SC_CartSession インスタンス
1453     * @param null $dummy1 互換性確保用(決済モジュール互換のため)
1454     * @param SC_Customer $objCustomer SC_Customer インスタンス
1455     * @return array 最終計算後の配列
1456     */
1457    function sfTotalConfirm($arrData, &$objPage, &$objCartSess, $dummy1 = null, $objCustomer = "") {
1458        // 店舗基本情報を取得する
1459        $arrInfo = SC_Helper_DB_Ex::sf_getBasisData();
1460
1461        // 未定義変数を定義
1462        if (!isset($arrData['deliv_pref'])) $arrData['deliv_pref'] = "";
1463        if (!isset($arrData['payment_id'])) $arrData['payment_id'] = "";
1464        if (!isset($arrData['charge'])) $arrData['charge'] = "";
1465        if (!isset($arrData['use_point'])) $arrData['use_point'] = "";
1466        if (!isset($arrData['add_point'])) $arrData['add_point'] = 0;
1467
1468        // 税金の取得
1469        $arrData['tax'] = $objPage->tpl_total_tax;
1470        // 小計の取得
1471        $arrData['subtotal'] = $objPage->tpl_total_pretax;
1472
1473        // 合計送料の取得
1474        $arrData['deliv_fee'] = 0;
1475
1476        // 商品ごとの送料が有効の場合
1477        if (OPTION_PRODUCT_DELIV_FEE == 1) {
1478            // 全商品の合計送料を加算する
1479            $this->lfAddAllProductsDelivFee($arrData, $objPage, $objCartSess);
1480        }
1481
1482        // 配送業者の送料が有効の場合
1483        if (OPTION_DELIV_FEE == 1) {
1484            // 都道府県、支払い方法から配送料金を加算する
1485            $this->lfAddDelivFee($arrData);
1486        }
1487
1488        // 送料無料の購入数が設定されている場合
1489        if (DELIV_FREE_AMOUNT > 0) {
1490            // 商品の合計数量
1491            $total_quantity = $objCartSess->getTotalQuantity(true);
1492
1493            if($total_quantity >= DELIV_FREE_AMOUNT) {
1494                $arrData['deliv_fee'] = 0;
1495            }
1496        }
1497
1498        // ダウンロード商品のみの場合は送料無料
1499        if($this->chkCartDown($objCartSess)==2){
1500            $arrData['deliv_fee'] = 0;
1501        }
1502
1503        // 送料無料条件が設定されている場合
1504        if($arrInfo['free_rule'] > 0) {
1505            // 小計が無料条件を超えている場合
1506            if($arrData['subtotal'] >= $arrInfo['free_rule']) {
1507                $arrData['deliv_fee'] = 0;
1508            }
1509        }
1510
1511        // 合計の計算
1512        $arrData['total'] = $objPage->tpl_total_pretax; // 商品合計
1513        $arrData['total']+= $arrData['deliv_fee'];      // 送料
1514        $arrData['total']+= $arrData['charge'];         // 手数料
1515        // お支払い合計
1516        $arrData['payment_total'] = $arrData['total'] - ($arrData['use_point'] * POINT_VALUE);
1517        // 加算ポイントの計算
1518        if (USE_POINT !== false) {
1519            $arrData['add_point'] = SC_Helper_DB_Ex::sfGetAddPoint($objPage->tpl_total_point, $arrData['use_point']);
1520
1521            if($objCustomer != "") {
1522                // 誕生日月であった場合
1523                if($objCustomer->isBirthMonth()) {
1524                    $arrData['birth_point'] = BIRTH_MONTH_POINT;
1525                    $arrData['add_point'] += $arrData['birth_point'];
1526                }
1527            }
1528        }
1529
1530        if($arrData['add_point'] < 0) {
1531            $arrData['add_point'] = 0;
1532        }
1533        return $arrData;
1534    }
1535
1536    /**
1537     * レコードの存在チェックを行う.
1538     *
1539     * @param string $table テーブル名
1540     * @param string $col カラム名
1541     * @param array $arrval 要素の配列
1542     * @param array $addwhere SQL の AND 条件である WHERE 句
1543     * @return bool レコードが存在する場合 true
1544     */
1545    function sfIsRecord($table, $col, $arrval, $addwhere = "") {
1546        $objQuery =& SC_Query::getSingletonInstance();
1547        $arrCol = split("[, ]", $col);
1548
1549        $where = "del_flg = 0";
1550
1551        if($addwhere != "") {
1552            $where.= " AND $addwhere";
1553        }
1554
1555        foreach($arrCol as $val) {
1556            if($val != "") {
1557                if($where == "") {
1558                    $where = "$val = ?";
1559                } else {
1560                    $where.= " AND $val = ?";
1561                }
1562            }
1563        }
1564        $ret = $objQuery->get($table, $col, $where, $arrval);
1565
1566        if($ret != "") {
1567            return true;
1568        }
1569        return false;
1570    }
1571
1572    /**
1573     * メーカー商品数数の登録を行う.
1574     *
1575     * @param SC_Query $objQuery SC_Query インスタンス
1576     * @return void
1577     */
1578    function sfMaker_Count($objQuery){
1579        $sql = "";
1580
1581        //テーブル内容の削除
1582        $objQuery->query("DELETE FROM dtb_maker_count");
1583
1584        //各メーカーの商品数を数えて格納
1585        $sql = " INSERT INTO dtb_maker_count(maker_id, product_count, create_date) ";
1586        $sql .= " SELECT T1.maker_id, count(T2.maker_id), now() ";
1587        $sql .= " FROM dtb_maker AS T1 LEFT JOIN dtb_products AS T2";
1588        $sql .= " ON T1.maker_id = T2.maker_id ";
1589        $sql .= " WHERE T2.del_flg = 0 AND T2.status = 1 ";
1590        $sql .= " GROUP BY T1.maker_id, T2.maker_id ";
1591        $objQuery->query($sql);
1592    }
1593
1594    /**
1595     * 選択中の商品のメーカーを取得する.
1596     *
1597     * @param integer $product_id プロダクトID
1598     * @param integer $maker_id メーカーID
1599     * @return array 選択中の商品のメーカーIDの配列
1600     *
1601     */
1602    function sfGetMakerId($product_id, $maker_id = 0, $closed = false) {
1603        if ($closed) {
1604            $status = "";
1605        } else {
1606            $status = "status = 1";
1607        }
1608
1609        if (!$this->g_maker_on) {
1610            $this->g_maker_on = true;
1611            $maker_id = (int) $maker_id;
1612            $product_id = (int) $product_id;
1613            if (SC_Utils_Ex::sfIsInt($maker_id) && $maker_id != 0 && $this->sfIsRecord("dtb_maker","maker_id", $maker_id)) {
1614                $this->g_maker_id = array($maker_id);
1615            } else if (SC_Utils_Ex::sfIsInt($product_id) && $product_id != 0 && $this->sfIsRecord("dtb_products","product_id", $product_id, $status)) {
1616                $objQuery =& SC_Query::getSingletonInstance();
1617                $where = "product_id = ?";
1618                $maker_id = $objQuery->getCol("dtb_products", "maker_id", "product_id = ?", array($product_id));
1619                $this->g_maker_id = $maker_id;
1620            } else {
1621                // 不正な場合は、空の配列を返す。
1622                $this->g_maker_id = array();
1623            }
1624        }
1625        return $this->g_maker_id;
1626    }
1627
1628    /**
1629     * メーカーの取得を行う.
1630     *
1631     * $products_check:true商品登録済みのものだけ取得する
1632     *
1633     * @param string $addwhere 追加する WHERE 句
1634     * @param bool $products_check 商品の存在するカテゴリのみ取得する場合 true
1635     * @return array カテゴリツリーの配列
1636     */
1637    function sfGetMakerList($addwhere = "", $products_check = false) {
1638        $objQuery =& SC_Query::getSingletonInstance();
1639        $where = "del_flg = 0";
1640
1641        if($addwhere != "") {
1642            $where.= " AND $addwhere";
1643        }
1644
1645        $objQuery->setOption("ORDER BY rank DESC");
1646
1647        if($products_check) {
1648            $col = "T1.maker_id, name";
1649            $from = "dtb_maker AS T1 LEFT JOIN dtb_maker_count AS T2 ON T1.maker_id = T2.maker_id";
1650            $where .= " AND product_count > 0";
1651        } else {
1652            $col = "maker_id, name";
1653            $from = "dtb_maker";
1654        }
1655
1656        $arrRet = $objQuery->select($col, $from, $where);
1657
1658        $max = count($arrRet);
1659        for($cnt = 0; $cnt < $max; $cnt++) {
1660            $id = $arrRet[$cnt]['maker_id'];
1661            $name = $arrRet[$cnt]['name'];
1662            $arrList[$id].= $name;
1663        }
1664        return $arrList;
1665    }
1666
1667    /**
1668     * 全商品の合計送料を加算する
1669     */
1670    function lfAddAllProductsDelivFee(&$arrData, &$objPage, &$objCartSess) {
1671        $arrData['deliv_fee'] += $this->lfCalcAllProductsDelivFee($arrData, $objCartSess);
1672    }
1673
1674    /**
1675     * 全商品の合計送料を計算する
1676     */
1677    function lfCalcAllProductsDelivFee(&$arrData, &$objCartSess) {
1678        $objQuery =& SC_Query::getSingletonInstance();
1679        $deliv_fee_total = 0;
1680        $max = $objCartSess->getMax();
1681        for ($i = 0; $i <= $max; $i++) {
1682            // 商品送料
1683            $deliv_fee = $objQuery->getOne('SELECT deliv_fee FROM dtb_products WHERE product_id = ?', array($_SESSION[$objCartSess->key][$i]['id'][0]));
1684            // 数量
1685            $quantity = $_SESSION[$objCartSess->key][$i]['quantity'];
1686            // 累積
1687            $deliv_fee_total += $deliv_fee * $quantity;
1688        }
1689        return $deliv_fee_total;
1690    }
1691
1692    /**
1693     * 都道府県、支払い方法から配送料金を加算する.
1694     *
1695     * @param array $arrData 各種情報
1696     */
1697    function lfAddDelivFee(&$arrData) {
1698        $arrData['deliv_fee'] += $this->sfGetDelivFee($arrData);
1699    }
1700
1701    /**
1702     * 受注の名称列を更新する
1703     *
1704     * @param integer $order_id 更新対象の注文番号
1705     * @param boolean $temp_table 更新対象は「受注_Temp」か
1706     * @static
1707     */
1708    function sfUpdateOrderNameCol($order_id, $temp_table = false) {
1709        $objQuery =& SC_Query::getSingletonInstance();
1710
1711        if ($temp_table) {
1712            $tgt_table = 'dtb_order_temp';
1713            $sql_where = 'WHERE order_temp_id = ?';
1714        } else {
1715            $tgt_table = 'dtb_order';
1716            $sql_where = 'WHERE order_id = ?';
1717        }
1718
1719        $sql = <<< __EOS__
1720            UPDATE
1721                {$tgt_table}
1722            SET
1723                 payment_method = (SELECT payment_method FROM dtb_payment WHERE payment_id = {$tgt_table}.payment_id)
1724                ,deliv_time = (SELECT deliv_time FROM dtb_delivtime WHERE time_id = {$tgt_table}.deliv_time_id AND deliv_id = {$tgt_table}.deliv_id)
1725            $sql_where
1726__EOS__;
1727
1728        $objQuery->query($sql, array($order_id));
1729    }
1730
1731    /**
1732     * 店舗基本情報に基づいて税金額を返す
1733     *
1734     * @param integer $price 計算対象の金額
1735     * @return integer 税金額
1736     */
1737    function sfTax($price) {
1738        // 店舗基本情報を取得
1739        $CONF = SC_Helper_DB_Ex::sf_getBasisData();
1740
1741        return SC_Utils_Ex::sfTax($price, $CONF['tax'], $CONF['tax_rule']);
1742    }
1743
1744    /**
1745     * 店舗基本情報に基づいて税金付与した金額を返す
1746     *
1747     * @param integer $price 計算対象の金額
1748     * @return integer 税金付与した金額
1749     */
1750    function sfPreTax($price, $tax = null, $tax_rule = null) {
1751        // 店舗基本情報を取得
1752        $CONF = SC_Helper_DB_Ex::sf_getBasisData();
1753
1754        return SC_Utils_Ex::sfPreTax($price, $CONF['tax'], $CONF['tax_rule']);
1755    }
1756
1757    /**
1758     * 店舗基本情報に基づいて加算ポイントを返す
1759     *
1760     * @param integer $totalpoint
1761     * @param integer $use_point
1762     * @return integer 加算ポイント
1763     */
1764    function sfGetAddPoint($totalpoint, $use_point) {
1765        // 店舗基本情報を取得
1766        $CONF = SC_Helper_DB_Ex::sf_getBasisData();
1767
1768        return SC_Utils_Ex::sfGetAddPoint($totalpoint, $use_point, $CONF['point_rate']);
1769    }
1770
1771    /**
1772     * 受注.対応状況の更新
1773     *
1774     * ・必ず呼び出し元でトランザクションブロックを開いておくこと。
1775     *
1776     * @param integer $orderId 注文番号
1777     * @param integer|null $newStatus 対応状況 (null=変更無し)
1778     * @param integer|null $newAddPoint 加算ポイント (null=変更無し)
1779     * @param integer|null $newUsePoint 使用ポイント (null=変更無し)
1780     * @return void
1781     */
1782    function sfUpdateOrderStatus($orderId, $newStatus = null, $newAddPoint = null, $newUsePoint = null) {
1783        $objQuery =& SC_Query::getSingletonInstance();
1784
1785        $arrOrderOld = $objQuery->getRow('dtb_order', 'status, add_point, use_point, customer_id', 'order_id = ?', array($orderId));
1786
1787        // 対応状況が変更無しの場合、DB値を引き継ぐ
1788        if (is_null($newStatus)) {
1789            $newStatus = $arrOrderOld['status'];
1790        }
1791
1792        // 使用ポイント、DB値を引き継ぐ
1793        if (is_null($newUsePoint)) {
1794            $newUsePoint = $arrOrderOld['use_point'];
1795        }
1796
1797        // 加算ポイント、DB値を引き継ぐ
1798        if (is_null($newAddPoint)) {
1799            $newAddPoint = $arrOrderOld['add_point'];
1800        }
1801
1802        if (USE_POINT !== false) {
1803            // 顧客.ポイントの加減値
1804            $addCustomerPoint = 0;
1805
1806            // ▼使用ポイント
1807            // 変更前の対応状況が利用対象の場合、変更前の使用ポイント分を戻す
1808            if (SC_Utils_Ex::sfIsUsePoint($arrOrderOld['status'])) {
1809                $addCustomerPoint += $arrOrderOld['use_point'];
1810            }
1811
1812            // 変更後の対応状況が利用対象の場合、変更後の使用ポイント分を引く
1813            if (SC_Utils_Ex::sfIsUsePoint($newStatus)) {
1814                $addCustomerPoint -= $newUsePoint;
1815            }
1816            // ▲使用ポイント
1817
1818            // ▼加算ポイント
1819            // 変更前の対応状況が加算対象の場合、変更前の加算ポイント分を戻す
1820            if (SC_Utils_Ex::sfIsAddPoint($arrOrderOld['status'])) {
1821                $addCustomerPoint -= $arrOrderOld['add_point'];
1822            }
1823
1824            // 変更後の対応状況が加算対象の場合、変更後の加算ポイント分を足す
1825            if (SC_Utils_Ex::sfIsAddPoint($newStatus)) {
1826                $addCustomerPoint += $newAddPoint;
1827            }
1828            // ▲加算ポイント
1829
1830            if ($addCustomerPoint != 0) {
1831                // ▼顧客テーブルの更新
1832                $sqlval = array();
1833                $where = '';
1834                $arrVal = array();
1835                $arrRawSql = array();
1836                $arrRawSqlVal = array();
1837
1838                $sqlval['update_date'] = 'Now()';
1839                $arrRawSql['point'] = 'point + ?';
1840                $arrRawSqlVal[] = $addCustomerPoint;
1841                $where .= 'customer_id = ?';
1842                $arrVal[] = $arrOrderOld['customer_id'];
1843
1844                $objQuery->update('dtb_customer', $sqlval, $where, $arrVal, $arrRawSql, $arrRawSqlVal);
1845                // ▲顧客テーブルの更新
1846
1847                // 顧客.ポイントをマイナスした場合、
1848                if ($addCustomerPoint < 0) {
1849                    $sql = 'SELECT point FROM dtb_customer WHERE customer_id = ?';
1850                    $point = $objQuery->getOne($sql, array($arrOrderOld['customer_id']));
1851                    // 変更後の顧客.ポイントがマイナスの場合、
1852                    if ($point < 0) {
1853                        // ロールバック
1854                        $objQuery->rollback();
1855                        // エラー
1856                        SC_Utils_Ex::sfDispSiteError(LACK_POINT);
1857                    }
1858                }
1859            }
1860        }
1861
1862        // ▼受注テーブルの更新
1863        $sqlval = array();
1864        if (USE_POINT !== false) {
1865            $sqlval['add_point'] = $newAddPoint;
1866            $sqlval['use_point'] = $newUsePoint;
1867        }
1868        // ステータスが発送済みに変更の場合、発送日を更新
1869        if ($arrOrderOld['status'] != ORDER_DELIV && $newStatus == ORDER_DELIV) {
1870            $sqlval['commit_date'] = 'Now()';
1871        }
1872        $sqlval['status'] = $newStatus;
1873        $sqlval['update_date'] = 'Now()';
1874
1875        $objQuery->update('dtb_order', $sqlval, 'order_id = ?', array($orderId));
1876        // ▲受注テーブルの更新
1877    }
1878
1879    /**
1880     * 指定ファイルが存在する場合 SQL として実行
1881     *
1882     * XXX プラグイン用に追加。将来消すかも。
1883     *
1884     * @param string $sqlFilePath SQL ファイルのパス
1885     * @return void
1886     */
1887    function sfExecSqlByFile($sqlFilePath) {
1888        if (file_exists($sqlFilePath)) {
1889            $objQuery =& SC_Query::getSingletonInstance();
1890
1891            $sqls = file_get_contents($sqlFilePath);
1892            if ($sqls === false) SC_Utils_Ex::sfDispException('ファイルは存在するが読み込めない');
1893
1894            foreach (explode(';', $sqls) as $sql) {
1895                $sql = trim($sql);
1896                if (strlen($sql) == 0) continue;
1897                $objQuery->query($sql);
1898            }
1899        }
1900    }
1901
1902    /**
1903     * 商品規格を設定しているか
1904     *
1905     * @param integer $product_id 商品ID
1906     * @return bool 商品規格が存在する場合:true, それ以外:false
1907     */
1908    function sfHasProductClass($product_id) {
1909        if (!SC_Utils_Ex::sfIsInt($product_id)) return false;
1910
1911        $objQuery =& SC_Query::getSingletonInstance();
1912        $where = 'product_id = ? AND (classcategory_id1 <> 0 OR classcategory_id2 <> 0)';
1913        $count = $objQuery->count('dtb_products_class', $where, array($product_id));
1914
1915        return $count >= 1;
1916    }
1917    /**
1918     * カート内の商品の販売方法判定処理
1919     *
1920     * @param $objCartSess  SC_CartSession  カートセッション
1921     * @return  bool        0:ダウンロード販売無 1:ダウンロード販売無 2:全てダウンロード販売
1922     */
1923    function chkCartDown($objCartSess) {
1924        $objQuery =& SC_Query::getSingletonInstance();
1925        $down = false;
1926        $nodown = false;
1927        $ret = 0;
1928        $arrID = $objCartSess->getAllProductID();
1929        if(!is_null($arrID)){
1930            //カート内のIDから販売方法を取得
1931            foreach ($arrID as $rec) {
1932                $arrRet = $objQuery->select("down", "dtb_products", "product_id = " . $rec);
1933                if ($arrRet[0]['down'] == "2"){
1934                    $down = true;
1935                }else{
1936                    $nodown = true;
1937                }
1938            }
1939        }
1940        //混在
1941        if($nodown && $down){
1942            $ret = 1;
1943        //全てダウンロード商品
1944        }else if(!$nodown && $down){
1945            $ret = 2;
1946        }
1947        return $ret;
1948    }
1949
1950    /* 会員情報の住所を一時受注テーブルへ */
1951    function sfRegistDelivData($uniqid, $objCustomer) {
1952
1953        // 登録データの作成
1954        $sqlval['order_temp_id'] = $uniqid;
1955        $sqlval['update_date'] = 'Now()';
1956        $sqlval['customer_id'] = $objCustomer->getValue('customer_id');
1957        $sqlval['deliv_check'] = '-1';
1958        $sqlval['order_name01'] = $objCustomer->getValue('name01');
1959        $sqlval['order_name02'] = $objCustomer->getValue('name02');
1960        $sqlval['order_kana01'] = $objCustomer->getValue('kana01');
1961        $sqlval['order_kana02'] = $objCustomer->getValue('kana02');
1962        $sqlval['order_zip01'] = $objCustomer->getValue('zip01');
1963        $sqlval['order_zip02'] = $objCustomer->getValue('zip02');
1964        $sqlval['order_pref'] = $objCustomer->getValue('pref');
1965        $sqlval['order_addr01'] = $objCustomer->getValue('addr01');
1966        $sqlval['order_addr02'] = $objCustomer->getValue('addr02');
1967        $sqlval['order_tel01'] = $objCustomer->getValue('tel01');
1968        $sqlval['order_tel02'] = $objCustomer->getValue('tel02');
1969        $sqlval['order_tel03'] = $objCustomer->getValue('tel03');
1970        $sqlval['order_fax01'] = $objCustomer->getValue('fax01');
1971        $sqlval['order_fax02'] = $objCustomer->getValue('fax02');
1972        $sqlval['order_fax03'] = $objCustomer->getValue('fax03');
1973        $sqlval['deliv_name01'] = $objCustomer->getValue('name01');
1974        $sqlval['deliv_name02'] = $objCustomer->getValue('name02');
1975        $sqlval['deliv_kana01'] = $objCustomer->getValue('kana01');
1976        $sqlval['deliv_kana02'] = $objCustomer->getValue('kana02');
1977        $sqlval['deliv_zip01'] = $objCustomer->getValue('zip01');
1978        $sqlval['deliv_zip02'] = $objCustomer->getValue('zip02');
1979        $sqlval['deliv_pref'] = $objCustomer->getValue('pref');
1980        $sqlval['deliv_addr01'] = $objCustomer->getValue('addr01');
1981        $sqlval['deliv_addr02'] = $objCustomer->getValue('addr02');
1982        $sqlval['deliv_tel01'] = $objCustomer->getValue('tel01');
1983        $sqlval['deliv_tel02'] = $objCustomer->getValue('tel02');
1984        $sqlval['deliv_tel03'] = $objCustomer->getValue('tel03');
1985        $sqlval['deliv_fax01'] = $objCustomer->getValue('fax01');
1986        $sqlval['deliv_fax02'] = $objCustomer->getValue('fax02');
1987        $sqlval['deliv_fax03'] = $objCustomer->getValue('fax03');
1988
1989        $this->sfRegistTempOrder($uniqid, $sqlval);
1990    }
1991
1992
1993}
1994?>
Note: See TracBrowser for help on using the repository browser.