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

Revision 15237, 16.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    // }}}
18    // {{{ functions
19
20    /**
21     * データベースのバージョンを所得する.
22     *
23     * @param string $dsn データソース名
24     * @return string データベースのバージョン
25     */
26    function sfGetDBVersion($dsn = "") {
27        $dbFactory = SC_DB_DBFactory::getInstance();
28        return $dbFactory->sfGetDBVersion($dsn);
29    }
30
31    /**
32     * テーブルの存在をチェックする.
33     *
34     * @param string $table_name チェック対象のテーブル名
35     * @param string $dsn データソース名
36     * @return テーブルが存在する場合 true
37     */
38    function sfTabaleExists($table_name, $dsn = "") {
39        $dbFactory = SC_DB_DBFactory::getInstance();
40        $dsn = $dbFactory->getDSN($dsn);
41
42        $objQuery = new SC_Query($dsn, true, true);
43        // 正常に接続されている場合
44        if(!$objQuery->isError()) {
45            list($db_type) = split(":", $dsn);
46            $sql = $dbFactory->getTableExistsSql();
47            $arrRet = $objQuery->getAll($sql, array($table_name));
48            if(count($arrRet) > 0) {
49                return true;
50            }
51        }
52        return false;
53    }
54
55    /**
56     * カラムの存在チェックと作成を行う.
57     *
58     * チェック対象のテーブルに, 該当のカラムが存在するかチェックする.
59     * 引数 $add が true の場合, 該当のカラムが存在しない場合は, カラムの生成を行う.
60     * カラムの生成も行う場合は, $col_type も必須となる.
61     *
62     * @param string $table_name テーブル名
63     * @param string $column_name カラム名
64     * @param string $col_type カラムのデータ型
65     * @param string $dsn データソース名
66     * @param bool $add カラムの作成も行う場合 true
67     * @return bool カラムが存在する場合とカラムの生成に成功した場合 true,
68     *               テーブルが存在しない場合 false,
69     *               引数 $add == false でカラムが存在しない場合 false
70     */
71    function sfColumnExists($table_name, $col_name, $col_type = "", $dsn = "", $add = false) {
72        $dbFactory = SC_DB_DBFactory::getInstance();
73        $dsn = $dbFactory->getDSN($dsn);
74
75        // テーブルが無ければエラー
76        if(!$this->sfTabaleExists($table_name, $dsn)) return false;
77
78        $objQuery = new SC_Query($dsn, true, true);
79        // 正常に接続されている場合
80        if(!$objQuery->isError()) {
81            list($db_type) = split(":", $dsn);
82
83            // カラムリストを取得
84            $arrRet = $dbFactory->sfGetColumnList($table_name);
85            if(count($arrRet) > 0) {
86                if(in_array($col_name, $arrRet)){
87                    return true;
88                }
89            }
90        }
91
92        // カラムを追加する
93        if($add){
94            $objQuery->query("ALTER TABLE $table_name ADD $col_name $col_type ");
95            return true;
96        }
97        return false;
98    }
99
100    /**
101     * インデックスの存在チェックと作成を行う.
102     *
103     * チェック対象のテーブルに, 該当のインデックスが存在するかチェックする.
104     * 引数 $add が true の場合, 該当のインデックスが存在しない場合は, インデックスの生成を行う.
105     * インデックスの生成も行う場合で, DB_TYPE が mysql の場合は, $length も必須となる.
106     *
107     * @param string $table_name テーブル名
108     * @param string $column_name カラム名
109     * @param string $index_name インデックス名
110     * @param integer|string $length インデックスを作成するデータ長
111     * @param string $dsn データソース名
112     * @param bool $add インデックスの生成もする場合 true
113     * @return bool インデックスが存在する場合とインデックスの生成に成功した場合 true,
114     *               テーブルが存在しない場合 false,
115     *               引数 $add == false でインデックスが存在しない場合 false
116     */
117    function sfIndexExists($table_name, $col_name, $index_name, $length = "", $dsn = "", $add = false) {
118        $dbFactory = SC_DB_DBFactory::getInstance();
119        $dsn = $dbFactory->getDSN($dsn);
120
121        // テーブルが無ければエラー
122        if (!$this->sfTabaleExists($table_name, $dsn)) return false;
123
124        $objQuery = new SC_Query($dsn, true, true);
125        $arrRet = $dbFactory->getTableIndex($index_name, $table_name);
126
127        // すでにインデックスが存在する場合
128        if(count($arrRet) > 0) {
129            return true;
130        }
131
132        // インデックスを作成する
133        if($add){
134            $dbFactory->createTableIndex($index_name, $table_name, $col_name, $length());
135            return true;
136        }
137        return false;
138    }
139
140    /**
141     * データの存在チェックを行う.
142     *
143     * @param string $table_name テーブル名
144     * @param string $where データを検索する WHERE 句
145     * @param string $dsn データソース名
146     * @param string $sql データの追加を行う場合の SQL文
147     * @param bool $add データの追加も行う場合 true
148     * @return bool データが存在する場合 true, データの追加に成功した場合 true,
149     *               $add == false で, データが存在しない場合 false
150     */
151    function sfDataExists($table_name, $where, $arrval, $dsn = "", $sql = "", $add = false) {
152        $dbFactory = SC_DB_DBFactory::getInstance();
153        $dsn = $dbFactory->getDSN($dsn);
154
155        $objQuery = new SC_Query($dsn, true, true);
156        $count = $objQuery->count($table_name, $where, $arrval);
157
158        if($count > 0) {
159            $ret = true;
160        } else {
161            $ret = false;
162        }
163        // データを追加する
164        if(!$ret && $add) {
165            $objQuery->exec($sql);
166        }
167        return $ret;
168    }
169
170    /**
171     * 商品規格情報を取得する.
172     *
173     * @param array $arrID 規格ID
174     * @return array 規格情報の配列
175     */
176    function sfGetProductsClass($arrID) {
177        list($product_id, $classcategory_id1, $classcategory_id2) = $arrID;
178
179        if($classcategory_id1 == "") {
180            $classcategory_id1 = '0';
181        }
182        if($classcategory_id2 == "") {
183            $classcategory_id2 = '0';
184        }
185
186        // 商品規格取得
187        $objQuery = new SC_Query();
188        $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";
189        $table = "vw_product_class AS prdcls";
190        $where = "product_id = ? AND classcategory_id1 = ? AND classcategory_id2 = ?";
191        $objQuery->setorder("rank1 DESC, rank2 DESC");
192        $arrRet = $objQuery->select($col, $table, $where, array($product_id, $classcategory_id1, $classcategory_id2));
193        return $arrRet[0];
194    }
195
196    /**
197     * カート内商品の集計処理を行う.
198     *
199     * @param LC_Page $objPage ページクラスのインスタンス
200     * @param SC_CartSession $objCartSess カートセッションのインスタンス
201     * @param array $arrInfo 商品情報の配列
202     * @return LC_Page 集計処理後のページクラスインスタンス
203     */
204    function sfTotalCart($objPage, $objCartSess, $arrInfo) {
205        // 規格名一覧
206        $arrClassName = SC_Utils_Ex::sfGetIDValueList("dtb_class", "class_id", "name");
207        // 規格分類名一覧
208        $arrClassCatName = SC_Utils_Ex::sfGetIDValueList("dtb_classcategory", "classcategory_id", "name");
209
210        $objPage->tpl_total_pretax = 0;     // 費用合計(税込み)
211        $objPage->tpl_total_tax = 0;        // 消費税合計
212        $objPage->tpl_total_point = 0;      // ポイント合計
213
214        // カート内情報の取得
215        $arrCart = $objCartSess->getCartList();
216        $max = count($arrCart);
217        $cnt = 0;
218
219        for ($i = 0; $i < $max; $i++) {
220            // 商品規格情報の取得
221            $arrData = $this->sfGetProductsClass($arrCart[$i]['id']);
222            $limit = "";
223            // DBに存在する商品
224            if (count($arrData) > 0) {
225
226                // 購入制限数を求める。
227                if ($arrData['stock_unlimited'] != '1' && $arrData['sale_unlimited'] != '1') {
228                    if($arrData['sale_limit'] < $arrData['stock']) {
229                        $limit = $arrData['sale_limit'];
230                    } else {
231                        $limit = $arrData['stock'];
232                    }
233                } else {
234                    if ($arrData['sale_unlimited'] != '1') {
235                        $limit = $arrData['sale_limit'];
236                    }
237                    if ($arrData['stock_unlimited'] != '1') {
238                        $limit = $arrData['stock'];
239                    }
240                }
241
242                if($limit != "" && $limit < $arrCart[$i]['quantity']) {
243                    // カート内商品数を制限に合わせる
244                    $objCartSess->setProductValue($arrCart[$i]['id'], 'quantity', $limit);
245                    $quantity = $limit;
246                    $objPage->tpl_message = "※「" . $arrData['name'] . "」は販売制限しております、一度にこれ以上の購入はできません。";
247                } else {
248                    $quantity = $arrCart[$i]['quantity'];
249                }
250
251                $objPage->arrProductsClass[$cnt] = $arrData;
252                $objPage->arrProductsClass[$cnt]['quantity'] = $quantity;
253                $objPage->arrProductsClass[$cnt]['cart_no'] = $arrCart[$i]['cart_no'];
254                $objPage->arrProductsClass[$cnt]['class_name1'] = $arrClassName[$arrData['class_id1']];
255                $objPage->arrProductsClass[$cnt]['class_name2'] = $arrClassName[$arrData['class_id2']];
256                $objPage->arrProductsClass[$cnt]['classcategory_name1'] = $arrClassCatName[$arrData['classcategory_id1']];
257                $objPage->arrProductsClass[$cnt]['classcategory_name2'] = $arrClassCatName[$arrData['classcategory_id2']];
258
259                // 画像サイズ
260                list($image_width, $image_height) = getimagesize(IMAGE_SAVE_DIR . basename($objPage->arrProductsClass[$cnt]["main_image"]));
261                $objPage->arrProductsClass[$cnt]["tpl_image_width"] = $image_width + 60;
262                $objPage->arrProductsClass[$cnt]["tpl_image_height"] = $image_height + 80;
263
264                // 価格の登録
265                if ($arrData['price02'] != "") {
266                    $objCartSess->setProductValue($arrCart[$i]['id'], 'price', $arrData['price02']);
267                    $objPage->arrProductsClass[$cnt]['uniq_price'] = $arrData['price02'];
268                } else {
269                    $objCartSess->setProductValue($arrCart[$i]['id'], 'price', $arrData['price01']);
270                    $objPage->arrProductsClass[$cnt]['uniq_price'] = $arrData['price01'];
271                }
272                // ポイント付与率の登録
273                $objCartSess->setProductValue($arrCart[$i]['id'], 'point_rate', $arrData['point_rate']);
274                // 商品ごとの合計金額
275                $objPage->arrProductsClass[$cnt]['total_pretax'] = $objCartSess->getProductTotal($arrInfo, $arrCart[$i]['id']);
276                // 送料の合計を計算する
277                $objPage->tpl_total_deliv_fee+= ($arrData['deliv_fee'] * $arrCart[$i]['quantity']);
278                $cnt++;
279            } else {
280                // DBに商品が見つからない場合はカート商品の削除
281                $objCartSess->delProductKey('id', $arrCart[$i]['id']);
282            }
283        }
284
285        // 全商品合計金額(税込み)
286        $objPage->tpl_total_pretax = $objCartSess->getAllProductsTotal($arrInfo);
287        // 全商品合計消費税
288        $objPage->tpl_total_tax = $objCartSess->getAllProductsTax($arrInfo);
289        // 全商品合計ポイント
290        $objPage->tpl_total_point = $objCartSess->getAllProductsPoint();
291
292        return $objPage;
293    }
294
295    /**
296     * 受注一時テーブルへの書き込み処理を行う.
297     *
298     * @param string $uniqid ユニークID
299     * @param array $sqlval SQLの値の配列
300     * @return void
301     */
302    function sfRegistTempOrder($uniqid, $sqlval) {
303        if($uniqid != "") {
304            // 既存データのチェック
305            $objQuery = new SC_Query();
306            $where = "order_temp_id = ?";
307            $cnt = $objQuery->count("dtb_order_temp", $where, array($uniqid));
308            // 既存データがない場合
309            if ($cnt == 0) {
310                // 初回書き込み時に会員の登録済み情報を取り込む
311                $sqlval = $this->sfGetCustomerSqlVal($uniqid, $sqlval);
312                $sqlval['create_date'] = "now()";
313                $objQuery->insert("dtb_order_temp", $sqlval);
314            } else {
315                $objQuery->update("dtb_order_temp", $sqlval, $where, array($uniqid));
316            }
317        }
318    }
319
320    /**
321     * 会員情報から SQL文の値を生成する.
322     *
323     * @param string $uniqid ユニークID
324     * @param array $sqlval SQL の値の配列
325     * @return array 会員情報を含んだ SQL の値の配列
326     */
327    function sfGetCustomerSqlVal($uniqid, $sqlval) {
328        $objCustomer = new SC_Customer();
329        // 会員情報登録処理
330        if ($objCustomer->isLoginSuccess()) {
331            // 登録データの作成
332            $sqlval['order_temp_id'] = $uniqid;
333            $sqlval['update_date'] = 'Now()';
334            $sqlval['customer_id'] = $objCustomer->getValue('customer_id');
335            $sqlval['order_name01'] = $objCustomer->getValue('name01');
336            $sqlval['order_name02'] = $objCustomer->getValue('name02');
337            $sqlval['order_kana01'] = $objCustomer->getValue('kana01');
338            $sqlval['order_kana02'] = $objCustomer->getValue('kana02');
339            $sqlval['order_sex'] = $objCustomer->getValue('sex');
340            $sqlval['order_zip01'] = $objCustomer->getValue('zip01');
341            $sqlval['order_zip02'] = $objCustomer->getValue('zip02');
342            $sqlval['order_pref'] = $objCustomer->getValue('pref');
343            $sqlval['order_addr01'] = $objCustomer->getValue('addr01');
344            $sqlval['order_addr02'] = $objCustomer->getValue('addr02');
345            $sqlval['order_tel01'] = $objCustomer->getValue('tel01');
346            $sqlval['order_tel02'] = $objCustomer->getValue('tel02');
347            $sqlval['order_tel03'] = $objCustomer->getValue('tel03');
348            if (defined('MOBILE_SITE')) {
349                $sqlval['order_email'] = $objCustomer->getValue('email_mobile');
350            } else {
351                $sqlval['order_email'] = $objCustomer->getValue('email');
352            }
353            $sqlval['order_job'] = $objCustomer->getValue('job');
354            $sqlval['order_birth'] = $objCustomer->getValue('birth');
355        }
356        return $sqlval;
357    }
358
359    /**
360     * 受注一時テーブルから情報を取得する.
361     *
362     * @param integer $order_temp_id 受注一時ID
363     * @return array 受注一時情報の配列
364     */
365    function sfGetOrderTemp($order_temp_id) {
366        $objQuery = new SC_Query();
367        $where = "order_temp_id = ?";
368        $arrRet = $objQuery->select("*", "dtb_order_temp", $where, array($order_temp_id));
369        return $arrRet[0];
370    }
371
372    /**
373     * SELECTボックス用リストを作成する.
374     *
375     * @param string $table テーブル名
376     * @param string $keyname プライマリーキーのカラム名
377     * @param string $valname データ内容のカラム名
378     * @return array SELECT ボックス用リストの配列
379     */
380    function sfGetIDValueList($table, $keyname, $valname) {
381        $objQuery = new SC_Query();
382        $col = "$keyname, $valname";
383        $objQuery->setwhere("del_flg = 0");
384        $objQuery->setorder("rank DESC");
385        $arrList = $objQuery->select($col, $table);
386        $count = count($arrList);
387        for($cnt = 0; $cnt < $count; $cnt++) {
388            $key = $arrList[$cnt][$keyname];
389            $val = $arrList[$cnt][$valname];
390            $arrRet[$key] = $val;
391        }
392        return $arrRet;
393    }
394}
395?>
Note: See TracBrowser for help on using the repository browser.