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

Revision 15343, 32.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        // 規格名一覧
256        $arrClassName = SC_Utils_Ex::sfGetIDValueList("dtb_class", "class_id", "name");
257        // 規格分類名一覧
258        $arrClassCatName = SC_Utils_Ex::sfGetIDValueList("dtb_classcategory", "classcategory_id", "name");
259
260        $objPage->tpl_total_pretax = 0;     // 費用合計(税込み)
261        $objPage->tpl_total_tax = 0;        // 消費税合計
262        $objPage->tpl_total_point = 0;      // ポイント合計
263
264        // カート内情報の取得
265        $arrCart = $objCartSess->getCartList();
266        $max = count($arrCart);
267        $cnt = 0;
268
269        for ($i = 0; $i < $max; $i++) {
270            // 商品規格情報の取得
271            $arrData = $this->sfGetProductsClass($arrCart[$i]['id']);
272            $limit = "";
273            // DBに存在する商品
274            if (count($arrData) > 0) {
275
276                // 購入制限数を求める。
277                if ($arrData['stock_unlimited'] != '1' && $arrData['sale_unlimited'] != '1') {
278                    if($arrData['sale_limit'] < $arrData['stock']) {
279                        $limit = $arrData['sale_limit'];
280                    } else {
281                        $limit = $arrData['stock'];
282                    }
283                } else {
284                    if ($arrData['sale_unlimited'] != '1') {
285                        $limit = $arrData['sale_limit'];
286                    }
287                    if ($arrData['stock_unlimited'] != '1') {
288                        $limit = $arrData['stock'];
289                    }
290                }
291
292                if($limit != "" && $limit < $arrCart[$i]['quantity']) {
293                    // カート内商品数を制限に合わせる
294                    $objCartSess->setProductValue($arrCart[$i]['id'], 'quantity', $limit);
295                    $quantity = $limit;
296                    $objPage->tpl_message = "※「" . $arrData['name'] . "」は販売制限しております、一度にこれ以上の購入はできません。";
297                } else {
298                    $quantity = $arrCart[$i]['quantity'];
299                }
300
301                $objPage->arrProductsClass[$cnt] = $arrData;
302                $objPage->arrProductsClass[$cnt]['quantity'] = $quantity;
303                $objPage->arrProductsClass[$cnt]['cart_no'] = $arrCart[$i]['cart_no'];
304                $objPage->arrProductsClass[$cnt]['class_name1'] = $arrClassName[$arrData['class_id1']];
305                $objPage->arrProductsClass[$cnt]['class_name2'] = $arrClassName[$arrData['class_id2']];
306                $objPage->arrProductsClass[$cnt]['classcategory_name1'] = $arrClassCatName[$arrData['classcategory_id1']];
307                $objPage->arrProductsClass[$cnt]['classcategory_name2'] = $arrClassCatName[$arrData['classcategory_id2']];
308
309                // 画像サイズ
310                list($image_width, $image_height) = getimagesize(IMAGE_SAVE_DIR . basename($objPage->arrProductsClass[$cnt]["main_image"]));
311                $objPage->arrProductsClass[$cnt]["tpl_image_width"] = $image_width + 60;
312                $objPage->arrProductsClass[$cnt]["tpl_image_height"] = $image_height + 80;
313
314                // 価格の登録
315                if ($arrData['price02'] != "") {
316                    $objCartSess->setProductValue($arrCart[$i]['id'], 'price', $arrData['price02']);
317                    $objPage->arrProductsClass[$cnt]['uniq_price'] = $arrData['price02'];
318                } else {
319                    $objCartSess->setProductValue($arrCart[$i]['id'], 'price', $arrData['price01']);
320                    $objPage->arrProductsClass[$cnt]['uniq_price'] = $arrData['price01'];
321                }
322                // ポイント付与率の登録
323                $objCartSess->setProductValue($arrCart[$i]['id'], 'point_rate', $arrData['point_rate']);
324                // 商品ごとの合計金額
325                $objPage->arrProductsClass[$cnt]['total_pretax'] = $objCartSess->getProductTotal($arrInfo, $arrCart[$i]['id']);
326                // 送料の合計を計算する
327                $objPage->tpl_total_deliv_fee+= ($arrData['deliv_fee'] * $arrCart[$i]['quantity']);
328                $cnt++;
329            } else {
330                // DBに商品が見つからない場合はカート商品の削除
331                $objCartSess->delProductKey('id', $arrCart[$i]['id']);
332            }
333        }
334
335        // 全商品合計金額(税込み)
336        $objPage->tpl_total_pretax = $objCartSess->getAllProductsTotal($arrInfo);
337        // 全商品合計消費税
338        $objPage->tpl_total_tax = $objCartSess->getAllProductsTax($arrInfo);
339        // 全商品合計ポイント
340        $objPage->tpl_total_point = $objCartSess->getAllProductsPoint();
341
342        return $objPage;
343    }
344
345    /**
346     * 受注一時テーブルへの書き込み処理を行う.
347     *
348     * @param string $uniqid ユニークID
349     * @param array $sqlval SQLの値の配列
350     * @return void
351     */
352    function sfRegistTempOrder($uniqid, $sqlval) {
353        if($uniqid != "") {
354            // 既存データのチェック
355            $objQuery = new SC_Query();
356            $where = "order_temp_id = ?";
357            $cnt = $objQuery->count("dtb_order_temp", $where, array($uniqid));
358            // 既存データがない場合
359            if ($cnt == 0) {
360                // 初回書き込み時に会員の登録済み情報を取り込む
361                $sqlval = $this->sfGetCustomerSqlVal($uniqid, $sqlval);
362                $sqlval['create_date'] = "now()";
363                $objQuery->insert("dtb_order_temp", $sqlval);
364            } else {
365                $objQuery->update("dtb_order_temp", $sqlval, $where, array($uniqid));
366            }
367        }
368    }
369
370    /**
371     * 会員情報から SQL文の値を生成する.
372     *
373     * @param string $uniqid ユニークID
374     * @param array $sqlval SQL の値の配列
375     * @return array 会員情報を含んだ SQL の値の配列
376     */
377    function sfGetCustomerSqlVal($uniqid, $sqlval) {
378        $objCustomer = new SC_Customer();
379        // 会員情報登録処理
380        if ($objCustomer->isLoginSuccess()) {
381            // 登録データの作成
382            $sqlval['order_temp_id'] = $uniqid;
383            $sqlval['update_date'] = 'Now()';
384            $sqlval['customer_id'] = $objCustomer->getValue('customer_id');
385            $sqlval['order_name01'] = $objCustomer->getValue('name01');
386            $sqlval['order_name02'] = $objCustomer->getValue('name02');
387            $sqlval['order_kana01'] = $objCustomer->getValue('kana01');
388            $sqlval['order_kana02'] = $objCustomer->getValue('kana02');
389            $sqlval['order_sex'] = $objCustomer->getValue('sex');
390            $sqlval['order_zip01'] = $objCustomer->getValue('zip01');
391            $sqlval['order_zip02'] = $objCustomer->getValue('zip02');
392            $sqlval['order_pref'] = $objCustomer->getValue('pref');
393            $sqlval['order_addr01'] = $objCustomer->getValue('addr01');
394            $sqlval['order_addr02'] = $objCustomer->getValue('addr02');
395            $sqlval['order_tel01'] = $objCustomer->getValue('tel01');
396            $sqlval['order_tel02'] = $objCustomer->getValue('tel02');
397            $sqlval['order_tel03'] = $objCustomer->getValue('tel03');
398            if (defined('MOBILE_SITE')) {
399                $sqlval['order_email'] = $objCustomer->getValue('email_mobile');
400            } else {
401                $sqlval['order_email'] = $objCustomer->getValue('email');
402            }
403            $sqlval['order_job'] = $objCustomer->getValue('job');
404            $sqlval['order_birth'] = $objCustomer->getValue('birth');
405        }
406        return $sqlval;
407    }
408
409    /**
410     * カテゴリツリーの取得を行う.
411     *
412     * $products_check:true商品登録済みのものだけ取得する
413     *
414     * @param string $addwhere 追加する WHERE 句
415     * @param bool $products_check 商品の存在するカテゴリのみ取得する場合 true
416     * @param string $head カテゴリ名のプレフィックス文字列
417     * @return array カテゴリツリーの配列
418     */
419    function sfGetCategoryList($addwhere = "", $products_check = false, $head = CATEGORY_HEAD) {
420        $objQuery = new SC_Query();
421        $where = "del_flg = 0";
422
423        if($addwhere != "") {
424            $where.= " AND $addwhere";
425        }
426
427        $objQuery->setoption("ORDER BY rank DESC");
428
429        if($products_check) {
430            $col = "T1.category_id, category_name, level";
431            $from = "dtb_category AS T1 LEFT JOIN dtb_category_total_count AS T2 ON T1.category_id = T2.category_id";
432            $where .= " AND product_count > 0";
433        } else {
434            $col = "category_id, category_name, level";
435            $from = "dtb_category";
436        }
437
438        $arrRet = $objQuery->select($col, $from, $where);
439
440        $max = count($arrRet);
441        for($cnt = 0; $cnt < $max; $cnt++) {
442            $id = $arrRet[$cnt]['category_id'];
443            $name = $arrRet[$cnt]['category_name'];
444            $arrList[$id] = "";
445            /*
446            for($n = 1; $n < $arrRet[$cnt]['level']; $n++) {
447                $arrList[$id].= " ";
448            }
449            */
450            for($cat_cnt = 0; $cat_cnt < $arrRet[$cnt]['level']; $cat_cnt++) {
451                $arrList[$id].= $head;
452            }
453            $arrList[$id].= $name;
454        }
455        return $arrList;
456    }
457
458    /**
459     * カテゴリーツリーの取得を行う.
460     *
461     * 親カテゴリの Value=0 を対象とする
462     *
463     * @param bool $parent_zero 親カテゴリの Value=0 の場合 true
464     * @return array カテゴリツリーの配列
465     */
466    function sfGetLevelCatList($parent_zero = true) {
467        $objQuery = new SC_Query();
468        $col = "category_id, category_name, level";
469        $where = "del_flg = 0";
470        $objQuery->setoption("ORDER BY rank DESC");
471        $arrRet = $objQuery->select($col, "dtb_category", $where);
472        $max = count($arrRet);
473
474        for($cnt = 0; $cnt < $max; $cnt++) {
475            if($parent_zero) {
476                if($arrRet[$cnt]['level'] == LEVEL_MAX) {
477                    $arrValue[$cnt] = $arrRet[$cnt]['category_id'];
478                } else {
479                    $arrValue[$cnt] = "";
480                }
481            } else {
482                $arrValue[$cnt] = $arrRet[$cnt]['category_id'];
483            }
484
485            $arrOutput[$cnt] = "";
486            /*
487            for($n = 1; $n < $arrRet[$cnt]['level']; $n++) {
488                $arrOutput[$cnt].= " ";
489            }
490            */
491            for($cat_cnt = 0; $cat_cnt < $arrRet[$cnt]['level']; $cat_cnt++) {
492                $arrOutput[$cnt].= CATEGORY_HEAD;
493            }
494            $arrOutput[$cnt].= $arrRet[$cnt]['category_name'];
495        }
496        return array($arrValue, $arrOutput);
497    }
498
499    /**
500     * カテゴリ数の登録を行う.
501     *
502     * @param SC_Query $objQuery SC_Query インスタンス
503     * @return void
504     */
505    function sfCategory_Count($objQuery){
506        $sql = "";
507
508        //テーブル内容の削除
509        $objQuery->query("DELETE FROM dtb_category_count");
510        $objQuery->query("DELETE FROM dtb_category_total_count");
511
512        //各カテゴリ内の商品数を数えて格納
513        $sql = " INSERT INTO dtb_category_count(category_id, product_count, create_date) ";
514        $sql .= " SELECT T1.category_id, count(T2.category_id), now() FROM dtb_category AS T1 LEFT JOIN dtb_products AS T2 ";
515        $sql .= " ON T1.category_id = T2.category_id  ";
516        $sql .= " WHERE T2.del_flg = 0 AND T2.status = 1 ";
517        $sql .= " GROUP BY T1.category_id, T2.category_id ";
518        $objQuery->query($sql);
519
520        //子カテゴリ内の商品数を集計する
521        $arrCat = $objQuery->getAll("SELECT * FROM dtb_category");
522
523        $sql = "";
524        foreach($arrCat as $key => $val){
525
526            // 子ID一覧を取得
527            $arrRet = $this->sfGetChildrenArray('dtb_category', 'parent_category_id', 'category_id', $val['category_id']);
528            $line = SC_Utils_Ex::sfGetCommaList($arrRet);
529
530            $sql = " INSERT INTO dtb_category_total_count(category_id, product_count, create_date) ";
531            $sql .= " SELECT ?, SUM(product_count), now() FROM dtb_category_count ";
532            $sql .= " WHERE category_id IN (" . $line . ")";
533
534            $objQuery->query($sql, array($val['category_id']));
535        }
536    }
537
538    /**
539     * 階層構造のテーブルから子ID配列を取得する.
540     *
541     * @param string $table テーブル名
542     * @param string $pid_name 親ID名
543     * @param string $id_name ID名
544     * @param integer $id ID番号
545     * @return array 子IDの配列
546     */
547    function sfGetChildrenArray($table, $pid_name, $id_name, $id) {
548        $objQuery = new SC_Query();
549        $col = $pid_name . "," . $id_name;
550         $arrData = $objQuery->select($col, $table);
551
552        $arrPID = array();
553        $arrPID[] = $id;
554        $arrChildren = array();
555        $arrChildren[] = $id;
556
557        $arrRet = $this->sfGetChildrenArraySub($arrData, $pid_name, $id_name, $arrPID);
558
559        while(count($arrRet) > 0) {
560            $arrChildren = array_merge($arrChildren, $arrRet);
561            $arrRet = $this->sfGetChildrenArraySub($arrData, $pid_name, $id_name, $arrRet);
562        }
563
564        return $arrChildren;
565    }
566
567    /**
568     * 親ID直下の子IDをすべて取得する.
569     *
570     * @param array $arrData 親カテゴリの配列
571     * @param string $pid_name 親ID名
572     * @param string $id_name ID名
573     * @param array $arrPID 親IDの配列
574     * @return array 子IDの配列
575     */
576    function sfGetChildrenArraySub($arrData, $pid_name, $id_name, $arrPID) {
577        $arrChildren = array();
578        $max = count($arrData);
579
580        for($i = 0; $i < $max; $i++) {
581            foreach($arrPID as $val) {
582                if($arrData[$i][$pid_name] == $val) {
583                    $arrChildren[] = $arrData[$i][$id_name];
584                }
585            }
586        }
587        return $arrChildren;
588    }
589
590    /**
591     * 階層構造のテーブルから親ID配列を取得する.
592     *
593     * @param string $table テーブル名
594     * @param string $pid_name 親ID名
595     * @param string $id_name ID名
596     * @param integer $id ID
597     * @return array 親IDの配列
598     */
599    function sfGetParentsArray($table, $pid_name, $id_name, $id) {
600        $objQuery = new SC_Query();
601        $col = $pid_name . "," . $id_name;
602         $arrData = $objQuery->select($col, $table);
603
604        $arrParents = array();
605        $arrParents[] = $id;
606        $child = $id;
607
608        $ret = SC_Utils::sfGetParentsArraySub($arrData, $pid_name, $id_name, $child);
609
610        while($ret != "") {
611            $arrParents[] = $ret;
612            $ret = SC_Utils::sfGetParentsArraySub($arrData, $pid_name, $id_name, $ret);
613        }
614
615        $arrParents = array_reverse($arrParents);
616
617        return $arrParents;
618    }
619
620    /**
621     * 受注一時テーブルから情報を取得する.
622     *
623     * @param integer $order_temp_id 受注一時ID
624     * @return array 受注一時情報の配列
625     */
626    function sfGetOrderTemp($order_temp_id) {
627        $objQuery = new SC_Query();
628        $where = "order_temp_id = ?";
629        $arrRet = $objQuery->select("*", "dtb_order_temp", $where, array($order_temp_id));
630        return $arrRet[0];
631    }
632
633    /**
634     * SELECTボックス用リストを作成する.
635     *
636     * @param string $table テーブル名
637     * @param string $keyname プライマリーキーのカラム名
638     * @param string $valname データ内容のカラム名
639     * @return array SELECT ボックス用リストの配列
640     */
641    function sfGetIDValueList($table, $keyname, $valname) {
642        $objQuery = new SC_Query();
643        $col = "$keyname, $valname";
644        $objQuery->setwhere("del_flg = 0");
645        $objQuery->setorder("rank DESC");
646        $arrList = $objQuery->select($col, $table);
647        $count = count($arrList);
648        for($cnt = 0; $cnt < $count; $cnt++) {
649            $key = $arrList[$cnt][$keyname];
650            $val = $arrList[$cnt][$valname];
651            $arrRet[$key] = $val;
652        }
653        return $arrRet;
654    }
655
656    /**
657     * ランキングを上げる.
658     *
659     * @param string $table テーブル名
660     * @param string $colname カラム名
661     * @param string|integer $id テーブルのキー
662     * @param string $andwhere SQL の AND 条件である WHERE 句
663     * @return void
664     */
665    function sfRankUp($table, $colname, $id, $andwhere = "") {
666        $objQuery = new SC_Query();
667        $objQuery->begin();
668        $where = "$colname = ?";
669        if($andwhere != "") {
670            $where.= " AND $andwhere";
671        }
672        // 対象項目のランクを取得
673        $rank = $objQuery->get($table, "rank", $where, array($id));
674        // ランクの最大値を取得
675        $maxrank = $objQuery->max($table, "rank", $andwhere);
676        // ランクが最大値よりも小さい場合に実行する。
677        if($rank < $maxrank) {
678            // ランクが一つ上のIDを取得する。
679            $where = "rank = ?";
680            if($andwhere != "") {
681                $where.= " AND $andwhere";
682            }
683            $uprank = $rank + 1;
684            $up_id = $objQuery->get($table, $colname, $where, array($uprank));
685            // ランク入れ替えの実行
686            $sqlup = "UPDATE $table SET rank = ?, update_date = Now() WHERE $colname = ?";
687            $objQuery->exec($sqlup, array($rank + 1, $id));
688            $objQuery->exec($sqlup, array($rank, $up_id));
689        }
690        $objQuery->commit();
691    }
692
693    /**
694     * ランキングを下げる.
695     *
696     * @param string $table テーブル名
697     * @param string $colname カラム名
698     * @param string|integer $id テーブルのキー
699     * @param string $andwhere SQL の AND 条件である WHERE 句
700     * @return void
701     */
702    function sfRankDown($table, $colname, $id, $andwhere = "") {
703        $objQuery = new SC_Query();
704        $objQuery->begin();
705        $where = "$colname = ?";
706        if($andwhere != "") {
707            $where.= " AND $andwhere";
708        }
709        // 対象項目のランクを取得
710        $rank = $objQuery->get($table, "rank", $where, array($id));
711
712        // ランクが1(最小値)よりも大きい場合に実行する。
713        if($rank > 1) {
714            // ランクが一つ下のIDを取得する。
715            $where = "rank = ?";
716            if($andwhere != "") {
717                $where.= " AND $andwhere";
718            }
719            $downrank = $rank - 1;
720            $down_id = $objQuery->get($table, $colname, $where, array($downrank));
721            // ランク入れ替えの実行
722            $sqlup = "UPDATE $table SET rank = ?, update_date = Now() WHERE $colname = ?";
723            $objQuery->exec($sqlup, array($rank - 1, $id));
724            $objQuery->exec($sqlup, array($rank, $down_id));
725        }
726        $objQuery->commit();
727    }
728
729    /**
730     * 指定順位へ移動する.
731     *
732     * @param string $tableName テーブル名
733     * @param string $keyIdColumn キーを保持するカラム名
734     * @param string|integer $keyId キーの値
735     * @param integer $pos 指定順位
736     * @param string $where SQL の AND 条件である WHERE 句
737     * @return void
738     */
739    function sfMoveRank($tableName, $keyIdColumn, $keyId, $pos, $where = "") {
740        $objQuery = new SC_Query();
741        $objQuery->begin();
742
743        // 自身のランクを取得する
744        $rank = $objQuery->get($tableName, "rank", "$keyIdColumn = ?", array($keyId));
745        $max = $objQuery->max($tableName, "rank", $where);
746
747        // 値の調整(逆順)
748        if($pos > $max) {
749            $position = 1;
750        } else if($pos < 1) {
751            $position = $max;
752        } else {
753            $position = $max - $pos + 1;
754        }
755
756        if( $position > $rank ) $term = "rank - 1"; //入れ替え先の順位が入れ換え元の順位より大きい場合
757        if( $position < $rank ) $term = "rank + 1"; //入れ替え先の順位が入れ換え元の順位より小さい場合
758
759        // 指定した順位の商品から移動させる商品までのrankを1つずらす
760        $sql = "UPDATE $tableName SET rank = $term, update_date = NOW() WHERE rank BETWEEN ? AND ? AND del_flg = 0";
761        if($where != "") {
762            $sql.= " AND $where";
763        }
764
765        if( $position > $rank ) $objQuery->exec( $sql, array( $rank + 1, $position ));
766        if( $position < $rank ) $objQuery->exec( $sql, array( $position, $rank - 1 ));
767
768        // 指定した順位へrankを書き換える。
769        $sql  = "UPDATE $tableName SET rank = ?, update_date = NOW() WHERE $keyIdColumn = ? AND del_flg = 0 ";
770        if($where != "") {
771            $sql.= " AND $where";
772        }
773
774        $objQuery->exec( $sql, array( $position, $keyId ) );
775        $objQuery->commit();
776    }
777
778    /**
779     * ランクを含むレコードを削除する.
780     *
781     * レコードごと削除する場合は、$deleteをtrueにする
782     *
783     * @param string $table テーブル名
784     * @param string $colname カラム名
785     * @param string|integer $id テーブルのキー
786     * @param string $andwhere SQL の AND 条件である WHERE 句
787     * @param bool $delete レコードごと削除する場合 true,
788     *                     レコードごと削除しない場合 false
789     * @return void
790     */
791    function sfDeleteRankRecord($table, $colname, $id, $andwhere = "",
792                                $delete = false) {
793        $objQuery = new SC_Query();
794        $objQuery->begin();
795        // 削除レコードのランクを取得する。
796        $where = "$colname = ?";
797        if($andwhere != "") {
798            $where.= " AND $andwhere";
799        }
800        $rank = $objQuery->get($table, "rank", $where, array($id));
801
802        if(!$delete) {
803            // ランクを最下位にする、DELフラグON
804            $sqlup = "UPDATE $table SET rank = 0, del_flg = 1, update_date = Now() ";
805            $sqlup.= "WHERE $colname = ?";
806            // UPDATEの実行
807            $objQuery->exec($sqlup, array($id));
808        } else {
809            $objQuery->delete($table, "$colname = ?", array($id));
810        }
811
812        // 追加レコードのランクより上のレコードを一つずらす。
813        $where = "rank > ?";
814        if($andwhere != "") {
815            $where.= " AND $andwhere";
816        }
817        $sqlup = "UPDATE $table SET rank = (rank - 1) WHERE $where";
818        $objQuery->exec($sqlup, array($rank));
819        $objQuery->commit();
820    }
821
822    /**
823     * レコードの存在チェックを行う.
824     *
825     * @param string $table テーブル名
826     * @param string $col カラム名
827     * @param array $arrval 要素の配列
828     * @param array $addwhere SQL の AND 条件である WHERE 句
829     * @return bool レコードが存在する場合 true
830     */
831    function sfIsRecord($table, $col, $arrval, $addwhere = "") {
832        $objQuery = new SC_Query();
833        $arrCol = split("[, ]", $col);
834
835        $where = "del_flg = 0";
836
837        if($addwhere != "") {
838            $where.= " AND $addwhere";
839        }
840
841        foreach($arrCol as $val) {
842            if($val != "") {
843                if($where == "") {
844                    $where = "$val = ?";
845                } else {
846                    $where.= " AND $val = ?";
847                }
848            }
849        }
850        $ret = $objQuery->get($table, $col, $where, $arrval);
851
852        if($ret != "") {
853            return true;
854        }
855        return false;
856    }
857}
858?>
Note: See TracBrowser for help on using the repository browser.