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

Revision 20786, 50.4 KB checked in by shutta, 13 years ago (diff)

refs #1201 配送業者の削除が正常に動作しない
送料取得の際に削除した配送業者の分まで取得していたのを修正。

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