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

Revision 20729, 50.5 KB checked in by AMUAMU, 13 years ago (diff)

#928 (カテゴリカウント不具合) 関連修正

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