source: branches/version-2_11-dev/data/class/helper/SC_Helper_DB.php @ 21328

Revision 21328, 51.3 KB checked in by Seasoft, 12 years ago (diff)

#1545 (SC_Product#alldtlSQL dtb_products_class.del_flg = 0 を常に付加する)

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
  • 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-2011 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     * チェック対象のテーブルに, 該当のカラムが存在するかチェックする.
54     * 引数 $add が true の場合, 該当のカラムが存在しない場合は, カラムの生成を行う.
55     * カラムの生成も行う場合は, $col_type も必須となる.
56     *
57     * @param string $table_name テーブル名
58     * @param string $column_name カラム名
59     * @param string $col_type カラムのデータ型
60     * @param string $dsn データソース名
61     * @param bool $add カラムの作成も行う場合 true
62     * @return bool カラムが存在する場合とカラムの生成に成功した場合 true,
63     *               テーブルが存在しない場合 false,
64     *               引数 $add == false でカラムが存在しない場合 false
65     */
66    function sfColumnExists($table_name, $col_name, $col_type = "", $dsn = "", $add = false) {
67        $dbFactory = SC_DB_DBFactory_Ex::getInstance();
68        $dsn = $dbFactory->getDSN($dsn);
69
70        $objQuery =& SC_Query_Ex::getSingletonInstance($dsn);
71
72        // テーブルが無ければエラー
73        if(!in_array($table_name, $objQuery->listTables())) return false;
74
75        // 正常に接続されている場合
76        if(!$objQuery->isError()) {
77            list($db_type) = explode(":", $dsn);
78
79            // カラムリストを取得
80            $columns = $objQuery->listTableFields($table_name);
81
82            if(in_array($col_name, $columns)){
83                return true;
84            }
85        }
86
87        // カラムを追加する
88        if($add){
89            $objQuery->query("ALTER TABLE $table_name ADD $col_name $col_type ");
90            return true;
91        }
92        return false;
93    }
94
95    /**
96     * データの存在チェックを行う.
97     *
98     * @param string $table_name テーブル名
99     * @param string $where データを検索する WHERE 句
100     * @param string $dsn データソース名
101     * @param string $sql データの追加を行う場合の SQL文
102     * @param bool $add データの追加も行う場合 true
103     * @return bool データが存在する場合 true, データの追加に成功した場合 true,
104     *               $add == false で, データが存在しない場合 false
105     */
106    function sfDataExists($table_name, $where, $arrval, $dsn = "", $sql = "", $add = false) {
107        $dbFactory = SC_DB_DBFactory_Ex::getInstance();
108        $dsn = $dbFactory->getDSN($dsn);
109
110        $objQuery =& SC_Query_Ex::getSingletonInstance();
111        $count = $objQuery->count($table_name, $where, $arrval);
112
113        if($count > 0) {
114            $ret = true;
115        } else {
116            $ret = false;
117        }
118        // データを追加する
119        if(!$ret && $add) {
120            $objQuery->exec($sql);
121        }
122        return $ret;
123    }
124
125    /**
126     * 店舗基本情報を取得する.
127     *
128     * 引数 $force が false の場合は, 初回のみ DB 接続し,
129     * 2回目以降はキャッシュされた結果を使用する.
130     *
131     * @param boolean $force 強制的にDB取得するか
132     * @param string $col 取得カラムを指定する
133     * @return array 店舗基本情報の配列
134     */
135    function sfGetBasisData($force = false, $col = "") {
136        static $data;
137
138        if ($force || !isset($data)) {
139            $objQuery =& SC_Query_Ex::getSingletonInstance();
140
141            if ($col === "") {
142                $arrRet = $objQuery->select('*', 'dtb_baseinfo');
143            } else {
144                $arrRet = $objQuery->select($col, "dtb_baseinfo");
145            }
146
147            if (isset($arrRet[0])) {
148                $data = $arrRet[0];
149            } else {
150                $data = array();
151            }
152        }
153        return $data;
154    }
155
156    /**
157     * 基本情報の登録数を取得する
158     *
159     * @return int
160     */
161    function sfGetBasisCount() {
162        $objQuery =& SC_Query_Ex::getSingletonInstance();
163
164        return $objQuery->count("dtb_baseinfo");
165    }
166
167    /* 選択中のアイテムのルートカテゴリIDを取得する */
168    function sfGetRootId() {
169
170        if(!$this->g_root_on)   {
171            $this->g_root_on = true;
172            $objQuery =& SC_Query_Ex::getSingletonInstance();
173
174            if (!isset($_GET['product_id'])) $_GET['product_id'] = "";
175            if (!isset($_GET['category_id'])) $_GET['category_id'] = "";
176
177            if(!empty($_GET['product_id']) || !empty($_GET['category_id'])) {
178                // 選択中のカテゴリIDを判定する
179                $category_id = $this->sfGetCategoryId($_GET['product_id'], $_GET['category_id']);
180                // ROOTカテゴリIDの取得
181                if(count($category_id) > 0) {
182                    $arrRet = $this->sfGetParents('dtb_category', 'parent_category_id', 'category_id', $category_id);
183                    $root_id = isset($arrRet[0]) ? $arrRet[0] : "";
184                } else {
185                    $root_id = "";
186                }
187            } else {
188                // ROOTカテゴリIDをなしに設定する
189                $root_id = "";
190            }
191            $this->g_root_id = $root_id;
192        }
193        return $this->g_root_id;
194    }
195
196    /**
197     * 受注番号、最終ポイント、加算ポイント、利用ポイントから「オーダー前ポイント」を取得する
198     *
199     * @param integer $order_id 受注番号
200     * @param integer $use_point 利用ポイント
201     * @param integer $add_point 加算ポイント
202     * @param integer $order_status 対応状況
203     * @return array オーダー前ポイントの配列
204     */
205    function sfGetRollbackPoint($order_id, $use_point, $add_point, $order_status) {
206        $objQuery = new SC_Query_Ex();
207        $arrRet = $objQuery->select("customer_id", "dtb_order", "order_id = ?", array($order_id));
208        $customer_id = $arrRet[0]['customer_id'];
209        if($customer_id != "" && $customer_id >= 1) {
210            $arrRet = $objQuery->select('point', "dtb_customer", "customer_id = ?", array($customer_id));
211            $point = $arrRet[0]['point'];
212            $rollback_point = $arrRet[0]['point'];
213
214            // 対応状況がポイント利用対象の場合、使用ポイント分を戻す
215            if (SC_Helper_Purchase_Ex::isUsePoint($order_status)) {
216                $rollback_point += $use_point;
217            }
218
219            // 対応状況がポイント加算対象の場合、加算ポイント分を戻す
220            if (SC_Helper_Purchase_Ex::isAddPoint($order_status)) {
221                $rollback_point -= $add_point;
222            }
223        } else {
224            $rollback_point = "";
225            $point = "";
226        }
227        return array($point, $rollback_point);
228    }
229
230    /**
231     * カテゴリツリーの取得を行う.
232     *
233     * @param integer $parent_category_id 親カテゴリID
234     * @param bool $count_check 登録商品数のチェックを行う場合 true
235     * @return array カテゴリツリーの配列
236     */
237    function sfGetCatTree($parent_category_id, $count_check = false) {
238        $objQuery =& SC_Query_Ex::getSingletonInstance();
239        $col = "";
240        $col .= " cat.category_id,";
241        $col .= " cat.category_name,";
242        $col .= " cat.parent_category_id,";
243        $col .= " cat.level,";
244        $col .= " cat.rank,";
245        $col .= " cat.creator_id,";
246        $col .= " cat.create_date,";
247        $col .= " cat.update_date,";
248        $col .= " cat.del_flg, ";
249        $col .= " ttl.product_count";
250        $from = "dtb_category as cat left join dtb_category_total_count as ttl on ttl.category_id = cat.category_id";
251        // 登録商品数のチェック
252        if($count_check) {
253            $where = "del_flg = 0 AND product_count > 0";
254        } else {
255            $where = "del_flg = 0";
256        }
257        $objQuery->setOption("ORDER BY rank DESC");
258        $arrRet = $objQuery->select($col, $from, $where);
259
260        $arrParentID = SC_Helper_DB_Ex::sfGetParents('dtb_category', 'parent_category_id', 'category_id', $parent_category_id);
261
262        foreach($arrRet as $key => $array) {
263            foreach($arrParentID as $val) {
264                if($array['category_id'] == $val) {
265                    $arrRet[$key]['display'] = 1;
266                    break;
267                }
268            }
269        }
270
271        return $arrRet;
272    }
273
274    /**
275     * カテゴリツリーを走査し, パンくずリスト用の配列を生成する.
276     *
277     * @param array カテゴリの配列
278     * @param integer $parent 上位カテゴリID
279     * @param array パンくずリスト用の配列
280     * @result void
281     * @see sfGetCatTree()
282     */
283    function findTree(&$arrTree, $parent, &$result) {
284        if ($result[count($result) - 1]['parent_category_id'] === 0) {
285            return;
286        } else {
287            foreach ($arrTree as $key => $val) {
288               if ($val['category_id'] == $parent) {
289                    $result[] = array('category_id' => $val['category_id'],
290                                      'parent_category_id' => (int) $val['parent_category_id'],
291                                      'category_name' => $val['category_name']);
292                    $this->findTree($arrTree, $val['parent_category_id'], $result);
293               }
294            }
295        }
296    }
297
298    /**
299     * カテゴリツリーの取得を複数カテゴリで行う.
300     *
301     * @param integer $product_id 商品ID
302     * @param bool $count_check 登録商品数のチェックを行う場合 true
303     * @return array カテゴリツリーの配列
304     */
305    function sfGetMultiCatTree($product_id, $count_check = false) {
306        $objQuery =& SC_Query_Ex::getSingletonInstance();
307        $col = "";
308        $col .= " cat.category_id,";
309        $col .= " cat.category_name,";
310        $col .= " cat.parent_category_id,";
311        $col .= " cat.level,";
312        $col .= " cat.rank,";
313        $col .= " cat.creator_id,";
314        $col .= " cat.create_date,";
315        $col .= " cat.update_date,";
316        $col .= " cat.del_flg, ";
317        $col .= " ttl.product_count";
318        $from = "dtb_category as cat left join dtb_category_total_count as ttl on ttl.category_id = cat.category_id";
319        // 登録商品数のチェック
320        if($count_check) {
321            $where = "del_flg = 0 AND product_count > 0";
322        } else {
323            $where = "del_flg = 0";
324        }
325        $objQuery->setOption("ORDER BY rank DESC");
326        $arrRet = $objQuery->select($col, $from, $where);
327
328        $arrCategory_id = SC_Helper_DB_Ex::sfGetCategoryId($product_id);
329
330        $arrCatTree = array();
331        foreach ($arrCategory_id as $pkey => $parent_category_id) {
332            $arrParentID = SC_Helper_DB_Ex::sfGetParents('dtb_category', 'parent_category_id', 'category_id', $parent_category_id);
333
334            foreach($arrParentID as $pid) {
335                foreach($arrRet as $key => $array) {
336                    if($array['category_id'] == $pid) {
337                        $arrCatTree[$pkey][] = $arrRet[$key];
338                        break;
339                    }
340                }
341            }
342        }
343
344        return $arrCatTree;
345    }
346
347    /**
348     * 親カテゴリを連結した文字列を取得する.
349     *
350     * @param integer $category_id カテゴリID
351     * @return string 親カテゴリを連結した文字列
352     */
353    function sfGetCatCombName($category_id){
354        // 商品が属するカテゴリIDを縦に取得
355        $objQuery =& SC_Query_Ex::getSingletonInstance();
356        $arrCatID = $this->sfGetParents("dtb_category", "parent_category_id", "category_id", $category_id);
357        $ConbName = "";
358
359        // カテゴリ名称を取得する
360        foreach($arrCatID as $key => $val){
361            $sql = "SELECT category_name FROM dtb_category WHERE category_id = ?";
362            $arrVal = array($val);
363            $CatName = $objQuery->getOne($sql,$arrVal);
364            $ConbName .= $CatName . ' | ';
365        }
366        // 最後の | をカットする
367        $ConbName = substr_replace($ConbName, "", strlen($ConbName) - 2, 2);
368
369        return $ConbName;
370    }
371
372    /**
373     * 指定したカテゴリIDのカテゴリを取得する.
374     *
375     * @param integer $category_id カテゴリID
376     * @return array 指定したカテゴリIDのカテゴリ
377     */
378    function sfGetCat($category_id){
379        $objQuery =& SC_Query_Ex::getSingletonInstance();
380
381        // カテゴリを取得する
382        $arrVal = array($category_id);
383        $res = $objQuery->select('category_id AS id, category_name AS name', 'dtb_category', 'category_id = ?', $arrVal);
384
385        return $res[0];
386    }
387
388    /**
389     * 指定したカテゴリIDの大カテゴリを取得する.
390     *
391     * @param integer $category_id カテゴリID
392     * @return array 指定したカテゴリIDの大カテゴリ
393     */
394    function sfGetFirstCat($category_id){
395        // 商品が属するカテゴリIDを縦に取得
396        $objQuery =& SC_Query_Ex::getSingletonInstance();
397        $arrRet = array();
398        $arrCatID = $this->sfGetParents("dtb_category", "parent_category_id", "category_id", $category_id);
399        $arrRet['id'] = $arrCatID[0];
400
401        // カテゴリ名称を取得する
402        $sql = "SELECT category_name FROM dtb_category WHERE category_id = ?";
403        $arrVal = array($arrRet['id']);
404        $arrRet['name'] = $objQuery->getOne($sql,$arrVal);
405
406        return $arrRet;
407    }
408
409    /**
410     * カテゴリツリーの取得を行う.
411     *
412     * $products_check:true商品登録済みのものだけ取得する
413     *
414     * @param string $addwhere 追加する WHERE 句
415     * @param bool $products_check 商品の存在するカテゴリのみ取得する場合 true
416     * @param string $head カテゴリ名のプレフィックス文字列
417     * @return array カテゴリツリーの配列
418     */
419    function sfGetCategoryList($addwhere = "", $products_check = false, $head = CATEGORY_HEAD) {
420        $objQuery =& SC_Query_Ex::getSingletonInstance();
421        $where = "del_flg = 0";
422
423        if($addwhere != "") {
424            $where.= " AND $addwhere";
425        }
426
427        $objQuery->setOption("ORDER BY rank DESC");
428
429        if($products_check) {
430            $col = "T1.category_id, category_name, level";
431            $from = "dtb_category AS T1 LEFT JOIN dtb_category_total_count AS T2 ON T1.category_id = T2.category_id";
432            $where .= " AND product_count > 0";
433        } else {
434            $col = "category_id, category_name, level";
435            $from = "dtb_category";
436        }
437
438        $arrRet = $objQuery->select($col, $from, $where);
439
440        $max = count($arrRet);
441        for($cnt = 0; $cnt < $max; $cnt++) {
442            $id = $arrRet[$cnt]['category_id'];
443            $name = $arrRet[$cnt]['category_name'];
444            $arrList[$id] = str_repeat($head, $arrRet[$cnt]['level']) . $name;
445        }
446        return $arrList;
447    }
448
449    /**
450     * カテゴリツリーの取得を行う.
451     *
452     * 親カテゴリの Value=0 を対象とする
453     *
454     * @param bool $parent_zero 親カテゴリの Value=0 の場合 true
455     * @return array カテゴリツリーの配列
456     */
457    function sfGetLevelCatList($parent_zero = true) {
458        $objQuery =& SC_Query_Ex::getSingletonInstance();
459
460        // カテゴリ名リストを取得
461        $col = "category_id, parent_category_id, category_name";
462        $where = "del_flg = 0";
463        $objQuery->setOption("ORDER BY level");
464        $arrRet = $objQuery->select($col, "dtb_category", $where);
465        $arrCatName = array();
466        foreach ($arrRet as $arrTmp) {
467            $arrCatName[$arrTmp['category_id']] =
468                (($arrTmp['parent_category_id'] > 0)?
469                    $arrCatName[$arrTmp['parent_category_id']] : "")
470                . CATEGORY_HEAD . $arrTmp['category_name'];
471        }
472
473        $col = "category_id, parent_category_id, category_name, level";
474        $where = "del_flg = 0";
475        $objQuery->setOption("ORDER BY rank DESC");
476        $arrRet = $objQuery->select($col, "dtb_category", $where);
477        $max = count($arrRet);
478
479        for($cnt = 0; $cnt < $max; $cnt++) {
480            if($parent_zero) {
481                if($arrRet[$cnt]['level'] == LEVEL_MAX) {
482                    $arrValue[$cnt] = $arrRet[$cnt]['category_id'];
483                } else {
484                    $arrValue[$cnt] = "";
485                }
486            } else {
487                $arrValue[$cnt] = $arrRet[$cnt]['category_id'];
488            }
489
490            $arrOutput[$cnt] = $arrCatName[$arrRet[$cnt]['category_id']];
491        }
492
493        return array($arrValue, $arrOutput);
494    }
495
496    /**
497     * 選択中の商品のカテゴリを取得する.
498     *
499     * @param integer $product_id プロダクトID
500     * @param integer $category_id カテゴリID
501     * @return array 選択中の商品のカテゴリIDの配列
502     *
503     */
504    function sfGetCategoryId($product_id, $category_id = 0, $closed = false) {
505        if ($closed) {
506            $status = "";
507        } else {
508            $status = "status = 1";
509        }
510        $category_id = (int) $category_id;
511        $product_id = (int) $product_id;
512        if (SC_Utils_Ex::sfIsInt($category_id) && $category_id != 0 && SC_Helper_DB_Ex::sfIsRecord("dtb_category","category_id", $category_id)) {
513            $category_id = array($category_id);
514        } else if (SC_Utils_Ex::sfIsInt($product_id) && $product_id != 0 && SC_Helper_DB_Ex::sfIsRecord("dtb_products","product_id", $product_id, $status)) {
515            $objQuery =& SC_Query_Ex::getSingletonInstance();
516            $where = "product_id = ?";
517            $category_id = $objQuery->getCol("category_id", "dtb_product_categories", "product_id = ?", array($product_id));
518        } else {
519            // 不正な場合は、空の配列を返す。
520            $category_id = array();
521        }
522        return $category_id;
523    }
524
525    /**
526     * 商品をカテゴリの先頭に追加する.
527     *
528     * @param integer $category_id カテゴリID
529     * @param integer $product_id プロダクトID
530     * @return void
531     */
532    function addProductBeforCategories($category_id, $product_id) {
533
534        $sqlval = array("category_id" => $category_id,
535                        "product_id" => $product_id);
536
537        $objQuery =& SC_Query_Ex::getSingletonInstance();
538
539        // 現在の商品カテゴリを取得
540        $arrCat = $objQuery->select("product_id, category_id, rank",
541                                    "dtb_product_categories",
542                                    "category_id = ?",
543                                    array($category_id));
544
545        $max = "0";
546        foreach ($arrCat as $val) {
547            // 同一商品が存在する場合は登録しない
548            if ($val["product_id"] == $product_id) {
549                return;
550            }
551            // 最上位ランクを取得
552            $max = ($max < $val['rank']) ? $val['rank'] : $max;
553        }
554        $sqlval['rank'] = $max + 1;
555        $objQuery->insert("dtb_product_categories", $sqlval);
556    }
557
558    /**
559     * 商品をカテゴリの末尾に追加する.
560     *
561     * @param integer $category_id カテゴリID
562     * @param integer $product_id プロダクトID
563     * @return void
564     */
565    function addProductAfterCategories($category_id, $product_id) {
566        $sqlval = array("category_id" => $category_id,
567                        "product_id" => $product_id);
568
569        $objQuery =& SC_Query_Ex::getSingletonInstance();
570
571        // 現在の商品カテゴリを取得
572        $arrCat = $objQuery->select("product_id, category_id, rank",
573                                    "dtb_product_categories",
574                                    "category_id = ?",
575                                    array($category_id));
576
577        $min = 0;
578        foreach ($arrCat as $val) {
579            // 同一商品が存在する場合は登録しない
580            if ($val["product_id"] == $product_id) {
581                return;
582            }
583            // 最下位ランクを取得
584            $min = ($min < $val['rank']) ? $val['rank'] : $min;
585        }
586        $sqlval['rank'] = $min;
587        $objQuery->insert("dtb_product_categories", $sqlval);
588    }
589
590    /**
591     * 商品をカテゴリから削除する.
592     *
593     * @param integer $category_id カテゴリID
594     * @param integer $product_id プロダクトID
595     * @return void
596     */
597    function removeProductByCategories($category_id, $product_id) {
598        $objQuery =& SC_Query_Ex::getSingletonInstance();
599        $objQuery->delete("dtb_product_categories",
600                          "category_id = ? AND product_id = ?", array($category_id, $product_id));
601    }
602
603    /**
604     * 商品カテゴリを更新する.
605     *
606     * @param array $arrCategory_id 登録するカテゴリIDの配列
607     * @param integer $product_id プロダクトID
608     * @return void
609     */
610    function updateProductCategories($arrCategory_id, $product_id) {
611        $objQuery =& SC_Query_Ex::getSingletonInstance();
612
613        // 現在のカテゴリ情報を取得
614        $arrCurrentCat = $objQuery->select("product_id, category_id, rank",
615                                           "dtb_product_categories",
616                                           "product_id = ?",
617                                           array($product_id));
618
619        // 登録するカテゴリ情報と比較
620        foreach ($arrCurrentCat as $val) {
621
622            // 登録しないカテゴリを削除
623            if (!in_array($val["category_id"], $arrCategory_id)) {
624                $this->removeProductByCategories($val["category_id"], $product_id);
625            }
626        }
627
628        // カテゴリを登録
629        foreach ($arrCategory_id as $category_id) {
630            $this->addProductBeforCategories($category_id, $product_id);
631        }
632    }
633
634    /**
635     * カテゴリ数の登録を行う.
636     *
637     *
638     * @param SC_Query $objQuery SC_Query インスタンス
639     * @param boolean $is_force_all_count 全カテゴリの集計を強制する場合 true
640     * @return void
641     */
642    function sfCountCategory($objQuery = NULL, $is_force_all_count = false){
643        $objProduct = new SC_Product_Ex();
644
645        if($objQuery == NULL) {
646            $objQuery =& SC_Query_Ex::getSingletonInstance();
647        }
648
649        $is_out_trans = false;
650        if(!$objQuery->inTransaction()){
651            $objQuery->begin();
652            $is_out_trans = true;
653        }
654
655        //共通のfrom/where文の構築
656        $sql_where = 'alldtl.del_flg = 0 AND alldtl.status = 1';
657        // 在庫無し商品の非表示
658        if (NOSTOCK_HIDDEN) {
659            $where_products_class = '(stock >= 1 OR stock_unlimited = 1)';
660            $from = $objProduct->alldtlSQL($where_products_class);
661        }else{
662            $from = 'dtb_products as alldtl';
663        }
664
665        //dtb_category_countの構成
666        // 各カテゴリに所属する商品の数を集計。集計対象には子カテゴリを含まない。
667
668        //まずテーブル内容の元を取得
669        if(!$is_force_all_count) {
670            $arrCategoryCountOld = $objQuery->select('category_id,product_count','dtb_category_count');
671        }else{
672            $arrCategoryCountOld = array();
673        }
674
675        //各カテゴリ内の商品数を数えて取得
676        $sql = <<< __EOS__
677            SELECT T1.category_id, count(T2.category_id) as product_count
678            FROM dtb_category AS T1
679                LEFT JOIN dtb_product_categories AS T2
680                    ON T1.category_id = T2.category_id
681                LEFT JOIN $from
682                    ON T2.product_id = alldtl.product_id
683            WHERE $sql_where
684            GROUP BY T1.category_id, T2.category_id
685__EOS__;
686
687        $arrCategoryCountNew = $objQuery->getAll($sql);
688        // 各カテゴリに所属する商品の数を集計。集計対象には子カテゴリを「含む」。
689        //差分を取得して、更新対象カテゴリだけを確認する。
690
691        //各カテゴリ毎のデータ値において以前との差を見る
692        //古いデータの構造入れ替え
693        $arrOld = array();
694        foreach($arrCategoryCountOld as $item){
695            $arrOld[$item['category_id']] = $item['product_count'];
696        }
697        //新しいデータの構造入れ替え
698        $arrNew = array();
699        foreach($arrCategoryCountNew as $item){
700            $arrNew[$item['category_id']] = $item['product_count'];
701        }
702
703        $arrDiffCategory_id = array();
704        //新しいカテゴリ一覧から見て商品数が異なるデータが無いか確認
705        foreach($arrNew as $cid => $count){
706            if($arrOld[$cid] != $count){
707                $arrDiffCategory_id[] = $cid;
708            }
709        }
710        //削除カテゴリを想定して、古いカテゴリ一覧から見て商品数が異なるデータが無いか確認。
711        foreach($arrOld as $cid => $count){
712            if($arrNew[$cid] != $count && $count > 0){
713                $arrDiffCategory_id[] = $cid;
714            }
715        }
716
717        //対象IDが無ければ終了
718        if(count($arrDiffCategory_id) == 0){
719            if($is_out_trans) {
720                $objQuery->commit();
721            }
722            return;
723        }
724
725        //差分対象カテゴリIDの重複を除去
726        $arrDiffCategory_id = array_unique($arrDiffCategory_id);
727
728        //dtb_category_countの更新 差分のあったカテゴリだけ更新する。
729        foreach($arrDiffCategory_id as $cid) {
730            $sqlval = array();
731            $sqlval['create_date'] = 'CURRENT_TIMESTAMP';
732            $sqlval['product_count'] = (string)$arrNew[$cid];
733            if($sqlval['product_count'] =="") {
734                $sqlval['product_count'] = (string)'0';
735            }
736            if(isset($arrOld[$cid])) {
737                $objQuery->update('dtb_category_count', $sqlval, 'category_id = ?', array($cid));
738            }else{
739                if ($is_force_all_count) {
740                    $ret = $objQuery->update('dtb_category_count', $sqlval, 'category_id = ?', array($cid));
741                    if ($ret > 0) {
742                        continue;
743                    }
744                }
745                $sqlval['category_id'] = $cid;
746                $objQuery->insert('dtb_category_count', $sqlval);
747            }
748        }
749
750        //差分があったIDとその親カテゴリIDのリストを取得する
751        $arrTgtCategory_id = array();
752        foreach ($arrDiffCategory_id as $parent_category_id) {
753            $arrTgtCategory_id[] = $parent_category_id;
754            $arrParentID = $this->sfGetParents('dtb_category', 'parent_category_id', 'category_id', $parent_category_id);
755            $arrTgtCategory_id = array_merge($arrTgtCategory_id, $arrParentID);
756        }
757
758        //重複を取り除く
759        $arrTgtCategory_id = array_unique($arrTgtCategory_id);
760
761        //dtb_category_total_count 集計処理開始
762        //更新対象カテゴリIDだけ集計しなおす。
763        $arrUpdateData = array();
764        foreach ($arrTgtCategory_id as $category_id) {
765            $arrval = array();
766            list($tmp_where, $tmp_arrval) = $this->sfGetCatWhere($category_id);
767            if ($tmp_where != "") {
768                $sql_where_product_ids = "product_id IN (SELECT product_id FROM dtb_product_categories WHERE " . $tmp_where . ")";
769                $arrval = array_merge((array)$tmp_arrval, (array)$tmp_arrval);
770            } else {
771                $sql_where_product_ids = '0<>0'; // 一致させない
772            }
773            $where = "($sql_where) AND ($sql_where_product_ids)";
774
775            $where_products_class = '';
776            if (NOSTOCK_HIDDEN) {
777                $where_products_class .= '(stock >= 1 OR stock_unlimited = 1)';
778            }
779
780            $from = $objProduct->alldtlSQL($where_products_class);
781            $sql = "SELECT count(*) FROM $from WHERE $where ";
782            $arrUpdateData[ $category_id ] = $objQuery->getOne($sql, $arrval);
783        }
784        // 更新対象だけを更新。
785        foreach($arrUpdateData as $cid => $count) {
786            $sqlval = array();
787            $sqlval['create_date'] = 'CURRENT_TIMESTAMP';
788            $sqlval['product_count'] = $count;
789            if($sqlval['product_count'] =="") {
790                $sqlval['product_count'] = (string)'0';
791            }
792            $ret = $objQuery->update('dtb_category_total_count', $sqlval, 'category_id = ?', array($cid));
793            if(!$ret) {
794                $sqlval['category_id'] = $cid;
795                $ret = $objQuery->insert('dtb_category_total_count', $sqlval);
796            }
797        }
798        // トランザクション終了処理
799        if($is_out_trans) {
800            $objQuery->commit();
801        }
802    }
803
804    /**
805     * 子IDの配列を返す.
806     *
807     * @param string $table テーブル名
808     * @param string $pid_name 親ID名
809     * @param string $id_name ID名
810     * @param integer $id ID
811     * @param array 子ID の配列
812     */
813    function sfGetChildsID($table, $pid_name, $id_name, $id) {
814        $arrRet = $this->sfGetChildrenArray($table, $pid_name, $id_name, $id);
815        return $arrRet;
816    }
817
818    /**
819     * 階層構造のテーブルから子ID配列を取得する.
820     *
821     * @param string $table テーブル名
822     * @param string $pid_name 親ID名
823     * @param string $id_name ID名
824     * @param integer $id ID番号
825     * @return array 子IDの配列
826     */
827    function sfGetChildrenArray($table, $pid_name, $id_name, $id) {
828        $arrChildren = array();
829        $arrRet = array($id);
830
831        while(count($arrRet) > 0) {
832            $arrChildren = array_merge($arrChildren, $arrRet);
833            $arrRet = SC_Helper_DB_Ex::sfGetChildrenArraySub($table, $pid_name, $id_name, $arrRet);
834        }
835
836        return $arrChildren;
837    }
838
839    /**
840     * 親ID直下の子IDをすべて取得する.
841     *
842     * @param array $arrData 親カテゴリの配列
843     * @param string $pid_name 親ID名
844     * @param string $id_name ID名
845     * @param array $arrPID 親IDの配列
846     * @return array 子IDの配列
847     */
848    function sfGetChildrenArraySub($table, $pid_name, $id_name, $arrPID) {
849        $objQuery =& SC_Query_Ex::getSingletonInstance();
850
851        $where = "$pid_name IN (" . implode(',', array_fill(0, count($arrPID), '?')) . ")";
852
853        $ret = $objQuery->select($id_name, $table, $where, $arrPID);
854
855        $arrChildren = array();
856        foreach ($ret as $val) {
857            $arrChildren[] = $val[$id_name];
858        }
859
860        return $arrChildren;
861    }
862
863    /**
864     * 所属するすべての階層の親IDを配列で返す.
865     *
866     * @param SC_Query $objQuery SC_Query インスタンス
867     * @param string $table テーブル名
868     * @param string $pid_name 親ID名
869     * @param string $id_name ID名
870     * @param integer $id ID
871     * @return array 親IDの配列
872     */
873    function sfGetParents($table, $pid_name, $id_name, $id) {
874        $arrRet = SC_Helper_DB_Ex::sfGetParentsArray($table, $pid_name, $id_name, $id);
875        return $arrRet;
876    }
877
878    /**
879     * 階層構造のテーブルから親ID配列を取得する.
880     *
881     * @param string $table テーブル名
882     * @param string $pid_name 親ID名
883     * @param string $id_name ID名
884     * @param integer $id ID
885     * @return array 親IDの配列
886     */
887    function sfGetParentsArray($table, $pid_name, $id_name, $id) {
888        $arrParents = array();
889        $ret = $id;
890
891        while($ret != "0" && !SC_Utils_Ex::isBlank($ret)) {
892            $arrParents[] = $ret;
893            $ret = SC_Helper_DB_Ex::sfGetParentsArraySub($table, $pid_name, $id_name, $ret);
894        }
895
896        $arrParents = array_reverse($arrParents);
897
898        return $arrParents;
899    }
900
901    /* 子ID所属する親IDを取得する */
902    function sfGetParentsArraySub($table, $pid_name, $id_name, $child) {
903        if(SC_Utils_Ex::isBlank($child)) {
904            return false;
905        }
906        $objQuery =& SC_Query_Ex::getSingletonInstance();
907        if(!is_array($child)) {
908            $child = array($child);
909        }
910        $parent = $objQuery->get($pid_name, $table, "$id_name = ?", $child);
911        return $parent;
912    }
913
914    /**
915     * カテゴリから商品を検索する場合のWHERE文と値を返す.
916     *
917     * @param integer $category_id カテゴリID
918     * @return array 商品を検索する場合の配列
919     */
920    function sfGetCatWhere($category_id) {
921        // 子カテゴリIDの取得
922        $arrRet = SC_Helper_DB_Ex::sfGetChildrenArray("dtb_category", "parent_category_id", "category_id", $category_id);
923
924        $where = "category_id IN (" . implode(',', array_fill(0, count($arrRet), '?')) . ")";
925
926        return array($where, $arrRet);
927    }
928
929    /**
930     * SELECTボックス用リストを作成する.
931     *
932     * @param string $table テーブル名
933     * @param string $keyname プライマリーキーのカラム名
934     * @param string $valname データ内容のカラム名
935     * @param string $where WHERE句
936     * @param array $arrval プレースホルダ
937     * @return array SELECT ボックス用リストの配列
938     */
939    function sfGetIDValueList($table, $keyname, $valname, $where = '', $arrVal = array()) {
940        $objQuery =& SC_Query_Ex::getSingletonInstance();
941        $col = "$keyname, $valname";
942        $objQuery->setWhere("del_flg = 0");
943        $objQuery->setOrder("rank DESC");
944        $arrList = $objQuery->select($col, $table, $where, $arrVal);
945        $count = count($arrList);
946        for($cnt = 0; $cnt < $count; $cnt++) {
947            $key = $arrList[$cnt][$keyname];
948            $val = $arrList[$cnt][$valname];
949            $arrRet[$key] = $val;
950        }
951        return $arrRet;
952    }
953
954    /**
955     * ランキングを上げる.
956     *
957     * @param string $table テーブル名
958     * @param string $colname カラム名
959     * @param string|integer $id テーブルのキー
960     * @param string $andwhere SQL の AND 条件である WHERE 句
961     * @return void
962     */
963    function sfRankUp($table, $colname, $id, $andwhere = "") {
964        $objQuery =& SC_Query_Ex::getSingletonInstance();
965        $objQuery->begin();
966        $where = "$colname = ?";
967        if($andwhere != "") {
968            $where.= " AND $andwhere";
969        }
970        // 対象項目のランクを取得
971        $rank = $objQuery->get('rank', $table, $where, array($id));
972        // ランクの最大値を取得
973        $maxrank = $objQuery->max('rank', $table, $andwhere);
974        // ランクが最大値よりも小さい場合に実行する。
975        if($rank < $maxrank) {
976            // ランクが一つ上のIDを取得する。
977            $where = "rank = ?";
978            if($andwhere != "") {
979                $where.= " AND $andwhere";
980            }
981            $uprank = $rank + 1;
982            $up_id = $objQuery->get($colname, $table, $where, array($uprank));
983            // ランク入れ替えの実行
984            $sqlup = "UPDATE $table SET rank = ? WHERE $colname = ?";
985            if($andwhere != "") {
986                $sqlup.= " AND $andwhere";
987            }
988            $objQuery->exec($sqlup, array($rank + 1, $id));
989            $objQuery->exec($sqlup, array($rank, $up_id));
990        }
991        $objQuery->commit();
992    }
993
994    /**
995     * ランキングを下げる.
996     *
997     * @param string $table テーブル名
998     * @param string $colname カラム名
999     * @param string|integer $id テーブルのキー
1000     * @param string $andwhere SQL の AND 条件である WHERE 句
1001     * @return void
1002     */
1003    function sfRankDown($table, $colname, $id, $andwhere = "") {
1004        $objQuery =& SC_Query_Ex::getSingletonInstance();
1005        $objQuery->begin();
1006        $where = "$colname = ?";
1007        if($andwhere != "") {
1008            $where.= " AND $andwhere";
1009        }
1010        // 対象項目のランクを取得
1011        $rank = $objQuery->get('rank', $table, $where, array($id));
1012
1013        // ランクが1(最小値)よりも大きい場合に実行する。
1014        if($rank > 1) {
1015            // ランクが一つ下のIDを取得する。
1016            $where = "rank = ?";
1017            if($andwhere != "") {
1018                $where.= " AND $andwhere";
1019            }
1020            $downrank = $rank - 1;
1021            $down_id = $objQuery->get($colname, $table, $where, array($downrank));
1022            // ランク入れ替えの実行
1023            $sqlup = "UPDATE $table SET rank = ? WHERE $colname = ?";
1024            if($andwhere != "") {
1025                $sqlup.= " AND $andwhere";
1026            }
1027            $objQuery->exec($sqlup, array($rank - 1, $id));
1028            $objQuery->exec($sqlup, array($rank, $down_id));
1029        }
1030        $objQuery->commit();
1031    }
1032
1033    /**
1034     * 指定順位へ移動する.
1035     *
1036     * @param string $tableName テーブル名
1037     * @param string $keyIdColumn キーを保持するカラム名
1038     * @param string|integer $keyId キーの値
1039     * @param integer $pos 指定順位
1040     * @param string $where SQL の AND 条件である WHERE 句
1041     * @return void
1042     */
1043    function sfMoveRank($tableName, $keyIdColumn, $keyId, $pos, $where = "") {
1044        $objQuery =& SC_Query_Ex::getSingletonInstance();
1045        $objQuery->begin();
1046
1047        // 自身のランクを取得する
1048        if($where != "") {
1049            $getWhere = "$keyIdColumn = ? AND " . $where;
1050        } else {
1051            $getWhere = "$keyIdColumn = ?";
1052        }
1053        $rank = $objQuery->get('rank', $tableName, $getWhere, array($keyId));
1054
1055        $max = $objQuery->max('rank', $tableName, $where);
1056
1057        // 値の調整(逆順)
1058        if($pos > $max) {
1059            $position = 1;
1060        } else if($pos < 1) {
1061            $position = $max;
1062        } else {
1063            $position = $max - $pos + 1;
1064        }
1065
1066        //入れ替え先の順位が入れ換え元の順位より大きい場合
1067        if( $position > $rank ) $term = "rank - 1";
1068
1069        //入れ替え先の順位が入れ換え元の順位より小さい場合
1070        if( $position < $rank ) $term = "rank + 1";
1071
1072        // XXX 入れ替え先の順位が入れ替え元の順位と同じ場合
1073        if (!isset($term)) $term = 'rank';
1074
1075        // 指定した順位の商品から移動させる商品までのrankを1つずらす
1076        $sql = "UPDATE $tableName SET rank = $term WHERE rank BETWEEN ? AND ?";
1077        if($where != "") {
1078            $sql.= " AND $where";
1079        }
1080
1081        if( $position > $rank ) $objQuery->exec( $sql, array( $rank + 1, $position ));
1082        if( $position < $rank ) $objQuery->exec( $sql, array( $position, $rank - 1 ));
1083
1084        // 指定した順位へrankを書き換える。
1085        $sql  = "UPDATE $tableName SET rank = ? WHERE $keyIdColumn = ? ";
1086        if($where != "") {
1087            $sql.= " AND $where";
1088        }
1089
1090        $objQuery->exec( $sql, array( $position, $keyId ) );
1091        $objQuery->commit();
1092    }
1093
1094    /**
1095     * ランクを含むレコードを削除する.
1096     *
1097     * レコードごと削除する場合は、$deleteをtrueにする
1098     *
1099     * @param string $table テーブル名
1100     * @param string $colname カラム名
1101     * @param string|integer $id テーブルのキー
1102     * @param string $andwhere SQL の AND 条件である WHERE 句
1103     * @param bool $delete レコードごと削除する場合 true,
1104     *                     レコードごと削除しない場合 false
1105     * @return void
1106     */
1107    function sfDeleteRankRecord($table, $colname, $id, $andwhere = "",
1108                                $delete = false) {
1109        $objQuery =& SC_Query_Ex::getSingletonInstance();
1110        $objQuery->begin();
1111        // 削除レコードのランクを取得する。
1112        $where = "$colname = ?";
1113        if($andwhere != "") {
1114            $where.= " AND $andwhere";
1115        }
1116        $rank = $objQuery->get('rank', $table, $where, array($id));
1117
1118        if(!$delete) {
1119            // ランクを最下位にする、DELフラグON
1120            $sqlup = "UPDATE $table SET rank = 0, del_flg = 1 ";
1121            $sqlup.= "WHERE $colname = ?";
1122            // UPDATEの実行
1123            $objQuery->exec($sqlup, array($id));
1124        } else {
1125            $objQuery->delete($table, "$colname = ?", array($id));
1126        }
1127
1128        // 追加レコードのランクより上のレコードを一つずらす。
1129        $where = "rank > ?";
1130        if($andwhere != "") {
1131            $where.= " AND $andwhere";
1132        }
1133        $sqlup = "UPDATE $table SET rank = (rank - 1) WHERE $where";
1134        $objQuery->exec($sqlup, array($rank));
1135        $objQuery->commit();
1136    }
1137
1138    /**
1139     * 親IDの配列を元に特定のカラムを取得する.
1140     *
1141     * @param SC_Query $objQuery SC_Query インスタンス
1142     * @param string $table テーブル名
1143     * @param string $id_name ID名
1144     * @param string $col_name カラム名
1145     * @param array $arrId IDの配列
1146     * @return array 特定のカラムの配列
1147     */
1148    function sfGetParentsCol($objQuery, $table, $id_name, $col_name, $arrId ) {
1149        $col = $col_name;
1150        $len = count($arrId);
1151        $where = "";
1152
1153        for($cnt = 0; $cnt < $len; $cnt++) {
1154            if($where == "") {
1155                $where = "$id_name = ?";
1156            } else {
1157                $where.= " OR $id_name = ?";
1158            }
1159        }
1160
1161        $objQuery->setOrder('level');
1162        $arrRet = $objQuery->select($col, $table, $where, $arrId);
1163        return $arrRet;
1164    }
1165
1166    /**
1167     * カテゴリ変更時の移動処理を行う.
1168     *
1169     * @param SC_Query $objQuery SC_Query インスタンス
1170     * @param string $table テーブル名
1171     * @param string $id_name ID名
1172     * @param string $cat_name カテゴリ名
1173     * @param integer $old_catid 旧カテゴリID
1174     * @param integer $new_catid 新カテゴリID
1175     * @param integer $id ID
1176     * @return void
1177     */
1178    function sfMoveCatRank($objQuery, $table, $id_name, $cat_name, $old_catid, $new_catid, $id) {
1179        if ($old_catid == $new_catid) {
1180            return;
1181        }
1182        // 旧カテゴリでのランク削除処理
1183        // 移動レコードのランクを取得する。
1184        $where = "$id_name = ?";
1185        $rank = $objQuery->get('rank', $table, $where, array($id));
1186        // 削除レコードのランクより上のレコードを一つ下にずらす。
1187        $where = "rank > ? AND $cat_name = ?";
1188        $sqlup = "UPDATE $table SET rank = (rank - 1) WHERE $where";
1189        $objQuery->exec($sqlup, array($rank, $old_catid));
1190        // 新カテゴリでの登録処理
1191        // 新カテゴリの最大ランクを取得する。
1192        $max_rank = $objQuery->max('rank', $table, "$cat_name = ?", array($new_catid)) + 1;
1193        $where = "$id_name = ?";
1194        $sqlup = "UPDATE $table SET rank = ? WHERE $where";
1195        $objQuery->exec($sqlup, array($max_rank, $id));
1196    }
1197
1198    /**
1199     * 都道府県から配送料金を取得する.
1200     *
1201     * @param integer|array $pref_id 都道府県ID 又は都道府県IDの配列
1202     * @param integer $deliv_id 配送業者ID
1203     * @return string 指定の都道府県, 配送業者の配送料金
1204     */
1205    function sfGetDelivFee($pref_id, $deliv_id = 0) {
1206        $objQuery =& SC_Query_Ex::getSingletonInstance();
1207        if (!is_array($pref_id)) {
1208            $pref_id = array($pref_id);
1209        }
1210        $sql = <<< __EOS__
1211            SELECT T1.fee AS fee
1212              FROM dtb_delivfee T1
1213              JOIN dtb_deliv T2
1214                ON T1.deliv_id = T2.deliv_id
1215             WHERE T1.pref = ?
1216               AND T1.deliv_id = ?
1217               AND T2.del_flg = 0
1218__EOS__;
1219        $result = 0;
1220        foreach ($pref_id as $pref) {
1221            $result += $objQuery->getOne($sql, array($pref, $deliv_id));
1222        }
1223        return $result;
1224    }
1225
1226    /**
1227     * レコードの存在チェックを行う.
1228     *
1229     * TODO SC_Query に移行するべきか?
1230     *
1231     * @param string $table テーブル名
1232     * @param string $col カラム名
1233     * @param array $arrval 要素の配列
1234     * @param array $addwhere SQL の AND 条件である WHERE 句
1235     * @return bool レコードが存在する場合 true
1236     */
1237    function sfIsRecord($table, $col, $arrval, $addwhere = "") {
1238        $objQuery =& SC_Query_Ex::getSingletonInstance();
1239        $arrCol = preg_split("/[, ]/", $col);
1240
1241        $where = "del_flg = 0";
1242
1243        if($addwhere != "") {
1244            $where.= " AND $addwhere";
1245        }
1246
1247        foreach($arrCol as $val) {
1248            if($val != "") {
1249                if($where == "") {
1250                    $where = "$val = ?";
1251                } else {
1252                    $where.= " AND $val = ?";
1253                }
1254            }
1255        }
1256        $ret = $objQuery->get($col, $table, $where, $arrval);
1257
1258        if($ret != "") {
1259            return true;
1260        }
1261        return false;
1262    }
1263
1264    /**
1265     * メーカー商品数数の登録を行う.
1266     *
1267     * @param SC_Query $objQuery SC_Query インスタンス
1268     * @return void
1269     */
1270    function sfCountMaker($objQuery){
1271        $sql = "";
1272
1273        //テーブル内容の削除
1274        $objQuery->query("DELETE FROM dtb_maker_count");
1275
1276        //各メーカーの商品数を数えて格納
1277        $sql = " INSERT INTO dtb_maker_count(maker_id, product_count, create_date) ";
1278        $sql .= " SELECT T1.maker_id, count(T2.maker_id), CURRENT_TIMESTAMP ";
1279        $sql .= " FROM dtb_maker AS T1 LEFT JOIN dtb_products AS T2";
1280        $sql .= " ON T1.maker_id = T2.maker_id ";
1281        $sql .= " WHERE T2.del_flg = 0 AND T2.status = 1 ";
1282        $sql .= " GROUP BY T1.maker_id, T2.maker_id ";
1283        $objQuery->query($sql);
1284    }
1285
1286    /**
1287     * 選択中の商品のメーカーを取得する.
1288     *
1289     * @param integer $product_id プロダクトID
1290     * @param integer $maker_id メーカーID
1291     * @return array 選択中の商品のメーカーIDの配列
1292     *
1293     */
1294    function sfGetMakerId($product_id, $maker_id = 0, $closed = false) {
1295        if ($closed) {
1296            $status = "";
1297        } else {
1298            $status = "status = 1";
1299        }
1300
1301        if (!$this->g_maker_on) {
1302            $this->g_maker_on = true;
1303            $maker_id = (int) $maker_id;
1304            $product_id = (int) $product_id;
1305            if (SC_Utils_Ex::sfIsInt($maker_id) && $maker_id != 0 && $this->sfIsRecord("dtb_maker","maker_id", $maker_id)) {
1306                $this->g_maker_id = array($maker_id);
1307            } else if (SC_Utils_Ex::sfIsInt($product_id) && $product_id != 0 && $this->sfIsRecord("dtb_products","product_id", $product_id, $status)) {
1308                $objQuery =& SC_Query_Ex::getSingletonInstance();
1309                $where = "product_id = ?";
1310                $maker_id = $objQuery->getCol("maker_id", "dtb_products", "product_id = ?", array($product_id));
1311                $this->g_maker_id = $maker_id;
1312            } else {
1313                // 不正な場合は、空の配列を返す。
1314                $this->g_maker_id = array();
1315            }
1316        }
1317        return $this->g_maker_id;
1318    }
1319
1320    /**
1321     * メーカーの取得を行う.
1322     *
1323     * $products_check:true商品登録済みのものだけ取得する
1324     *
1325     * @param string $addwhere 追加する WHERE 句
1326     * @param bool $products_check 商品の存在するカテゴリのみ取得する場合 true
1327     * @return array カテゴリツリーの配列
1328     */
1329    function sfGetMakerList($addwhere = "", $products_check = false) {
1330        $objQuery =& SC_Query_Ex::getSingletonInstance();
1331        $where = "del_flg = 0";
1332
1333        if($addwhere != "") {
1334            $where.= " AND $addwhere";
1335        }
1336
1337        $objQuery->setOption("ORDER BY rank DESC");
1338
1339        if($products_check) {
1340            $col = "T1.maker_id, name";
1341            $from = "dtb_maker AS T1 LEFT JOIN dtb_maker_count AS T2 ON T1.maker_id = T2.maker_id";
1342            $where .= " AND product_count > 0";
1343        } else {
1344            $col = "maker_id, name";
1345            $from = "dtb_maker";
1346        }
1347
1348        $arrRet = $objQuery->select($col, $from, $where);
1349
1350        $max = count($arrRet);
1351        for($cnt = 0; $cnt < $max; $cnt++) {
1352            $id = $arrRet[$cnt]['maker_id'];
1353            $name = $arrRet[$cnt]['name'];
1354            $arrList[$id] = $name;
1355        }
1356        return $arrList;
1357    }
1358
1359    /**
1360     * 店舗基本情報に基づいて税金額を返す
1361     *
1362     * @param integer $price 計算対象の金額
1363     * @return integer 税金額
1364     */
1365    function sfTax($price) {
1366        // 店舗基本情報を取得
1367        $CONF = SC_Helper_DB_Ex::sfGetBasisData();
1368
1369        return SC_Utils_Ex::sfTax($price, $CONF['tax'], $CONF['tax_rule']);
1370    }
1371
1372    /**
1373     * 店舗基本情報に基づいて税金付与した金額を返す
1374     *
1375     * @param integer $price 計算対象の金額
1376     * @return integer 税金付与した金額
1377     */
1378    function sfCalcIncTax($price, $tax = null, $tax_rule = null) {
1379        // 店舗基本情報を取得
1380        $CONF = SC_Helper_DB_Ex::sfGetBasisData();
1381
1382        return SC_Utils_Ex::sfCalcIncTax($price, $CONF['tax'], $CONF['tax_rule']);
1383    }
1384
1385    /**
1386     * 店舗基本情報に基づいて加算ポイントを返す
1387     *
1388     * @param integer $totalpoint
1389     * @param integer $use_point
1390     * @return integer 加算ポイント
1391     */
1392    function sfGetAddPoint($totalpoint, $use_point) {
1393        // 店舗基本情報を取得
1394        $CONF = SC_Helper_DB_Ex::sfGetBasisData();
1395
1396        return SC_Utils_Ex::sfGetAddPoint($totalpoint, $use_point, $CONF['point_rate']);
1397    }
1398
1399    /**
1400     * 指定ファイルが存在する場合 SQL として実行
1401     *
1402     * XXX プラグイン用に追加。将来消すかも。
1403     *
1404     * @param string $sqlFilePath SQL ファイルのパス
1405     * @return void
1406     */
1407    function sfExecSqlByFile($sqlFilePath) {
1408        if (file_exists($sqlFilePath)) {
1409            $objQuery =& SC_Query_Ex::getSingletonInstance();
1410
1411            $sqls = file_get_contents($sqlFilePath);
1412            if ($sqls === false) SC_Utils_Ex::sfDispException('ファイルは存在するが読み込めない');
1413
1414            foreach (explode(';', $sqls) as $sql) {
1415                $sql = trim($sql);
1416                if (strlen($sql) == 0) continue;
1417                $objQuery->query($sql);
1418            }
1419        }
1420    }
1421
1422    /**
1423     * 商品規格を設定しているか
1424     *
1425     * @param integer $product_id 商品ID
1426     * @return bool 商品規格が存在する場合:true, それ以外:false
1427     */
1428    function sfHasProductClass($product_id) {
1429        if (!SC_Utils_Ex::sfIsInt($product_id)) return false;
1430
1431        $objQuery =& SC_Query_Ex::getSingletonInstance();
1432        $where = 'product_id = ? AND del_flg = 0 AND class_combination_id IS NOT NULL';
1433        $count = $objQuery->count('dtb_products_class', $where, array($product_id));
1434
1435        return $count >= 1;
1436    }
1437}
1438?>
Note: See TracBrowser for help on using the repository browser.