source: closed/version-2_5-dev/data/class/helper/SC_Helper_DB.php @ 20764

Revision 20764, 50.3 KB checked in by nanasess, 16 years ago (diff)

#601 (コピーライトの更新)

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