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

Revision 21750, 50.4 KB checked in by shutta, 12 years ago (diff)

#1579 SC_Query::getSingletonInstance()への置き換え

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