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

Revision 20428, 51.0 KB checked in by shutta, 13 years ago (diff)

refs #515
PHP5.3での非推奨関数split()をexplode(),preg_split()へ置き換え。

  • 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::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::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     * @param boolean $force 強制的にDB取得するか
140     * @param string $col 取得カラムを指定する
141     * @return array 店舗基本情報の配列
142     */
143    function sfGetBasisData($force = false, $col = "") {
144        static $data;
145
146        if ($force || !isset($data)) {
147            $objQuery =& SC_Query::getSingletonInstance();
148
149            if ($col === "") {
150                $arrRet = $objQuery->select('*', 'dtb_baseinfo');
151            } else {
152                $arrRet = $objQuery->select($col, "dtb_baseinfo");
153            }
154
155            if (isset($arrRet[0])) {
156                $data = $arrRet[0];
157            } else {
158                $data = array();
159            }
160        }
161        return $data;
162    }
163
164    /**
165     * 基本情報の登録数を取得する
166     *
167     * @return int
168     */
169    function sfGetBasisCount() {
170        $objQuery =& SC_Query::getSingletonInstance();
171
172        return $objQuery->count("dtb_baseinfo");
173    }
174
175
176    /* 選択中のアイテムのルートカテゴリIDを取得する */
177    function sfGetRootId() {
178
179        if(!$this->g_root_on)   {
180            $this->g_root_on = true;
181            $objQuery =& SC_Query::getSingletonInstance();
182
183            if (!isset($_GET['product_id'])) $_GET['product_id'] = "";
184            if (!isset($_GET['category_id'])) $_GET['category_id'] = "";
185
186            if(!empty($_GET['product_id']) || !empty($_GET['category_id'])) {
187                // 選択中のカテゴリIDを判定する
188                $category_id = $this->sfGetCategoryId($_GET['product_id'], $_GET['category_id']);
189                // ROOTカテゴリIDの取得
190                $arrRet = $this->sfGetParents('dtb_category', 'parent_category_id', 'category_id', $category_id);
191                $root_id = isset($arrRet[0]) ? $arrRet[0] : "";
192            } else {
193                // ROOTカテゴリIDをなしに設定する
194                $root_id = "";
195            }
196            $this->g_root_id = $root_id;
197        }
198        return $this->g_root_id;
199    }
200
201    /**
202     * 受注番号、最終ポイント、加算ポイント、利用ポイントから「オーダー前ポイント」を取得する
203     *
204     * @param integer $order_id 受注番号
205     * @param integer $use_point 利用ポイント
206     * @param integer $add_point 加算ポイント
207     * @return array オーダー前ポイントの配列
208     */
209    function sfGetRollbackPoint($order_id, $use_point, $add_point) {
210        $objQuery = new SC_Query();
211        $arrRet = $objQuery->select("customer_id", "dtb_order", "order_id = ?", array($order_id));
212        $customer_id = $arrRet[0]['customer_id'];
213        if($customer_id != "" && $customer_id >= 1) {
214            $arrRet = $objQuery->select("point", "dtb_customer", "customer_id = ?", array($customer_id));
215            $point = $arrRet[0]['point'];
216            $rollback_point = $arrRet[0]['point'] + $use_point - $add_point;
217        } else {
218            $rollback_point = "";
219            $point = "";
220        }
221        return array($point, $rollback_point);
222    }
223
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::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::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::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::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::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::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::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::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::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::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::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::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();
640
641        if($objQuery == NULL) {
642            $objQuery =& SC_Query::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::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::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_Utils::sfGetParentsArraySub($arrData, $pid_name, $id_name, $child);
895
896        while($ret != "") {
897            $arrParents[] = $ret;
898            $ret = SC_Utils::sfGetParentsArraySub($arrData, $pid_name, $id_name, $ret);
899        }
900
901        $arrParents = array_reverse($arrParents);
902
903        return $arrParents;
904    }
905
906    /**
907     * カテゴリから商品を検索する場合のWHERE文と値を返す.
908     *
909     * @param integer $category_id カテゴリID
910     * @return array 商品を検索する場合の配列
911     */
912    function sfGetCatWhere($category_id) {
913        // 子カテゴリIDの取得
914        $arrRet = SC_Helper_DB_Ex::sfGetChildrenArray("dtb_category", "parent_category_id", "category_id", $category_id);
915        $tmp_where = "";
916        foreach ($arrRet as $val) {
917            if($tmp_where == "") {
918                $tmp_where.= "category_id IN ( ?";
919            } else {
920                $tmp_where.= ",? ";
921            }
922            $arrval[] = $val;
923        }
924        $tmp_where.= " ) ";
925        return array($tmp_where, $arrval);
926    }
927
928    /**
929     * SELECTボックス用リストを作成する.
930     *
931     * @param string $table テーブル名
932     * @param string $keyname プライマリーキーのカラム名
933     * @param string $valname データ内容のカラム名
934     * @param string $where WHERE句
935     * @param array $arrval プレースホルダ
936     * @return array SELECT ボックス用リストの配列
937     */
938    function sfGetIDValueList($table, $keyname, $valname, $where = '', $arrVal = array()) {
939        $objQuery =& SC_Query::getSingletonInstance();
940        $col = "$keyname, $valname";
941        $objQuery->setWhere("del_flg = 0");
942        $objQuery->setOrder("rank DESC");
943        $arrList = $objQuery->select($col, $table, $where, $arrVal);
944        $count = count($arrList);
945        for($cnt = 0; $cnt < $count; $cnt++) {
946            $key = $arrList[$cnt][$keyname];
947            $val = $arrList[$cnt][$valname];
948            $arrRet[$key] = $val;
949        }
950        return $arrRet;
951    }
952
953    /**
954     * ランキングを上げる.
955     *
956     * @param string $table テーブル名
957     * @param string $colname カラム名
958     * @param string|integer $id テーブルのキー
959     * @param string $andwhere SQL の AND 条件である WHERE 句
960     * @return void
961     */
962    function sfRankUp($table, $colname, $id, $andwhere = "") {
963        $objQuery =& SC_Query::getSingletonInstance();
964        $objQuery->begin();
965        $where = "$colname = ?";
966        if($andwhere != "") {
967            $where.= " AND $andwhere";
968        }
969        // 対象項目のランクを取得
970        $rank = $objQuery->get("rank", $table, $where, array($id));
971        // ランクの最大値を取得
972        $maxrank = $objQuery->max("rank", $table, $andwhere);
973        // ランクが最大値よりも小さい場合に実行する。
974        if($rank < $maxrank) {
975            // ランクが一つ上のIDを取得する。
976            $where = "rank = ?";
977            if($andwhere != "") {
978                $where.= " AND $andwhere";
979            }
980            $uprank = $rank + 1;
981            $up_id = $objQuery->get($colname, $table, $where, array($uprank));
982            // ランク入れ替えの実行
983            $sqlup = "UPDATE $table SET rank = ? WHERE $colname = ?";
984            if($andwhere != "") {
985                $sqlup.= " AND $andwhere";
986            }
987            $objQuery->exec($sqlup, array($rank + 1, $id));
988            $objQuery->exec($sqlup, array($rank, $up_id));
989        }
990        $objQuery->commit();
991    }
992
993    /**
994     * ランキングを下げる.
995     *
996     * @param string $table テーブル名
997     * @param string $colname カラム名
998     * @param string|integer $id テーブルのキー
999     * @param string $andwhere SQL の AND 条件である WHERE 句
1000     * @return void
1001     */
1002    function sfRankDown($table, $colname, $id, $andwhere = "") {
1003        $objQuery =& SC_Query::getSingletonInstance();
1004        $objQuery->begin();
1005        $where = "$colname = ?";
1006        if($andwhere != "") {
1007            $where.= " AND $andwhere";
1008        }
1009        // 対象項目のランクを取得
1010        $rank = $objQuery->get("rank", $table, $where, array($id));
1011
1012        // ランクが1(最小値)よりも大きい場合に実行する。
1013        if($rank > 1) {
1014            // ランクが一つ下のIDを取得する。
1015            $where = "rank = ?";
1016            if($andwhere != "") {
1017                $where.= " AND $andwhere";
1018            }
1019            $downrank = $rank - 1;
1020            $down_id = $objQuery->get($colname, $table, $where, array($downrank));
1021            // ランク入れ替えの実行
1022            $sqlup = "UPDATE $table SET rank = ? WHERE $colname = ?";
1023            if($andwhere != "") {
1024                $sqlup.= " AND $andwhere";
1025            }
1026            $objQuery->exec($sqlup, array($rank - 1, $id));
1027            $objQuery->exec($sqlup, array($rank, $down_id));
1028        }
1029        $objQuery->commit();
1030    }
1031
1032    /**
1033     * 指定順位へ移動する.
1034     *
1035     * @param string $tableName テーブル名
1036     * @param string $keyIdColumn キーを保持するカラム名
1037     * @param string|integer $keyId キーの値
1038     * @param integer $pos 指定順位
1039     * @param string $where SQL の AND 条件である WHERE 句
1040     * @return void
1041     */
1042    function sfMoveRank($tableName, $keyIdColumn, $keyId, $pos, $where = "") {
1043        $objQuery =& SC_Query::getSingletonInstance();
1044        $objQuery->begin();
1045
1046        // 自身のランクを取得する
1047        if($where != "") {
1048            $getWhere = "$keyIdColumn = ? AND " . $where;
1049        } else {
1050            $getWhere = "$keyIdColumn = ?";
1051        }
1052        $rank = $objQuery->get("rank", $tableName, $getWhere, array($keyId));
1053
1054        $max = $objQuery->max("rank", $tableName, $where);
1055
1056        // 値の調整(逆順)
1057        if($pos > $max) {
1058            $position = 1;
1059        } else if($pos < 1) {
1060            $position = $max;
1061        } else {
1062            $position = $max - $pos + 1;
1063        }
1064
1065        //入れ替え先の順位が入れ換え元の順位より大きい場合
1066        if( $position > $rank ) $term = "rank - 1";
1067
1068        //入れ替え先の順位が入れ換え元の順位より小さい場合
1069        if( $position < $rank ) $term = "rank + 1";
1070
1071        // XXX 入れ替え先の順位が入れ替え元の順位と同じ場合
1072        if (!isset($term)) $term = "rank";
1073
1074        // 指定した順位の商品から移動させる商品までのrankを1つずらす
1075        $sql = "UPDATE $tableName SET rank = $term WHERE rank BETWEEN ? AND ?";
1076        if($where != "") {
1077            $sql.= " AND $where";
1078        }
1079
1080        if( $position > $rank ) $objQuery->exec( $sql, array( $rank + 1, $position ));
1081        if( $position < $rank ) $objQuery->exec( $sql, array( $position, $rank - 1 ));
1082
1083        // 指定した順位へrankを書き換える。
1084        $sql  = "UPDATE $tableName SET rank = ? WHERE $keyIdColumn = ? ";
1085        if($where != "") {
1086            $sql.= " AND $where";
1087        }
1088
1089        $objQuery->exec( $sql, array( $position, $keyId ) );
1090        $objQuery->commit();
1091    }
1092
1093    /**
1094     * ランクを含むレコードを削除する.
1095     *
1096     * レコードごと削除する場合は、$deleteをtrueにする
1097     *
1098     * @param string $table テーブル名
1099     * @param string $colname カラム名
1100     * @param string|integer $id テーブルのキー
1101     * @param string $andwhere SQL の AND 条件である WHERE 句
1102     * @param bool $delete レコードごと削除する場合 true,
1103     *                     レコードごと削除しない場合 false
1104     * @return void
1105     */
1106    function sfDeleteRankRecord($table, $colname, $id, $andwhere = "",
1107                                $delete = false) {
1108        $objQuery =& SC_Query::getSingletonInstance();
1109        $objQuery->begin();
1110        // 削除レコードのランクを取得する。
1111        $where = "$colname = ?";
1112        if($andwhere != "") {
1113            $where.= " AND $andwhere";
1114        }
1115        $rank = $objQuery->get("rank", $table, $where, array($id));
1116
1117        if(!$delete) {
1118            // ランクを最下位にする、DELフラグON
1119            $sqlup = "UPDATE $table SET rank = 0, del_flg = 1 ";
1120            $sqlup.= "WHERE $colname = ?";
1121            // UPDATEの実行
1122            $objQuery->exec($sqlup, array($id));
1123        } else {
1124            $objQuery->delete($table, "$colname = ?", array($id));
1125        }
1126
1127        // 追加レコードのランクより上のレコードを一つずらす。
1128        $where = "rank > ?";
1129        if($andwhere != "") {
1130            $where.= " AND $andwhere";
1131        }
1132        $sqlup = "UPDATE $table SET rank = (rank - 1) WHERE $where";
1133        $objQuery->exec($sqlup, array($rank));
1134        $objQuery->commit();
1135    }
1136
1137    /**
1138     * 親IDの配列を元に特定のカラムを取得する.
1139     *
1140     * @param SC_Query $objQuery SC_Query インスタンス
1141     * @param string $table テーブル名
1142     * @param string $id_name ID名
1143     * @param string $col_name カラム名
1144     * @param array $arrId IDの配列
1145     * @return array 特定のカラムの配列
1146     */
1147    function sfGetParentsCol($objQuery, $table, $id_name, $col_name, $arrId ) {
1148        $col = $col_name;
1149        $len = count($arrId);
1150        $where = "";
1151
1152        for($cnt = 0; $cnt < $len; $cnt++) {
1153            if($where == "") {
1154                $where = "$id_name = ?";
1155            } else {
1156                $where.= " OR $id_name = ?";
1157            }
1158        }
1159
1160        $objQuery->setOrder("level");
1161        $arrRet = $objQuery->select($col, $table, $where, $arrId);
1162        return $arrRet;
1163    }
1164
1165    /**
1166     * カテゴリ変更時の移動処理を行う.
1167     *
1168     * @param SC_Query $objQuery SC_Query インスタンス
1169     * @param string $table テーブル名
1170     * @param string $id_name ID名
1171     * @param string $cat_name カテゴリ名
1172     * @param integer $old_catid 旧カテゴリID
1173     * @param integer $new_catid 新カテゴリID
1174     * @param integer $id ID
1175     * @return void
1176     */
1177    function sfMoveCatRank($objQuery, $table, $id_name, $cat_name, $old_catid, $new_catid, $id) {
1178        if ($old_catid == $new_catid) {
1179            return;
1180        }
1181        // 旧カテゴリでのランク削除処理
1182        // 移動レコードのランクを取得する。
1183        $where = "$id_name = ?";
1184        $rank = $objQuery->get("rank", $table, $where, array($id));
1185        // 削除レコードのランクより上のレコードを一つ下にずらす。
1186        $where = "rank > ? AND $cat_name = ?";
1187        $sqlup = "UPDATE $table SET rank = (rank - 1) WHERE $where";
1188        $objQuery->exec($sqlup, array($rank, $old_catid));
1189        // 新カテゴリでの登録処理
1190        // 新カテゴリの最大ランクを取得する。
1191        $max_rank = $objQuery->max("rank", $table, "$cat_name = ?", array($new_catid)) + 1;
1192        $where = "$id_name = ?";
1193        $sqlup = "UPDATE $table SET rank = ? WHERE $where";
1194        $objQuery->exec($sqlup, array($max_rank, $id));
1195    }
1196
1197    /**
1198     * 都道府県から配送料金を取得する.
1199     *
1200     * @param integer|array $pref_id 都道府県ID 又は都道府県IDの配列
1201     * @return string 指定の都道府県, 商品種別の配送料金
1202     */
1203    function sfGetDelivFee($pref_id, $product_type_id) {
1204        $objQuery =& SC_Query::getSingletonInstance();
1205
1206        /*
1207         * FIXME 都道府県が指定されていない場合は、東京都の番号を指定しておく
1208         * http://svn.ec-cube.net/open_trac/ticket/410
1209         */
1210        if($pref_id == "") {
1211            $pref_id = 13;
1212        }
1213        if (!is_array($pref_id)) {
1214            $pref_id = array($pref_id);
1215        }
1216        $sql = <<< __EOS__
1217            SELECT SUM(T1.fee) AS fee
1218              FROM dtb_delivfee T1
1219              JOIN dtb_deliv T2
1220                ON T1.deliv_id = T2.deliv_id
1221             WHERE T1.pref = ? AND T2.product_type_id = ?
1222__EOS__;
1223
1224        $result = 0;
1225        foreach ($pref_id as $pref) {
1226            $result += $objQuery->getOne($sql, array($pref, $product_type_id));
1227        }
1228        return $result;
1229    }
1230
1231    /**
1232     * レコードの存在チェックを行う.
1233     *
1234     * TODO SC_Query に移行するべきか?
1235     *
1236     * @param string $table テーブル名
1237     * @param string $col カラム名
1238     * @param array $arrval 要素の配列
1239     * @param array $addwhere SQL の AND 条件である WHERE 句
1240     * @return bool レコードが存在する場合 true
1241     */
1242    function sfIsRecord($table, $col, $arrval, $addwhere = "") {
1243        $objQuery =& SC_Query::getSingletonInstance();
1244        $arrCol = preg_split("/[, ]/", $col);
1245
1246        $where = "del_flg = 0";
1247
1248        if($addwhere != "") {
1249            $where.= " AND $addwhere";
1250        }
1251
1252        foreach($arrCol as $val) {
1253            if($val != "") {
1254                if($where == "") {
1255                    $where = "$val = ?";
1256                } else {
1257                    $where.= " AND $val = ?";
1258                }
1259            }
1260        }
1261        $ret = $objQuery->get($col, $table, $where, $arrval);
1262
1263        if($ret != "") {
1264            return true;
1265        }
1266        return false;
1267    }
1268
1269    /**
1270     * メーカー商品数数の登録を行う.
1271     *
1272     * @param SC_Query $objQuery SC_Query インスタンス
1273     * @return void
1274     */
1275    function sfCountMaker($objQuery){
1276        $sql = "";
1277
1278        //テーブル内容の削除
1279        $objQuery->query("DELETE FROM dtb_maker_count");
1280
1281        //各メーカーの商品数を数えて格納
1282        $sql = " INSERT INTO dtb_maker_count(maker_id, product_count, create_date) ";
1283        $sql .= " SELECT T1.maker_id, count(T2.maker_id), now() ";
1284        $sql .= " FROM dtb_maker AS T1 LEFT JOIN dtb_products AS T2";
1285        $sql .= " ON T1.maker_id = T2.maker_id ";
1286        $sql .= " WHERE T2.del_flg = 0 AND T2.status = 1 ";
1287        $sql .= " GROUP BY T1.maker_id, T2.maker_id ";
1288        $objQuery->query($sql);
1289    }
1290
1291    /**
1292     * 選択中の商品のメーカーを取得する.
1293     *
1294     * @param integer $product_id プロダクトID
1295     * @param integer $maker_id メーカーID
1296     * @return array 選択中の商品のメーカーIDの配列
1297     *
1298     */
1299    function sfGetMakerId($product_id, $maker_id = 0, $closed = false) {
1300        if ($closed) {
1301            $status = "";
1302        } else {
1303            $status = "status = 1";
1304        }
1305
1306        if (!$this->g_maker_on) {
1307            $this->g_maker_on = true;
1308            $maker_id = (int) $maker_id;
1309            $product_id = (int) $product_id;
1310            if (SC_Utils_Ex::sfIsInt($maker_id) && $maker_id != 0 && $this->sfIsRecord("dtb_maker","maker_id", $maker_id)) {
1311                $this->g_maker_id = array($maker_id);
1312            } else if (SC_Utils_Ex::sfIsInt($product_id) && $product_id != 0 && $this->sfIsRecord("dtb_products","product_id", $product_id, $status)) {
1313                $objQuery =& SC_Query::getSingletonInstance();
1314                $where = "product_id = ?";
1315                $maker_id = $objQuery->getCol("maker_id", "dtb_products", "product_id = ?", array($product_id));
1316                $this->g_maker_id = $maker_id;
1317            } else {
1318                // 不正な場合は、空の配列を返す。
1319                $this->g_maker_id = array();
1320            }
1321        }
1322        return $this->g_maker_id;
1323    }
1324
1325    /**
1326     * メーカーの取得を行う.
1327     *
1328     * $products_check:true商品登録済みのものだけ取得する
1329     *
1330     * @param string $addwhere 追加する WHERE 句
1331     * @param bool $products_check 商品の存在するカテゴリのみ取得する場合 true
1332     * @return array カテゴリツリーの配列
1333     */
1334    function sfGetMakerList($addwhere = "", $products_check = false) {
1335        $objQuery =& SC_Query::getSingletonInstance();
1336        $where = "del_flg = 0";
1337
1338        if($addwhere != "") {
1339            $where.= " AND $addwhere";
1340        }
1341
1342        $objQuery->setOption("ORDER BY rank DESC");
1343
1344        if($products_check) {
1345            $col = "T1.maker_id, name";
1346            $from = "dtb_maker AS T1 LEFT JOIN dtb_maker_count AS T2 ON T1.maker_id = T2.maker_id";
1347            $where .= " AND product_count > 0";
1348        } else {
1349            $col = "maker_id, name";
1350            $from = "dtb_maker";
1351        }
1352
1353        $arrRet = $objQuery->select($col, $from, $where);
1354
1355        $max = count($arrRet);
1356        for($cnt = 0; $cnt < $max; $cnt++) {
1357            $id = $arrRet[$cnt]['maker_id'];
1358            $name = $arrRet[$cnt]['name'];
1359            $arrList[$id] = $name;
1360        }
1361        return $arrList;
1362    }
1363
1364    /**
1365     * 店舗基本情報に基づいて税金額を返す
1366     *
1367     * @param integer $price 計算対象の金額
1368     * @return integer 税金額
1369     */
1370    function sfTax($price) {
1371        // 店舗基本情報を取得
1372        $CONF = SC_Helper_DB_Ex::sfGetBasisData();
1373
1374        return SC_Utils_Ex::sfTax($price, $CONF['tax'], $CONF['tax_rule']);
1375    }
1376
1377    /**
1378     * 店舗基本情報に基づいて税金付与した金額を返す
1379     *
1380     * @param integer $price 計算対象の金額
1381     * @return integer 税金付与した金額
1382     */
1383    function sfCalcIncTax($price, $tax = null, $tax_rule = null) {
1384        // 店舗基本情報を取得
1385        $CONF = SC_Helper_DB_Ex::sfGetBasisData();
1386
1387        return SC_Utils_Ex::sfCalcIncTax($price, $CONF['tax'], $CONF['tax_rule']);
1388    }
1389
1390    /**
1391     * 店舗基本情報に基づいて加算ポイントを返す
1392     *
1393     * @param integer $totalpoint
1394     * @param integer $use_point
1395     * @return integer 加算ポイント
1396     */
1397    function sfGetAddPoint($totalpoint, $use_point) {
1398        // 店舗基本情報を取得
1399        $CONF = SC_Helper_DB_Ex::sfGetBasisData();
1400
1401        return SC_Utils_Ex::sfGetAddPoint($totalpoint, $use_point, $CONF['point_rate']);
1402    }
1403
1404    /**
1405     * 指定ファイルが存在する場合 SQL として実行
1406     *
1407     * XXX プラグイン用に追加。将来消すかも。
1408     *
1409     * @param string $sqlFilePath SQL ファイルのパス
1410     * @return void
1411     */
1412    function sfExecSqlByFile($sqlFilePath) {
1413        if (file_exists($sqlFilePath)) {
1414            $objQuery =& SC_Query::getSingletonInstance();
1415
1416            $sqls = file_get_contents($sqlFilePath);
1417            if ($sqls === false) SC_Utils_Ex::sfDispException('ファイルは存在するが読み込めない');
1418
1419            foreach (explode(';', $sqls) as $sql) {
1420                $sql = trim($sql);
1421                if (strlen($sql) == 0) continue;
1422                $objQuery->query($sql);
1423            }
1424        }
1425    }
1426
1427    /**
1428     * 商品規格を設定しているか
1429     *
1430     * @param integer $product_id 商品ID
1431     * @return bool 商品規格が存在する場合:true, それ以外:false
1432     */
1433    function sfHasProductClass($product_id) {
1434        if (!SC_Utils_Ex::sfIsInt($product_id)) return false;
1435
1436        $objQuery =& SC_Query::getSingletonInstance();
1437        $where = 'product_id = ? AND class_combination_id IS NOT NULL';
1438        $count = $objQuery->count('dtb_products_class', $where, array($product_id));
1439
1440        return $count >= 1;
1441    }
1442}
1443?>
Note: See TracBrowser for help on using the repository browser.