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

Revision 15567, 42.5 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     * @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 array $array パラメータの配列
429     * @param array $arrRegistColumn 登録するカラムの配列
430     * @return void
431     */
432    function sfEditCustomerData($array, $arrRegistColumn) {
433        $objQuery = new SC_Query();
434
435        foreach ($arrRegistColumn as $data) {
436            if ($data["column"] != "password") {
437                if($array[ $data['column'] ] != "") {
438                    $arrRegist[ $data["column"] ] = $array[ $data["column"] ];
439                } else {
440                    $arrRegist[ $data['column'] ] = NULL;
441                }
442            }
443        }
444        if (strlen($array["year"]) > 0 && strlen($array["month"]) > 0 && strlen($array["day"]) > 0) {
445            $arrRegist["birth"] = $array["year"] ."/". $array["month"] ."/". $array["day"] ." 00:00:00";
446        } else {
447            $arrRegist["birth"] = NULL;
448        }
449
450        //-- パスワードの更新がある場合は暗号化。(更新がない場合はUPDATE文を構成しない)
451        if ($array["password"] != DEFAULT_PASSWORD) $arrRegist["password"] = sha1($array["password"] . ":" . AUTH_MAGIC);
452        $arrRegist["update_date"] = "NOW()";
453
454        //-- 編集登録実行
455        if (defined('MOBILE_SITE')) {
456            $arrRegist['email_mobile'] = $arrRegist['email'];
457            unset($arrRegist['email']);
458        }
459        $objQuery->begin();
460        $objQuery->update("dtb_customer", $arrRegist, "customer_id = ? ", array($array['customer_id']));
461        $objQuery->commit();
462    }
463
464    /**
465     * カテゴリツリーの取得を行う.
466     *
467     * @param integer $parent_category_id 親カテゴリID
468     * @param bool $count_check 登録商品数のチェックを行う場合 true
469     * @return array カテゴリツリーの配列
470     */
471    function sfGetCatTree($parent_category_id, $count_check = false) {
472        $objQuery = new SC_Query();
473        $col = "";
474        $col .= " cat.category_id,";
475        $col .= " cat.category_name,";
476        $col .= " cat.parent_category_id,";
477        $col .= " cat.level,";
478        $col .= " cat.rank,";
479        $col .= " cat.creator_id,";
480        $col .= " cat.create_date,";
481        $col .= " cat.update_date,";
482        $col .= " cat.del_flg, ";
483        $col .= " ttl.product_count";
484        $from = "dtb_category as cat left join dtb_category_total_count as ttl on ttl.category_id = cat.category_id";
485        // 登録商品数のチェック
486        if($count_check) {
487            $where = "del_flg = 0 AND product_count > 0";
488        } else {
489            $where = "del_flg = 0";
490        }
491        $objQuery->setoption("ORDER BY rank DESC");
492        $arrRet = $objQuery->select($col, $from, $where);
493
494        $arrParentID = $this->sfGetParents($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $parent_category_id);
495
496        foreach($arrRet as $key => $array) {
497            foreach($arrParentID as $val) {
498                if($array['category_id'] == $val) {
499                    $arrRet[$key]['display'] = 1;
500                    break;
501                }
502            }
503        }
504
505        return $arrRet;
506    }
507
508    /**
509     * 親カテゴリーを連結した文字列を取得する.
510     *
511     * @param integer $category_id カテゴリID
512     * @return string 親カテゴリーを連結した文字列
513     */
514    function sfGetCatCombName($category_id){
515        // 商品が属するカテゴリIDを縦に取得
516        $objQuery = new SC_Query();
517        $arrCatID = sfGetParents($objQuery, "dtb_category", "parent_category_id", "category_id", $category_id);
518        $ConbName = "";
519
520        // カテゴリー名称を取得する
521        foreach($arrCatID as $key => $val){
522            $sql = "SELECT category_name FROM dtb_category WHERE category_id = ?";
523            $arrVal = array($val);
524            $CatName = $objQuery->getOne($sql,$arrVal);
525            $ConbName .= $CatName . ' | ';
526        }
527        // 最後の | をカットする
528        $ConbName = substr_replace($ConbName, "", strlen($ConbName) - 2, 2);
529
530        return $ConbName;
531    }
532
533    /**
534     * 指定したカテゴリーIDの大カテゴリーを取得する.
535     *
536     * @param integer $category_id カテゴリID
537     * @return array 指定したカテゴリーIDの大カテゴリー
538     */
539    function sfGetFirstCat($category_id){
540        // 商品が属するカテゴリIDを縦に取得
541        $objQuery = new SC_Query();
542        $arrRet = array();
543        $arrCatID = $this->sfGetParents($objQuery, "dtb_category", "parent_category_id", "category_id", $category_id);
544        $arrRet['id'] = $arrCatID[0];
545
546        // カテゴリー名称を取得する
547        $sql = "SELECT category_name FROM dtb_category WHERE category_id = ?";
548        $arrVal = array($arrRet['id']);
549        $arrRet['name'] = $objQuery->getOne($sql,$arrVal);
550
551        return $arrRet;
552    }
553
554    /**
555     * カテゴリツリーの取得を行う.
556     *
557     * $products_check:true商品登録済みのものだけ取得する
558     *
559     * @param string $addwhere 追加する WHERE 句
560     * @param bool $products_check 商品の存在するカテゴリのみ取得する場合 true
561     * @param string $head カテゴリ名のプレフィックス文字列
562     * @return array カテゴリツリーの配列
563     */
564    function sfGetCategoryList($addwhere = "", $products_check = false, $head = CATEGORY_HEAD) {
565        $objQuery = new SC_Query();
566        $where = "del_flg = 0";
567
568        if($addwhere != "") {
569            $where.= " AND $addwhere";
570        }
571
572        $objQuery->setoption("ORDER BY rank DESC");
573
574        if($products_check) {
575            $col = "T1.category_id, category_name, level";
576            $from = "dtb_category AS T1 LEFT JOIN dtb_category_total_count AS T2 ON T1.category_id = T2.category_id";
577            $where .= " AND product_count > 0";
578        } else {
579            $col = "category_id, category_name, level";
580            $from = "dtb_category";
581        }
582
583        $arrRet = $objQuery->select($col, $from, $where);
584
585        $max = count($arrRet);
586        for($cnt = 0; $cnt < $max; $cnt++) {
587            $id = $arrRet[$cnt]['category_id'];
588            $name = $arrRet[$cnt]['category_name'];
589            $arrList[$id] = "";
590            /*
591            for($n = 1; $n < $arrRet[$cnt]['level']; $n++) {
592                $arrList[$id].= " ";
593            }
594            */
595            for($cat_cnt = 0; $cat_cnt < $arrRet[$cnt]['level']; $cat_cnt++) {
596                $arrList[$id].= $head;
597            }
598            $arrList[$id].= $name;
599        }
600        return $arrList;
601    }
602
603    /**
604     * カテゴリーツリーの取得を行う.
605     *
606     * 親カテゴリの Value=0 を対象とする
607     *
608     * @param bool $parent_zero 親カテゴリの Value=0 の場合 true
609     * @return array カテゴリツリーの配列
610     */
611    function sfGetLevelCatList($parent_zero = true) {
612        $objQuery = new SC_Query();
613        $col = "category_id, category_name, level";
614        $where = "del_flg = 0";
615        $objQuery->setoption("ORDER BY rank DESC");
616        $arrRet = $objQuery->select($col, "dtb_category", $where);
617        $max = count($arrRet);
618
619        for($cnt = 0; $cnt < $max; $cnt++) {
620            if($parent_zero) {
621                if($arrRet[$cnt]['level'] == LEVEL_MAX) {
622                    $arrValue[$cnt] = $arrRet[$cnt]['category_id'];
623                } else {
624                    $arrValue[$cnt] = "";
625                }
626            } else {
627                $arrValue[$cnt] = $arrRet[$cnt]['category_id'];
628            }
629
630            $arrOutput[$cnt] = "";
631            /*
632            for($n = 1; $n < $arrRet[$cnt]['level']; $n++) {
633                $arrOutput[$cnt].= " ";
634            }
635            */
636            for($cat_cnt = 0; $cat_cnt < $arrRet[$cnt]['level']; $cat_cnt++) {
637                $arrOutput[$cnt].= CATEGORY_HEAD;
638            }
639            $arrOutput[$cnt].= $arrRet[$cnt]['category_name'];
640        }
641        return array($arrValue, $arrOutput);
642    }
643
644    /**
645     * 選択中のカテゴリを取得する.
646     *
647     * @param integer $product_id プロダクトID
648     * @param integer $category_id カテゴリID
649     * @return integer 選択中のカテゴリID
650     *
651     */
652    function sfGetCategoryId($product_id, $category_id) {
653
654        if(!$this->g_category_on)   {
655            $this->g_category_on = true;
656            $category_id = (int) $category_id;
657            $product_id = (int) $product_id;
658            if(SC_Utils_Ex::sfIsInt($category_id) && $this->sfIsRecord("dtb_category","category_id", $category_id)) {
659                $this->g_category_id = $category_id;
660            } else if (SC_Utils_Ex::sfIsInt($product_id) && $this->sfIsRecord("dtb_products","product_id", $product_id, "status = 1")) {
661                $objQuery = new SC_Query();
662                $where = "product_id = ?";
663                $category_id = $objQuery->get("dtb_products", "category_id", $where, array($product_id));
664                $this->g_category_id = $category_id;
665            } else {
666                // 不正な場合は、0を返す。
667                $this->g_category_id = 0;
668            }
669        }
670        return $this->g_category_id;
671    }
672
673    /**
674     * カテゴリ数の登録を行う.
675     *
676     * @param SC_Query $objQuery SC_Query インスタンス
677     * @return void
678     */
679    function sfCategory_Count($objQuery){
680        $sql = "";
681
682        //テーブル内容の削除
683        $objQuery->query("DELETE FROM dtb_category_count");
684        $objQuery->query("DELETE FROM dtb_category_total_count");
685
686        //各カテゴリ内の商品数を数えて格納
687        $sql = " INSERT INTO dtb_category_count(category_id, product_count, create_date) ";
688        $sql .= " SELECT T1.category_id, count(T2.category_id), now() FROM dtb_category AS T1 LEFT JOIN dtb_products AS T2 ";
689        $sql .= " ON T1.category_id = T2.category_id  ";
690        $sql .= " WHERE T2.del_flg = 0 AND T2.status = 1 ";
691        $sql .= " GROUP BY T1.category_id, T2.category_id ";
692        $objQuery->query($sql);
693
694        //子カテゴリ内の商品数を集計する
695        $arrCat = $objQuery->getAll("SELECT * FROM dtb_category");
696
697        $sql = "";
698        foreach($arrCat as $key => $val){
699
700            // 子ID一覧を取得
701            $arrRet = $this->sfGetChildrenArray('dtb_category', 'parent_category_id', 'category_id', $val['category_id']);
702            $line = SC_Utils_Ex::sfGetCommaList($arrRet);
703
704            $sql = " INSERT INTO dtb_category_total_count(category_id, product_count, create_date) ";
705            $sql .= " SELECT ?, SUM(product_count), now() FROM dtb_category_count ";
706            $sql .= " WHERE category_id IN (" . $line . ")";
707
708            $objQuery->query($sql, array($val['category_id']));
709        }
710    }
711
712    /**
713     * 子IDの配列を返す.
714     *
715     * @param string $table テーブル名
716     * @param string $pid_name 親ID名
717     * @param string $id_name ID名
718     * @param integer $id ID
719     * @param array 子ID の配列
720     */
721    function sfGetChildsID($table, $pid_name, $id_name, $id) {
722        $arrRet = $this->sfGetChildrenArray($table, $pid_name, $id_name, $id);
723        return $arrRet;
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     * @return array 子IDの配列
734     */
735    function sfGetChildrenArray($table, $pid_name, $id_name, $id) {
736        $objQuery = new SC_Query();
737        $col = $pid_name . "," . $id_name;
738         $arrData = $objQuery->select($col, $table);
739
740        $arrPID = array();
741        $arrPID[] = $id;
742        $arrChildren = array();
743        $arrChildren[] = $id;
744
745        $arrRet = $this->sfGetChildrenArraySub($arrData, $pid_name, $id_name, $arrPID);
746
747        while(count($arrRet) > 0) {
748            $arrChildren = array_merge($arrChildren, $arrRet);
749            $arrRet = $this->sfGetChildrenArraySub($arrData, $pid_name, $id_name, $arrRet);
750        }
751
752        return $arrChildren;
753    }
754
755    /**
756     * 親ID直下の子IDをすべて取得する.
757     *
758     * @param array $arrData 親カテゴリの配列
759     * @param string $pid_name 親ID名
760     * @param string $id_name ID名
761     * @param array $arrPID 親IDの配列
762     * @return array 子IDの配列
763     */
764    function sfGetChildrenArraySub($arrData, $pid_name, $id_name, $arrPID) {
765        $arrChildren = array();
766        $max = count($arrData);
767
768        for($i = 0; $i < $max; $i++) {
769            foreach($arrPID as $val) {
770                if($arrData[$i][$pid_name] == $val) {
771                    $arrChildren[] = $arrData[$i][$id_name];
772                }
773            }
774        }
775        return $arrChildren;
776    }
777
778    /**
779     * 所属するすべての階層の親IDを配列で返す.
780     *
781     * @param SC_Query $objQuery SC_Query インスタンス
782     * @param string $table テーブル名
783     * @param string $pid_name 親ID名
784     * @param string $id_name ID名
785     * @param integer $id ID
786     * @return array 親IDの配列
787     */
788    function sfGetParents($objQuery, $table, $pid_name, $id_name, $id) {
789        $arrRet = $this->sfGetParentsArray($table, $pid_name, $id_name, $id);
790        // 配列の先頭1つを削除する。
791        array_shift($arrRet);
792        return $arrRet;
793    }
794
795    /**
796     * 階層構造のテーブルから親ID配列を取得する.
797     *
798     * @param string $table テーブル名
799     * @param string $pid_name 親ID名
800     * @param string $id_name ID名
801     * @param integer $id ID
802     * @return array 親IDの配列
803     */
804    function sfGetParentsArray($table, $pid_name, $id_name, $id) {
805        $objQuery = new SC_Query();
806        $col = $pid_name . "," . $id_name;
807         $arrData = $objQuery->select($col, $table);
808
809        $arrParents = array();
810        $arrParents[] = $id;
811        $child = $id;
812
813        $ret = SC_Utils::sfGetParentsArraySub($arrData, $pid_name, $id_name, $child);
814
815        while($ret != "") {
816            $arrParents[] = $ret;
817            $ret = SC_Utils::sfGetParentsArraySub($arrData, $pid_name, $id_name, $ret);
818        }
819
820        $arrParents = array_reverse($arrParents);
821
822        return $arrParents;
823    }
824
825    /**
826     * カテゴリから商品を検索する場合のWHERE文と値を返す.
827     *
828     * @param integer $category_id カテゴリID
829     * @return array 商品を検索する場合の配列
830     */
831    function sfGetCatWhere($category_id) {
832        // 子カテゴリIDの取得
833        $arrRet = $this->sfGetChildsID("dtb_category", "parent_category_id", "category_id", $category_id);
834        $tmp_where = "";
835        foreach ($arrRet as $val) {
836            if($tmp_where == "") {
837                $tmp_where.= " category_id IN ( ?";
838            } else {
839                $tmp_where.= ",? ";
840            }
841            $arrval[] = $val;
842        }
843        $tmp_where.= " ) ";
844        return array($tmp_where, $arrval);
845    }
846
847    /**
848     * 受注一時テーブルから情報を取得する.
849     *
850     * @param integer $order_temp_id 受注一時ID
851     * @return array 受注一時情報の配列
852     */
853    function sfGetOrderTemp($order_temp_id) {
854        $objQuery = new SC_Query();
855        $where = "order_temp_id = ?";
856        $arrRet = $objQuery->select("*", "dtb_order_temp", $where, array($order_temp_id));
857        return $arrRet[0];
858    }
859
860    /**
861     * SELECTボックス用リストを作成する.
862     *
863     * @param string $table テーブル名
864     * @param string $keyname プライマリーキーのカラム名
865     * @param string $valname データ内容のカラム名
866     * @return array SELECT ボックス用リストの配列
867     */
868    function sfGetIDValueList($table, $keyname, $valname) {
869        $objQuery = new SC_Query();
870        $col = "$keyname, $valname";
871        $objQuery->setwhere("del_flg = 0");
872        $objQuery->setorder("rank DESC");
873        $arrList = $objQuery->select($col, $table);
874        $count = count($arrList);
875        for($cnt = 0; $cnt < $count; $cnt++) {
876            $key = $arrList[$cnt][$keyname];
877            $val = $arrList[$cnt][$valname];
878            $arrRet[$key] = $val;
879        }
880        return $arrRet;
881    }
882
883    /**
884     * ランキングを上げる.
885     *
886     * @param string $table テーブル名
887     * @param string $colname カラム名
888     * @param string|integer $id テーブルのキー
889     * @param string $andwhere SQL の AND 条件である WHERE 句
890     * @return void
891     */
892    function sfRankUp($table, $colname, $id, $andwhere = "") {
893        $objQuery = new SC_Query();
894        $objQuery->begin();
895        $where = "$colname = ?";
896        if($andwhere != "") {
897            $where.= " AND $andwhere";
898        }
899        // 対象項目のランクを取得
900        $rank = $objQuery->get($table, "rank", $where, array($id));
901        // ランクの最大値を取得
902        $maxrank = $objQuery->max($table, "rank", $andwhere);
903        // ランクが最大値よりも小さい場合に実行する。
904        if($rank < $maxrank) {
905            // ランクが一つ上のIDを取得する。
906            $where = "rank = ?";
907            if($andwhere != "") {
908                $where.= " AND $andwhere";
909            }
910            $uprank = $rank + 1;
911            $up_id = $objQuery->get($table, $colname, $where, array($uprank));
912            // ランク入れ替えの実行
913            $sqlup = "UPDATE $table SET rank = ?, update_date = Now() WHERE $colname = ?";
914            $objQuery->exec($sqlup, array($rank + 1, $id));
915            $objQuery->exec($sqlup, array($rank, $up_id));
916        }
917        $objQuery->commit();
918    }
919
920    /**
921     * ランキングを下げる.
922     *
923     * @param string $table テーブル名
924     * @param string $colname カラム名
925     * @param string|integer $id テーブルのキー
926     * @param string $andwhere SQL の AND 条件である WHERE 句
927     * @return void
928     */
929    function sfRankDown($table, $colname, $id, $andwhere = "") {
930        $objQuery = new SC_Query();
931        $objQuery->begin();
932        $where = "$colname = ?";
933        if($andwhere != "") {
934            $where.= " AND $andwhere";
935        }
936        // 対象項目のランクを取得
937        $rank = $objQuery->get($table, "rank", $where, array($id));
938
939        // ランクが1(最小値)よりも大きい場合に実行する。
940        if($rank > 1) {
941            // ランクが一つ下のIDを取得する。
942            $where = "rank = ?";
943            if($andwhere != "") {
944                $where.= " AND $andwhere";
945            }
946            $downrank = $rank - 1;
947            $down_id = $objQuery->get($table, $colname, $where, array($downrank));
948            // ランク入れ替えの実行
949            $sqlup = "UPDATE $table SET rank = ?, update_date = Now() WHERE $colname = ?";
950            $objQuery->exec($sqlup, array($rank - 1, $id));
951            $objQuery->exec($sqlup, array($rank, $down_id));
952        }
953        $objQuery->commit();
954    }
955
956    /**
957     * 指定順位へ移動する.
958     *
959     * @param string $tableName テーブル名
960     * @param string $keyIdColumn キーを保持するカラム名
961     * @param string|integer $keyId キーの値
962     * @param integer $pos 指定順位
963     * @param string $where SQL の AND 条件である WHERE 句
964     * @return void
965     */
966    function sfMoveRank($tableName, $keyIdColumn, $keyId, $pos, $where = "") {
967        $objQuery = new SC_Query();
968        $objQuery->begin();
969
970        // 自身のランクを取得する
971        $rank = $objQuery->get($tableName, "rank", "$keyIdColumn = ?", array($keyId));
972        $max = $objQuery->max($tableName, "rank", $where);
973
974        // 値の調整(逆順)
975        if($pos > $max) {
976            $position = 1;
977        } else if($pos < 1) {
978            $position = $max;
979        } else {
980            $position = $max - $pos + 1;
981        }
982
983        if( $position > $rank ) $term = "rank - 1"; //入れ替え先の順位が入れ換え元の順位より大きい場合
984        if( $position < $rank ) $term = "rank + 1"; //入れ替え先の順位が入れ換え元の順位より小さい場合
985
986        // 指定した順位の商品から移動させる商品までのrankを1つずらす
987        $sql = "UPDATE $tableName SET rank = $term, update_date = NOW() WHERE rank BETWEEN ? AND ? AND del_flg = 0";
988        if($where != "") {
989            $sql.= " AND $where";
990        }
991
992        if( $position > $rank ) $objQuery->exec( $sql, array( $rank + 1, $position ));
993        if( $position < $rank ) $objQuery->exec( $sql, array( $position, $rank - 1 ));
994
995        // 指定した順位へrankを書き換える。
996        $sql  = "UPDATE $tableName SET rank = ?, update_date = NOW() WHERE $keyIdColumn = ? AND del_flg = 0 ";
997        if($where != "") {
998            $sql.= " AND $where";
999        }
1000
1001        $objQuery->exec( $sql, array( $position, $keyId ) );
1002        $objQuery->commit();
1003    }
1004
1005    /**
1006     * ランクを含むレコードを削除する.
1007     *
1008     * レコードごと削除する場合は、$deleteをtrueにする
1009     *
1010     * @param string $table テーブル名
1011     * @param string $colname カラム名
1012     * @param string|integer $id テーブルのキー
1013     * @param string $andwhere SQL の AND 条件である WHERE 句
1014     * @param bool $delete レコードごと削除する場合 true,
1015     *                     レコードごと削除しない場合 false
1016     * @return void
1017     */
1018    function sfDeleteRankRecord($table, $colname, $id, $andwhere = "",
1019                                $delete = false) {
1020        $objQuery = new SC_Query();
1021        $objQuery->begin();
1022        // 削除レコードのランクを取得する。
1023        $where = "$colname = ?";
1024        if($andwhere != "") {
1025            $where.= " AND $andwhere";
1026        }
1027        $rank = $objQuery->get($table, "rank", $where, array($id));
1028
1029        if(!$delete) {
1030            // ランクを最下位にする、DELフラグON
1031            $sqlup = "UPDATE $table SET rank = 0, del_flg = 1, update_date = Now() ";
1032            $sqlup.= "WHERE $colname = ?";
1033            // UPDATEの実行
1034            $objQuery->exec($sqlup, array($id));
1035        } else {
1036            $objQuery->delete($table, "$colname = ?", array($id));
1037        }
1038
1039        // 追加レコードのランクより上のレコードを一つずらす。
1040        $where = "rank > ?";
1041        if($andwhere != "") {
1042            $where.= " AND $andwhere";
1043        }
1044        $sqlup = "UPDATE $table SET rank = (rank - 1) WHERE $where";
1045        $objQuery->exec($sqlup, array($rank));
1046        $objQuery->commit();
1047    }
1048
1049    /**
1050     * 親IDの配列を元に特定のカラムを取得する.
1051     *
1052     * @param SC_Query $objQuery SC_Query インスタンス
1053     * @param string $table テーブル名
1054     * @param string $id_name ID名
1055     * @param string $col_name カラム名
1056     * @param array $arrId IDの配列
1057     * @return array 特定のカラムの配列
1058     */
1059    function sfGetParentsCol($objQuery, $table, $id_name, $col_name, $arrId ) {
1060        $col = $col_name;
1061        $len = count($arrId);
1062        $where = "";
1063
1064        for($cnt = 0; $cnt < $len; $cnt++) {
1065            if($where == "") {
1066                $where = "$id_name = ?";
1067            } else {
1068                $where.= " OR $id_name = ?";
1069            }
1070        }
1071
1072        $objQuery->setorder("level");
1073        $arrRet = $objQuery->select($col, $table, $where, $arrId);
1074        return $arrRet;
1075    }
1076
1077    /**
1078     * カテゴリ変更時の移動処理を行う.
1079     *
1080     * @param SC_Query $objQuery SC_Query インスタンス
1081     * @param string $table テーブル名
1082     * @param string $id_name ID名
1083     * @param string $cat_name カテゴリ名
1084     * @param integer $old_catid 旧カテゴリID
1085     * @param integer $new_catid 新カテゴリID
1086     * @param integer $id ID
1087     * @return void
1088     */
1089    function sfMoveCatRank($objQuery, $table, $id_name, $cat_name, $old_catid, $new_catid, $id) {
1090        if ($old_catid == $new_catid) {
1091            return;
1092        }
1093        // 旧カテゴリでのランク削除処理
1094        // 移動レコードのランクを取得する。
1095        $where = "$id_name = ?";
1096        $rank = $objQuery->get($table, "rank", $where, array($id));
1097        // 削除レコードのランクより上のレコードを一つ下にずらす。
1098        $where = "rank > ? AND $cat_name = ?";
1099        $sqlup = "UPDATE $table SET rank = (rank - 1) WHERE $where";
1100        $objQuery->exec($sqlup, array($rank, $old_catid));
1101        // 新カテゴリでの登録処理
1102        // 新カテゴリの最大ランクを取得する。
1103        $max_rank = $objQuery->max($table, "rank", "$cat_name = ?", array($new_catid)) + 1;
1104        $where = "$id_name = ?";
1105        $sqlup = "UPDATE $table SET rank = ? WHERE $where";
1106        $objQuery->exec($sqlup, array($max_rank, $id));
1107    }
1108
1109    /**
1110     * レコードの存在チェックを行う.
1111     *
1112     * @param string $table テーブル名
1113     * @param string $col カラム名
1114     * @param array $arrval 要素の配列
1115     * @param array $addwhere SQL の AND 条件である WHERE 句
1116     * @return bool レコードが存在する場合 true
1117     */
1118    function sfIsRecord($table, $col, $arrval, $addwhere = "") {
1119        $objQuery = new SC_Query();
1120        $arrCol = split("[, ]", $col);
1121
1122        $where = "del_flg = 0";
1123
1124        if($addwhere != "") {
1125            $where.= " AND $addwhere";
1126        }
1127
1128        foreach($arrCol as $val) {
1129            if($val != "") {
1130                if($where == "") {
1131                    $where = "$val = ?";
1132                } else {
1133                    $where.= " AND $val = ?";
1134                }
1135            }
1136        }
1137        $ret = $objQuery->get($table, $col, $where, $arrval);
1138
1139        if($ret != "") {
1140            return true;
1141        }
1142        return false;
1143    }
1144
1145}
1146?>
Note: See TracBrowser for help on using the repository browser.