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

Revision 21927, 50.6 KB checked in by pineray, 12 years ago (diff)

#1669 #1859 初期化漏れ、無駄な変数等

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