source: branches/version-2_11-dev/data/class/helper/SC_Helper_DB.php @ 21039

Revision 21039, 50.6 KB checked in by shutta, 13 years ago (diff)

refs #1416 (SC_Helper_DB#sfCountCategory を is_force_all_count = true で実行時にdtb_category_countに重複して登録されてしまう)

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