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

Revision 20480, 51.5 KB checked in by shutta, 13 years ago (diff)

sfGetParentsArraySubをSC_UtilsからSC_Helper_DBへ移動。
SC_Helper_DBに、 呼び出し元のsfGetParents, sfGetParnetsArray があるので、sfGetParentsArraySubも同クラス内にあるべき。

  • 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_Helper_DB_Ex::sfGetParentsArraySub($arrData, $pid_name, $id_name, $child);
898
899        while($ret != "") {
900            $arrParents[] = $ret;
901            $ret = SC_Helper_DB_Ex::sfGetParentsArraySub($arrData, $pid_name, $id_name, $ret);
902        }
903
904        $arrParents = array_reverse($arrParents);
905
906        return $arrParents;
907    }
908
909    /* 子ID所属する親IDを取得する */
910    function sfGetParentsArraySub($arrData, $pid_name, $id_name, $child) {
911        $max = count($arrData);
912        $parent = "";
913        for($i = 0; $i < $max; $i++) {
914            if($arrData[$i][$id_name] == $child) {
915                $parent = $arrData[$i][$pid_name];
916                break;
917            }
918        }
919        return $parent;
920    }
921
922    /**
923     * カテゴリから商品を検索する場合のWHERE文と値を返す.
924     *
925     * @param integer $category_id カテゴリID
926     * @return array 商品を検索する場合の配列
927     */
928    function sfGetCatWhere($category_id) {
929        // 子カテゴリIDの取得
930        $arrRet = SC_Helper_DB_Ex::sfGetChildrenArray("dtb_category", "parent_category_id", "category_id", $category_id);
931        $tmp_where = "";
932        foreach ($arrRet as $val) {
933            if($tmp_where == "") {
934                $tmp_where.= "category_id IN ( ?";
935            } else {
936                $tmp_where.= ",? ";
937            }
938            $arrval[] = $val;
939        }
940        $tmp_where.= " ) ";
941        return array($tmp_where, $arrval);
942    }
943
944    /**
945     * SELECTボックス用リストを作成する.
946     *
947     * @param string $table テーブル名
948     * @param string $keyname プライマリーキーのカラム名
949     * @param string $valname データ内容のカラム名
950     * @param string $where WHERE句
951     * @param array $arrval プレースホルダ
952     * @return array SELECT ボックス用リストの配列
953     */
954    function sfGetIDValueList($table, $keyname, $valname, $where = '', $arrVal = array()) {
955        $objQuery =& SC_Query::getSingletonInstance();
956        $col = "$keyname, $valname";
957        $objQuery->setWhere("del_flg = 0");
958        $objQuery->setOrder("rank DESC");
959        $arrList = $objQuery->select($col, $table, $where, $arrVal);
960        $count = count($arrList);
961        for($cnt = 0; $cnt < $count; $cnt++) {
962            $key = $arrList[$cnt][$keyname];
963            $val = $arrList[$cnt][$valname];
964            $arrRet[$key] = $val;
965        }
966        return $arrRet;
967    }
968
969    /**
970     * ランキングを上げる.
971     *
972     * @param string $table テーブル名
973     * @param string $colname カラム名
974     * @param string|integer $id テーブルのキー
975     * @param string $andwhere SQL の AND 条件である WHERE 句
976     * @return void
977     */
978    function sfRankUp($table, $colname, $id, $andwhere = "") {
979        $objQuery =& SC_Query::getSingletonInstance();
980        $objQuery->begin();
981        $where = "$colname = ?";
982        if($andwhere != "") {
983            $where.= " AND $andwhere";
984        }
985        // 対象項目のランクを取得
986        $rank = $objQuery->get("rank", $table, $where, array($id));
987        // ランクの最大値を取得
988        $maxrank = $objQuery->max("rank", $table, $andwhere);
989        // ランクが最大値よりも小さい場合に実行する。
990        if($rank < $maxrank) {
991            // ランクが一つ上のIDを取得する。
992            $where = "rank = ?";
993            if($andwhere != "") {
994                $where.= " AND $andwhere";
995            }
996            $uprank = $rank + 1;
997            $up_id = $objQuery->get($colname, $table, $where, array($uprank));
998            // ランク入れ替えの実行
999            $sqlup = "UPDATE $table SET rank = ? WHERE $colname = ?";
1000            if($andwhere != "") {
1001                $sqlup.= " AND $andwhere";
1002            }
1003            $objQuery->exec($sqlup, array($rank + 1, $id));
1004            $objQuery->exec($sqlup, array($rank, $up_id));
1005        }
1006        $objQuery->commit();
1007    }
1008
1009    /**
1010     * ランキングを下げる.
1011     *
1012     * @param string $table テーブル名
1013     * @param string $colname カラム名
1014     * @param string|integer $id テーブルのキー
1015     * @param string $andwhere SQL の AND 条件である WHERE 句
1016     * @return void
1017     */
1018    function sfRankDown($table, $colname, $id, $andwhere = "") {
1019        $objQuery =& SC_Query::getSingletonInstance();
1020        $objQuery->begin();
1021        $where = "$colname = ?";
1022        if($andwhere != "") {
1023            $where.= " AND $andwhere";
1024        }
1025        // 対象項目のランクを取得
1026        $rank = $objQuery->get("rank", $table, $where, array($id));
1027
1028        // ランクが1(最小値)よりも大きい場合に実行する。
1029        if($rank > 1) {
1030            // ランクが一つ下のIDを取得する。
1031            $where = "rank = ?";
1032            if($andwhere != "") {
1033                $where.= " AND $andwhere";
1034            }
1035            $downrank = $rank - 1;
1036            $down_id = $objQuery->get($colname, $table, $where, array($downrank));
1037            // ランク入れ替えの実行
1038            $sqlup = "UPDATE $table SET rank = ? WHERE $colname = ?";
1039            if($andwhere != "") {
1040                $sqlup.= " AND $andwhere";
1041            }
1042            $objQuery->exec($sqlup, array($rank - 1, $id));
1043            $objQuery->exec($sqlup, array($rank, $down_id));
1044        }
1045        $objQuery->commit();
1046    }
1047
1048    /**
1049     * 指定順位へ移動する.
1050     *
1051     * @param string $tableName テーブル名
1052     * @param string $keyIdColumn キーを保持するカラム名
1053     * @param string|integer $keyId キーの値
1054     * @param integer $pos 指定順位
1055     * @param string $where SQL の AND 条件である WHERE 句
1056     * @return void
1057     */
1058    function sfMoveRank($tableName, $keyIdColumn, $keyId, $pos, $where = "") {
1059        $objQuery =& SC_Query::getSingletonInstance();
1060        $objQuery->begin();
1061
1062        // 自身のランクを取得する
1063        if($where != "") {
1064            $getWhere = "$keyIdColumn = ? AND " . $where;
1065        } else {
1066            $getWhere = "$keyIdColumn = ?";
1067        }
1068        $rank = $objQuery->get("rank", $tableName, $getWhere, array($keyId));
1069
1070        $max = $objQuery->max("rank", $tableName, $where);
1071
1072        // 値の調整(逆順)
1073        if($pos > $max) {
1074            $position = 1;
1075        } else if($pos < 1) {
1076            $position = $max;
1077        } else {
1078            $position = $max - $pos + 1;
1079        }
1080
1081        //入れ替え先の順位が入れ換え元の順位より大きい場合
1082        if( $position > $rank ) $term = "rank - 1";
1083
1084        //入れ替え先の順位が入れ換え元の順位より小さい場合
1085        if( $position < $rank ) $term = "rank + 1";
1086
1087        // XXX 入れ替え先の順位が入れ替え元の順位と同じ場合
1088        if (!isset($term)) $term = "rank";
1089
1090        // 指定した順位の商品から移動させる商品までのrankを1つずらす
1091        $sql = "UPDATE $tableName SET rank = $term WHERE rank BETWEEN ? AND ?";
1092        if($where != "") {
1093            $sql.= " AND $where";
1094        }
1095
1096        if( $position > $rank ) $objQuery->exec( $sql, array( $rank + 1, $position ));
1097        if( $position < $rank ) $objQuery->exec( $sql, array( $position, $rank - 1 ));
1098
1099        // 指定した順位へrankを書き換える。
1100        $sql  = "UPDATE $tableName SET rank = ? WHERE $keyIdColumn = ? ";
1101        if($where != "") {
1102            $sql.= " AND $where";
1103        }
1104
1105        $objQuery->exec( $sql, array( $position, $keyId ) );
1106        $objQuery->commit();
1107    }
1108
1109    /**
1110     * ランクを含むレコードを削除する.
1111     *
1112     * レコードごと削除する場合は、$deleteをtrueにする
1113     *
1114     * @param string $table テーブル名
1115     * @param string $colname カラム名
1116     * @param string|integer $id テーブルのキー
1117     * @param string $andwhere SQL の AND 条件である WHERE 句
1118     * @param bool $delete レコードごと削除する場合 true,
1119     *                     レコードごと削除しない場合 false
1120     * @return void
1121     */
1122    function sfDeleteRankRecord($table, $colname, $id, $andwhere = "",
1123                                $delete = false) {
1124        $objQuery =& SC_Query::getSingletonInstance();
1125        $objQuery->begin();
1126        // 削除レコードのランクを取得する。
1127        $where = "$colname = ?";
1128        if($andwhere != "") {
1129            $where.= " AND $andwhere";
1130        }
1131        $rank = $objQuery->get("rank", $table, $where, array($id));
1132
1133        if(!$delete) {
1134            // ランクを最下位にする、DELフラグON
1135            $sqlup = "UPDATE $table SET rank = 0, del_flg = 1 ";
1136            $sqlup.= "WHERE $colname = ?";
1137            // UPDATEの実行
1138            $objQuery->exec($sqlup, array($id));
1139        } else {
1140            $objQuery->delete($table, "$colname = ?", array($id));
1141        }
1142
1143        // 追加レコードのランクより上のレコードを一つずらす。
1144        $where = "rank > ?";
1145        if($andwhere != "") {
1146            $where.= " AND $andwhere";
1147        }
1148        $sqlup = "UPDATE $table SET rank = (rank - 1) WHERE $where";
1149        $objQuery->exec($sqlup, array($rank));
1150        $objQuery->commit();
1151    }
1152
1153    /**
1154     * 親IDの配列を元に特定のカラムを取得する.
1155     *
1156     * @param SC_Query $objQuery SC_Query インスタンス
1157     * @param string $table テーブル名
1158     * @param string $id_name ID名
1159     * @param string $col_name カラム名
1160     * @param array $arrId IDの配列
1161     * @return array 特定のカラムの配列
1162     */
1163    function sfGetParentsCol($objQuery, $table, $id_name, $col_name, $arrId ) {
1164        $col = $col_name;
1165        $len = count($arrId);
1166        $where = "";
1167
1168        for($cnt = 0; $cnt < $len; $cnt++) {
1169            if($where == "") {
1170                $where = "$id_name = ?";
1171            } else {
1172                $where.= " OR $id_name = ?";
1173            }
1174        }
1175
1176        $objQuery->setOrder("level");
1177        $arrRet = $objQuery->select($col, $table, $where, $arrId);
1178        return $arrRet;
1179    }
1180
1181    /**
1182     * カテゴリ変更時の移動処理を行う.
1183     *
1184     * @param SC_Query $objQuery SC_Query インスタンス
1185     * @param string $table テーブル名
1186     * @param string $id_name ID名
1187     * @param string $cat_name カテゴリ名
1188     * @param integer $old_catid 旧カテゴリID
1189     * @param integer $new_catid 新カテゴリID
1190     * @param integer $id ID
1191     * @return void
1192     */
1193    function sfMoveCatRank($objQuery, $table, $id_name, $cat_name, $old_catid, $new_catid, $id) {
1194        if ($old_catid == $new_catid) {
1195            return;
1196        }
1197        // 旧カテゴリでのランク削除処理
1198        // 移動レコードのランクを取得する。
1199        $where = "$id_name = ?";
1200        $rank = $objQuery->get("rank", $table, $where, array($id));
1201        // 削除レコードのランクより上のレコードを一つ下にずらす。
1202        $where = "rank > ? AND $cat_name = ?";
1203        $sqlup = "UPDATE $table SET rank = (rank - 1) WHERE $where";
1204        $objQuery->exec($sqlup, array($rank, $old_catid));
1205        // 新カテゴリでの登録処理
1206        // 新カテゴリの最大ランクを取得する。
1207        $max_rank = $objQuery->max("rank", $table, "$cat_name = ?", array($new_catid)) + 1;
1208        $where = "$id_name = ?";
1209        $sqlup = "UPDATE $table SET rank = ? WHERE $where";
1210        $objQuery->exec($sqlup, array($max_rank, $id));
1211    }
1212
1213    /**
1214     * 都道府県から配送料金を取得する.
1215     *
1216     * @param integer|array $pref_id 都道府県ID 又は都道府県IDの配列
1217     * @return string 指定の都道府県, 商品種別の配送料金
1218     */
1219    function sfGetDelivFee($pref_id, $product_type_id) {
1220        $objQuery =& SC_Query::getSingletonInstance();
1221
1222        /*
1223         * FIXME 都道府県が指定されていない場合は、東京都の番号を指定しておく
1224         * http://svn.ec-cube.net/open_trac/ticket/410
1225         */
1226        if($pref_id == "") {
1227            $pref_id = 13;
1228        }
1229        if (!is_array($pref_id)) {
1230            $pref_id = array($pref_id);
1231        }
1232        $sql = <<< __EOS__
1233            SELECT SUM(T1.fee) AS fee
1234              FROM dtb_delivfee T1
1235              JOIN dtb_deliv T2
1236                ON T1.deliv_id = T2.deliv_id
1237             WHERE T1.pref = ? AND T2.product_type_id = ?
1238__EOS__;
1239
1240        $result = 0;
1241        foreach ($pref_id as $pref) {
1242            $result += $objQuery->getOne($sql, array($pref, $product_type_id));
1243        }
1244        return $result;
1245    }
1246
1247    /**
1248     * レコードの存在チェックを行う.
1249     *
1250     * TODO SC_Query に移行するべきか?
1251     *
1252     * @param string $table テーブル名
1253     * @param string $col カラム名
1254     * @param array $arrval 要素の配列
1255     * @param array $addwhere SQL の AND 条件である WHERE 句
1256     * @return bool レコードが存在する場合 true
1257     */
1258    function sfIsRecord($table, $col, $arrval, $addwhere = "") {
1259        $objQuery =& SC_Query::getSingletonInstance();
1260        $arrCol = preg_split("/[, ]/", $col);
1261
1262        $where = "del_flg = 0";
1263
1264        if($addwhere != "") {
1265            $where.= " AND $addwhere";
1266        }
1267
1268        foreach($arrCol as $val) {
1269            if($val != "") {
1270                if($where == "") {
1271                    $where = "$val = ?";
1272                } else {
1273                    $where.= " AND $val = ?";
1274                }
1275            }
1276        }
1277        $ret = $objQuery->get($col, $table, $where, $arrval);
1278
1279        if($ret != "") {
1280            return true;
1281        }
1282        return false;
1283    }
1284
1285    /**
1286     * メーカー商品数数の登録を行う.
1287     *
1288     * @param SC_Query $objQuery SC_Query インスタンス
1289     * @return void
1290     */
1291    function sfCountMaker($objQuery){
1292        $sql = "";
1293
1294        //テーブル内容の削除
1295        $objQuery->query("DELETE FROM dtb_maker_count");
1296
1297        //各メーカーの商品数を数えて格納
1298        $sql = " INSERT INTO dtb_maker_count(maker_id, product_count, create_date) ";
1299        $sql .= " SELECT T1.maker_id, count(T2.maker_id), now() ";
1300        $sql .= " FROM dtb_maker AS T1 LEFT JOIN dtb_products AS T2";
1301        $sql .= " ON T1.maker_id = T2.maker_id ";
1302        $sql .= " WHERE T2.del_flg = 0 AND T2.status = 1 ";
1303        $sql .= " GROUP BY T1.maker_id, T2.maker_id ";
1304        $objQuery->query($sql);
1305    }
1306
1307    /**
1308     * 選択中の商品のメーカーを取得する.
1309     *
1310     * @param integer $product_id プロダクトID
1311     * @param integer $maker_id メーカーID
1312     * @return array 選択中の商品のメーカーIDの配列
1313     *
1314     */
1315    function sfGetMakerId($product_id, $maker_id = 0, $closed = false) {
1316        if ($closed) {
1317            $status = "";
1318        } else {
1319            $status = "status = 1";
1320        }
1321
1322        if (!$this->g_maker_on) {
1323            $this->g_maker_on = true;
1324            $maker_id = (int) $maker_id;
1325            $product_id = (int) $product_id;
1326            if (SC_Utils_Ex::sfIsInt($maker_id) && $maker_id != 0 && $this->sfIsRecord("dtb_maker","maker_id", $maker_id)) {
1327                $this->g_maker_id = array($maker_id);
1328            } else if (SC_Utils_Ex::sfIsInt($product_id) && $product_id != 0 && $this->sfIsRecord("dtb_products","product_id", $product_id, $status)) {
1329                $objQuery =& SC_Query::getSingletonInstance();
1330                $where = "product_id = ?";
1331                $maker_id = $objQuery->getCol("maker_id", "dtb_products", "product_id = ?", array($product_id));
1332                $this->g_maker_id = $maker_id;
1333            } else {
1334                // 不正な場合は、空の配列を返す。
1335                $this->g_maker_id = array();
1336            }
1337        }
1338        return $this->g_maker_id;
1339    }
1340
1341    /**
1342     * メーカーの取得を行う.
1343     *
1344     * $products_check:true商品登録済みのものだけ取得する
1345     *
1346     * @param string $addwhere 追加する WHERE 句
1347     * @param bool $products_check 商品の存在するカテゴリのみ取得する場合 true
1348     * @return array カテゴリツリーの配列
1349     */
1350    function sfGetMakerList($addwhere = "", $products_check = false) {
1351        $objQuery =& SC_Query::getSingletonInstance();
1352        $where = "del_flg = 0";
1353
1354        if($addwhere != "") {
1355            $where.= " AND $addwhere";
1356        }
1357
1358        $objQuery->setOption("ORDER BY rank DESC");
1359
1360        if($products_check) {
1361            $col = "T1.maker_id, name";
1362            $from = "dtb_maker AS T1 LEFT JOIN dtb_maker_count AS T2 ON T1.maker_id = T2.maker_id";
1363            $where .= " AND product_count > 0";
1364        } else {
1365            $col = "maker_id, name";
1366            $from = "dtb_maker";
1367        }
1368
1369        $arrRet = $objQuery->select($col, $from, $where);
1370
1371        $max = count($arrRet);
1372        for($cnt = 0; $cnt < $max; $cnt++) {
1373            $id = $arrRet[$cnt]['maker_id'];
1374            $name = $arrRet[$cnt]['name'];
1375            $arrList[$id] = $name;
1376        }
1377        return $arrList;
1378    }
1379
1380    /**
1381     * 店舗基本情報に基づいて税金額を返す
1382     *
1383     * @param integer $price 計算対象の金額
1384     * @return integer 税金額
1385     */
1386    function sfTax($price) {
1387        // 店舗基本情報を取得
1388        $CONF = SC_Helper_DB_Ex::sfGetBasisData();
1389
1390        return SC_Utils_Ex::sfTax($price, $CONF['tax'], $CONF['tax_rule']);
1391    }
1392
1393    /**
1394     * 店舗基本情報に基づいて税金付与した金額を返す
1395     *
1396     * @param integer $price 計算対象の金額
1397     * @return integer 税金付与した金額
1398     */
1399    function sfCalcIncTax($price, $tax = null, $tax_rule = null) {
1400        // 店舗基本情報を取得
1401        $CONF = SC_Helper_DB_Ex::sfGetBasisData();
1402
1403        return SC_Utils_Ex::sfCalcIncTax($price, $CONF['tax'], $CONF['tax_rule']);
1404    }
1405
1406    /**
1407     * 店舗基本情報に基づいて加算ポイントを返す
1408     *
1409     * @param integer $totalpoint
1410     * @param integer $use_point
1411     * @return integer 加算ポイント
1412     */
1413    function sfGetAddPoint($totalpoint, $use_point) {
1414        // 店舗基本情報を取得
1415        $CONF = SC_Helper_DB_Ex::sfGetBasisData();
1416
1417        return SC_Utils_Ex::sfGetAddPoint($totalpoint, $use_point, $CONF['point_rate']);
1418    }
1419
1420    /**
1421     * 指定ファイルが存在する場合 SQL として実行
1422     *
1423     * XXX プラグイン用に追加。将来消すかも。
1424     *
1425     * @param string $sqlFilePath SQL ファイルのパス
1426     * @return void
1427     */
1428    function sfExecSqlByFile($sqlFilePath) {
1429        if (file_exists($sqlFilePath)) {
1430            $objQuery =& SC_Query::getSingletonInstance();
1431
1432            $sqls = file_get_contents($sqlFilePath);
1433            if ($sqls === false) SC_Utils_Ex::sfDispException('ファイルは存在するが読み込めない');
1434
1435            foreach (explode(';', $sqls) as $sql) {
1436                $sql = trim($sql);
1437                if (strlen($sql) == 0) continue;
1438                $objQuery->query($sql);
1439            }
1440        }
1441    }
1442
1443    /**
1444     * 商品規格を設定しているか
1445     *
1446     * @param integer $product_id 商品ID
1447     * @return bool 商品規格が存在する場合:true, それ以外:false
1448     */
1449    function sfHasProductClass($product_id) {
1450        if (!SC_Utils_Ex::sfIsInt($product_id)) return false;
1451
1452        $objQuery =& SC_Query::getSingletonInstance();
1453        $where = 'product_id = ? AND class_combination_id IS NOT NULL';
1454        $count = $objQuery->count('dtb_products_class', $where, array($product_id));
1455
1456        return $count >= 1;
1457    }
1458}
1459?>
Note: See TracBrowser for help on using the repository browser.