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

Revision 21527, 51.6 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(
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
543        $sqlval = array('category_id' => $category_id,
544                        'product_id' => $product_id);
545
546        $objQuery =& SC_Query_Ex::getSingletonInstance();
547
548        // 現在の商品カテゴリを取得
549        $arrCat = $objQuery->select('product_id, category_id, rank',
550                                    'dtb_product_categories',
551                                    'category_id = ?',
552                                    array($category_id));
553
554        $max = '0';
555        foreach ($arrCat as $val) {
556            // 同一商品が存在する場合は登録しない
557            if ($val['product_id'] == $product_id) {
558                return;
559            }
560            // 最上位ランクを取得
561            $max = ($max < $val['rank']) ? $val['rank'] : $max;
562        }
563        $sqlval['rank'] = $max + 1;
564        $objQuery->insert('dtb_product_categories', $sqlval);
565    }
566
567    /**
568     * 商品をカテゴリの末尾に追加する.
569     *
570     * @param integer $category_id カテゴリID
571     * @param integer $product_id プロダクトID
572     * @return void
573     */
574    function addProductAfterCategories($category_id, $product_id) {
575        $sqlval = array('category_id' => $category_id,
576                        'product_id' => $product_id);
577
578        $objQuery =& SC_Query_Ex::getSingletonInstance();
579
580        // 現在の商品カテゴリを取得
581        $arrCat = $objQuery->select('product_id, category_id, rank',
582                                    'dtb_product_categories',
583                                    'category_id = ?',
584                                    array($category_id));
585
586        $min = 0;
587        foreach ($arrCat as $val) {
588            // 同一商品が存在する場合は登録しない
589            if ($val['product_id'] == $product_id) {
590                return;
591            }
592            // 最下位ランクを取得
593            $min = ($min < $val['rank']) ? $val['rank'] : $min;
594        }
595        $sqlval['rank'] = $min;
596        $objQuery->insert('dtb_product_categories', $sqlval);
597    }
598
599    /**
600     * 商品をカテゴリから削除する.
601     *
602     * @param integer $category_id カテゴリID
603     * @param integer $product_id プロダクトID
604     * @return void
605     */
606    function removeProductByCategories($category_id, $product_id) {
607        $objQuery =& SC_Query_Ex::getSingletonInstance();
608        $objQuery->delete('dtb_product_categories',
609                          'category_id = ? AND product_id = ?', array($category_id, $product_id));
610    }
611
612    /**
613     * 商品カテゴリを更新する.
614     *
615     * @param array $arrCategory_id 登録するカテゴリIDの配列
616     * @param integer $product_id プロダクトID
617     * @return void
618     */
619    function updateProductCategories($arrCategory_id, $product_id) {
620        $objQuery =& SC_Query_Ex::getSingletonInstance();
621
622        // 現在のカテゴリ情報を取得
623        $arrCurrentCat = $objQuery->select('product_id, category_id, rank',
624                                           'dtb_product_categories',
625                                           'product_id = ?',
626                                           array($product_id));
627
628        // 登録するカテゴリ情報と比較
629        foreach ($arrCurrentCat as $val) {
630
631            // 登録しないカテゴリを削除
632            if (!in_array($val['category_id'], $arrCategory_id)) {
633                $this->removeProductByCategories($val['category_id'], $product_id);
634            }
635        }
636
637        // カテゴリを登録
638        foreach ($arrCategory_id as $category_id) {
639            $this->addProductBeforCategories($category_id, $product_id);
640            SC_Utils_Ex::extendTimeOut();
641        }
642    }
643
644    /**
645     * カテゴリ数の登録を行う.
646     *
647     *
648     * @param SC_Query $objQuery SC_Query インスタンス
649     * @param boolean $is_force_all_count 全カテゴリの集計を強制する場合 true
650     * @return void
651     */
652    function sfCountCategory($objQuery = NULL, $is_force_all_count = false) {
653        $objProduct = new SC_Product_Ex();
654
655        if ($objQuery == NULL) {
656            $objQuery =& SC_Query_Ex::getSingletonInstance();
657        }
658
659        $is_out_trans = false;
660        if (!$objQuery->inTransaction()) {
661            $objQuery->begin();
662            $is_out_trans = true;
663        }
664
665        //共通のfrom/where文の構築
666        $sql_where = 'alldtl.del_flg = 0 AND alldtl.status = 1';
667        // 在庫無し商品の非表示
668        if (NOSTOCK_HIDDEN) {
669            $where_products_class = '(stock >= 1 OR stock_unlimited = 1)';
670            $from = $objProduct->alldtlSQL($where_products_class);
671        } else {
672            $from = 'dtb_products as alldtl';
673        }
674
675        //dtb_category_countの構成
676        // 各カテゴリに所属する商品の数を集計。集計対象には子カテゴリを含まない。
677
678        //まずテーブル内容の元を取得
679        if (!$is_force_all_count) {
680            $arrCategoryCountOld = $objQuery->select('category_id,product_count','dtb_category_count');
681        } else {
682            $arrCategoryCountOld = array();
683        }
684
685        //各カテゴリ内の商品数を数えて取得
686        $sql = <<< __EOS__
687            SELECT T1.category_id, count(T2.category_id) as product_count
688            FROM dtb_category AS T1
689                LEFT JOIN dtb_product_categories AS T2
690                    ON T1.category_id = T2.category_id
691                LEFT JOIN $from
692                    ON T2.product_id = alldtl.product_id
693            WHERE $sql_where
694            GROUP BY T1.category_id, T2.category_id
695__EOS__;
696
697        $arrCategoryCountNew = $objQuery->getAll($sql);
698        // 各カテゴリに所属する商品の数を集計。集計対象には子カテゴリを「含む」。
699        //差分を取得して、更新対象カテゴリだけを確認する。
700
701        //各カテゴリ毎のデータ値において以前との差を見る
702        //古いデータの構造入れ替え
703        $arrOld = array();
704        foreach ($arrCategoryCountOld as $item) {
705            $arrOld[$item['category_id']] = $item['product_count'];
706        }
707        //新しいデータの構造入れ替え
708        $arrNew = array();
709        foreach ($arrCategoryCountNew as $item) {
710            $arrNew[$item['category_id']] = $item['product_count'];
711        }
712
713        $arrDiffCategory_id = array();
714        //新しいカテゴリ一覧から見て商品数が異なるデータが無いか確認
715        foreach ($arrNew as $cid => $count) {
716            if ($arrOld[$cid] != $count) {
717                $arrDiffCategory_id[] = $cid;
718            }
719        }
720        //削除カテゴリを想定して、古いカテゴリ一覧から見て商品数が異なるデータが無いか確認。
721        foreach ($arrOld as $cid => $count) {
722            if ($arrNew[$cid] != $count && $count > 0) {
723                $arrDiffCategory_id[] = $cid;
724            }
725        }
726
727        //対象IDが無ければ終了
728        if (count($arrDiffCategory_id) == 0) {
729            if ($is_out_trans) {
730                $objQuery->commit();
731            }
732            return;
733        }
734
735        //差分対象カテゴリIDの重複を除去
736        $arrDiffCategory_id = array_unique($arrDiffCategory_id);
737
738        //dtb_category_countの更新 差分のあったカテゴリだけ更新する。
739        foreach ($arrDiffCategory_id as $cid) {
740            $sqlval = array();
741            $sqlval['create_date'] = 'CURRENT_TIMESTAMP';
742            $sqlval['product_count'] = (string)$arrNew[$cid];
743            if ($sqlval['product_count'] =='') {
744                $sqlval['product_count'] = (string)'0';
745            }
746            if (isset($arrOld[$cid])) {
747                $objQuery->update('dtb_category_count', $sqlval, 'category_id = ?', array($cid));
748            } else {
749                if ($is_force_all_count) {
750                    $ret = $objQuery->update('dtb_category_count', $sqlval, 'category_id = ?', array($cid));
751                    if ($ret > 0) {
752                        continue;
753                    }
754                }
755                $sqlval['category_id'] = $cid;
756                $objQuery->insert('dtb_category_count', $sqlval);
757            }
758        }
759
760        //差分があったIDとその親カテゴリIDのリストを取得する
761        $arrTgtCategory_id = array();
762        foreach ($arrDiffCategory_id as $parent_category_id) {
763            $arrTgtCategory_id[] = $parent_category_id;
764            $arrParentID = $this->sfGetParents('dtb_category', 'parent_category_id', 'category_id', $parent_category_id);
765            $arrTgtCategory_id = array_merge($arrTgtCategory_id, $arrParentID);
766        }
767
768        //重複を取り除く
769        $arrTgtCategory_id = array_unique($arrTgtCategory_id);
770
771        //dtb_category_total_count 集計処理開始
772        //更新対象カテゴリIDだけ集計しなおす。
773        $arrUpdateData = array();
774        foreach ($arrTgtCategory_id as $category_id) {
775            $arrval = array();
776            list($tmp_where, $tmp_arrval) = $this->sfGetCatWhere($category_id);
777            if ($tmp_where != '') {
778                $sql_where_product_ids = 'product_id IN (SELECT product_id FROM dtb_product_categories WHERE ' . $tmp_where . ')';
779                $arrval = array_merge((array)$tmp_arrval, (array)$tmp_arrval);
780            } else {
781                $sql_where_product_ids = '0<>0'; // 一致させない
782            }
783            $where = "($sql_where) AND ($sql_where_product_ids)";
784
785            $where_products_class = '';
786            if (NOSTOCK_HIDDEN) {
787                $where_products_class .= '(stock >= 1 OR stock_unlimited = 1)';
788            }
789
790            $from = $objProduct->alldtlSQL($where_products_class);
791            $sql = "SELECT count(*) FROM $from WHERE $where ";
792            $arrUpdateData[ $category_id ] = $objQuery->getOne($sql, $arrval);
793        }
794        // 更新対象だけを更新。
795        foreach ($arrUpdateData as $cid => $count) {
796            $sqlval = array();
797            $sqlval['create_date'] = 'CURRENT_TIMESTAMP';
798            $sqlval['product_count'] = $count;
799            if ($sqlval['product_count'] =='') {
800                $sqlval['product_count'] = (string)'0';
801            }
802            $ret = $objQuery->update('dtb_category_total_count', $sqlval, 'category_id = ?', array($cid));
803            if (!$ret) {
804                $sqlval['category_id'] = $cid;
805                $ret = $objQuery->insert('dtb_category_total_count', $sqlval);
806            }
807        }
808        // トランザクション終了処理
809        if ($is_out_trans) {
810            $objQuery->commit();
811        }
812    }
813
814    /**
815     * 子IDの配列を返す.
816     *
817     * @param string $table テーブル名
818     * @param string $pid_name 親ID名
819     * @param string $id_name ID名
820     * @param integer $id ID
821     * @param array 子ID の配列
822     */
823    function sfGetChildsID($table, $pid_name, $id_name, $id) {
824        $arrRet = $this->sfGetChildrenArray($table, $pid_name, $id_name, $id);
825        return $arrRet;
826    }
827
828    /**
829     * 階層構造のテーブルから子ID配列を取得する.
830     *
831     * @param string $table テーブル名
832     * @param string $pid_name 親ID名
833     * @param string $id_name ID名
834     * @param integer $id ID番号
835     * @return array 子IDの配列
836     */
837    function sfGetChildrenArray($table, $pid_name, $id_name, $id) {
838        $arrChildren = array();
839        $arrRet = array($id);
840
841        while (count($arrRet) > 0) {
842            $arrChildren = array_merge($arrChildren, $arrRet);
843            $arrRet = SC_Helper_DB_Ex::sfGetChildrenArraySub($table, $pid_name, $id_name, $arrRet);
844        }
845
846        return $arrChildren;
847    }
848
849    /**
850     * 親ID直下の子IDをすべて取得する.
851     *
852     * @param array $arrData 親カテゴリの配列
853     * @param string $pid_name 親ID名
854     * @param string $id_name ID名
855     * @param array $arrPID 親IDの配列
856     * @return array 子IDの配列
857     */
858    function sfGetChildrenArraySub($table, $pid_name, $id_name, $arrPID) {
859        $objQuery =& SC_Query_Ex::getSingletonInstance();
860
861        $where = "$pid_name IN (" . implode(',', array_fill(0, count($arrPID), '?')) . ")";
862
863        $ret = $objQuery->select($id_name, $table, $where, $arrPID);
864
865        $arrChildren = array();
866        foreach ($ret as $val) {
867            $arrChildren[] = $val[$id_name];
868        }
869
870        return $arrChildren;
871    }
872
873    /**
874     * 所属するすべての階層の親IDを配列で返す.
875     *
876     * @param SC_Query $objQuery SC_Query インスタンス
877     * @param string $table テーブル名
878     * @param string $pid_name 親ID名
879     * @param string $id_name ID名
880     * @param integer $id ID
881     * @return array 親IDの配列
882     */
883    function sfGetParents($table, $pid_name, $id_name, $id) {
884        $arrRet = SC_Helper_DB_Ex::sfGetParentsArray($table, $pid_name, $id_name, $id);
885        return $arrRet;
886    }
887
888    /**
889     * 階層構造のテーブルから親ID配列を取得する.
890     *
891     * @param string $table テーブル名
892     * @param string $pid_name 親ID名
893     * @param string $id_name ID名
894     * @param integer $id ID
895     * @return array 親IDの配列
896     */
897    function sfGetParentsArray($table, $pid_name, $id_name, $id) {
898        $arrParents = array();
899        $ret = $id;
900
901        while ($ret != '0' && !SC_Utils_Ex::isBlank($ret)) {
902            $arrParents[] = $ret;
903            $ret = SC_Helper_DB_Ex::sfGetParentsArraySub($table, $pid_name, $id_name, $ret);
904        }
905
906        $arrParents = array_reverse($arrParents);
907
908        return $arrParents;
909    }
910
911    /* 子ID所属する親IDを取得する */
912    function sfGetParentsArraySub($table, $pid_name, $id_name, $child) {
913        if (SC_Utils_Ex::isBlank($child)) {
914            return false;
915        }
916        $objQuery =& SC_Query_Ex::getSingletonInstance();
917        if (!is_array($child)) {
918            $child = array($child);
919        }
920        $parent = $objQuery->get($pid_name, $table, "$id_name = ?", $child);
921        return $parent;
922    }
923
924    /**
925     * カテゴリから商品を検索する場合のWHERE文と値を返す.
926     *
927     * @param integer $category_id カテゴリID
928     * @return array 商品を検索する場合の配列
929     */
930    function sfGetCatWhere($category_id) {
931        // 子カテゴリIDの取得
932        $arrRet = SC_Helper_DB_Ex::sfGetChildrenArray('dtb_category', 'parent_category_id', 'category_id', $category_id);
933
934        $where = 'category_id IN (' . implode(',', array_fill(0, count($arrRet), '?')) . ')';
935
936        return array($where, $arrRet);
937    }
938
939    /**
940     * SELECTボックス用リストを作成する.
941     *
942     * @param string $table テーブル名
943     * @param string $keyname プライマリーキーのカラム名
944     * @param string $valname データ内容のカラム名
945     * @param string $where WHERE句
946     * @param array $arrval プレースホルダ
947     * @return array SELECT ボックス用リストの配列
948     */
949    function sfGetIDValueList($table, $keyname, $valname, $where = '', $arrVal = array()) {
950        $objQuery =& SC_Query_Ex::getSingletonInstance();
951        $col = "$keyname, $valname";
952        $objQuery->setWhere('del_flg = 0');
953        $objQuery->setOrder('rank DESC');
954        $arrList = $objQuery->select($col, $table, $where, $arrVal);
955        $count = count($arrList);
956        for ($cnt = 0; $cnt < $count; $cnt++) {
957            $key = $arrList[$cnt][$keyname];
958            $val = $arrList[$cnt][$valname];
959            $arrRet[$key] = $val;
960        }
961        return $arrRet;
962    }
963
964    /**
965     * ランキングを上げる.
966     *
967     * @param string $table テーブル名
968     * @param string $colname カラム名
969     * @param string|integer $id テーブルのキー
970     * @param string $andwhere SQL の AND 条件である WHERE 句
971     * @return void
972     */
973    function sfRankUp($table, $colname, $id, $andwhere = '') {
974        $objQuery =& SC_Query_Ex::getSingletonInstance();
975        $objQuery->begin();
976        $where = "$colname = ?";
977        if ($andwhere != '') {
978            $where.= " AND $andwhere";
979        }
980        // 対象項目のランクを取得
981        $rank = $objQuery->get('rank', $table, $where, array($id));
982        // ランクの最大値を取得
983        $maxrank = $objQuery->max('rank', $table, $andwhere);
984        // ランクが最大値よりも小さい場合に実行する。
985        if ($rank < $maxrank) {
986            // ランクが一つ上のIDを取得する。
987            $where = 'rank = ?';
988            if ($andwhere != '') {
989                $where.= " AND $andwhere";
990            }
991            $uprank = $rank + 1;
992            $up_id = $objQuery->get($colname, $table, $where, array($uprank));
993            // ランク入れ替えの実行
994            $sqlup = "UPDATE $table SET rank = ? WHERE $colname = ?";
995            if ($andwhere != '') {
996                $sqlup.= " AND $andwhere";
997            }
998            $objQuery->exec($sqlup, array($rank + 1, $id));
999            $objQuery->exec($sqlup, array($rank, $up_id));
1000        }
1001        $objQuery->commit();
1002    }
1003
1004    /**
1005     * ランキングを下げる.
1006     *
1007     * @param string $table テーブル名
1008     * @param string $colname カラム名
1009     * @param string|integer $id テーブルのキー
1010     * @param string $andwhere SQL の AND 条件である WHERE 句
1011     * @return void
1012     */
1013    function sfRankDown($table, $colname, $id, $andwhere = '') {
1014        $objQuery =& SC_Query_Ex::getSingletonInstance();
1015        $objQuery->begin();
1016        $where = "$colname = ?";
1017        if ($andwhere != '') {
1018            $where.= " AND $andwhere";
1019        }
1020        // 対象項目のランクを取得
1021        $rank = $objQuery->get('rank', $table, $where, array($id));
1022
1023        // ランクが1(最小値)よりも大きい場合に実行する。
1024        if ($rank > 1) {
1025            // ランクが一つ下のIDを取得する。
1026            $where = 'rank = ?';
1027            if ($andwhere != '') {
1028                $where.= " AND $andwhere";
1029            }
1030            $downrank = $rank - 1;
1031            $down_id = $objQuery->get($colname, $table, $where, array($downrank));
1032            // ランク入れ替えの実行
1033            $sqlup = "UPDATE $table SET rank = ? WHERE $colname = ?";
1034            if ($andwhere != '') {
1035                $sqlup.= " AND $andwhere";
1036            }
1037            $objQuery->exec($sqlup, array($rank - 1, $id));
1038            $objQuery->exec($sqlup, array($rank, $down_id));
1039        }
1040        $objQuery->commit();
1041    }
1042
1043    /**
1044     * 指定順位へ移動する.
1045     *
1046     * @param string $tableName テーブル名
1047     * @param string $keyIdColumn キーを保持するカラム名
1048     * @param string|integer $keyId キーの値
1049     * @param integer $pos 指定順位
1050     * @param string $where SQL の AND 条件である WHERE 句
1051     * @return void
1052     */
1053    function sfMoveRank($tableName, $keyIdColumn, $keyId, $pos, $where = '') {
1054        $objQuery =& SC_Query_Ex::getSingletonInstance();
1055        $objQuery->begin();
1056
1057        // 自身のランクを取得する
1058        if ($where != '') {
1059            $getWhere = "$keyIdColumn = ? AND " . $where;
1060        } else {
1061            $getWhere = "$keyIdColumn = ?";
1062        }
1063        $rank = $objQuery->get('rank', $tableName, $getWhere, array($keyId));
1064
1065        $max = $objQuery->max('rank', $tableName, $where);
1066
1067        // 値の調整(逆順)
1068        if ($pos > $max) {
1069            $position = 1;
1070        } else if ($pos < 1) {
1071            $position = $max;
1072        } else {
1073            $position = $max - $pos + 1;
1074        }
1075
1076        //入れ替え先の順位が入れ換え元の順位より大きい場合
1077        if($position > $rank) $term = 'rank - 1';
1078
1079        //入れ替え先の順位が入れ換え元の順位より小さい場合
1080        if($position < $rank) $term = 'rank + 1';
1081
1082        // XXX 入れ替え先の順位が入れ替え元の順位と同じ場合
1083        if (!isset($term)) $term = 'rank';
1084
1085        // 指定した順位の商品から移動させる商品までのrankを1つずらす
1086        $sql = "UPDATE $tableName SET rank = $term WHERE rank BETWEEN ? AND ?";
1087        if ($where != '') {
1088            $sql.= " AND $where";
1089        }
1090
1091        if($position > $rank) $objQuery->exec($sql, array($rank + 1, $position));
1092        if($position < $rank) $objQuery->exec($sql, array($position, $rank - 1));
1093
1094        // 指定した順位へrankを書き換える。
1095        $sql  = "UPDATE $tableName SET rank = ? WHERE $keyIdColumn = ? ";
1096        if ($where != '') {
1097            $sql.= " AND $where";
1098        }
1099
1100        $objQuery->exec($sql, array($position, $keyId));
1101        $objQuery->commit();
1102    }
1103
1104    /**
1105     * ランクを含むレコードを削除する.
1106     *
1107     * レコードごと削除する場合は、$deleteをtrueにする
1108     *
1109     * @param string $table テーブル名
1110     * @param string $colname カラム名
1111     * @param string|integer $id テーブルのキー
1112     * @param string $andwhere SQL の AND 条件である WHERE 句
1113     * @param bool $delete レコードごと削除する場合 true,
1114     *                     レコードごと削除しない場合 false
1115     * @return void
1116     */
1117    function sfDeleteRankRecord($table, $colname, $id, $andwhere = '',
1118                                $delete = false) {
1119        $objQuery =& SC_Query_Ex::getSingletonInstance();
1120        $objQuery->begin();
1121        // 削除レコードのランクを取得する。
1122        $where = "$colname = ?";
1123        if ($andwhere != '') {
1124            $where.= " AND $andwhere";
1125        }
1126        $rank = $objQuery->get('rank', $table, $where, array($id));
1127
1128        if (!$delete) {
1129            // ランクを最下位にする、DELフラグON
1130            $sqlup = "UPDATE $table SET rank = 0, del_flg = 1 ";
1131            $sqlup.= "WHERE $colname = ?";
1132            // UPDATEの実行
1133            $objQuery->exec($sqlup, array($id));
1134        } else {
1135            $objQuery->delete($table, "$colname = ?", array($id));
1136        }
1137
1138        // 追加レコードのランクより上のレコードを一つずらす。
1139        $where = 'rank > ?';
1140        if ($andwhere != '') {
1141            $where.= " AND $andwhere";
1142        }
1143        $sqlup = "UPDATE $table SET rank = (rank - 1) WHERE $where";
1144        $objQuery->exec($sqlup, array($rank));
1145        $objQuery->commit();
1146    }
1147
1148    /**
1149     * 親IDの配列を元に特定のカラムを取得する.
1150     *
1151     * @param SC_Query $objQuery SC_Query インスタンス
1152     * @param string $table テーブル名
1153     * @param string $id_name ID名
1154     * @param string $col_name カラム名
1155     * @param array $arrId IDの配列
1156     * @return array 特定のカラムの配列
1157     */
1158    function sfGetParentsCol($objQuery, $table, $id_name, $col_name, $arrId) {
1159        $col = $col_name;
1160        $len = count($arrId);
1161        $where = '';
1162
1163        for ($cnt = 0; $cnt < $len; $cnt++) {
1164            if ($where == '') {
1165                $where = "$id_name = ?";
1166            } else {
1167                $where.= " OR $id_name = ?";
1168            }
1169        }
1170
1171        $objQuery->setOrder('level');
1172        $arrRet = $objQuery->select($col, $table, $where, $arrId);
1173        return $arrRet;
1174    }
1175
1176    /**
1177     * カテゴリ変更時の移動処理を行う.
1178     *
1179     * @param SC_Query $objQuery SC_Query インスタンス
1180     * @param string $table テーブル名
1181     * @param string $id_name ID名
1182     * @param string $cat_name カテゴリ名
1183     * @param integer $old_catid 旧カテゴリID
1184     * @param integer $new_catid 新カテゴリID
1185     * @param integer $id ID
1186     * @return void
1187     */
1188    function sfMoveCatRank($objQuery, $table, $id_name, $cat_name, $old_catid, $new_catid, $id) {
1189        if ($old_catid == $new_catid) {
1190            return;
1191        }
1192        // 旧カテゴリでのランク削除処理
1193        // 移動レコードのランクを取得する。
1194        $where = "$id_name = ?";
1195        $rank = $objQuery->get('rank', $table, $where, array($id));
1196        // 削除レコードのランクより上のレコードを一つ下にずらす。
1197        $where = "rank > ? AND $cat_name = ?";
1198        $sqlup = "UPDATE $table SET rank = (rank - 1) WHERE $where";
1199        $objQuery->exec($sqlup, array($rank, $old_catid));
1200        // 新カテゴリでの登録処理
1201        // 新カテゴリの最大ランクを取得する。
1202        $max_rank = $objQuery->max('rank', $table, "$cat_name = ?", array($new_catid)) + 1;
1203        $where = "$id_name = ?";
1204        $sqlup = "UPDATE $table SET rank = ? WHERE $where";
1205        $objQuery->exec($sqlup, array($max_rank, $id));
1206    }
1207
1208    /**
1209     * 都道府県から配送料金を取得する.
1210     *
1211     * @param integer|array $pref_id 都道府県ID 又は都道府県IDの配列
1212     * @param integer $deliv_id 配送業者ID
1213     * @return string 指定の都道府県, 配送業者の配送料金
1214     */
1215    function sfGetDelivFee($pref_id, $deliv_id = 0) {
1216        $objQuery =& SC_Query_Ex::getSingletonInstance();
1217        if (!is_array($pref_id)) {
1218            $pref_id = array($pref_id);
1219        }
1220        $sql = <<< __EOS__
1221            SELECT T1.fee AS fee
1222            FROM dtb_delivfee T1
1223                JOIN dtb_deliv T2
1224                    ON T1.deliv_id = T2.deliv_id
1225            WHERE T1.pref = ?
1226                AND T1.deliv_id = ?
1227                AND T2.del_flg = 0
1228__EOS__;
1229        $result = 0;
1230        foreach ($pref_id as $pref) {
1231            $result += $objQuery->getOne($sql, array($pref, $deliv_id));
1232        }
1233        return $result;
1234    }
1235
1236    /**
1237     * レコードの存在チェックを行う.
1238     *
1239     * TODO SC_Query に移行するべきか?
1240     *
1241     * @param string $table テーブル名
1242     * @param string $col カラム名
1243     * @param array $arrval 要素の配列
1244     * @param array $addwhere SQL の AND 条件である WHERE 句
1245     * @return bool レコードが存在する場合 true
1246     */
1247    function sfIsRecord($table, $col, $arrval, $addwhere = '') {
1248        $objQuery =& SC_Query_Ex::getSingletonInstance();
1249        $arrCol = preg_split('/[, ]/', $col);
1250
1251        $where = 'del_flg = 0';
1252
1253        if ($addwhere != '') {
1254            $where.= " AND $addwhere";
1255        }
1256
1257        foreach ($arrCol as $val) {
1258            if ($val != '') {
1259                if ($where == '') {
1260                    $where = "$val = ?";
1261                } else {
1262                    $where.= " AND $val = ?";
1263                }
1264            }
1265        }
1266        $ret = $objQuery->get($col, $table, $where, $arrval);
1267
1268        if ($ret != '') {
1269            return true;
1270        }
1271        return false;
1272    }
1273
1274    /**
1275     * メーカー商品数数の登録を行う.
1276     *
1277     * @param SC_Query $objQuery SC_Query インスタンス
1278     * @return void
1279     */
1280    function sfCountMaker($objQuery) {
1281        $sql = '';
1282
1283        //テーブル内容の削除
1284        $objQuery->query('DELETE FROM dtb_maker_count');
1285
1286        //各メーカーの商品数を数えて格納
1287        $sql = ' INSERT INTO dtb_maker_count(maker_id, product_count, create_date) ';
1288        $sql .= ' SELECT T1.maker_id, count(T2.maker_id), CURRENT_TIMESTAMP ';
1289        $sql .= ' FROM dtb_maker AS T1 LEFT JOIN dtb_products AS T2';
1290        $sql .= ' ON T1.maker_id = T2.maker_id ';
1291        $sql .= ' WHERE T2.del_flg = 0 AND T2.status = 1 ';
1292        $sql .= ' GROUP BY T1.maker_id, T2.maker_id ';
1293        $objQuery->query($sql);
1294    }
1295
1296    /**
1297     * 選択中の商品のメーカーを取得する.
1298     *
1299     * @param integer $product_id プロダクトID
1300     * @param integer $maker_id メーカーID
1301     * @return array 選択中の商品のメーカーIDの配列
1302     *
1303     */
1304    function sfGetMakerId($product_id, $maker_id = 0, $closed = false) {
1305        if ($closed) {
1306            $status = '';
1307        } else {
1308            $status = 'status = 1';
1309        }
1310
1311        if (!$this->g_maker_on) {
1312            $this->g_maker_on = true;
1313            $maker_id = (int) $maker_id;
1314            $product_id = (int) $product_id;
1315            if (SC_Utils_Ex::sfIsInt($maker_id) && $maker_id != 0 && $this->sfIsRecord('dtb_maker','maker_id', $maker_id)) {
1316                $this->g_maker_id = array($maker_id);
1317            } else if (SC_Utils_Ex::sfIsInt($product_id) && $product_id != 0 && $this->sfIsRecord('dtb_products','product_id', $product_id, $status)) {
1318                $objQuery =& SC_Query_Ex::getSingletonInstance();
1319                $where = 'product_id = ?';
1320                $maker_id = $objQuery->getCol('maker_id', 'dtb_products', 'product_id = ?', array($product_id));
1321                $this->g_maker_id = $maker_id;
1322            } else {
1323                // 不正な場合は、空の配列を返す。
1324                $this->g_maker_id = array();
1325            }
1326        }
1327        return $this->g_maker_id;
1328    }
1329
1330    /**
1331     * メーカーの取得を行う.
1332     *
1333     * $products_check:true商品登録済みのものだけ取得する
1334     *
1335     * @param string $addwhere 追加する WHERE 句
1336     * @param bool $products_check 商品の存在するカテゴリのみ取得する場合 true
1337     * @return array カテゴリツリーの配列
1338     */
1339    function sfGetMakerList($addwhere = '', $products_check = false) {
1340        $objQuery =& SC_Query_Ex::getSingletonInstance();
1341        $where = 'del_flg = 0';
1342
1343        if ($addwhere != '') {
1344            $where.= " AND $addwhere";
1345        }
1346
1347        $objQuery->setOption('ORDER BY rank DESC');
1348
1349        if ($products_check) {
1350            $col = 'T1.maker_id, name';
1351            $from = 'dtb_maker AS T1 LEFT JOIN dtb_maker_count AS T2 ON T1.maker_id = T2.maker_id';
1352            $where .= ' AND product_count > 0';
1353        } else {
1354            $col = 'maker_id, name';
1355            $from = 'dtb_maker';
1356        }
1357
1358        $arrRet = $objQuery->select($col, $from, $where);
1359
1360        $max = count($arrRet);
1361        for ($cnt = 0; $cnt < $max; $cnt++) {
1362            $id = $arrRet[$cnt]['maker_id'];
1363            $name = $arrRet[$cnt]['name'];
1364            $arrList[$id] = $name;
1365        }
1366        return $arrList;
1367    }
1368
1369    /**
1370     * 店舗基本情報に基づいて税金額を返す
1371     *
1372     * @param integer $price 計算対象の金額
1373     * @return integer 税金額
1374     */
1375    function sfTax($price) {
1376        // 店舗基本情報を取得
1377        $CONF = SC_Helper_DB_Ex::sfGetBasisData();
1378
1379        return SC_Utils_Ex::sfTax($price, $CONF['tax'], $CONF['tax_rule']);
1380    }
1381
1382    /**
1383     * 店舗基本情報に基づいて税金付与した金額を返す
1384     *
1385     * @param integer $price 計算対象の金額
1386     * @return integer 税金付与した金額
1387     */
1388    function sfCalcIncTax($price, $tax = null, $tax_rule = null) {
1389        // 店舗基本情報を取得
1390        $CONF = SC_Helper_DB_Ex::sfGetBasisData();
1391
1392        return SC_Utils_Ex::sfCalcIncTax($price, $CONF['tax'], $CONF['tax_rule']);
1393    }
1394
1395    /**
1396     * 店舗基本情報に基づいて加算ポイントを返す
1397     *
1398     * @param integer $totalpoint
1399     * @param integer $use_point
1400     * @return integer 加算ポイント
1401     */
1402    function sfGetAddPoint($totalpoint, $use_point) {
1403        // 店舗基本情報を取得
1404        $CONF = SC_Helper_DB_Ex::sfGetBasisData();
1405
1406        return SC_Utils_Ex::sfGetAddPoint($totalpoint, $use_point, $CONF['point_rate']);
1407    }
1408
1409    /**
1410     * 指定ファイルが存在する場合 SQL として実行
1411     *
1412     * XXX プラグイン用に追加。将来消すかも。
1413     *
1414     * @param string $sqlFilePath SQL ファイルのパス
1415     * @return void
1416     */
1417    function sfExecSqlByFile($sqlFilePath) {
1418        if (file_exists($sqlFilePath)) {
1419            $objQuery =& SC_Query_Ex::getSingletonInstance();
1420
1421            $sqls = file_get_contents($sqlFilePath);
1422            if ($sqls === false) SC_Utils_Ex::sfDispException('ファイルは存在するが読み込めない');
1423
1424            foreach (explode(';', $sqls) as $sql) {
1425                $sql = trim($sql);
1426                if (strlen($sql) == 0) continue;
1427                $objQuery->query($sql);
1428            }
1429        }
1430    }
1431
1432    /**
1433     * 商品規格を設定しているか
1434     *
1435     * @param integer $product_id 商品ID
1436     * @return bool 商品規格が存在する場合:true, それ以外:false
1437     */
1438    function sfHasProductClass($product_id) {
1439        if (!SC_Utils_Ex::sfIsInt($product_id)) return false;
1440
1441        $objQuery =& SC_Query_Ex::getSingletonInstance();
1442        $where = 'product_id = ? AND del_flg = 0 AND classcategory_id1 != 0 AND classcategory_id2 != 0';
1443        $exists = $objQuery->exists('dtb_products_class', $where, array($product_id));
1444
1445        return $exists;
1446    }
1447}
Note: See TracBrowser for help on using the repository browser.