source: branches/version-2_12-dev/data/class/helper/SC_Helper_DB.php @ 21420

Revision 21420, 51.5 KB checked in by Seasoft, 12 years ago (diff)

#1613 (ソース整形・ソースコメントの改善)

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