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

Revision 20459, 51.1 KB checked in by nanasess, 13 years ago (diff)

#997(冗長な dtb_baseinfo の取得)

  • SC_View のコンストラクタで取得していたのを LC_Page へ移動

#793 (非推奨機能の削除)

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