source: branches/version-2_5-dev/data/class/helper/SC_Helper_DB.php @ 20541

Revision 20541, 51.6 KB checked in by Seasoft, 13 years ago (diff)

#627(ソース整形・ソースコメントの改善)

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