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

Revision 22012, 51.8 KB checked in by shutta, 12 years ago (diff)

refs #1925 (SC_Queryのupdateメソッドの使用を推奨)
SC_Queryのupdateメソッドを使用するように書き換えた。

  • 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            // ランク入れ替えの実行
989            $where = "$colname = ?";
990            if ($andwhere != '') {
991                $where .= " AND $andwhere";
992            }
993
994            $sqlval = array(
995                'rank' => $rank + 1,
996            );
997            $arrWhereVal = array($id);
998            $objQuery->update($table, $sqlval, $where, $arrWhereVal);
999
1000            $sqlval = array(
1001                'rank' => $rank,
1002            );
1003            $arrWhereVal = array($up_id);
1004            $objQuery->update($table, $sqlval, $where, $arrWhereVal);
1005        }
1006        $objQuery->commit();
1007    }
1008
1009    /**
1010     * ランキングを下げる.
1011     *
1012     * @param string $table テーブル名
1013     * @param string $colname カラム名
1014     * @param string|integer $id テーブルのキー
1015     * @param string $andwhere SQL の AND 条件である WHERE 句
1016     * @return void
1017     */
1018    function sfRankDown($table, $colname, $id, $andwhere = '') {
1019        $objQuery =& SC_Query_Ex::getSingletonInstance();
1020        $objQuery->begin();
1021        $where = "$colname = ?";
1022        if ($andwhere != '') {
1023            $where.= " AND $andwhere";
1024        }
1025        // 対象項目のランクを取得
1026        $rank = $objQuery->get('rank', $table, $where, array($id));
1027
1028        // ランクが1(最小値)よりも大きい場合に実行する。
1029        if ($rank > 1) {
1030            // ランクが一つ下のIDを取得する。
1031            $where = 'rank = ?';
1032            if ($andwhere != '') {
1033                $where.= " AND $andwhere";
1034            }
1035            $downrank = $rank - 1;
1036            $down_id = $objQuery->get($colname, $table, $where, array($downrank));
1037
1038            // ランク入れ替えの実行
1039            $where = "$colname = ?";
1040            if ($andwhere != '') {
1041                $where .= " AND $andwhere";
1042            }
1043
1044            $sqlval = array(
1045                'rank' => $rank - 1,
1046            );
1047            $arrWhereVal = array($id);
1048            $objQuery->update($table, $sqlval, $where, $arrWhereVal);
1049
1050            $sqlval = array(
1051                'rank' => $rank,
1052            );
1053            $arrWhereVal = array($down_id);
1054            $objQuery->update($table, $sqlval, $where, $arrWhereVal);
1055        }
1056        $objQuery->commit();
1057    }
1058
1059    /**
1060     * 指定順位へ移動する.
1061     *
1062     * @param string $tableName テーブル名
1063     * @param string $keyIdColumn キーを保持するカラム名
1064     * @param string|integer $keyId キーの値
1065     * @param integer $pos 指定順位
1066     * @param string $where SQL の AND 条件である WHERE 句
1067     * @return void
1068     */
1069    function sfMoveRank($tableName, $keyIdColumn, $keyId, $pos, $where = '') {
1070        $objQuery =& SC_Query_Ex::getSingletonInstance();
1071        $objQuery->begin();
1072
1073        // 自身のランクを取得する
1074        if ($where != '') {
1075            $getWhere = "$keyIdColumn = ? AND " . $where;
1076        } else {
1077            $getWhere = "$keyIdColumn = ?";
1078        }
1079        $rank = $objQuery->get('rank', $tableName, $getWhere, array($keyId));
1080
1081        $max = $objQuery->max('rank', $tableName, $where);
1082
1083        // 値の調整(逆順)
1084        if ($pos > $max) {
1085            $position = 1;
1086        } else if ($pos < 1) {
1087            $position = $max;
1088        } else {
1089            $position = $max - $pos + 1;
1090        }
1091
1092        //入れ替え先の順位が入れ換え元の順位より大きい場合
1093        if ($position > $rank) $term = 'rank - 1';
1094
1095        //入れ替え先の順位が入れ換え元の順位より小さい場合
1096        if ($position < $rank) $term = 'rank + 1';
1097
1098        // XXX 入れ替え先の順位が入れ替え元の順位と同じ場合
1099        if (!isset($term)) $term = 'rank';
1100
1101        // 指定した順位の商品から移動させる商品までのrankを1つずらす
1102        $sqlval = array();
1103        $arrRawSql = array(
1104            'rank' => $term,
1105        );
1106        $str_where = 'rank BETWEEN ? AND ?';
1107        if ($where != '') {
1108            $str_where .= " AND $where";
1109        }
1110
1111        if ($position > $rank) {
1112            $arrWhereVal = array($rank + 1, $position);
1113            $objQuery->update($tableName, $sqlval, $str_where, $arrWhereVal, $arrRawSql);
1114        }
1115        if ($position < $rank) {
1116            $arrWhereVal = array($position, $rank - 1);
1117            $objQuery->update($tableName, $sqlval, $str_where, $arrWhereVal, $arrRawSql);
1118        }
1119
1120        // 指定した順位へrankを書き換える。
1121        $sqlval = array(
1122            'rank' => $position,
1123        );
1124        $str_where = "$keyIdColumn = ?";
1125        if ($where != '') {
1126            $str_where .= " AND $where";
1127        }
1128        $arrWhereVal = array($keyId);
1129        $objQuery->update($tableName, $sqlval, $str_where, $arrWhereVal);
1130
1131        $objQuery->commit();
1132    }
1133
1134    /**
1135     * ランクを含むレコードを削除する.
1136     *
1137     * レコードごと削除する場合は、$deleteをtrueにする
1138     *
1139     * @param string $table テーブル名
1140     * @param string $colname カラム名
1141     * @param string|integer $id テーブルのキー
1142     * @param string $andwhere SQL の AND 条件である WHERE 句
1143     * @param bool $delete レコードごと削除する場合 true,
1144     *                     レコードごと削除しない場合 false
1145     * @return void
1146     */
1147    function sfDeleteRankRecord($table, $colname, $id, $andwhere = '',
1148                                $delete = false) {
1149        $objQuery =& SC_Query_Ex::getSingletonInstance();
1150        $objQuery->begin();
1151        // 削除レコードのランクを取得する。
1152        $where = "$colname = ?";
1153        if ($andwhere != '') {
1154            $where.= " AND $andwhere";
1155        }
1156        $rank = $objQuery->get('rank', $table, $where, array($id));
1157
1158        if (!$delete) {
1159            // ランクを最下位にする、DELフラグON
1160            $sqlval = array(
1161                'rank'      => 0,
1162                'del_flg'   => 1,
1163            );
1164            $where = "$colname = ?";
1165            $arrWhereVal = array($id);
1166            $objQuery->update($table, $sqlval, $where, $arrWhereVal);
1167        } else {
1168            $objQuery->delete($table, "$colname = ?", array($id));
1169        }
1170
1171        // 追加レコードのランクより上のレコードを一つずらす。
1172        $sqlval = array();
1173        $where = 'rank > ?';
1174        if ($andwhere != '') {
1175            $where .= " AND $andwhere";
1176        }
1177        $arrWhereVal = array($rank);
1178        $arrRawSql = array(
1179            'rank' => '(rank - 1)',
1180        );
1181        $objQuery->update($table, $sqlval, $where, $arrWhereVal, $arrRawSql);
1182
1183        $objQuery->commit();
1184    }
1185
1186    /**
1187     * 親IDの配列を元に特定のカラムを取得する.
1188     *
1189     * @param SC_Query $objQuery SC_Query インスタンス
1190     * @param string $table テーブル名
1191     * @param string $id_name ID名
1192     * @param string $col_name カラム名
1193     * @param array $arrId IDの配列
1194     * @return array 特定のカラムの配列
1195     */
1196    function sfGetParentsCol($objQuery, $table, $id_name, $col_name, $arrId) {
1197        $col = $col_name;
1198        $len = count($arrId);
1199        $where = '';
1200
1201        for ($cnt = 0; $cnt < $len; $cnt++) {
1202            if ($where == '') {
1203                $where = "$id_name = ?";
1204            } else {
1205                $where.= " OR $id_name = ?";
1206            }
1207        }
1208
1209        $objQuery->setOrder('level');
1210        $arrRet = $objQuery->select($col, $table, $where, $arrId);
1211        return $arrRet;
1212    }
1213
1214    /**
1215     * カテゴリ変更時の移動処理を行う.
1216     *
1217     * ※この関数って、どこからも呼ばれていないのでは??
1218     *
1219     * @param SC_Query $objQuery SC_Query インスタンス
1220     * @param string $table テーブル名
1221     * @param string $id_name ID名
1222     * @param string $cat_name カテゴリ名
1223     * @param integer $old_catid 旧カテゴリID
1224     * @param integer $new_catid 新カテゴリID
1225     * @param integer $id ID
1226     * @return void
1227     */
1228    function sfMoveCatRank($objQuery, $table, $id_name, $cat_name, $old_catid, $new_catid, $id) {
1229        if ($old_catid == $new_catid) {
1230            return;
1231        }
1232        // 旧カテゴリでのランク削除処理
1233        // 移動レコードのランクを取得する。
1234        $sqlval = array();
1235        $where = "$id_name = ?";
1236        $rank = $objQuery->get('rank', $table, $where, array($id));
1237        // 削除レコードのランクより上のレコードを一つ下にずらす。
1238        $where = "rank > ? AND $cat_name = ?";
1239        $arrWhereVal = array($rank, $old_catid);
1240        $arrRawSql = array(
1241            'rank' => '(rank - 1)',
1242        );
1243        $objQuery->update($table, $sqlval, $where, $arrWhereVal, $arrRawSql);
1244
1245        // 新カテゴリでの登録処理
1246        // 新カテゴリの最大ランクを取得する。
1247        $max_rank = $objQuery->max('rank', $table, "$cat_name = ?", array($new_catid)) + 1;
1248        $sqlval = array(
1249            'rank' => $max_rank,
1250        );
1251        $where = "$id_name = ?";
1252        $arrWhereVal = array($id);
1253        $objQuery->update($table, $sqlval, $where, $arrWhereVal);
1254    }
1255
1256    /**
1257     * レコードの存在チェックを行う.
1258     *
1259     * TODO SC_Query に移行するべきか?
1260     *
1261     * @param string $table テーブル名
1262     * @param string $col カラム名
1263     * @param array $arrVal 要素の配列
1264     * @param array $addwhere SQL の AND 条件である WHERE 句
1265     * @return bool レコードが存在する場合 true
1266     */
1267    function sfIsRecord($table, $col, $arrVal, $addwhere = '') {
1268        $objQuery =& SC_Query_Ex::getSingletonInstance();
1269        $arrCol = preg_split('/[, ]/', $col);
1270
1271        $where = 'del_flg = 0';
1272
1273        if ($addwhere != '') {
1274            $where.= " AND $addwhere";
1275        }
1276
1277        foreach ($arrCol as $val) {
1278            if ($val != '') {
1279                if ($where == '') {
1280                    $where = "$val = ?";
1281                } else {
1282                    $where.= " AND $val = ?";
1283                }
1284            }
1285        }
1286        $ret = $objQuery->get($col, $table, $where, $arrVal);
1287
1288        if ($ret != '') {
1289            return true;
1290        }
1291        return false;
1292    }
1293
1294    /**
1295     * メーカー商品数数の登録を行う.
1296     *
1297     * @param SC_Query $objQuery SC_Query インスタンス
1298     * @return void
1299     */
1300    function sfCountMaker($objQuery) {
1301        $sql = '';
1302
1303        //テーブル内容の削除
1304        $objQuery->query('DELETE FROM dtb_maker_count');
1305
1306        //各メーカーの商品数を数えて格納
1307        $sql = ' INSERT INTO dtb_maker_count(maker_id, product_count, create_date) ';
1308        $sql .= ' SELECT T1.maker_id, count(T2.maker_id), CURRENT_TIMESTAMP ';
1309        $sql .= ' FROM dtb_maker AS T1 LEFT JOIN dtb_products AS T2';
1310        $sql .= ' ON T1.maker_id = T2.maker_id ';
1311        $sql .= ' WHERE T2.del_flg = 0 AND T2.status = 1 ';
1312        $sql .= ' GROUP BY T1.maker_id, T2.maker_id ';
1313        $objQuery->query($sql);
1314    }
1315
1316    /**
1317     * 選択中の商品のメーカーを取得する.
1318     *
1319     * @param integer $product_id プロダクトID
1320     * @param integer $maker_id メーカーID
1321     * @return array 選択中の商品のメーカーIDの配列
1322     *
1323     */
1324    function sfGetMakerId($product_id, $maker_id = 0, $closed = false) {
1325        if ($closed) {
1326            $status = '';
1327        } else {
1328            $status = 'status = 1';
1329        }
1330
1331        if (!$this->g_maker_on) {
1332            $this->g_maker_on = true;
1333            $maker_id = (int) $maker_id;
1334            $product_id = (int) $product_id;
1335            if (SC_Utils_Ex::sfIsInt($maker_id) && $maker_id != 0 && $this->sfIsRecord('dtb_maker','maker_id', $maker_id)) {
1336                $this->g_maker_id = array($maker_id);
1337            } else if (SC_Utils_Ex::sfIsInt($product_id) && $product_id != 0 && $this->sfIsRecord('dtb_products','product_id', $product_id, $status)) {
1338                $objQuery =& SC_Query_Ex::getSingletonInstance();
1339                $maker_id = $objQuery->getCol('maker_id', 'dtb_products', 'product_id = ?', array($product_id));
1340                $this->g_maker_id = $maker_id;
1341            } else {
1342                // 不正な場合は、空の配列を返す。
1343                $this->g_maker_id = array();
1344            }
1345        }
1346        return $this->g_maker_id;
1347    }
1348
1349    /**
1350     * メーカーの取得を行う.
1351     *
1352     * $products_check:true商品登録済みのものだけ取得する
1353     *
1354     * @param string $addwhere 追加する WHERE 句
1355     * @param bool $products_check 商品の存在するカテゴリのみ取得する場合 true
1356     * @return array カテゴリツリーの配列
1357     */
1358    function sfGetMakerList($addwhere = '', $products_check = false) {
1359        $objQuery =& SC_Query_Ex::getSingletonInstance();
1360        $where = 'del_flg = 0';
1361
1362        if ($addwhere != '') {
1363            $where.= " AND $addwhere";
1364        }
1365
1366        $objQuery->setOption('ORDER BY rank DESC');
1367
1368        if ($products_check) {
1369            $col = 'T1.maker_id, name';
1370            $from = 'dtb_maker AS T1 LEFT JOIN dtb_maker_count AS T2 ON T1.maker_id = T2.maker_id';
1371            $where .= ' AND product_count > 0';
1372        } else {
1373            $col = 'maker_id, name';
1374            $from = 'dtb_maker';
1375        }
1376
1377        $arrRet = $objQuery->select($col, $from, $where);
1378
1379        $max = count($arrRet);
1380        $arrList = array();
1381        for ($cnt = 0; $cnt < $max; $cnt++) {
1382            $id = $arrRet[$cnt]['maker_id'];
1383            $name = $arrRet[$cnt]['name'];
1384            $arrList[$id] = $name;
1385        }
1386        return $arrList;
1387    }
1388
1389    /**
1390     * 店舗基本情報に基づいて税金額を返す
1391     *
1392     * @param integer $price 計算対象の金額
1393     * @return integer 税金額
1394     */
1395    function sfTax($price) {
1396        // 店舗基本情報を取得
1397        $CONF = SC_Helper_DB_Ex::sfGetBasisData();
1398
1399        return SC_Utils_Ex::sfTax($price, $CONF['tax'], $CONF['tax_rule']);
1400    }
1401
1402    /**
1403     * 店舗基本情報に基づいて税金付与した金額を返す
1404     * SC_Utils_Ex::sfCalcIncTax とどちらか統一したほうが良い
1405     *
1406     * @param integer $price 計算対象の金額
1407     * @return integer 税金付与した金額
1408     */
1409    function sfCalcIncTax($price, $tax = null, $tax_rule = null) {
1410        // 店舗基本情報を取得
1411        $CONF = SC_Helper_DB_Ex::sfGetBasisData();
1412
1413        return SC_Utils_Ex::sfCalcIncTax($price, $CONF['tax'], $CONF['tax_rule']);
1414    }
1415
1416    /**
1417     * 店舗基本情報に基づいて加算ポイントを返す
1418     *
1419     * @param integer $totalpoint
1420     * @param integer $use_point
1421     * @return integer 加算ポイント
1422     */
1423    function sfGetAddPoint($totalpoint, $use_point) {
1424        // 店舗基本情報を取得
1425        $CONF = SC_Helper_DB_Ex::sfGetBasisData();
1426
1427        return SC_Utils_Ex::sfGetAddPoint($totalpoint, $use_point, $CONF['point_rate']);
1428    }
1429
1430    /**
1431     * 指定ファイルが存在する場合 SQL として実行
1432     *
1433     * XXX プラグイン用に追加。将来消すかも。
1434     *
1435     * @param string $sqlFilePath SQL ファイルのパス
1436     * @return void
1437     */
1438    function sfExecSqlByFile($sqlFilePath) {
1439        if (file_exists($sqlFilePath)) {
1440            $objQuery =& SC_Query_Ex::getSingletonInstance();
1441
1442            $sqls = file_get_contents($sqlFilePath);
1443            if ($sqls === false) trigger_error('ファイルは存在するが読み込めない', E_USER_ERROR);
1444
1445            foreach (explode(';', $sqls) as $sql) {
1446                $sql = trim($sql);
1447                if (strlen($sql) == 0) continue;
1448                $objQuery->query($sql);
1449            }
1450        }
1451    }
1452
1453    /**
1454     * 商品規格を設定しているか
1455     *
1456     * @param integer $product_id 商品ID
1457     * @return bool 商品規格が存在する場合:true, それ以外:false
1458     */
1459    function sfHasProductClass($product_id) {
1460        if (!SC_Utils_Ex::sfIsInt($product_id)) return false;
1461
1462        $objQuery =& SC_Query_Ex::getSingletonInstance();
1463        $where = 'product_id = ? AND del_flg = 0 AND (classcategory_id1 != 0 OR classcategory_id2 != 0)';
1464        $exists = $objQuery->exists('dtb_products_class', $where, array($product_id));
1465
1466        return $exists;
1467    }
1468}
Note: See TracBrowser for help on using the repository browser.