Warning: Can't use blame annotator:
svn blame failed on branches/feature-module-update/data/class/helper/SC_Helper_DB.php: バイナリファイル 'file:///home/svn/open/branches/feature-module-update/data/class/helper/SC_Helper_DB.php' に対しては blame で各行の最終変更者を計算できません 195004

source: branches/feature-module-update/data/class/helper/SC_Helper_DB.php @ 15399

Revision 15399, 38.7 KB checked in by nanasess, 17 years ago (diff)

引数で LC_Page インスタンスを渡している個所を参照渡しに変更(PHP5対応)

  • Property svn:keywords set to Id Revision Date
  • Property svn:mime-type set to application/x-httpd-php
RevLine 
1<?php
2/*
3 * Copyright(c) 2000-2007 LOCKON CO.,LTD. All Rights Reserved.
4 *
5 * http://www.lockon.co.jp/
6 */
7
8/**
9 * DB関連のヘルパークラス.
10 *
11 * @package Helper
12 * @author LOCKON CO.,LTD.
13 * @version $Id$
14 */
15class SC_Helper_DB {
16
17    // {{{ properties
18
19    /** ルートカテゴリ取得フラグ */
20    var $g_root_on;
21
22    /** ルートカテゴリID */
23    var $g_root_id;
24
25    /** 選択中カテゴリ取得フラグ */
26    var $g_category_on;
27
28    /** 選択中カテゴリID */
29    var $g_category_id;
30
31    // }}}
32    // {{{ functions
33
34    /**
35     * データベースのバージョンを所得する.
36     *
37     * @param string $dsn データソース名
38     * @return string データベースのバージョン
39     */
40    function sfGetDBVersion($dsn = "") {
41        $dbFactory = SC_DB_DBFactory::getInstance();
42        return $dbFactory->sfGetDBVersion($dsn);
43    }
44
45    /**
46     * テーブルの存在をチェックする.
47     *
48     * @param string $table_name チェック対象のテーブル名
49     * @param string $dsn データソース名
50     * @return テーブルが存在する場合 true
51     */
52    function sfTabaleExists($table_name, $dsn = "") {
53        $dbFactory = SC_DB_DBFactory::getInstance();
54        $dsn = $dbFactory->getDSN($dsn);
55
56        $objQuery = new SC_Query($dsn, true, true);
57        // 正常に接続されている場合
58        if(!$objQuery->isError()) {
59            list($db_type) = split(":", $dsn);
60            $sql = $dbFactory->getTableExistsSql();
61            $arrRet = $objQuery->getAll($sql, array($table_name));
62            if(count($arrRet) > 0) {
63                return true;
64            }
65        }
66        return false;
67    }
68
69    /**
70     * カラムの存在チェックと作成を行う.
71     *
72     * チェック対象のテーブルに, 該当のカラムが存在するかチェックする.
73     * 引数 $add が true の場合, 該当のカラムが存在しない場合は, カラムの生成を行う.
74     * カラムの生成も行う場合は, $col_type も必須となる.
75     *
76     * @param string $table_name テーブル名
77     * @param string $column_name カラム名
78     * @param string $col_type カラムのデータ型
79     * @param string $dsn データソース名
80     * @param bool $add カラムの作成も行う場合 true
81     * @return bool カラムが存在する場合とカラムの生成に成功した場合 true,
82     *               テーブルが存在しない場合 false,
83     *               引数 $add == false でカラムが存在しない場合 false
84     */
85    function sfColumnExists($table_name, $col_name, $col_type = "", $dsn = "", $add = false) {
86        $dbFactory = SC_DB_DBFactory::getInstance();
87        $dsn = $dbFactory->getDSN($dsn);
88
89        // テーブルが無ければエラー
90        if(!$this->sfTabaleExists($table_name, $dsn)) return false;
91
92        $objQuery = new SC_Query($dsn, true, true);
93        // 正常に接続されている場合
94        if(!$objQuery->isError()) {
95            list($db_type) = split(":", $dsn);
96
97            // カラムリストを取得
98            $arrRet = $dbFactory->sfGetColumnList($table_name);
99            if(count($arrRet) > 0) {
100                if(in_array($col_name, $arrRet)){
101                    return true;
102                }
103            }
104        }
105
106        // カラムを追加する
107        if($add){
108            $objQuery->query("ALTER TABLE $table_name ADD $col_name $col_type ");
109            return true;
110        }
111        return false;
112    }
113
114    /**
115     * インデックスの存在チェックと作成を行う.
116     *
117     * チェック対象のテーブルに, 該当のインデックスが存在するかチェックする.
118     * 引数 $add が true の場合, 該当のインデックスが存在しない場合は, インデックスの生成を行う.
119     * インデックスの生成も行う場合で, DB_TYPE が mysql の場合は, $length も必須となる.
120     *
121     * @param string $table_name テーブル名
122     * @param string $column_name カラム名
123     * @param string $index_name インデックス名
124     * @param integer|string $length インデックスを作成するデータ長
125     * @param string $dsn データソース名
126     * @param bool $add インデックスの生成もする場合 true
127     * @return bool インデックスが存在する場合とインデックスの生成に成功した場合 true,
128     *               テーブルが存在しない場合 false,
129     *               引数 $add == false でインデックスが存在しない場合 false
130     */
131    function sfIndexExists($table_name, $col_name, $index_name, $length = "", $dsn = "", $add = false) {
132        $dbFactory = SC_DB_DBFactory::getInstance();
133        $dsn = $dbFactory->getDSN($dsn);
134
135        // テーブルが無ければエラー
136        if (!$this->sfTabaleExists($table_name, $dsn)) return false;
137
138        $objQuery = new SC_Query($dsn, true, true);
139        $arrRet = $dbFactory->getTableIndex($index_name, $table_name);
140
141        // すでにインデックスが存在する場合
142        if(count($arrRet) > 0) {
143            return true;
144        }
145
146        // インデックスを作成する
147        if($add){
148            $dbFactory->createTableIndex($index_name, $table_name, $col_name, $length());
149            return true;
150        }
151        return false;
152    }
153
154    /**
155     * データの存在チェックを行う.
156     *
157     * @param string $table_name テーブル名
158     * @param string $where データを検索する WHERE 句
159     * @param string $dsn データソース名
160     * @param string $sql データの追加を行う場合の SQL文
161     * @param bool $add データの追加も行う場合 true
162     * @return bool データが存在する場合 true, データの追加に成功した場合 true,
163     *               $add == false で, データが存在しない場合 false
164     */
165    function sfDataExists($table_name, $where, $arrval, $dsn = "", $sql = "", $add = false) {
166        $dbFactory = SC_DB_DBFactory::getInstance();
167        $dsn = $dbFactory->getDSN($dsn);
168
169        $objQuery = new SC_Query($dsn, true, true);
170        $count = $objQuery->count($table_name, $where, $arrval);
171
172        if($count > 0) {
173            $ret = true;
174        } else {
175            $ret = false;
176        }
177        // データを追加する
178        if(!$ret && $add) {
179            $objQuery->exec($sql);
180        }
181        return $ret;
182    }
183
184    /**
185     * 店舗基本情報を取得する.
186     *
187     * @return array 店舗基本情報の配列
188     */
189    function sf_getBasisData() {
190        //DBから設定情報を取得
191        $objConn = new SC_DbConn();
192        $result = $objConn->getAll("SELECT * FROM dtb_baseinfo");
193        if(is_array($result[0])) {
194            foreach ( $result[0] as $key=>$value ){
195                $CONF["$key"] = $value;
196            }
197        }
198        return $CONF;
199    }
200
201    /* 選択中のアイテムのルートカテゴリIDを取得する */
202    function sfGetRootId() {
203
204        if(!$this->g_root_on)   {
205            $this->g_root_on = true;
206            $objQuery = new SC_Query();
207
208            if (!isset($_GET['product_id'])) $_GET['product_id'] = "";
209            if (!isset($_GET['category_id'])) $_GET['category_id'] = "";
210
211            if(!empty($_GET['product_id']) || !empty($_GET['category_id'])) {
212                // 選択中のカテゴリIDを判定する
213                $category_id = $this->sfGetCategoryId($_GET['product_id'], $_GET['category_id']);
214                // ROOTカテゴリIDの取得
215                $arrRet = $this->sfGetParents($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $category_id);
216                $root_id = $arrRet[0];
217            } else {
218                // ROOTカテゴリIDをなしに設定する
219                $root_id = "";
220            }
221            $this->g_root_id = $root_id;
222        }
223        return $this->g_root_id;
224    }
225
226    /**
227     * 商品規格情報を取得する.
228     *
229     * @param array $arrID 規格ID
230     * @return array 規格情報の配列
231     */
232    function sfGetProductsClass($arrID) {
233        list($product_id, $classcategory_id1, $classcategory_id2) = $arrID;
234
235        if($classcategory_id1 == "") {
236            $classcategory_id1 = '0';
237        }
238        if($classcategory_id2 == "") {
239            $classcategory_id2 = '0';
240        }
241
242        // 商品規格取得
243        $objQuery = new SC_Query();
244        $col = "product_id, deliv_fee, name, product_code, main_list_image, main_image, price01, price02, point_rate, product_class_id, classcategory_id1, classcategory_id2, class_id1, class_id2, stock, stock_unlimited, sale_limit, sale_unlimited";
245        $table = "vw_product_class AS prdcls";
246        $where = "product_id = ? AND classcategory_id1 = ? AND classcategory_id2 = ?";
247        $objQuery->setorder("rank1 DESC, rank2 DESC");
248        $arrRet = $objQuery->select($col, $table, $where, array($product_id, $classcategory_id1, $classcategory_id2));
249        return $arrRet[0];
250    }
251
252    /**
253     * カート内商品の集計処理を行う.
254     *
255     * @param LC_Page $objPage ページクラスのインスタンス
256     * @param SC_CartSession $objCartSess カートセッションのインスタンス
257     * @param array $arrInfo 商品情報の配列
258     * @return LC_Page 集計処理後のページクラスインスタンス
259     */
260    function sfTotalCart(&$objPage, $objCartSess, $arrInfo) {
261        $objDb = new SC_Helper_DB_Ex();
262        // 規格名一覧
263        $arrClassName = $objDb->sfGetIDValueList("dtb_class", "class_id", "name");
264        // 規格分類名一覧
265        $arrClassCatName = $objDb->sfGetIDValueList("dtb_classcategory", "classcategory_id", "name");
266
267        $objPage->tpl_total_pretax = 0;     // 費用合計(税込み)
268        $objPage->tpl_total_tax = 0;        // 消費税合計
269        $objPage->tpl_total_point = 0;      // ポイント合計
270
271        // カート内情報の取得
272        $arrCart = $objCartSess->getCartList();
273        $max = count($arrCart);
274        $cnt = 0;
275
276        for ($i = 0; $i < $max; $i++) {
277            // 商品規格情報の取得
278            $arrData = $this->sfGetProductsClass($arrCart[$i]['id']);
279            $limit = "";
280            // DBに存在する商品
281            if (count($arrData) > 0) {
282
283                // 購入制限数を求める。
284                if ($arrData['stock_unlimited'] != '1' && $arrData['sale_unlimited'] != '1') {
285                    if($arrData['sale_limit'] < $arrData['stock']) {
286                        $limit = $arrData['sale_limit'];
287                    } else {
288                        $limit = $arrData['stock'];
289                    }
290                } else {
291                    if ($arrData['sale_unlimited'] != '1') {
292                        $limit = $arrData['sale_limit'];
293                    }
294                    if ($arrData['stock_unlimited'] != '1') {
295                        $limit = $arrData['stock'];
296                    }
297                }
298
299                if($limit != "" && $limit < $arrCart[$i]['quantity']) {
300                    // カート内商品数を制限に合わせる
301                    $objCartSess->setProductValue($arrCart[$i]['id'], 'quantity', $limit);
302                    $quantity = $limit;
303                    $objPage->tpl_message = "※「" . $arrData['name'] . "」は販売制限しております、一度にこれ以上の購入はできません。";
304                } else {
305                    $quantity = $arrCart[$i]['quantity'];
306                }
307
308                $objPage->arrProductsClass[$cnt] = $arrData;
309                $objPage->arrProductsClass[$cnt]['quantity'] = $quantity;
310                $objPage->arrProductsClass[$cnt]['cart_no'] = $arrCart[$i]['cart_no'];
311                $objPage->arrProductsClass[$cnt]['class_name1'] =
312                    isset($arrClassName[$arrData['class_id1']])
313                        ? $arrClassName[$arrData['class_id1']] : "";
314
315                $objPage->arrProductsClass[$cnt]['class_name2'] =
316                    isset($arrClassName[$arrData['class_id2']])
317                        ? $arrClassName[$arrData['class_id2']] : "";
318
319                $objPage->arrProductsClass[$cnt]['classcategory_name1'] =
320                    $arrClassCatName[$arrData['classcategory_id1']];
321
322                $objPage->arrProductsClass[$cnt]['classcategory_name2'] =
323                    $arrClassCatName[$arrData['classcategory_id2']];
324
325                // 画像サイズ
326                list($image_width, $image_height) = getimagesize(IMAGE_SAVE_DIR . basename($objPage->arrProductsClass[$cnt]["main_image"]));
327                $objPage->arrProductsClass[$cnt]["tpl_image_width"] = $image_width + 60;
328                $objPage->arrProductsClass[$cnt]["tpl_image_height"] = $image_height + 80;
329
330                // 価格の登録
331                if ($arrData['price02'] != "") {
332                    $objCartSess->setProductValue($arrCart[$i]['id'], 'price', $arrData['price02']);
333                    $objPage->arrProductsClass[$cnt]['uniq_price'] = $arrData['price02'];
334                } else {
335                    $objCartSess->setProductValue($arrCart[$i]['id'], 'price', $arrData['price01']);
336                    $objPage->arrProductsClass[$cnt]['uniq_price'] = $arrData['price01'];
337                }
338                // ポイント付与率の登録
339                $objCartSess->setProductValue($arrCart[$i]['id'], 'point_rate', $arrData['point_rate']);
340                // 商品ごとの合計金額
341                $objPage->arrProductsClass[$cnt]['total_pretax'] = $objCartSess->getProductTotal($arrInfo, $arrCart[$i]['id']);
342                // 送料の合計を計算する
343                $objPage->tpl_total_deliv_fee+= ($arrData['deliv_fee'] * $arrCart[$i]['quantity']);
344                $cnt++;
345            } else {
346                // DBに商品が見つからない場合はカート商品の削除
347                $objCartSess->delProductKey('id', $arrCart[$i]['id']);
348            }
349        }
350
351        // 全商品合計金額(税込み)
352        $objPage->tpl_total_pretax = $objCartSess->getAllProductsTotal($arrInfo);
353        // 全商品合計消費税
354        $objPage->tpl_total_tax = $objCartSess->getAllProductsTax($arrInfo);
355        // 全商品合計ポイント
356        $objPage->tpl_total_point = $objCartSess->getAllProductsPoint();
357
358        return $objPage;
359    }
360
361    /**
362     * 受注一時テーブルへの書き込み処理を行う.
363     *
364     * @param string $uniqid ユニークID
365     * @param array $sqlval SQLの値の配列
366     * @return void
367     */
368    function sfRegistTempOrder($uniqid, $sqlval) {
369        if($uniqid != "") {
370            // 既存データのチェック
371            $objQuery = new SC_Query();
372            $where = "order_temp_id = ?";
373            $cnt = $objQuery->count("dtb_order_temp", $where, array($uniqid));
374            // 既存データがない場合
375            if ($cnt == 0) {
376                // 初回書き込み時に会員の登録済み情報を取り込む
377                $sqlval = $this->sfGetCustomerSqlVal($uniqid, $sqlval);
378                $sqlval['create_date'] = "now()";
379                $objQuery->insert("dtb_order_temp", $sqlval);
380            } else {
381                $objQuery->update("dtb_order_temp", $sqlval, $where, array($uniqid));
382            }
383        }
384    }
385
386    /**
387     * 会員情報から SQL文の値を生成する.
388     *
389     * @param string $uniqid ユニークID
390     * @param array $sqlval SQL の値の配列
391     * @return array 会員情報を含んだ SQL の値の配列
392     */
393    function sfGetCustomerSqlVal($uniqid, $sqlval) {
394        $objCustomer = new SC_Customer();
395        // 会員情報登録処理
396        if ($objCustomer->isLoginSuccess()) {
397            // 登録データの作成
398            $sqlval['order_temp_id'] = $uniqid;
399            $sqlval['update_date'] = 'Now()';
400            $sqlval['customer_id'] = $objCustomer->getValue('customer_id');
401            $sqlval['order_name01'] = $objCustomer->getValue('name01');
402            $sqlval['order_name02'] = $objCustomer->getValue('name02');
403            $sqlval['order_kana01'] = $objCustomer->getValue('kana01');
404            $sqlval['order_kana02'] = $objCustomer->getValue('kana02');
405            $sqlval['order_sex'] = $objCustomer->getValue('sex');
406            $sqlval['order_zip01'] = $objCustomer->getValue('zip01');
407            $sqlval['order_zip02'] = $objCustomer->getValue('zip02');
408            $sqlval['order_pref'] = $objCustomer->getValue('pref');
409            $sqlval['order_addr01'] = $objCustomer->getValue('addr01');
410            $sqlval['order_addr02'] = $objCustomer->getValue('addr02');
411            $sqlval['order_tel01'] = $objCustomer->getValue('tel01');
412            $sqlval['order_tel02'] = $objCustomer->getValue('tel02');
413            $sqlval['order_tel03'] = $objCustomer->getValue('tel03');
414            if (defined('MOBILE_SITE')) {
415                $sqlval['order_email'] = $objCustomer->getValue('email_mobile');
416            } else {
417                $sqlval['order_email'] = $objCustomer->getValue('email');
418            }
419            $sqlval['order_job'] = $objCustomer->getValue('job');
420            $sqlval['order_birth'] = $objCustomer->getValue('birth');
421        }
422        return $sqlval;
423    }
424
425    /**
426     * カテゴリツリーの取得を行う.
427     *
428     * @param integer $parent_category_id 親カテゴリID
429     * @param bool $count_check 登録商品数のチェックを行う場合 true
430     * @return array カテゴリツリーの配列
431     */
432    function sfGetCatTree($parent_category_id, $count_check = false) {
433        $objQuery = new SC_Query();
434        $col = "";
435        $col .= " cat.category_id,";
436        $col .= " cat.category_name,";
437        $col .= " cat.parent_category_id,";
438        $col .= " cat.level,";
439        $col .= " cat.rank,";
440        $col .= " cat.creator_id,";
441        $col .= " cat.create_date,";
442        $col .= " cat.update_date,";
443        $col .= " cat.del_flg, ";
444        $col .= " ttl.product_count";
445        $from = "dtb_category as cat left join dtb_category_total_count as ttl on ttl.category_id = cat.category_id";
446        // 登録商品数のチェック
447        if($count_check) {
448            $where = "del_flg = 0 AND product_count > 0";
449        } else {
450            $where = "del_flg = 0";
451        }
452        $objQuery->setoption("ORDER BY rank DESC");
453        $arrRet = $objQuery->select($col, $from, $where);
454
455        $arrParentID = sfGetParents($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $parent_category_id);
456
457        foreach($arrRet as $key => $array) {
458            foreach($arrParentID as $val) {
459                if($array['category_id'] == $val) {
460                    $arrRet[$key]['display'] = 1;
461                    break;
462                }
463            }
464        }
465
466        return $arrRet;
467    }
468
469    /**
470     * 親カテゴリーを連結した文字列を取得する.
471     *
472     * @param integer $category_id カテゴリID
473     * @return string 親カテゴリーを連結した文字列
474     */
475    function sfGetCatCombName($category_id){
476        // 商品が属するカテゴリIDを縦に取得
477        $objQuery = new SC_Query();
478        $arrCatID = sfGetParents($objQuery, "dtb_category", "parent_category_id", "category_id", $category_id);
479        $ConbName = "";
480
481        // カテゴリー名称を取得する
482        foreach($arrCatID as $key => $val){
483            $sql = "SELECT category_name FROM dtb_category WHERE category_id = ?";
484            $arrVal = array($val);
485            $CatName = $objQuery->getOne($sql,$arrVal);
486            $ConbName .= $CatName . ' | ';
487        }
488        // 最後の | をカットする
489        $ConbName = substr_replace($ConbName, "", strlen($ConbName) - 2, 2);
490
491        return $ConbName;
492    }
493
494    /**
495     * 指定したカテゴリーIDの大カテゴリーを取得する.
496     *
497     * @param integer $category_id カテゴリID
498     * @return array 指定したカテゴリーIDの大カテゴリー
499     */
500    function sfGetFirstCat($category_id){
501        // 商品が属するカテゴリIDを縦に取得
502        $objQuery = new SC_Query();
503        $arrRet = array();
504        $arrCatID = $this->sfGetParents($objQuery, "dtb_category", "parent_category_id", "category_id", $category_id);
505        $arrRet['id'] = $arrCatID[0];
506
507        // カテゴリー名称を取得する
508        $sql = "SELECT category_name FROM dtb_category WHERE category_id = ?";
509        $arrVal = array($arrRet['id']);
510        $arrRet['name'] = $objQuery->getOne($sql,$arrVal);
511
512        return $arrRet;
513    }
514
515    /**
516     * カテゴリツリーの取得を行う.
517     *
518     * $products_check:true商品登録済みのものだけ取得する
519     *
520     * @param string $addwhere 追加する WHERE 句
521     * @param bool $products_check 商品の存在するカテゴリのみ取得する場合 true
522     * @param string $head カテゴリ名のプレフィックス文字列
523     * @return array カテゴリツリーの配列
524     */
525    function sfGetCategoryList($addwhere = "", $products_check = false, $head = CATEGORY_HEAD) {
526        $objQuery = new SC_Query();
527        $where = "del_flg = 0";
528
529        if($addwhere != "") {
530            $where.= " AND $addwhere";
531        }
532
533        $objQuery->setoption("ORDER BY rank DESC");
534
535        if($products_check) {
536            $col = "T1.category_id, category_name, level";
537            $from = "dtb_category AS T1 LEFT JOIN dtb_category_total_count AS T2 ON T1.category_id = T2.category_id";
538            $where .= " AND product_count > 0";
539        } else {
540            $col = "category_id, category_name, level";
541            $from = "dtb_category";
542        }
543
544        $arrRet = $objQuery->select($col, $from, $where);
545
546        $max = count($arrRet);
547        for($cnt = 0; $cnt < $max; $cnt++) {
548            $id = $arrRet[$cnt]['category_id'];
549            $name = $arrRet[$cnt]['category_name'];
550            $arrList[$id] = "";
551            /*
552            for($n = 1; $n < $arrRet[$cnt]['level']; $n++) {
553                $arrList[$id].= " ";
554            }
555            */
556            for($cat_cnt = 0; $cat_cnt < $arrRet[$cnt]['level']; $cat_cnt++) {
557                $arrList[$id].= $head;
558            }
559            $arrList[$id].= $name;
560        }
561        return $arrList;
562    }
563
564    /**
565     * カテゴリーツリーの取得を行う.
566     *
567     * 親カテゴリの Value=0 を対象とする
568     *
569     * @param bool $parent_zero 親カテゴリの Value=0 の場合 true
570     * @return array カテゴリツリーの配列
571     */
572    function sfGetLevelCatList($parent_zero = true) {
573        $objQuery = new SC_Query();
574        $col = "category_id, category_name, level";
575        $where = "del_flg = 0";
576        $objQuery->setoption("ORDER BY rank DESC");
577        $arrRet = $objQuery->select($col, "dtb_category", $where);
578        $max = count($arrRet);
579
580        for($cnt = 0; $cnt < $max; $cnt++) {
581            if($parent_zero) {
582                if($arrRet[$cnt]['level'] == LEVEL_MAX) {
583                    $arrValue[$cnt] = $arrRet[$cnt]['category_id'];
584                } else {
585                    $arrValue[$cnt] = "";
586                }
587            } else {
588                $arrValue[$cnt] = $arrRet[$cnt]['category_id'];
589            }
590
591            $arrOutput[$cnt] = "";
592            /*
593            for($n = 1; $n < $arrRet[$cnt]['level']; $n++) {
594                $arrOutput[$cnt].= " ";
595            }
596            */
597            for($cat_cnt = 0; $cat_cnt < $arrRet[$cnt]['level']; $cat_cnt++) {
598                $arrOutput[$cnt].= CATEGORY_HEAD;
599            }
600            $arrOutput[$cnt].= $arrRet[$cnt]['category_name'];
601        }
602        return array($arrValue, $arrOutput);
603    }
604
605    /**
606     * 選択中のカテゴリを取得する.
607     *
608     * @param integer $product_id プロダクトID
609     * @param integer $category_id カテゴリID
610     * @return integer 選択中のカテゴリID
611     *
612     */
613    function sfGetCategoryId($product_id, $category_id) {
614
615        if(!$this->g_category_on)   {
616            $this->g_category_on = true;
617            $category_id = (int) $category_id;
618            $product_id = (int) $product_id;
619            if(SC_Utils_Ex::sfIsInt($category_id) && $this->sfIsRecord("dtb_category","category_id", $category_id)) {
620                $this->g_category_id = $category_id;
621            } else if (SC_Utils_Ex::sfIsInt($product_id) && $this->sfIsRecord("dtb_products","product_id", $product_id, "status = 1")) {
622                $objQuery = new SC_Query();
623                $where = "product_id = ?";
624                $category_id = $objQuery->get("dtb_products", "category_id", $where, array($product_id));
625                $this->g_category_id = $category_id;
626            } else {
627                // 不正な場合は、0を返す。
628                $this->g_category_id = 0;
629            }
630        }
631        return $this->g_category_id;
632    }
633
634    /**
635     * カテゴリ数の登録を行う.
636     *
637     * @param SC_Query $objQuery SC_Query インスタンス
638     * @return void
639     */
640    function sfCategory_Count($objQuery){
641        $sql = "";
642
643        //テーブル内容の削除
644        $objQuery->query("DELETE FROM dtb_category_count");
645        $objQuery->query("DELETE FROM dtb_category_total_count");
646
647        //各カテゴリ内の商品数を数えて格納
648        $sql = " INSERT INTO dtb_category_count(category_id, product_count, create_date) ";
649        $sql .= " SELECT T1.category_id, count(T2.category_id), now() FROM dtb_category AS T1 LEFT JOIN dtb_products AS T2 ";
650        $sql .= " ON T1.category_id = T2.category_id  ";
651        $sql .= " WHERE T2.del_flg = 0 AND T2.status = 1 ";
652        $sql .= " GROUP BY T1.category_id, T2.category_id ";
653        $objQuery->query($sql);
654
655        //子カテゴリ内の商品数を集計する
656        $arrCat = $objQuery->getAll("SELECT * FROM dtb_category");
657
658        $sql = "";
659        foreach($arrCat as $key => $val){
660
661            // 子ID一覧を取得
662            $arrRet = $this->sfGetChildrenArray('dtb_category', 'parent_category_id', 'category_id', $val['category_id']);
663            $line = SC_Utils_Ex::sfGetCommaList($arrRet);
664
665            $sql = " INSERT INTO dtb_category_total_count(category_id, product_count, create_date) ";
666            $sql .= " SELECT ?, SUM(product_count), now() FROM dtb_category_count ";
667            $sql .= " WHERE category_id IN (" . $line . ")";
668
669            $objQuery->query($sql, array($val['category_id']));
670        }
671    }
672
673    /**
674     * 子IDの配列を返す.
675     *
676     * @param string $table テーブル名
677     * @param string $pid_name 親ID名
678     * @param string $id_name ID名
679     * @param integer $id ID
680     * @param array 子ID の配列
681     */
682    function sfGetChildsID($table, $pid_name, $id_name, $id) {
683        $arrRet = $this->sfGetChildrenArray($table, $pid_name, $id_name, $id);
684        return $arrRet;
685    }
686
687    /**
688     * 階層構造のテーブルから子ID配列を取得する.
689     *
690     * @param string $table テーブル名
691     * @param string $pid_name 親ID名
692     * @param string $id_name ID名
693     * @param integer $id ID番号
694     * @return array 子IDの配列
695     */
696    function sfGetChildrenArray($table, $pid_name, $id_name, $id) {
697        $objQuery = new SC_Query();
698        $col = $pid_name . "," . $id_name;
699         $arrData = $objQuery->select($col, $table);
700
701        $arrPID = array();
702        $arrPID[] = $id;
703        $arrChildren = array();
704        $arrChildren[] = $id;
705
706        $arrRet = $this->sfGetChildrenArraySub($arrData, $pid_name, $id_name, $arrPID);
707
708        while(count($arrRet) > 0) {
709            $arrChildren = array_merge($arrChildren, $arrRet);
710            $arrRet = $this->sfGetChildrenArraySub($arrData, $pid_name, $id_name, $arrRet);
711        }
712
713        return $arrChildren;
714    }
715
716    /**
717     * 親ID直下の子IDをすべて取得する.
718     *
719     * @param array $arrData 親カテゴリの配列
720     * @param string $pid_name 親ID名
721     * @param string $id_name ID名
722     * @param array $arrPID 親IDの配列
723     * @return array 子IDの配列
724     */
725    function sfGetChildrenArraySub($arrData, $pid_name, $id_name, $arrPID) {
726        $arrChildren = array();
727        $max = count($arrData);
728
729        for($i = 0; $i < $max; $i++) {
730            foreach($arrPID as $val) {
731                if($arrData[$i][$pid_name] == $val) {
732                    $arrChildren[] = $arrData[$i][$id_name];
733                }
734            }
735        }
736        return $arrChildren;
737    }
738
739    /**
740     * 所属するすべての階層の親IDを配列で返す.
741     *
742     * @param SC_Query $objQuery SC_Query インスタンス
743     * @param string $table テーブル名
744     * @param string $pid_name 親ID名
745     * @param string $id_name ID名
746     * @param integer $id ID
747     * @return array 親IDの配列
748     */
749    function sfGetParents($objQuery, $table, $pid_name, $id_name, $id) {
750        $arrRet = $this->sfGetParentsArray($table, $pid_name, $id_name, $id);
751        // 配列の先頭1つを削除する。
752        array_shift($arrRet);
753        return $arrRet;
754    }
755
756    /**
757     * 階層構造のテーブルから親ID配列を取得する.
758     *
759     * @param string $table テーブル名
760     * @param string $pid_name 親ID名
761     * @param string $id_name ID名
762     * @param integer $id ID
763     * @return array 親IDの配列
764     */
765    function sfGetParentsArray($table, $pid_name, $id_name, $id) {
766        $objQuery = new SC_Query();
767        $col = $pid_name . "," . $id_name;
768         $arrData = $objQuery->select($col, $table);
769
770        $arrParents = array();
771        $arrParents[] = $id;
772        $child = $id;
773
774        $ret = SC_Utils::sfGetParentsArraySub($arrData, $pid_name, $id_name, $child);
775
776        while($ret != "") {
777            $arrParents[] = $ret;
778            $ret = SC_Utils::sfGetParentsArraySub($arrData, $pid_name, $id_name, $ret);
779        }
780
781        $arrParents = array_reverse($arrParents);
782
783        return $arrParents;
784    }
785
786    /**
787     * カテゴリから商品を検索する場合のWHERE文と値を返す.
788     *
789     * @param integer $category_id カテゴリID
790     * @return array 商品を検索する場合の配列
791     */
792    function sfGetCatWhere($category_id) {
793        // 子カテゴリIDの取得
794        $arrRet = $this->sfGetChildsID("dtb_category", "parent_category_id", "category_id", $category_id);
795        $tmp_where = "";
796        foreach ($arrRet as $val) {
797            if($tmp_where == "") {
798                $tmp_where.= " category_id IN ( ?";
799            } else {
800                $tmp_where.= ",? ";
801            }
802            $arrval[] = $val;
803        }
804        $tmp_where.= " ) ";
805        return array($tmp_where, $arrval);
806    }
807
808    /**
809     * 受注一時テーブルから情報を取得する.
810     *
811     * @param integer $order_temp_id 受注一時ID
812     * @return array 受注一時情報の配列
813     */
814    function sfGetOrderTemp($order_temp_id) {
815        $objQuery = new SC_Query();
816        $where = "order_temp_id = ?";
817        $arrRet = $objQuery->select("*", "dtb_order_temp", $where, array($order_temp_id));
818        return $arrRet[0];
819    }
820
821    /**
822     * SELECTボックス用リストを作成する.
823     *
824     * @param string $table テーブル名
825     * @param string $keyname プライマリーキーのカラム名
826     * @param string $valname データ内容のカラム名
827     * @return array SELECT ボックス用リストの配列
828     */
829    function sfGetIDValueList($table, $keyname, $valname) {
830        $objQuery = new SC_Query();
831        $col = "$keyname, $valname";
832        $objQuery->setwhere("del_flg = 0");
833        $objQuery->setorder("rank DESC");
834        $arrList = $objQuery->select($col, $table);
835        $count = count($arrList);
836        for($cnt = 0; $cnt < $count; $cnt++) {
837            $key = $arrList[$cnt][$keyname];
838            $val = $arrList[$cnt][$valname];
839            $arrRet[$key] = $val;
840        }
841        return $arrRet;
842    }
843
844    /**
845     * ランキングを上げる.
846     *
847     * @param string $table テーブル名
848     * @param string $colname カラム名
849     * @param string|integer $id テーブルのキー
850     * @param string $andwhere SQL の AND 条件である WHERE 句
851     * @return void
852     */
853    function sfRankUp($table, $colname, $id, $andwhere = "") {
854        $objQuery = new SC_Query();
855        $objQuery->begin();
856        $where = "$colname = ?";
857        if($andwhere != "") {
858            $where.= " AND $andwhere";
859        }
860        // 対象項目のランクを取得
861        $rank = $objQuery->get($table, "rank", $where, array($id));
862        // ランクの最大値を取得
863        $maxrank = $objQuery->max($table, "rank", $andwhere);
864        // ランクが最大値よりも小さい場合に実行する。
865        if($rank < $maxrank) {
866            // ランクが一つ上のIDを取得する。
867            $where = "rank = ?";
868            if($andwhere != "") {
869                $where.= " AND $andwhere";
870            }
871            $uprank = $rank + 1;
872            $up_id = $objQuery->get($table, $colname, $where, array($uprank));
873            // ランク入れ替えの実行
874            $sqlup = "UPDATE $table SET rank = ?, update_date = Now() WHERE $colname = ?";
875            $objQuery->exec($sqlup, array($rank + 1, $id));
876            $objQuery->exec($sqlup, array($rank, $up_id));
877        }
878        $objQuery->commit();
879    }
880
881    /**
882     * ランキングを下げる.
883     *
884     * @param string $table テーブル名
885     * @param string $colname カラム名
886     * @param string|integer $id テーブルのキー
887     * @param string $andwhere SQL の AND 条件である WHERE 句
888     * @return void
889     */
890    function sfRankDown($table, $colname, $id, $andwhere = "") {
891        $objQuery = new SC_Query();
892        $objQuery->begin();
893        $where = "$colname = ?";
894        if($andwhere != "") {
895            $where.= " AND $andwhere";
896        }
897        // 対象項目のランクを取得
898        $rank = $objQuery->get($table, "rank", $where, array($id));
899
900        // ランクが1(最小値)よりも大きい場合に実行する。
901        if($rank > 1) {
902            // ランクが一つ下のIDを取得する。
903            $where = "rank = ?";
904            if($andwhere != "") {
905                $where.= " AND $andwhere";
906            }
907            $downrank = $rank - 1;
908            $down_id = $objQuery->get($table, $colname, $where, array($downrank));
909            // ランク入れ替えの実行
910            $sqlup = "UPDATE $table SET rank = ?, update_date = Now() WHERE $colname = ?";
911            $objQuery->exec($sqlup, array($rank - 1, $id));
912            $objQuery->exec($sqlup, array($rank, $down_id));
913        }
914        $objQuery->commit();
915    }
916
917    /**
918     * 指定順位へ移動する.
919     *
920     * @param string $tableName テーブル名
921     * @param string $keyIdColumn キーを保持するカラム名
922     * @param string|integer $keyId キーの値
923     * @param integer $pos 指定順位
924     * @param string $where SQL の AND 条件である WHERE 句
925     * @return void
926     */
927    function sfMoveRank($tableName, $keyIdColumn, $keyId, $pos, $where = "") {
928        $objQuery = new SC_Query();
929        $objQuery->begin();
930
931        // 自身のランクを取得する
932        $rank = $objQuery->get($tableName, "rank", "$keyIdColumn = ?", array($keyId));
933        $max = $objQuery->max($tableName, "rank", $where);
934
935        // 値の調整(逆順)
936        if($pos > $max) {
937            $position = 1;
938        } else if($pos < 1) {
939            $position = $max;
940        } else {
941            $position = $max - $pos + 1;
942        }
943
944        if( $position > $rank ) $term = "rank - 1"; //入れ替え先の順位が入れ換え元の順位より大きい場合
945        if( $position < $rank ) $term = "rank + 1"; //入れ替え先の順位が入れ換え元の順位より小さい場合
946
947        // 指定した順位の商品から移動させる商品までのrankを1つずらす
948        $sql = "UPDATE $tableName SET rank = $term, update_date = NOW() WHERE rank BETWEEN ? AND ? AND del_flg = 0";
949        if($where != "") {
950            $sql.= " AND $where";
951        }
952
953        if( $position > $rank ) $objQuery->exec( $sql, array( $rank + 1, $position ));
954        if( $position < $rank ) $objQuery->exec( $sql, array( $position, $rank - 1 ));
955
956        // 指定した順位へrankを書き換える。
957        $sql  = "UPDATE $tableName SET rank = ?, update_date = NOW() WHERE $keyIdColumn = ? AND del_flg = 0 ";
958        if($where != "") {
959            $sql.= " AND $where";
960        }
961
962        $objQuery->exec( $sql, array( $position, $keyId ) );
963        $objQuery->commit();
964    }
965
966    /**
967     * ランクを含むレコードを削除する.
968     *
969     * レコードごと削除する場合は、$deleteをtrueにする
970     *
971     * @param string $table テーブル名
972     * @param string $colname カラム名
973     * @param string|integer $id テーブルのキー
974     * @param string $andwhere SQL の AND 条件である WHERE 句
975     * @param bool $delete レコードごと削除する場合 true,
976     *                     レコードごと削除しない場合 false
977     * @return void
978     */
979    function sfDeleteRankRecord($table, $colname, $id, $andwhere = "",
980                                $delete = false) {
981        $objQuery = new SC_Query();
982        $objQuery->begin();
983        // 削除レコードのランクを取得する。
984        $where = "$colname = ?";
985        if($andwhere != "") {
986            $where.= " AND $andwhere";
987        }
988        $rank = $objQuery->get($table, "rank", $where, array($id));
989
990        if(!$delete) {
991            // ランクを最下位にする、DELフラグON
992            $sqlup = "UPDATE $table SET rank = 0, del_flg = 1, update_date = Now() ";
993            $sqlup.= "WHERE $colname = ?";
994            // UPDATEの実行
995            $objQuery->exec($sqlup, array($id));
996        } else {
997            $objQuery->delete($table, "$colname = ?", array($id));
998        }
999
1000        // 追加レコードのランクより上のレコードを一つずらす。
1001        $where = "rank > ?";
1002        if($andwhere != "") {
1003            $where.= " AND $andwhere";
1004        }
1005        $sqlup = "UPDATE $table SET rank = (rank - 1) WHERE $where";
1006        $objQuery->exec($sqlup, array($rank));
1007        $objQuery->commit();
1008    }
1009
1010    /**
1011     * レコードの存在チェックを行う.
1012     *
1013     * @param string $table テーブル名
1014     * @param string $col カラム名
1015     * @param array $arrval 要素の配列
1016     * @param array $addwhere SQL の AND 条件である WHERE 句
1017     * @return bool レコードが存在する場合 true
1018     */
1019    function sfIsRecord($table, $col, $arrval, $addwhere = "") {
1020        $objQuery = new SC_Query();
1021        $arrCol = split("[, ]", $col);
1022
1023        $where = "del_flg = 0";
1024
1025        if($addwhere != "") {
1026            $where.= " AND $addwhere";
1027        }
1028
1029        foreach($arrCol as $val) {
1030            if($val != "") {
1031                if($where == "") {
1032                    $where = "$val = ?";
1033                } else {
1034                    $where.= " AND $val = ?";
1035                }
1036            }
1037        }
1038        $ret = $objQuery->get($table, $col, $where, $arrval);
1039
1040        if($ret != "") {
1041            return true;
1042        }
1043        return false;
1044    }
1045
1046}
1047?>
Note: See TracBrowser for help on using the repository browser.