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

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

Revision 15349, 33.6 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
RevLine 
1<?php
2/*
3 * Copyright(c) 2000-2007 LOCKON CO.,LTD. All Rights Reserved.
4 *
5 * http://www.lockon.co.jp/
6 */
7
8/**
9 * DB関連のヘルパークラス.
10 *
11 * @package Helper
12 * @author LOCKON CO.,LTD.
13 * @version $Id$
14 */
15class SC_Helper_DB {
16
17    // {{{ properties
18
19    /** ルートカテゴリ取得フラグ */
20    var $g_root_on;
21
22    /** ルートカテゴリID */
23    var $g_root_id;
24
25    // }}}
26    // {{{ 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 string $table テーブル名
618     * @param string $pid_name 親ID名
619     * @param string $id_name ID名
620     * @param integer $id ID
621     * @return array 親IDの配列
622     */
623    function sfGetParentsArray($table, $pid_name, $id_name, $id) {
624        $objQuery = new SC_Query();
625        $col = $pid_name . "," . $id_name;
626         $arrData = $objQuery->select($col, $table);
627
628        $arrParents = array();
629        $arrParents[] = $id;
630        $child = $id;
631
632        $ret = SC_Utils::sfGetParentsArraySub($arrData, $pid_name, $id_name, $child);
633
634        while($ret != "") {
635            $arrParents[] = $ret;
636            $ret = SC_Utils::sfGetParentsArraySub($arrData, $pid_name, $id_name, $ret);
637        }
638
639        $arrParents = array_reverse($arrParents);
640
641        return $arrParents;
642    }
643
644    /**
645     * カテゴリから商品を検索する場合のWHERE文と値を返す.
646     *
647     * @param integer $category_id カテゴリID
648     * @return array 商品を検索する場合の配列
649     */
650    function sfGetCatWhere($category_id) {
651        // 子カテゴリIDの取得
652        $arrRet = $this->sfGetChildsID("dtb_category", "parent_category_id", "category_id", $category_id);
653        $tmp_where = "";
654        foreach ($arrRet as $val) {
655            if($tmp_where == "") {
656                $tmp_where.= " category_id IN ( ?";
657            } else {
658                $tmp_where.= ",? ";
659            }
660            $arrval[] = $val;
661        }
662        $tmp_where.= " ) ";
663        return array($tmp_where, $arrval);
664    }
665
666    /**
667     * 受注一時テーブルから情報を取得する.
668     *
669     * @param integer $order_temp_id 受注一時ID
670     * @return array 受注一時情報の配列
671     */
672    function sfGetOrderTemp($order_temp_id) {
673        $objQuery = new SC_Query();
674        $where = "order_temp_id = ?";
675        $arrRet = $objQuery->select("*", "dtb_order_temp", $where, array($order_temp_id));
676        return $arrRet[0];
677    }
678
679    /**
680     * SELECTボックス用リストを作成する.
681     *
682     * @param string $table テーブル名
683     * @param string $keyname プライマリーキーのカラム名
684     * @param string $valname データ内容のカラム名
685     * @return array SELECT ボックス用リストの配列
686     */
687    function sfGetIDValueList($table, $keyname, $valname) {
688        $objQuery = new SC_Query();
689        $col = "$keyname, $valname";
690        $objQuery->setwhere("del_flg = 0");
691        $objQuery->setorder("rank DESC");
692        $arrList = $objQuery->select($col, $table);
693        $count = count($arrList);
694        for($cnt = 0; $cnt < $count; $cnt++) {
695            $key = $arrList[$cnt][$keyname];
696            $val = $arrList[$cnt][$valname];
697            $arrRet[$key] = $val;
698        }
699        return $arrRet;
700    }
701
702    /**
703     * ランキングを上げる.
704     *
705     * @param string $table テーブル名
706     * @param string $colname カラム名
707     * @param string|integer $id テーブルのキー
708     * @param string $andwhere SQL の AND 条件である WHERE 句
709     * @return void
710     */
711    function sfRankUp($table, $colname, $id, $andwhere = "") {
712        $objQuery = new SC_Query();
713        $objQuery->begin();
714        $where = "$colname = ?";
715        if($andwhere != "") {
716            $where.= " AND $andwhere";
717        }
718        // 対象項目のランクを取得
719        $rank = $objQuery->get($table, "rank", $where, array($id));
720        // ランクの最大値を取得
721        $maxrank = $objQuery->max($table, "rank", $andwhere);
722        // ランクが最大値よりも小さい場合に実行する。
723        if($rank < $maxrank) {
724            // ランクが一つ上のIDを取得する。
725            $where = "rank = ?";
726            if($andwhere != "") {
727                $where.= " AND $andwhere";
728            }
729            $uprank = $rank + 1;
730            $up_id = $objQuery->get($table, $colname, $where, array($uprank));
731            // ランク入れ替えの実行
732            $sqlup = "UPDATE $table SET rank = ?, update_date = Now() WHERE $colname = ?";
733            $objQuery->exec($sqlup, array($rank + 1, $id));
734            $objQuery->exec($sqlup, array($rank, $up_id));
735        }
736        $objQuery->commit();
737    }
738
739    /**
740     * ランキングを下げる.
741     *
742     * @param string $table テーブル名
743     * @param string $colname カラム名
744     * @param string|integer $id テーブルのキー
745     * @param string $andwhere SQL の AND 条件である WHERE 句
746     * @return void
747     */
748    function sfRankDown($table, $colname, $id, $andwhere = "") {
749        $objQuery = new SC_Query();
750        $objQuery->begin();
751        $where = "$colname = ?";
752        if($andwhere != "") {
753            $where.= " AND $andwhere";
754        }
755        // 対象項目のランクを取得
756        $rank = $objQuery->get($table, "rank", $where, array($id));
757
758        // ランクが1(最小値)よりも大きい場合に実行する。
759        if($rank > 1) {
760            // ランクが一つ下のIDを取得する。
761            $where = "rank = ?";
762            if($andwhere != "") {
763                $where.= " AND $andwhere";
764            }
765            $downrank = $rank - 1;
766            $down_id = $objQuery->get($table, $colname, $where, array($downrank));
767            // ランク入れ替えの実行
768            $sqlup = "UPDATE $table SET rank = ?, update_date = Now() WHERE $colname = ?";
769            $objQuery->exec($sqlup, array($rank - 1, $id));
770            $objQuery->exec($sqlup, array($rank, $down_id));
771        }
772        $objQuery->commit();
773    }
774
775    /**
776     * 指定順位へ移動する.
777     *
778     * @param string $tableName テーブル名
779     * @param string $keyIdColumn キーを保持するカラム名
780     * @param string|integer $keyId キーの値
781     * @param integer $pos 指定順位
782     * @param string $where SQL の AND 条件である WHERE 句
783     * @return void
784     */
785    function sfMoveRank($tableName, $keyIdColumn, $keyId, $pos, $where = "") {
786        $objQuery = new SC_Query();
787        $objQuery->begin();
788
789        // 自身のランクを取得する
790        $rank = $objQuery->get($tableName, "rank", "$keyIdColumn = ?", array($keyId));
791        $max = $objQuery->max($tableName, "rank", $where);
792
793        // 値の調整(逆順)
794        if($pos > $max) {
795            $position = 1;
796        } else if($pos < 1) {
797            $position = $max;
798        } else {
799            $position = $max - $pos + 1;
800        }
801
802        if( $position > $rank ) $term = "rank - 1"; //入れ替え先の順位が入れ換え元の順位より大きい場合
803        if( $position < $rank ) $term = "rank + 1"; //入れ替え先の順位が入れ換え元の順位より小さい場合
804
805        // 指定した順位の商品から移動させる商品までのrankを1つずらす
806        $sql = "UPDATE $tableName SET rank = $term, update_date = NOW() WHERE rank BETWEEN ? AND ? AND del_flg = 0";
807        if($where != "") {
808            $sql.= " AND $where";
809        }
810
811        if( $position > $rank ) $objQuery->exec( $sql, array( $rank + 1, $position ));
812        if( $position < $rank ) $objQuery->exec( $sql, array( $position, $rank - 1 ));
813
814        // 指定した順位へrankを書き換える。
815        $sql  = "UPDATE $tableName SET rank = ?, update_date = NOW() WHERE $keyIdColumn = ? AND del_flg = 0 ";
816        if($where != "") {
817            $sql.= " AND $where";
818        }
819
820        $objQuery->exec( $sql, array( $position, $keyId ) );
821        $objQuery->commit();
822    }
823
824    /**
825     * ランクを含むレコードを削除する.
826     *
827     * レコードごと削除する場合は、$deleteをtrueにする
828     *
829     * @param string $table テーブル名
830     * @param string $colname カラム名
831     * @param string|integer $id テーブルのキー
832     * @param string $andwhere SQL の AND 条件である WHERE 句
833     * @param bool $delete レコードごと削除する場合 true,
834     *                     レコードごと削除しない場合 false
835     * @return void
836     */
837    function sfDeleteRankRecord($table, $colname, $id, $andwhere = "",
838                                $delete = false) {
839        $objQuery = new SC_Query();
840        $objQuery->begin();
841        // 削除レコードのランクを取得する。
842        $where = "$colname = ?";
843        if($andwhere != "") {
844            $where.= " AND $andwhere";
845        }
846        $rank = $objQuery->get($table, "rank", $where, array($id));
847
848        if(!$delete) {
849            // ランクを最下位にする、DELフラグON
850            $sqlup = "UPDATE $table SET rank = 0, del_flg = 1, update_date = Now() ";
851            $sqlup.= "WHERE $colname = ?";
852            // UPDATEの実行
853            $objQuery->exec($sqlup, array($id));
854        } else {
855            $objQuery->delete($table, "$colname = ?", array($id));
856        }
857
858        // 追加レコードのランクより上のレコードを一つずらす。
859        $where = "rank > ?";
860        if($andwhere != "") {
861            $where.= " AND $andwhere";
862        }
863        $sqlup = "UPDATE $table SET rank = (rank - 1) WHERE $where";
864        $objQuery->exec($sqlup, array($rank));
865        $objQuery->commit();
866    }
867
868    /**
869     * レコードの存在チェックを行う.
870     *
871     * @param string $table テーブル名
872     * @param string $col カラム名
873     * @param array $arrval 要素の配列
874     * @param array $addwhere SQL の AND 条件である WHERE 句
875     * @return bool レコードが存在する場合 true
876     */
877    function sfIsRecord($table, $col, $arrval, $addwhere = "") {
878        $objQuery = new SC_Query();
879        $arrCol = split("[, ]", $col);
880
881        $where = "del_flg = 0";
882
883        if($addwhere != "") {
884            $where.= " AND $addwhere";
885        }
886
887        foreach($arrCol as $val) {
888            if($val != "") {
889                if($where == "") {
890                    $where = "$val = ?";
891                } else {
892                    $where.= " AND $val = ?";
893                }
894            }
895        }
896        $ret = $objQuery->get($table, $col, $where, $arrval);
897
898        if($ret != "") {
899            return true;
900        }
901        return false;
902    }
903
904}
905?>
Note: See TracBrowser for help on using the repository browser.