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

Revision 15578, 43.0 KB checked in by nanasess, 17 years ago (diff)

リファクタリング

  • DB インスタンスを生成する関数の移動
  • Property svn:keywords set to Id Revision Date
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
Line 
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:SC_Helper_DB.php 15532 2007-08-31 14:39:46Z nanasess $
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     * @return void
256     */
257    function sfGetPayment() {
258        $objQuery = new SC_Query();
259        // 購入金額が条件額以下の項目を取得
260        $where = "del_flg = 0";
261        $objQuery->setorder("fix, rank DESC");
262        $arrRet = $objQuery->select("payment_id, payment_method, rule", "dtb_payment", $where);
263        return $arrRet;
264    }
265
266    /**
267     * カート内商品の集計処理を行う.
268     *
269     * @param LC_Page $objPage ページクラスのインスタンス
270     * @param SC_CartSession $objCartSess カートセッションのインスタンス
271     * @param array $arrInfo 商品情報の配列
272     * @return LC_Page 集計処理後のページクラスインスタンス
273     */
274    function sfTotalCart(&$objPage, $objCartSess, $arrInfo) {
275        $objDb = new SC_Helper_DB_Ex();
276        // 規格名一覧
277        $arrClassName = $objDb->sfGetIDValueList("dtb_class", "class_id", "name");
278        // 規格分類名一覧
279        $arrClassCatName = $objDb->sfGetIDValueList("dtb_classcategory", "classcategory_id", "name");
280
281        $objPage->tpl_total_pretax = 0;     // 費用合計(税込み)
282        $objPage->tpl_total_tax = 0;        // 消費税合計
283        $objPage->tpl_total_point = 0;      // ポイント合計
284
285        // カート内情報の取得
286        $arrCart = $objCartSess->getCartList();
287        $max = count($arrCart);
288        $cnt = 0;
289
290        for ($i = 0; $i < $max; $i++) {
291            // 商品規格情報の取得
292            $arrData = $this->sfGetProductsClass($arrCart[$i]['id']);
293            $limit = "";
294            // DBに存在する商品
295            if (count($arrData) > 0) {
296
297                // 購入制限数を求める。
298                if ($arrData['stock_unlimited'] != '1' && $arrData['sale_unlimited'] != '1') {
299                    if($arrData['sale_limit'] < $arrData['stock']) {
300                        $limit = $arrData['sale_limit'];
301                    } else {
302                        $limit = $arrData['stock'];
303                    }
304                } else {
305                    if ($arrData['sale_unlimited'] != '1') {
306                        $limit = $arrData['sale_limit'];
307                    }
308                    if ($arrData['stock_unlimited'] != '1') {
309                        $limit = $arrData['stock'];
310                    }
311                }
312
313                if($limit != "" && $limit < $arrCart[$i]['quantity']) {
314                    // カート内商品数を制限に合わせる
315                    $objCartSess->setProductValue($arrCart[$i]['id'], 'quantity', $limit);
316                    $quantity = $limit;
317                    $objPage->tpl_message = "※「" . $arrData['name'] . "」は販売制限しております、一度にこれ以上の購入はできません。";
318                } else {
319                    $quantity = $arrCart[$i]['quantity'];
320                }
321
322                $objPage->arrProductsClass[$cnt] = $arrData;
323                $objPage->arrProductsClass[$cnt]['quantity'] = $quantity;
324                $objPage->arrProductsClass[$cnt]['cart_no'] = $arrCart[$i]['cart_no'];
325                $objPage->arrProductsClass[$cnt]['class_name1'] =
326                    isset($arrClassName[$arrData['class_id1']])
327                        ? $arrClassName[$arrData['class_id1']] : "";
328
329                $objPage->arrProductsClass[$cnt]['class_name2'] =
330                    isset($arrClassName[$arrData['class_id2']])
331                        ? $arrClassName[$arrData['class_id2']] : "";
332
333                $objPage->arrProductsClass[$cnt]['classcategory_name1'] =
334                    $arrClassCatName[$arrData['classcategory_id1']];
335
336                $objPage->arrProductsClass[$cnt]['classcategory_name2'] =
337                    $arrClassCatName[$arrData['classcategory_id2']];
338
339                // 画像サイズ
340                list($image_width, $image_height) = getimagesize(IMAGE_SAVE_DIR . basename($objPage->arrProductsClass[$cnt]["main_image"]));
341                $objPage->arrProductsClass[$cnt]["tpl_image_width"] = $image_width + 60;
342                $objPage->arrProductsClass[$cnt]["tpl_image_height"] = $image_height + 80;
343
344                // 価格の登録
345                if ($arrData['price02'] != "") {
346                    $objCartSess->setProductValue($arrCart[$i]['id'], 'price', $arrData['price02']);
347                    $objPage->arrProductsClass[$cnt]['uniq_price'] = $arrData['price02'];
348                } else {
349                    $objCartSess->setProductValue($arrCart[$i]['id'], 'price', $arrData['price01']);
350                    $objPage->arrProductsClass[$cnt]['uniq_price'] = $arrData['price01'];
351                }
352                // ポイント付与率の登録
353                $objCartSess->setProductValue($arrCart[$i]['id'], 'point_rate', $arrData['point_rate']);
354                // 商品ごとの合計金額
355                $objPage->arrProductsClass[$cnt]['total_pretax'] = $objCartSess->getProductTotal($arrInfo, $arrCart[$i]['id']);
356                // 送料の合計を計算する
357                $objPage->tpl_total_deliv_fee+= ($arrData['deliv_fee'] * $arrCart[$i]['quantity']);
358                $cnt++;
359            } else {
360                // DBに商品が見つからない場合はカート商品の削除
361                $objCartSess->delProductKey('id', $arrCart[$i]['id']);
362            }
363        }
364
365        // 全商品合計金額(税込み)
366        $objPage->tpl_total_pretax = $objCartSess->getAllProductsTotal($arrInfo);
367        // 全商品合計消費税
368        $objPage->tpl_total_tax = $objCartSess->getAllProductsTax($arrInfo);
369        // 全商品合計ポイント
370        $objPage->tpl_total_point = $objCartSess->getAllProductsPoint();
371
372        return $objPage;
373    }
374
375    /**
376     * 受注一時テーブルへの書き込み処理を行う.
377     *
378     * @param string $uniqid ユニークID
379     * @param array $sqlval SQLの値の配列
380     * @return void
381     */
382    function sfRegistTempOrder($uniqid, $sqlval) {
383        if($uniqid != "") {
384            // 既存データのチェック
385            $objQuery = new SC_Query();
386            $where = "order_temp_id = ?";
387            $cnt = $objQuery->count("dtb_order_temp", $where, array($uniqid));
388            // 既存データがない場合
389            if ($cnt == 0) {
390                // 初回書き込み時に会員の登録済み情報を取り込む
391                $sqlval = $this->sfGetCustomerSqlVal($uniqid, $sqlval);
392                $sqlval['create_date'] = "now()";
393                $objQuery->insert("dtb_order_temp", $sqlval);
394            } else {
395                $objQuery->update("dtb_order_temp", $sqlval, $where, array($uniqid));
396            }
397        }
398    }
399
400    /**
401     * 会員情報から SQL文の値を生成する.
402     *
403     * @param string $uniqid ユニークID
404     * @param array $sqlval SQL の値の配列
405     * @return array 会員情報を含んだ SQL の値の配列
406     */
407    function sfGetCustomerSqlVal($uniqid, $sqlval) {
408        $objCustomer = new SC_Customer();
409        // 会員情報登録処理
410        if ($objCustomer->isLoginSuccess()) {
411            // 登録データの作成
412            $sqlval['order_temp_id'] = $uniqid;
413            $sqlval['update_date'] = 'Now()';
414            $sqlval['customer_id'] = $objCustomer->getValue('customer_id');
415            $sqlval['order_name01'] = $objCustomer->getValue('name01');
416            $sqlval['order_name02'] = $objCustomer->getValue('name02');
417            $sqlval['order_kana01'] = $objCustomer->getValue('kana01');
418            $sqlval['order_kana02'] = $objCustomer->getValue('kana02');
419            $sqlval['order_sex'] = $objCustomer->getValue('sex');
420            $sqlval['order_zip01'] = $objCustomer->getValue('zip01');
421            $sqlval['order_zip02'] = $objCustomer->getValue('zip02');
422            $sqlval['order_pref'] = $objCustomer->getValue('pref');
423            $sqlval['order_addr01'] = $objCustomer->getValue('addr01');
424            $sqlval['order_addr02'] = $objCustomer->getValue('addr02');
425            $sqlval['order_tel01'] = $objCustomer->getValue('tel01');
426            $sqlval['order_tel02'] = $objCustomer->getValue('tel02');
427            $sqlval['order_tel03'] = $objCustomer->getValue('tel03');
428            if (defined('MOBILE_SITE')) {
429                $sqlval['order_email'] = $objCustomer->getValue('email_mobile');
430            } else {
431                $sqlval['order_email'] = $objCustomer->getValue('email');
432            }
433            $sqlval['order_job'] = $objCustomer->getValue('job');
434            $sqlval['order_birth'] = $objCustomer->getValue('birth');
435        }
436        return $sqlval;
437    }
438
439    /**
440     * 会員編集登録処理を行う.
441     *
442     * @param array $array パラメータの配列
443     * @param array $arrRegistColumn 登録するカラムの配列
444     * @return void
445     */
446    function sfEditCustomerData($array, $arrRegistColumn) {
447        $objQuery = new SC_Query();
448
449        foreach ($arrRegistColumn as $data) {
450            if ($data["column"] != "password") {
451                if($array[ $data['column'] ] != "") {
452                    $arrRegist[ $data["column"] ] = $array[ $data["column"] ];
453                } else {
454                    $arrRegist[ $data['column'] ] = NULL;
455                }
456            }
457        }
458        if (strlen($array["year"]) > 0 && strlen($array["month"]) > 0 && strlen($array["day"]) > 0) {
459            $arrRegist["birth"] = $array["year"] ."/". $array["month"] ."/". $array["day"] ." 00:00:00";
460        } else {
461            $arrRegist["birth"] = NULL;
462        }
463
464        //-- パスワードの更新がある場合は暗号化。(更新がない場合はUPDATE文を構成しない)
465        if ($array["password"] != DEFAULT_PASSWORD) $arrRegist["password"] = sha1($array["password"] . ":" . AUTH_MAGIC);
466        $arrRegist["update_date"] = "NOW()";
467
468        //-- 編集登録実行
469        if (defined('MOBILE_SITE')) {
470            $arrRegist['email_mobile'] = $arrRegist['email'];
471            unset($arrRegist['email']);
472        }
473        $objQuery->begin();
474        $objQuery->update("dtb_customer", $arrRegist, "customer_id = ? ", array($array['customer_id']));
475        $objQuery->commit();
476    }
477
478    /**
479     * カテゴリツリーの取得を行う.
480     *
481     * @param integer $parent_category_id 親カテゴリID
482     * @param bool $count_check 登録商品数のチェックを行う場合 true
483     * @return array カテゴリツリーの配列
484     */
485    function sfGetCatTree($parent_category_id, $count_check = false) {
486        $objQuery = new SC_Query();
487        $col = "";
488        $col .= " cat.category_id,";
489        $col .= " cat.category_name,";
490        $col .= " cat.parent_category_id,";
491        $col .= " cat.level,";
492        $col .= " cat.rank,";
493        $col .= " cat.creator_id,";
494        $col .= " cat.create_date,";
495        $col .= " cat.update_date,";
496        $col .= " cat.del_flg, ";
497        $col .= " ttl.product_count";
498        $from = "dtb_category as cat left join dtb_category_total_count as ttl on ttl.category_id = cat.category_id";
499        // 登録商品数のチェック
500        if($count_check) {
501            $where = "del_flg = 0 AND product_count > 0";
502        } else {
503            $where = "del_flg = 0";
504        }
505        $objQuery->setoption("ORDER BY rank DESC");
506        $arrRet = $objQuery->select($col, $from, $where);
507
508        $arrParentID = $this->sfGetParents($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $parent_category_id);
509
510        foreach($arrRet as $key => $array) {
511            foreach($arrParentID as $val) {
512                if($array['category_id'] == $val) {
513                    $arrRet[$key]['display'] = 1;
514                    break;
515                }
516            }
517        }
518
519        return $arrRet;
520    }
521
522    /**
523     * 親カテゴリーを連結した文字列を取得する.
524     *
525     * @param integer $category_id カテゴリID
526     * @return string 親カテゴリーを連結した文字列
527     */
528    function sfGetCatCombName($category_id){
529        // 商品が属するカテゴリIDを縦に取得
530        $objQuery = new SC_Query();
531        $arrCatID = sfGetParents($objQuery, "dtb_category", "parent_category_id", "category_id", $category_id);
532        $ConbName = "";
533
534        // カテゴリー名称を取得する
535        foreach($arrCatID as $key => $val){
536            $sql = "SELECT category_name FROM dtb_category WHERE category_id = ?";
537            $arrVal = array($val);
538            $CatName = $objQuery->getOne($sql,$arrVal);
539            $ConbName .= $CatName . ' | ';
540        }
541        // 最後の | をカットする
542        $ConbName = substr_replace($ConbName, "", strlen($ConbName) - 2, 2);
543
544        return $ConbName;
545    }
546
547    /**
548     * 指定したカテゴリーIDの大カテゴリーを取得する.
549     *
550     * @param integer $category_id カテゴリID
551     * @return array 指定したカテゴリーIDの大カテゴリー
552     */
553    function sfGetFirstCat($category_id){
554        // 商品が属するカテゴリIDを縦に取得
555        $objQuery = new SC_Query();
556        $arrRet = array();
557        $arrCatID = $this->sfGetParents($objQuery, "dtb_category", "parent_category_id", "category_id", $category_id);
558        $arrRet['id'] = $arrCatID[0];
559
560        // カテゴリー名称を取得する
561        $sql = "SELECT category_name FROM dtb_category WHERE category_id = ?";
562        $arrVal = array($arrRet['id']);
563        $arrRet['name'] = $objQuery->getOne($sql,$arrVal);
564
565        return $arrRet;
566    }
567
568    /**
569     * カテゴリツリーの取得を行う.
570     *
571     * $products_check:true商品登録済みのものだけ取得する
572     *
573     * @param string $addwhere 追加する WHERE 句
574     * @param bool $products_check 商品の存在するカテゴリのみ取得する場合 true
575     * @param string $head カテゴリ名のプレフィックス文字列
576     * @return array カテゴリツリーの配列
577     */
578    function sfGetCategoryList($addwhere = "", $products_check = false, $head = CATEGORY_HEAD) {
579        $objQuery = new SC_Query();
580        $where = "del_flg = 0";
581
582        if($addwhere != "") {
583            $where.= " AND $addwhere";
584        }
585
586        $objQuery->setoption("ORDER BY rank DESC");
587
588        if($products_check) {
589            $col = "T1.category_id, category_name, level";
590            $from = "dtb_category AS T1 LEFT JOIN dtb_category_total_count AS T2 ON T1.category_id = T2.category_id";
591            $where .= " AND product_count > 0";
592        } else {
593            $col = "category_id, category_name, level";
594            $from = "dtb_category";
595        }
596
597        $arrRet = $objQuery->select($col, $from, $where);
598
599        $max = count($arrRet);
600        for($cnt = 0; $cnt < $max; $cnt++) {
601            $id = $arrRet[$cnt]['category_id'];
602            $name = $arrRet[$cnt]['category_name'];
603            $arrList[$id] = "";
604            /*
605            for($n = 1; $n < $arrRet[$cnt]['level']; $n++) {
606                $arrList[$id].= " ";
607            }
608            */
609            for($cat_cnt = 0; $cat_cnt < $arrRet[$cnt]['level']; $cat_cnt++) {
610                $arrList[$id].= $head;
611            }
612            $arrList[$id].= $name;
613        }
614        return $arrList;
615    }
616
617    /**
618     * カテゴリーツリーの取得を行う.
619     *
620     * 親カテゴリの Value=0 を対象とする
621     *
622     * @param bool $parent_zero 親カテゴリの Value=0 の場合 true
623     * @return array カテゴリツリーの配列
624     */
625    function sfGetLevelCatList($parent_zero = true) {
626        $objQuery = new SC_Query();
627        $col = "category_id, category_name, level";
628        $where = "del_flg = 0";
629        $objQuery->setoption("ORDER BY rank DESC");
630        $arrRet = $objQuery->select($col, "dtb_category", $where);
631        $max = count($arrRet);
632
633        for($cnt = 0; $cnt < $max; $cnt++) {
634            if($parent_zero) {
635                if($arrRet[$cnt]['level'] == LEVEL_MAX) {
636                    $arrValue[$cnt] = $arrRet[$cnt]['category_id'];
637                } else {
638                    $arrValue[$cnt] = "";
639                }
640            } else {
641                $arrValue[$cnt] = $arrRet[$cnt]['category_id'];
642            }
643
644            $arrOutput[$cnt] = "";
645            /*
646            for($n = 1; $n < $arrRet[$cnt]['level']; $n++) {
647                $arrOutput[$cnt].= " ";
648            }
649            */
650            for($cat_cnt = 0; $cat_cnt < $arrRet[$cnt]['level']; $cat_cnt++) {
651                $arrOutput[$cnt].= CATEGORY_HEAD;
652            }
653            $arrOutput[$cnt].= $arrRet[$cnt]['category_name'];
654        }
655        return array($arrValue, $arrOutput);
656    }
657
658    /**
659     * 選択中のカテゴリを取得する.
660     *
661     * @param integer $product_id プロダクトID
662     * @param integer $category_id カテゴリID
663     * @return integer 選択中のカテゴリID
664     *
665     */
666    function sfGetCategoryId($product_id, $category_id) {
667
668        if(!$this->g_category_on)   {
669            $this->g_category_on = true;
670            $category_id = (int) $category_id;
671            $product_id = (int) $product_id;
672            if(SC_Utils_Ex::sfIsInt($category_id) && $this->sfIsRecord("dtb_category","category_id", $category_id)) {
673                $this->g_category_id = $category_id;
674            } else if (SC_Utils_Ex::sfIsInt($product_id) && $this->sfIsRecord("dtb_products","product_id", $product_id, "status = 1")) {
675                $objQuery = new SC_Query();
676                $where = "product_id = ?";
677                $category_id = $objQuery->get("dtb_products", "category_id", $where, array($product_id));
678                $this->g_category_id = $category_id;
679            } else {
680                // 不正な場合は、0を返す。
681                $this->g_category_id = 0;
682            }
683        }
684        return $this->g_category_id;
685    }
686
687    /**
688     * カテゴリ数の登録を行う.
689     *
690     * @param SC_Query $objQuery SC_Query インスタンス
691     * @return void
692     */
693    function sfCategory_Count($objQuery){
694        $sql = "";
695
696        //テーブル内容の削除
697        $objQuery->query("DELETE FROM dtb_category_count");
698        $objQuery->query("DELETE FROM dtb_category_total_count");
699
700        //各カテゴリ内の商品数を数えて格納
701        $sql = " INSERT INTO dtb_category_count(category_id, product_count, create_date) ";
702        $sql .= " SELECT T1.category_id, count(T2.category_id), now() FROM dtb_category AS T1 LEFT JOIN dtb_products AS T2 ";
703        $sql .= " ON T1.category_id = T2.category_id  ";
704        $sql .= " WHERE T2.del_flg = 0 AND T2.status = 1 ";
705        $sql .= " GROUP BY T1.category_id, T2.category_id ";
706        $objQuery->query($sql);
707
708        //子カテゴリ内の商品数を集計する
709        $arrCat = $objQuery->getAll("SELECT * FROM dtb_category");
710
711        $sql = "";
712        foreach($arrCat as $key => $val){
713
714            // 子ID一覧を取得
715            $arrRet = $this->sfGetChildrenArray('dtb_category', 'parent_category_id', 'category_id', $val['category_id']);
716            $line = SC_Utils_Ex::sfGetCommaList($arrRet);
717
718            $sql = " INSERT INTO dtb_category_total_count(category_id, product_count, create_date) ";
719            $sql .= " SELECT ?, SUM(product_count), now() FROM dtb_category_count ";
720            $sql .= " WHERE category_id IN (" . $line . ")";
721
722            $objQuery->query($sql, array($val['category_id']));
723        }
724    }
725
726    /**
727     * 子IDの配列を返す.
728     *
729     * @param string $table テーブル名
730     * @param string $pid_name 親ID名
731     * @param string $id_name ID名
732     * @param integer $id ID
733     * @param array 子ID の配列
734     */
735    function sfGetChildsID($table, $pid_name, $id_name, $id) {
736        $arrRet = $this->sfGetChildrenArray($table, $pid_name, $id_name, $id);
737        return $arrRet;
738    }
739
740    /**
741     * 階層構造のテーブルから子ID配列を取得する.
742     *
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 sfGetChildrenArray($table, $pid_name, $id_name, $id) {
750        $objQuery = new SC_Query();
751        $col = $pid_name . "," . $id_name;
752         $arrData = $objQuery->select($col, $table);
753
754        $arrPID = array();
755        $arrPID[] = $id;
756        $arrChildren = array();
757        $arrChildren[] = $id;
758
759        $arrRet = $this->sfGetChildrenArraySub($arrData, $pid_name, $id_name, $arrPID);
760
761        while(count($arrRet) > 0) {
762            $arrChildren = array_merge($arrChildren, $arrRet);
763            $arrRet = $this->sfGetChildrenArraySub($arrData, $pid_name, $id_name, $arrRet);
764        }
765
766        return $arrChildren;
767    }
768
769    /**
770     * 親ID直下の子IDをすべて取得する.
771     *
772     * @param array $arrData 親カテゴリの配列
773     * @param string $pid_name 親ID名
774     * @param string $id_name ID名
775     * @param array $arrPID 親IDの配列
776     * @return array 子IDの配列
777     */
778    function sfGetChildrenArraySub($arrData, $pid_name, $id_name, $arrPID) {
779        $arrChildren = array();
780        $max = count($arrData);
781
782        for($i = 0; $i < $max; $i++) {
783            foreach($arrPID as $val) {
784                if($arrData[$i][$pid_name] == $val) {
785                    $arrChildren[] = $arrData[$i][$id_name];
786                }
787            }
788        }
789        return $arrChildren;
790    }
791
792    /**
793     * 所属するすべての階層の親IDを配列で返す.
794     *
795     * @param SC_Query $objQuery SC_Query インスタンス
796     * @param string $table テーブル名
797     * @param string $pid_name 親ID名
798     * @param string $id_name ID名
799     * @param integer $id ID
800     * @return array 親IDの配列
801     */
802    function sfGetParents($objQuery, $table, $pid_name, $id_name, $id) {
803        $arrRet = $this->sfGetParentsArray($table, $pid_name, $id_name, $id);
804        // 配列の先頭1つを削除する。
805        array_shift($arrRet);
806        return $arrRet;
807    }
808
809    /**
810     * 階層構造のテーブルから親ID配列を取得する.
811     *
812     * @param string $table テーブル名
813     * @param string $pid_name 親ID名
814     * @param string $id_name ID名
815     * @param integer $id ID
816     * @return array 親IDの配列
817     */
818    function sfGetParentsArray($table, $pid_name, $id_name, $id) {
819        $objQuery = new SC_Query();
820        $col = $pid_name . "," . $id_name;
821         $arrData = $objQuery->select($col, $table);
822
823        $arrParents = array();
824        $arrParents[] = $id;
825        $child = $id;
826
827        $ret = SC_Utils::sfGetParentsArraySub($arrData, $pid_name, $id_name, $child);
828
829        while($ret != "") {
830            $arrParents[] = $ret;
831            $ret = SC_Utils::sfGetParentsArraySub($arrData, $pid_name, $id_name, $ret);
832        }
833
834        $arrParents = array_reverse($arrParents);
835
836        return $arrParents;
837    }
838
839    /**
840     * カテゴリから商品を検索する場合のWHERE文と値を返す.
841     *
842     * @param integer $category_id カテゴリID
843     * @return array 商品を検索する場合の配列
844     */
845    function sfGetCatWhere($category_id) {
846        // 子カテゴリIDの取得
847        $arrRet = $this->sfGetChildsID("dtb_category", "parent_category_id", "category_id", $category_id);
848        $tmp_where = "";
849        foreach ($arrRet as $val) {
850            if($tmp_where == "") {
851                $tmp_where.= " category_id IN ( ?";
852            } else {
853                $tmp_where.= ",? ";
854            }
855            $arrval[] = $val;
856        }
857        $tmp_where.= " ) ";
858        return array($tmp_where, $arrval);
859    }
860
861    /**
862     * 受注一時テーブルから情報を取得する.
863     *
864     * @param integer $order_temp_id 受注一時ID
865     * @return array 受注一時情報の配列
866     */
867    function sfGetOrderTemp($order_temp_id) {
868        $objQuery = new SC_Query();
869        $where = "order_temp_id = ?";
870        $arrRet = $objQuery->select("*", "dtb_order_temp", $where, array($order_temp_id));
871        return $arrRet[0];
872    }
873
874    /**
875     * SELECTボックス用リストを作成する.
876     *
877     * @param string $table テーブル名
878     * @param string $keyname プライマリーキーのカラム名
879     * @param string $valname データ内容のカラム名
880     * @return array SELECT ボックス用リストの配列
881     */
882    function sfGetIDValueList($table, $keyname, $valname) {
883        $objQuery = new SC_Query();
884        $col = "$keyname, $valname";
885        $objQuery->setwhere("del_flg = 0");
886        $objQuery->setorder("rank DESC");
887        $arrList = $objQuery->select($col, $table);
888        $count = count($arrList);
889        for($cnt = 0; $cnt < $count; $cnt++) {
890            $key = $arrList[$cnt][$keyname];
891            $val = $arrList[$cnt][$valname];
892            $arrRet[$key] = $val;
893        }
894        return $arrRet;
895    }
896
897    /**
898     * ランキングを上げる.
899     *
900     * @param string $table テーブル名
901     * @param string $colname カラム名
902     * @param string|integer $id テーブルのキー
903     * @param string $andwhere SQL の AND 条件である WHERE 句
904     * @return void
905     */
906    function sfRankUp($table, $colname, $id, $andwhere = "") {
907        $objQuery = new SC_Query();
908        $objQuery->begin();
909        $where = "$colname = ?";
910        if($andwhere != "") {
911            $where.= " AND $andwhere";
912        }
913        // 対象項目のランクを取得
914        $rank = $objQuery->get($table, "rank", $where, array($id));
915        // ランクの最大値を取得
916        $maxrank = $objQuery->max($table, "rank", $andwhere);
917        // ランクが最大値よりも小さい場合に実行する。
918        if($rank < $maxrank) {
919            // ランクが一つ上のIDを取得する。
920            $where = "rank = ?";
921            if($andwhere != "") {
922                $where.= " AND $andwhere";
923            }
924            $uprank = $rank + 1;
925            $up_id = $objQuery->get($table, $colname, $where, array($uprank));
926            // ランク入れ替えの実行
927            $sqlup = "UPDATE $table SET rank = ?, update_date = Now() WHERE $colname = ?";
928            $objQuery->exec($sqlup, array($rank + 1, $id));
929            $objQuery->exec($sqlup, array($rank, $up_id));
930        }
931        $objQuery->commit();
932    }
933
934    /**
935     * ランキングを下げる.
936     *
937     * @param string $table テーブル名
938     * @param string $colname カラム名
939     * @param string|integer $id テーブルのキー
940     * @param string $andwhere SQL の AND 条件である WHERE 句
941     * @return void
942     */
943    function sfRankDown($table, $colname, $id, $andwhere = "") {
944        $objQuery = new SC_Query();
945        $objQuery->begin();
946        $where = "$colname = ?";
947        if($andwhere != "") {
948            $where.= " AND $andwhere";
949        }
950        // 対象項目のランクを取得
951        $rank = $objQuery->get($table, "rank", $where, array($id));
952
953        // ランクが1(最小値)よりも大きい場合に実行する。
954        if($rank > 1) {
955            // ランクが一つ下のIDを取得する。
956            $where = "rank = ?";
957            if($andwhere != "") {
958                $where.= " AND $andwhere";
959            }
960            $downrank = $rank - 1;
961            $down_id = $objQuery->get($table, $colname, $where, array($downrank));
962            // ランク入れ替えの実行
963            $sqlup = "UPDATE $table SET rank = ?, update_date = Now() WHERE $colname = ?";
964            $objQuery->exec($sqlup, array($rank - 1, $id));
965            $objQuery->exec($sqlup, array($rank, $down_id));
966        }
967        $objQuery->commit();
968    }
969
970    /**
971     * 指定順位へ移動する.
972     *
973     * @param string $tableName テーブル名
974     * @param string $keyIdColumn キーを保持するカラム名
975     * @param string|integer $keyId キーの値
976     * @param integer $pos 指定順位
977     * @param string $where SQL の AND 条件である WHERE 句
978     * @return void
979     */
980    function sfMoveRank($tableName, $keyIdColumn, $keyId, $pos, $where = "") {
981        $objQuery = new SC_Query();
982        $objQuery->begin();
983
984        // 自身のランクを取得する
985        $rank = $objQuery->get($tableName, "rank", "$keyIdColumn = ?", array($keyId));
986        $max = $objQuery->max($tableName, "rank", $where);
987
988        // 値の調整(逆順)
989        if($pos > $max) {
990            $position = 1;
991        } else if($pos < 1) {
992            $position = $max;
993        } else {
994            $position = $max - $pos + 1;
995        }
996
997        if( $position > $rank ) $term = "rank - 1"; //入れ替え先の順位が入れ換え元の順位より大きい場合
998        if( $position < $rank ) $term = "rank + 1"; //入れ替え先の順位が入れ換え元の順位より小さい場合
999
1000        // 指定した順位の商品から移動させる商品までのrankを1つずらす
1001        $sql = "UPDATE $tableName SET rank = $term, update_date = NOW() WHERE rank BETWEEN ? AND ? AND del_flg = 0";
1002        if($where != "") {
1003            $sql.= " AND $where";
1004        }
1005
1006        if( $position > $rank ) $objQuery->exec( $sql, array( $rank + 1, $position ));
1007        if( $position < $rank ) $objQuery->exec( $sql, array( $position, $rank - 1 ));
1008
1009        // 指定した順位へrankを書き換える。
1010        $sql  = "UPDATE $tableName SET rank = ?, update_date = NOW() WHERE $keyIdColumn = ? AND del_flg = 0 ";
1011        if($where != "") {
1012            $sql.= " AND $where";
1013        }
1014
1015        $objQuery->exec( $sql, array( $position, $keyId ) );
1016        $objQuery->commit();
1017    }
1018
1019    /**
1020     * ランクを含むレコードを削除する.
1021     *
1022     * レコードごと削除する場合は、$deleteをtrueにする
1023     *
1024     * @param string $table テーブル名
1025     * @param string $colname カラム名
1026     * @param string|integer $id テーブルのキー
1027     * @param string $andwhere SQL の AND 条件である WHERE 句
1028     * @param bool $delete レコードごと削除する場合 true,
1029     *                     レコードごと削除しない場合 false
1030     * @return void
1031     */
1032    function sfDeleteRankRecord($table, $colname, $id, $andwhere = "",
1033                                $delete = false) {
1034        $objQuery = new SC_Query();
1035        $objQuery->begin();
1036        // 削除レコードのランクを取得する。
1037        $where = "$colname = ?";
1038        if($andwhere != "") {
1039            $where.= " AND $andwhere";
1040        }
1041        $rank = $objQuery->get($table, "rank", $where, array($id));
1042
1043        if(!$delete) {
1044            // ランクを最下位にする、DELフラグON
1045            $sqlup = "UPDATE $table SET rank = 0, del_flg = 1, update_date = Now() ";
1046            $sqlup.= "WHERE $colname = ?";
1047            // UPDATEの実行
1048            $objQuery->exec($sqlup, array($id));
1049        } else {
1050            $objQuery->delete($table, "$colname = ?", array($id));
1051        }
1052
1053        // 追加レコードのランクより上のレコードを一つずらす。
1054        $where = "rank > ?";
1055        if($andwhere != "") {
1056            $where.= " AND $andwhere";
1057        }
1058        $sqlup = "UPDATE $table SET rank = (rank - 1) WHERE $where";
1059        $objQuery->exec($sqlup, array($rank));
1060        $objQuery->commit();
1061    }
1062
1063    /**
1064     * 親IDの配列を元に特定のカラムを取得する.
1065     *
1066     * @param SC_Query $objQuery SC_Query インスタンス
1067     * @param string $table テーブル名
1068     * @param string $id_name ID名
1069     * @param string $col_name カラム名
1070     * @param array $arrId IDの配列
1071     * @return array 特定のカラムの配列
1072     */
1073    function sfGetParentsCol($objQuery, $table, $id_name, $col_name, $arrId ) {
1074        $col = $col_name;
1075        $len = count($arrId);
1076        $where = "";
1077
1078        for($cnt = 0; $cnt < $len; $cnt++) {
1079            if($where == "") {
1080                $where = "$id_name = ?";
1081            } else {
1082                $where.= " OR $id_name = ?";
1083            }
1084        }
1085
1086        $objQuery->setorder("level");
1087        $arrRet = $objQuery->select($col, $table, $where, $arrId);
1088        return $arrRet;
1089    }
1090
1091    /**
1092     * カテゴリ変更時の移動処理を行う.
1093     *
1094     * @param SC_Query $objQuery SC_Query インスタンス
1095     * @param string $table テーブル名
1096     * @param string $id_name ID名
1097     * @param string $cat_name カテゴリ名
1098     * @param integer $old_catid 旧カテゴリID
1099     * @param integer $new_catid 新カテゴリID
1100     * @param integer $id ID
1101     * @return void
1102     */
1103    function sfMoveCatRank($objQuery, $table, $id_name, $cat_name, $old_catid, $new_catid, $id) {
1104        if ($old_catid == $new_catid) {
1105            return;
1106        }
1107        // 旧カテゴリでのランク削除処理
1108        // 移動レコードのランクを取得する。
1109        $where = "$id_name = ?";
1110        $rank = $objQuery->get($table, "rank", $where, array($id));
1111        // 削除レコードのランクより上のレコードを一つ下にずらす。
1112        $where = "rank > ? AND $cat_name = ?";
1113        $sqlup = "UPDATE $table SET rank = (rank - 1) WHERE $where";
1114        $objQuery->exec($sqlup, array($rank, $old_catid));
1115        // 新カテゴリでの登録処理
1116        // 新カテゴリの最大ランクを取得する。
1117        $max_rank = $objQuery->max($table, "rank", "$cat_name = ?", array($new_catid)) + 1;
1118        $where = "$id_name = ?";
1119        $sqlup = "UPDATE $table SET rank = ? WHERE $where";
1120        $objQuery->exec($sqlup, array($max_rank, $id));
1121    }
1122
1123    /**
1124     * レコードの存在チェックを行う.
1125     *
1126     * @param string $table テーブル名
1127     * @param string $col カラム名
1128     * @param array $arrval 要素の配列
1129     * @param array $addwhere SQL の AND 条件である WHERE 句
1130     * @return bool レコードが存在する場合 true
1131     */
1132    function sfIsRecord($table, $col, $arrval, $addwhere = "") {
1133        $objQuery = new SC_Query();
1134        $arrCol = split("[, ]", $col);
1135
1136        $where = "del_flg = 0";
1137
1138        if($addwhere != "") {
1139            $where.= " AND $addwhere";
1140        }
1141
1142        foreach($arrCol as $val) {
1143            if($val != "") {
1144                if($where == "") {
1145                    $where = "$val = ?";
1146                } else {
1147                    $where.= " AND $val = ?";
1148                }
1149            }
1150        }
1151        $ret = $objQuery->get($table, $col, $where, $arrval);
1152
1153        if($ret != "") {
1154            return true;
1155        }
1156        return false;
1157    }
1158
1159}
1160?>
Note: See TracBrowser for help on using the repository browser.