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

Revision 20726, 50.7 KB checked in by shutta, 13 years ago (diff)

refs #1173
カテゴリーカウント処理が重かったのを修正。
sfGetParents*、sfGetChildren* 絡みの処理効率を改善。

  • 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        // 2.5で消える予定だったが復活させます。DELETE処理は無くしました。
664
665        //まずテーブル内容の元を取得
666        if(!$is_force_all_count) {
667            $arrCategoryCountOld = $objQuery->select('category_id,product_count','dtb_category_count');
668        }else{
669            $arrCategoryCountOld = array();
670        }
671
672        //各カテゴリ内の商品数を数えて取得
673        $sql = <<< __EOS__
674            SELECT T1.category_id, count(T2.category_id) as product_count
675            FROM dtb_category AS T1
676                LEFT JOIN dtb_product_categories AS T2
677                    ON T1.category_id = T2.category_id
678                LEFT JOIN $from
679                    ON T2.product_id = alldtl.product_id
680            WHERE $sql_where
681            GROUP BY T1.category_id, T2.category_id
682__EOS__;
683
684        $arrCategoryCountNew = $objQuery->getAll($sql);
685        // 各カテゴリに所属する商品の数を集計。集計対象には子カテゴリを「含む」。
686        //差分を取得して、更新対象カテゴリだけを確認する。
687
688        //各カテゴリ毎のデータ値において以前との差を見る
689        //古いデータの構造入れ替え
690        $arrOld = array();
691        foreach($arrCategoryCountOld as $item){
692            $arrOld[$item['category_id']] = $item['product_count'];
693        }
694        //新しいデータの構造入れ替え
695        $arrNew = array();
696        foreach($arrCategoryCountNew as $item){
697            $arrNew[$item['category_id']] = $item['product_count'];
698        }
699
700        $arrDiffCategory_id = array();
701        //新しいカテゴリ一覧から見て商品数が異なるデータが無いか確認
702        foreach($arrNew as $cid => $count){
703            if($arrOld[$cid] != $count){
704                $arrDiffCategory_id[] = $cid;
705            }
706        }
707        //削除カテゴリを想定して、古いカテゴリ一覧から見て商品数が異なるデータが無いか確認。
708        foreach($arrOld as $cid => $count){
709            if($arrNew[$cid] != $count && $count > 0){
710                $arrDiffCategory_id[] = $cid;
711            }
712        }
713
714        //対象IDが無ければ終了
715        if(count($arrDiffCategory_id) == 0){
716            if($is_out_trans) {
717                $objQuery->commit();
718            }
719            return;
720        }
721
722        //差分対象カテゴリIDの重複を除去
723        $arrDiffCategory_id = array_unique($arrDiffCategory_id);
724
725        //dtb_category_countの更新 差分のあったカテゴリだけ更新する。
726        foreach($arrDiffCategory_id as $cid) {
727            $sqlval = array();
728            $sqlval['create_date'] = 'Now()';
729            $sqlval['product_count'] = (string)$arrNew[$cid];
730            if($sqlval['product_count'] =="") {
731                $sqlval['product_count'] = (string)'0';
732            }
733            if(isset($arrOld[$cid])) {
734                $objQuery->update('dtb_category_count', $sqlval, 'category_id = ?', array($cid));
735            }else{
736                $sqlval['category_id'] = $cid;
737                $objQuery->insert('dtb_category_count', $sqlval);
738            }
739        }
740
741        //差分があったIDとその親カテゴリIDのリストを取得する
742        $arrTgtCategory_id = array();
743        foreach ($arrDiffCategory_id as $parent_category_id) {
744            $arrTgtCategory_id[] = $parent_category_id;
745            $arrParentID = $this->sfGetParents('dtb_category', 'parent_category_id', 'category_id', $parent_category_id);
746            $arrTgtCategory_id = array_merge($arrTgtCategory_id, $arrParentID);
747        }
748
749        //重複を取り除く
750        $arrTgtCategory_id = array_unique($arrTgtCategory_id);
751
752        //dtb_category_total_count 集計処理開始
753        //更新対象カテゴリIDだけ集計しなおす。
754        $arrUpdateData = array();
755        foreach ($arrTgtCategory_id as $category_id) {
756            $arrval = array();
757            $arrval[] = $category_id;
758            list($tmp_where, $tmp_arrval) = $this->sfGetCatWhere($category_id);
759            if ($tmp_where != "") {
760                $sql_where_product_ids = "product_id IN (SELECT product_id FROM dtb_product_categories WHERE " . $tmp_where . ")";
761                $arrval = array_merge((array)$arrval, (array)$tmp_arrval, (array)$tmp_arrval);
762            } else {
763                $sql_where_product_ids = '0<>0'; // 一致させない
764            }
765            $where = "($sql_where) AND ($sql_where_product_ids)";
766
767            $from = $objProduct->alldtlSQL($sql_where_product_ids);
768            $sql = "SELECT count(*) FROM $from WHERE $where ";
769            $arrUpdateData[ $category_id ] = $objQuery->getOne($sql, $arrval);
770        }
771        // 更新対象だけを更新。
772        foreach($arrUpdateData as $cid => $count) {
773            $sqlval = array();
774            $sqlval['create_date'] = 'Now()';
775            $sqlval['product_count'] = $count;
776            if($sqlval['product_count'] =="") {
777                $sqlval['product_count'] = (string)'0';
778            }
779            $ret = $objQuery->update('dtb_category_total_count', $sqlval, 'category_id = ?', array($cid));
780            if(!$ret) {
781                $sqlval['category_id'] = $cid;
782                $ret = $objQuery->insert('dtb_category_total_count', $sqlval);
783            }
784        }
785        // トランザクション終了処理
786        if($is_out_trans) {
787            $objQuery->commit();
788        }
789    }
790
791    /**
792     * 子IDの配列を返す.
793     *
794     * @param string $table テーブル名
795     * @param string $pid_name 親ID名
796     * @param string $id_name ID名
797     * @param integer $id ID
798     * @param array 子ID の配列
799     */
800    function sfGetChildsID($table, $pid_name, $id_name, $id) {
801        $arrRet = $this->sfGetChildrenArray($table, $pid_name, $id_name, $id);
802        return $arrRet;
803    }
804
805    /**
806     * 階層構造のテーブルから子ID配列を取得する.
807     *
808     * @param string $table テーブル名
809     * @param string $pid_name 親ID名
810     * @param string $id_name ID名
811     * @param integer $id ID番号
812     * @return array 子IDの配列
813     */
814    function sfGetChildrenArray($table, $pid_name, $id_name, $id) {
815        $arrChildren = array();
816        $arrRet = array($id);
817
818        while(count($arrRet) > 0) {
819            $arrChildren = array_merge($arrChildren, $arrRet);
820            $arrRet = SC_Helper_DB_Ex::sfGetChildrenArraySub($table, $pid_name, $id_name, $arrRet);
821        }
822
823        return $arrChildren;
824    }
825
826    /**
827     * 親ID直下の子IDをすべて取得する.
828     *
829     * @param array $arrData 親カテゴリの配列
830     * @param string $pid_name 親ID名
831     * @param string $id_name ID名
832     * @param array $arrPID 親IDの配列
833     * @return array 子IDの配列
834     */
835    function sfGetChildrenArraySub($table, $pid_name, $id_name, $arrPID) {
836        $objQuery =& SC_Query_Ex::getSingletonInstance();
837
838        $where = "$pid_name IN (" . implode(',', array_fill(0, count($arrPID), '?')) . ")";
839
840        $ret = $objQuery->select($id_name, $table, $where, $arrPID);
841
842        $arrChildren = array();
843        foreach ($ret as $val) {
844            $arrChildren[] = $val[$id_name];
845        }
846
847        return $arrChildren;
848    }
849
850    /**
851     * 所属するすべての階層の親IDを配列で返す.
852     *
853     * @param SC_Query $objQuery SC_Query インスタンス
854     * @param string $table テーブル名
855     * @param string $pid_name 親ID名
856     * @param string $id_name ID名
857     * @param integer $id ID
858     * @return array 親IDの配列
859     */
860    function sfGetParents($table, $pid_name, $id_name, $id) {
861        $arrRet = SC_Helper_DB_Ex::sfGetParentsArray($table, $pid_name, $id_name, $id);
862        return $arrRet;
863    }
864
865    /**
866     * 階層構造のテーブルから親ID配列を取得する.
867     *
868     * @param string $table テーブル名
869     * @param string $pid_name 親ID名
870     * @param string $id_name ID名
871     * @param integer $id ID
872     * @return array 親IDの配列
873     */
874    function sfGetParentsArray($table, $pid_name, $id_name, $id) {
875        $arrParents = array();
876        $ret = $id;
877
878        while($ret != "0") {
879            $arrParents[] = $ret;
880            $ret = SC_Helper_DB_Ex::sfGetParentsArraySub($table, $pid_name, $id_name, $ret);
881        }
882
883        return $arrParents;
884    }
885
886    /* 子ID所属する親IDを取得する */
887    function sfGetParentsArraySub($table, $pid_name, $id_name, $child) {
888        $objQuery =& SC_Query_Ex::getSingletonInstance();
889        $parent = $objQuery->get($pid_name, $table, "$id_name = ?", $child);
890        return $parent;
891    }
892
893    /**
894     * カテゴリから商品を検索する場合のWHERE文と値を返す.
895     *
896     * @param integer $category_id カテゴリID
897     * @return array 商品を検索する場合の配列
898     */
899    function sfGetCatWhere($category_id) {
900        // 子カテゴリIDの取得
901        $arrRet = SC_Helper_DB_Ex::sfGetChildrenArray("dtb_category", "parent_category_id", "category_id", $category_id);
902
903        $where = "category_id IN (" . implode(',', array_fill(0, count($arrRet), '?')) . ")";
904
905        return array($where, $arrRet);
906    }
907
908    /**
909     * SELECTボックス用リストを作成する.
910     *
911     * @param string $table テーブル名
912     * @param string $keyname プライマリーキーのカラム名
913     * @param string $valname データ内容のカラム名
914     * @param string $where WHERE句
915     * @param array $arrval プレースホルダ
916     * @return array SELECT ボックス用リストの配列
917     */
918    function sfGetIDValueList($table, $keyname, $valname, $where = '', $arrVal = array()) {
919        $objQuery =& SC_Query_Ex::getSingletonInstance();
920        $col = "$keyname, $valname";
921        $objQuery->setWhere("del_flg = 0");
922        $objQuery->setOrder("rank DESC");
923        $arrList = $objQuery->select($col, $table, $where, $arrVal);
924        $count = count($arrList);
925        for($cnt = 0; $cnt < $count; $cnt++) {
926            $key = $arrList[$cnt][$keyname];
927            $val = $arrList[$cnt][$valname];
928            $arrRet[$key] = $val;
929        }
930        return $arrRet;
931    }
932
933    /**
934     * ランキングを上げる.
935     *
936     * @param string $table テーブル名
937     * @param string $colname カラム名
938     * @param string|integer $id テーブルのキー
939     * @param string $andwhere SQL の AND 条件である WHERE 句
940     * @return void
941     */
942    function sfRankUp($table, $colname, $id, $andwhere = "") {
943        $objQuery =& SC_Query_Ex::getSingletonInstance();
944        $objQuery->begin();
945        $where = "$colname = ?";
946        if($andwhere != "") {
947            $where.= " AND $andwhere";
948        }
949        // 対象項目のランクを取得
950        $rank = $objQuery->get('rank', $table, $where, array($id));
951        // ランクの最大値を取得
952        $maxrank = $objQuery->max('rank', $table, $andwhere);
953        // ランクが最大値よりも小さい場合に実行する。
954        if($rank < $maxrank) {
955            // ランクが一つ上のIDを取得する。
956            $where = "rank = ?";
957            if($andwhere != "") {
958                $where.= " AND $andwhere";
959            }
960            $uprank = $rank + 1;
961            $up_id = $objQuery->get($colname, $table, $where, array($uprank));
962            // ランク入れ替えの実行
963            $sqlup = "UPDATE $table SET rank = ? WHERE $colname = ?";
964            if($andwhere != "") {
965                $sqlup.= " AND $andwhere";
966            }
967            $objQuery->exec($sqlup, array($rank + 1, $id));
968            $objQuery->exec($sqlup, array($rank, $up_id));
969        }
970        $objQuery->commit();
971    }
972
973    /**
974     * ランキングを下げる.
975     *
976     * @param string $table テーブル名
977     * @param string $colname カラム名
978     * @param string|integer $id テーブルのキー
979     * @param string $andwhere SQL の AND 条件である WHERE 句
980     * @return void
981     */
982    function sfRankDown($table, $colname, $id, $andwhere = "") {
983        $objQuery =& SC_Query_Ex::getSingletonInstance();
984        $objQuery->begin();
985        $where = "$colname = ?";
986        if($andwhere != "") {
987            $where.= " AND $andwhere";
988        }
989        // 対象項目のランクを取得
990        $rank = $objQuery->get('rank', $table, $where, array($id));
991
992        // ランクが1(最小値)よりも大きい場合に実行する。
993        if($rank > 1) {
994            // ランクが一つ下のIDを取得する。
995            $where = "rank = ?";
996            if($andwhere != "") {
997                $where.= " AND $andwhere";
998            }
999            $downrank = $rank - 1;
1000            $down_id = $objQuery->get($colname, $table, $where, array($downrank));
1001            // ランク入れ替えの実行
1002            $sqlup = "UPDATE $table SET rank = ? WHERE $colname = ?";
1003            if($andwhere != "") {
1004                $sqlup.= " AND $andwhere";
1005            }
1006            $objQuery->exec($sqlup, array($rank - 1, $id));
1007            $objQuery->exec($sqlup, array($rank, $down_id));
1008        }
1009        $objQuery->commit();
1010    }
1011
1012    /**
1013     * 指定順位へ移動する.
1014     *
1015     * @param string $tableName テーブル名
1016     * @param string $keyIdColumn キーを保持するカラム名
1017     * @param string|integer $keyId キーの値
1018     * @param integer $pos 指定順位
1019     * @param string $where SQL の AND 条件である WHERE 句
1020     * @return void
1021     */
1022    function sfMoveRank($tableName, $keyIdColumn, $keyId, $pos, $where = "") {
1023        $objQuery =& SC_Query_Ex::getSingletonInstance();
1024        $objQuery->begin();
1025
1026        // 自身のランクを取得する
1027        if($where != "") {
1028            $getWhere = "$keyIdColumn = ? AND " . $where;
1029        } else {
1030            $getWhere = "$keyIdColumn = ?";
1031        }
1032        $rank = $objQuery->get('rank', $tableName, $getWhere, array($keyId));
1033
1034        $max = $objQuery->max('rank', $tableName, $where);
1035
1036        // 値の調整(逆順)
1037        if($pos > $max) {
1038            $position = 1;
1039        } else if($pos < 1) {
1040            $position = $max;
1041        } else {
1042            $position = $max - $pos + 1;
1043        }
1044
1045        //入れ替え先の順位が入れ換え元の順位より大きい場合
1046        if( $position > $rank ) $term = "rank - 1";
1047
1048        //入れ替え先の順位が入れ換え元の順位より小さい場合
1049        if( $position < $rank ) $term = "rank + 1";
1050
1051        // XXX 入れ替え先の順位が入れ替え元の順位と同じ場合
1052        if (!isset($term)) $term = 'rank';
1053
1054        // 指定した順位の商品から移動させる商品までのrankを1つずらす
1055        $sql = "UPDATE $tableName SET rank = $term WHERE rank BETWEEN ? AND ?";
1056        if($where != "") {
1057            $sql.= " AND $where";
1058        }
1059
1060        if( $position > $rank ) $objQuery->exec( $sql, array( $rank + 1, $position ));
1061        if( $position < $rank ) $objQuery->exec( $sql, array( $position, $rank - 1 ));
1062
1063        // 指定した順位へrankを書き換える。
1064        $sql  = "UPDATE $tableName SET rank = ? WHERE $keyIdColumn = ? ";
1065        if($where != "") {
1066            $sql.= " AND $where";
1067        }
1068
1069        $objQuery->exec( $sql, array( $position, $keyId ) );
1070        $objQuery->commit();
1071    }
1072
1073    /**
1074     * ランクを含むレコードを削除する.
1075     *
1076     * レコードごと削除する場合は、$deleteをtrueにする
1077     *
1078     * @param string $table テーブル名
1079     * @param string $colname カラム名
1080     * @param string|integer $id テーブルのキー
1081     * @param string $andwhere SQL の AND 条件である WHERE 句
1082     * @param bool $delete レコードごと削除する場合 true,
1083     *                     レコードごと削除しない場合 false
1084     * @return void
1085     */
1086    function sfDeleteRankRecord($table, $colname, $id, $andwhere = "",
1087                                $delete = false) {
1088        $objQuery =& SC_Query_Ex::getSingletonInstance();
1089        $objQuery->begin();
1090        // 削除レコードのランクを取得する。
1091        $where = "$colname = ?";
1092        if($andwhere != "") {
1093            $where.= " AND $andwhere";
1094        }
1095        $rank = $objQuery->get('rank', $table, $where, array($id));
1096
1097        if(!$delete) {
1098            // ランクを最下位にする、DELフラグON
1099            $sqlup = "UPDATE $table SET rank = 0, del_flg = 1 ";
1100            $sqlup.= "WHERE $colname = ?";
1101            // UPDATEの実行
1102            $objQuery->exec($sqlup, array($id));
1103        } else {
1104            $objQuery->delete($table, "$colname = ?", array($id));
1105        }
1106
1107        // 追加レコードのランクより上のレコードを一つずらす。
1108        $where = "rank > ?";
1109        if($andwhere != "") {
1110            $where.= " AND $andwhere";
1111        }
1112        $sqlup = "UPDATE $table SET rank = (rank - 1) WHERE $where";
1113        $objQuery->exec($sqlup, array($rank));
1114        $objQuery->commit();
1115    }
1116
1117    /**
1118     * 親IDの配列を元に特定のカラムを取得する.
1119     *
1120     * @param SC_Query $objQuery SC_Query インスタンス
1121     * @param string $table テーブル名
1122     * @param string $id_name ID名
1123     * @param string $col_name カラム名
1124     * @param array $arrId IDの配列
1125     * @return array 特定のカラムの配列
1126     */
1127    function sfGetParentsCol($objQuery, $table, $id_name, $col_name, $arrId ) {
1128        $col = $col_name;
1129        $len = count($arrId);
1130        $where = "";
1131
1132        for($cnt = 0; $cnt < $len; $cnt++) {
1133            if($where == "") {
1134                $where = "$id_name = ?";
1135            } else {
1136                $where.= " OR $id_name = ?";
1137            }
1138        }
1139
1140        $objQuery->setOrder('level');
1141        $arrRet = $objQuery->select($col, $table, $where, $arrId);
1142        return $arrRet;
1143    }
1144
1145    /**
1146     * カテゴリ変更時の移動処理を行う.
1147     *
1148     * @param SC_Query $objQuery SC_Query インスタンス
1149     * @param string $table テーブル名
1150     * @param string $id_name ID名
1151     * @param string $cat_name カテゴリ名
1152     * @param integer $old_catid 旧カテゴリID
1153     * @param integer $new_catid 新カテゴリID
1154     * @param integer $id ID
1155     * @return void
1156     */
1157    function sfMoveCatRank($objQuery, $table, $id_name, $cat_name, $old_catid, $new_catid, $id) {
1158        if ($old_catid == $new_catid) {
1159            return;
1160        }
1161        // 旧カテゴリでのランク削除処理
1162        // 移動レコードのランクを取得する。
1163        $where = "$id_name = ?";
1164        $rank = $objQuery->get('rank', $table, $where, array($id));
1165        // 削除レコードのランクより上のレコードを一つ下にずらす。
1166        $where = "rank > ? AND $cat_name = ?";
1167        $sqlup = "UPDATE $table SET rank = (rank - 1) WHERE $where";
1168        $objQuery->exec($sqlup, array($rank, $old_catid));
1169        // 新カテゴリでの登録処理
1170        // 新カテゴリの最大ランクを取得する。
1171        $max_rank = $objQuery->max('rank', $table, "$cat_name = ?", array($new_catid)) + 1;
1172        $where = "$id_name = ?";
1173        $sqlup = "UPDATE $table SET rank = ? WHERE $where";
1174        $objQuery->exec($sqlup, array($max_rank, $id));
1175    }
1176
1177    /**
1178     * 都道府県から配送料金を取得する.
1179     *
1180     * @param integer|array $pref_id 都道府県ID 又は都道府県IDの配列
1181     * @return string 指定の都道府県, 商品種別の配送料金
1182     */
1183    function sfGetDelivFee($pref_id, $product_type_id) {
1184        $objQuery =& SC_Query_Ex::getSingletonInstance();
1185
1186        /*
1187         * FIXME 都道府県が指定されていない場合は、東京都の番号を指定しておく
1188         * http://svn.ec-cube.net/open_trac/ticket/410
1189         */
1190        if($pref_id == "") {
1191            $pref_id = 13;
1192        }
1193        if (!is_array($pref_id)) {
1194            $pref_id = array($pref_id);
1195        }
1196        $sql = <<< __EOS__
1197            SELECT SUM(T1.fee) AS fee
1198              FROM dtb_delivfee T1
1199              JOIN dtb_deliv T2
1200                ON T1.deliv_id = T2.deliv_id
1201             WHERE T1.pref = ? AND T2.product_type_id = ?
1202__EOS__;
1203
1204        $result = 0;
1205        foreach ($pref_id as $pref) {
1206            $result += $objQuery->getOne($sql, array($pref, $product_type_id));
1207        }
1208        return $result;
1209    }
1210
1211    /**
1212     * レコードの存在チェックを行う.
1213     *
1214     * TODO SC_Query に移行するべきか?
1215     *
1216     * @param string $table テーブル名
1217     * @param string $col カラム名
1218     * @param array $arrval 要素の配列
1219     * @param array $addwhere SQL の AND 条件である WHERE 句
1220     * @return bool レコードが存在する場合 true
1221     */
1222    function sfIsRecord($table, $col, $arrval, $addwhere = "") {
1223        $objQuery =& SC_Query_Ex::getSingletonInstance();
1224        $arrCol = preg_split("/[, ]/", $col);
1225
1226        $where = "del_flg = 0";
1227
1228        if($addwhere != "") {
1229            $where.= " AND $addwhere";
1230        }
1231
1232        foreach($arrCol as $val) {
1233            if($val != "") {
1234                if($where == "") {
1235                    $where = "$val = ?";
1236                } else {
1237                    $where.= " AND $val = ?";
1238                }
1239            }
1240        }
1241        $ret = $objQuery->get($col, $table, $where, $arrval);
1242
1243        if($ret != "") {
1244            return true;
1245        }
1246        return false;
1247    }
1248
1249    /**
1250     * メーカー商品数数の登録を行う.
1251     *
1252     * @param SC_Query $objQuery SC_Query インスタンス
1253     * @return void
1254     */
1255    function sfCountMaker($objQuery){
1256        $sql = "";
1257
1258        //テーブル内容の削除
1259        $objQuery->query("DELETE FROM dtb_maker_count");
1260
1261        //各メーカーの商品数を数えて格納
1262        $sql = " INSERT INTO dtb_maker_count(maker_id, product_count, create_date) ";
1263        $sql .= " SELECT T1.maker_id, count(T2.maker_id), now() ";
1264        $sql .= " FROM dtb_maker AS T1 LEFT JOIN dtb_products AS T2";
1265        $sql .= " ON T1.maker_id = T2.maker_id ";
1266        $sql .= " WHERE T2.del_flg = 0 AND T2.status = 1 ";
1267        $sql .= " GROUP BY T1.maker_id, T2.maker_id ";
1268        $objQuery->query($sql);
1269    }
1270
1271    /**
1272     * 選択中の商品のメーカーを取得する.
1273     *
1274     * @param integer $product_id プロダクトID
1275     * @param integer $maker_id メーカーID
1276     * @return array 選択中の商品のメーカーIDの配列
1277     *
1278     */
1279    function sfGetMakerId($product_id, $maker_id = 0, $closed = false) {
1280        if ($closed) {
1281            $status = "";
1282        } else {
1283            $status = "status = 1";
1284        }
1285
1286        if (!$this->g_maker_on) {
1287            $this->g_maker_on = true;
1288            $maker_id = (int) $maker_id;
1289            $product_id = (int) $product_id;
1290            if (SC_Utils_Ex::sfIsInt($maker_id) && $maker_id != 0 && $this->sfIsRecord("dtb_maker","maker_id", $maker_id)) {
1291                $this->g_maker_id = array($maker_id);
1292            } else if (SC_Utils_Ex::sfIsInt($product_id) && $product_id != 0 && $this->sfIsRecord("dtb_products","product_id", $product_id, $status)) {
1293                $objQuery =& SC_Query_Ex::getSingletonInstance();
1294                $where = "product_id = ?";
1295                $maker_id = $objQuery->getCol("maker_id", "dtb_products", "product_id = ?", array($product_id));
1296                $this->g_maker_id = $maker_id;
1297            } else {
1298                // 不正な場合は、空の配列を返す。
1299                $this->g_maker_id = array();
1300            }
1301        }
1302        return $this->g_maker_id;
1303    }
1304
1305    /**
1306     * メーカーの取得を行う.
1307     *
1308     * $products_check:true商品登録済みのものだけ取得する
1309     *
1310     * @param string $addwhere 追加する WHERE 句
1311     * @param bool $products_check 商品の存在するカテゴリのみ取得する場合 true
1312     * @return array カテゴリツリーの配列
1313     */
1314    function sfGetMakerList($addwhere = "", $products_check = false) {
1315        $objQuery =& SC_Query_Ex::getSingletonInstance();
1316        $where = "del_flg = 0";
1317
1318        if($addwhere != "") {
1319            $where.= " AND $addwhere";
1320        }
1321
1322        $objQuery->setOption("ORDER BY rank DESC");
1323
1324        if($products_check) {
1325            $col = "T1.maker_id, name";
1326            $from = "dtb_maker AS T1 LEFT JOIN dtb_maker_count AS T2 ON T1.maker_id = T2.maker_id";
1327            $where .= " AND product_count > 0";
1328        } else {
1329            $col = "maker_id, name";
1330            $from = "dtb_maker";
1331        }
1332
1333        $arrRet = $objQuery->select($col, $from, $where);
1334
1335        $max = count($arrRet);
1336        for($cnt = 0; $cnt < $max; $cnt++) {
1337            $id = $arrRet[$cnt]['maker_id'];
1338            $name = $arrRet[$cnt]['name'];
1339            $arrList[$id] = $name;
1340        }
1341        return $arrList;
1342    }
1343
1344    /**
1345     * 店舗基本情報に基づいて税金額を返す
1346     *
1347     * @param integer $price 計算対象の金額
1348     * @return integer 税金額
1349     */
1350    function sfTax($price) {
1351        // 店舗基本情報を取得
1352        $CONF = SC_Helper_DB_Ex::sfGetBasisData();
1353
1354        return SC_Utils_Ex::sfTax($price, $CONF['tax'], $CONF['tax_rule']);
1355    }
1356
1357    /**
1358     * 店舗基本情報に基づいて税金付与した金額を返す
1359     *
1360     * @param integer $price 計算対象の金額
1361     * @return integer 税金付与した金額
1362     */
1363    function sfCalcIncTax($price, $tax = null, $tax_rule = null) {
1364        // 店舗基本情報を取得
1365        $CONF = SC_Helper_DB_Ex::sfGetBasisData();
1366
1367        return SC_Utils_Ex::sfCalcIncTax($price, $CONF['tax'], $CONF['tax_rule']);
1368    }
1369
1370    /**
1371     * 店舗基本情報に基づいて加算ポイントを返す
1372     *
1373     * @param integer $totalpoint
1374     * @param integer $use_point
1375     * @return integer 加算ポイント
1376     */
1377    function sfGetAddPoint($totalpoint, $use_point) {
1378        // 店舗基本情報を取得
1379        $CONF = SC_Helper_DB_Ex::sfGetBasisData();
1380
1381        return SC_Utils_Ex::sfGetAddPoint($totalpoint, $use_point, $CONF['point_rate']);
1382    }
1383
1384    /**
1385     * 指定ファイルが存在する場合 SQL として実行
1386     *
1387     * XXX プラグイン用に追加。将来消すかも。
1388     *
1389     * @param string $sqlFilePath SQL ファイルのパス
1390     * @return void
1391     */
1392    function sfExecSqlByFile($sqlFilePath) {
1393        if (file_exists($sqlFilePath)) {
1394            $objQuery =& SC_Query_Ex::getSingletonInstance();
1395
1396            $sqls = file_get_contents($sqlFilePath);
1397            if ($sqls === false) SC_Utils_Ex::sfDispException('ファイルは存在するが読み込めない');
1398
1399            foreach (explode(';', $sqls) as $sql) {
1400                $sql = trim($sql);
1401                if (strlen($sql) == 0) continue;
1402                $objQuery->query($sql);
1403            }
1404        }
1405    }
1406
1407    /**
1408     * 商品規格を設定しているか
1409     *
1410     * @param integer $product_id 商品ID
1411     * @return bool 商品規格が存在する場合:true, それ以外:false
1412     */
1413    function sfHasProductClass($product_id) {
1414        if (!SC_Utils_Ex::sfIsInt($product_id)) return false;
1415
1416        $objQuery =& SC_Query_Ex::getSingletonInstance();
1417        $where = 'product_id = ? AND del_flg = 0 AND class_combination_id IS NOT NULL';
1418        $count = $objQuery->count('dtb_products_class', $where, array($product_id));
1419
1420        return $count >= 1;
1421    }
1422}
1423?>
Note: See TracBrowser for help on using the repository browser.