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

Revision 15359, 34.2 KB checked in by nanasess, 17 years ago (diff)

リファクタリング

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