source: branches/version-2_5-dev/data/class/pages/products/LC_Page_Products_List.php @ 20597

Revision 20597, 17.9 KB checked in by kimoto, 13 years ago (diff)

ソースコード整形
冗長な処理を削除
整合性が取れてない変数の処理が多くて追いきれない・・・

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
Line 
1<?php
2/*
3 * This file is part of EC-CUBE
4 *
5 * Copyright(c) 2000-2010 LOCKON CO.,LTD. All Rights Reserved.
6 *
7 * http://www.lockon.co.jp/
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22 */
23
24// {{{ requires
25require_once CLASS_EX_REALDIR . 'page_extends/LC_Page_Ex.php';
26
27/**
28 * 商品一覧 のページクラス.
29 *
30 * @package Page
31 * @author LOCKON CO.,LTD.
32 * @version $Id$
33 */
34class LC_Page_Products_List extends LC_Page_Ex {
35
36    // {{{ properties
37
38    /** テンプレートクラス名1 */
39    var $tpl_class_name1 = array();
40
41    /** テンプレートクラス名2 */
42    var $tpl_class_name2 = array();
43
44    /** JavaScript テンプレート */
45    var $tpl_javascript;
46
47    var $orderby;
48
49    var $mode;
50
51    /** 検索条件(内部データ) */
52    var $arrSearchData = array();
53
54    /** 検索条件(表示用) */
55    var $arrSearch = array();
56
57    var $tpl_subtitle = '';
58
59    /** ランダム文字列 **/
60    var $tpl_rnd = '';
61
62    // }}}
63    // {{{ functions
64
65    /**
66     * Page を初期化する.
67     *
68     * @return void
69     */
70    function init() {
71        parent::init();
72
73        $masterData                 = new SC_DB_MasterData_Ex();
74        $this->arrSTATUS            = $masterData->getMasterData("mtb_status");
75        $this->arrSTATUS_IMAGE      = $masterData->getMasterData("mtb_status_image");
76        $this->arrDELIVERYDATE      = $masterData->getMasterData("mtb_delivery_date");
77        $this->arrPRODUCTLISTMAX    = $masterData->getMasterData("mtb_product_list_max");
78    }
79
80    /**
81     * Page のプロセス.
82     *
83     * @return void
84     */
85    function process() {
86        parent::process();
87        $this->action();
88        $this->sendResponse();
89    }
90
91    /**
92     * Page のAction.
93     *
94     * @return void
95     */
96    function action() {
97        $objQuery   =& SC_Query_Ex::getSingletonInstance();
98        $objProduct = new SC_Product_Ex();
99
100        $this->arrForm = $_REQUEST;//時間が無いのでコレで勘弁してください。 tao_s
101        //modeの取得
102        $this->mode = $this->getMode();
103
104        //表示条件の取得
105        $this->arrSearchData = array(
106            'category_id'   => $this->lfGetCategoryId(intval($this->arrForm['category_id'])),
107            'maker_id'      => intval($this->arrForm['maker_id']),
108            'name'          => $this->arrForm['name']
109        );
110        $this->orderby = $this->arrForm['orderby'];
111
112        //ページング設定
113        $this->tpl_pageno   = $this->arrForm['pageno'];
114        $this->disp_number  = $this->lfGetDisplayNum($this->arrForm['disp_number']);
115
116        // 画面に表示するサブタイトルの設定
117        $this->tpl_subtitle = $this->lfGetPageTitle($this->mode, $this->arrSearchData['category_id']);
118
119        // 画面に表示する検索条件を設定
120        $this->arrSearch    = $this->lfGetSearchConditionDisp($this->arrSearchData);
121
122        // 商品一覧データの取得
123        $arrSearchCondition = $this->lfGetSearchCondition($this->arrSearchData);
124        $this->tpl_linemax  = $this->lfGetProductAllNum($arrSearchCondition);
125        $urlParam           = "category_id={$this->arrSearchData['category_id']}&pageno=#page#";
126        $this->objNavi      = new SC_PageNavi_Ex($this->tpl_pageno, $this->tpl_linemax, $this->disp_number, 'fnNaviPage', NAVI_PMAX, $urlParam, SC_Display_Ex::detectDevice() !== DEVICE_TYPE_MOBILE);
127        $this->arrProducts  = $this->lfGetProductsList($arrSearchCondition, $this->disp_number, $this->objNavi->start_row, $this->tpl_linemax, $objProduct);
128        //商品一覧の表示処理
129        $strnavi            = $this->objNavi->strnavi;
130        // 表示文字列
131        $this->tpl_strnavi  = empty($strnavi) ? "&nbsp;" : $strnavi;
132
133        // 規格1クラス名
134        $this->tpl_class_name1  = $objProduct->className1;
135
136        // 規格2クラス名
137        $this->tpl_class_name2  = $objProduct->className2;
138
139        // 規格1
140        $this->arrClassCat1     = $objProduct->classCats1;
141
142        // 規格1が設定されている
143        $this->tpl_classcat_find1 = $objProduct->classCat1_find;
144        // 規格2が設定されている
145        $this->tpl_classcat_find2 = $objProduct->classCat2_find;
146
147        $this->tpl_stock_find       = $objProduct->stock_find;
148        $this->tpl_product_class_id = $objProduct->product_class_id;
149        $this->tpl_product_type     = $objProduct->product_type;
150
151        // 商品ステータスを取得
152        $this->productStatus = $this->arrProducts['productStatus'];
153        unset($this->arrProducts['productStatus']);
154        $this->tpl_javascript .= 'var productsClassCategories = ' . SC_Utils_Ex::jsonEncode($objProduct->classCategories) . ';';
155        //onloadスクリプトを設定
156        foreach ($this->arrProducts as $arrProduct) {
157            $js_fnOnLoad .= "fnSetClassCategories(document.product_form{$arrProduct['product_id']});";
158        }
159
160        //カート処理
161        $target_product_id = intval($this->arrForm['product_id']);
162        if ( $target_product_id > 0) {
163            // 商品IDの正当性チェック
164            if (!SC_Utils_Ex::sfIsInt($this->arrForm['product_id'])
165                || !SC_Helper_DB_Ex::sfIsRecord("dtb_products", "product_id", $this->arrForm['product_id'], "del_flg = 0 AND status = 1")) {
166                SC_Utils_Ex::sfDispSiteError(PRODUCT_NOT_FOUND);
167            }
168
169            // 入力内容のチェック
170            $arrErr = $this->lfCheckError($target_product_id, $this->arrForm, $this->tpl_classcat_find1, $this->tpl_classcat_find2);
171            if (empty($arrErr)) {
172                $this->lfAddCart($this->arrForm, $_SERVER['HTTP_REFERER']);
173                SC_Response_Ex::sendRedirect(CART_URLPATH);
174                exit;
175            }
176            $js_fnOnLoad .= $this->lfSetSelectedData($this->arrProducts, $this->arrForm, $arrErr, $target_product_id);
177        }
178
179        $this->tpl_javascript   .= 'function fnOnLoad(){' . $js_fnOnLoad . '}';
180        $this->tpl_onload       .= 'fnOnLoad(); ';
181
182        $this->tpl_rnd          = SC_Utils_Ex::sfGetRandomString(3);
183    }
184
185    /**
186     * デストラクタ.
187     *
188     * @return void
189     */
190    function destroy() {
191        parent::destroy();
192    }
193
194    /**
195     * カテゴリIDの取得
196     *
197     * @return integer カテゴリID
198     */
199    function lfGetCategoryId($category_id) {
200
201        // 指定なしの場合、0 を返す
202        if (empty($category_id)) return 0;
203
204        // 正当性チェック
205        if (!SC_Utils_Ex::sfIsInt($category_id)
206            || SC_Utils_Ex::sfIsZeroFilling($category_id)
207            || !SC_Helper_DB_Ex::sfIsRecord('dtb_category', 'category_id', (array)$category_id, 'del_flg = 0')
208            ) {
209            SC_Utils_Ex::sfDispSiteError(CATEGORY_NOT_FOUND);
210        }
211
212        // 指定されたカテゴリIDを元に正しいカテゴリIDを取得する。
213        $arrCategory_id = SC_Helper_DB_Ex::sfGetCategoryId('', $category_id);
214
215        if (empty($arrCategory_id)) {
216            SC_Utils_Ex::sfDispSiteError(CATEGORY_NOT_FOUND);
217        }
218
219        return $arrCategory_id[0];
220    }
221
222    /* 商品一覧の表示 */
223    function lfGetProductsList($searchCondition, $disp_number, $startno, $linemax, &$objProduct) {
224
225        $arrval_order = array();
226
227        $objQuery =& SC_Query_Ex::getSingletonInstance();
228        // 表示順序
229        switch ($this->orderby) {
230            // 販売価格が安い順
231            case 'price':
232                $objProduct->setProductsOrder('price02', 'dtb_products_class', 'ASC');
233                break;
234
235            // 新着順
236            case 'date':
237                $objProduct->setProductsOrder('create_date', 'dtb_products', 'DESC');
238                break;
239
240            default:
241                if (strlen($searchCondition["where_category"]) >= 1) {
242                    $dtb_product_categories = "(SELECT * FROM dtb_product_categories WHERE ".$searchCondition["where_category"].")";
243                    $arrval_order           = array_merge($searchCondition['arrvalCategory'], $searchCondition['arrvalCategory']);
244                } else {
245                    $dtb_product_categories = 'dtb_product_categories';
246                }
247                $order = <<< __EOS__
248                    (
249                        SELECT
250                             T3.rank
251                        FROM
252                            $dtb_product_categories T2
253                            JOIN dtb_category T3
254                                USING (category_id)
255                        WHERE T2.product_id = alldtl.product_id
256                        ORDER BY T3.rank DESC, T2.rank DESC
257                        LIMIT 1
258                    ) DESC
259                    ,(
260                        SELECT
261                            T2.rank
262                        FROM
263                            $dtb_product_categories T2
264                            JOIN dtb_category T3
265                                USING (category_id)
266                        WHERE T2.product_id = alldtl.product_id
267                        ORDER BY T3.rank DESC, T2.rank DESC
268                        LIMIT 1
269                    ) DESC
270                    ,product_id
271__EOS__;
272                    $objQuery->setOrder($order);
273                break;
274        }
275        // 取得範囲の指定(開始行番号、行数のセット)
276        $objQuery->setLimitOffset($disp_number, $startno);
277        $objQuery->setWhere($searchCondition['where']);
278
279         // 表示すべきIDとそのIDの並び順を一気に取得
280        $arrProduct_id = $objProduct->findProductIdsOrder($objQuery, array_merge($searchCondition['arrval'], $arrval_order));
281
282        // 取得した表示すべきIDだけを指定して情報を取得。
283        $where = "";
284        if (is_array($arrProduct_id) && !empty($arrProduct_id)) {
285            $where = 'product_id IN (' . implode(',', $arrProduct_id) . ')';
286        } else {
287            // 一致させない
288            $where = '0<>0';
289        }
290
291        $where .= ' AND del_flg = 0'; // 商品規格の削除フラグ
292        $objQuery =& SC_Query_Ex::getSingletonInstance();
293        $objQuery->setWhere($where);
294        $arrProducts = $objProduct->lists($objQuery, $arrProduct_id);
295
296        //取得している並び順で並び替え
297        $arrProducts2 = array();
298        foreach($arrProducts as $item) {
299            $arrProducts2[ $item['product_id'] ] = $item;
300        }
301        $arrProducts = array();
302        foreach($arrProduct_id as $product_id) {
303            $arrProducts[] = $arrProducts2[$product_id];
304        }
305
306        // 規格を設定
307        $objProduct->setProductsClassByProductIds($arrProduct_id);
308        $arrProducts += array('productStatus' => $objProduct->getProductStatus($arrProduct_id));
309        return $arrProducts;
310    }
311
312    /* 入力内容のチェック */
313    function lfCheckError($product_id, &$arrForm, $tpl_classcat_find1, $tpl_classcat_find2) {
314
315        // 入力データを渡す。
316        $objErr = new SC_CheckError_Ex($arrForm);
317
318        // 複数項目チェック
319        if ($tpl_classcat_find1[$product_id]) {
320            $objErr->doFunc(array("規格1", 'classcategory_id1', INT_LEN), array("EXIST_CHECK", "NUM_CHECK", "MAX_LENGTH_CHECK"));
321        }
322        if ($tpl_classcat_find2[$product_id]) {
323            $objErr->doFunc(array("規格2", 'classcategory_id2', INT_LEN), array("EXIST_CHECK", "NUM_CHECK", "MAX_LENGTH_CHECK"));
324        }
325
326        $objErr->doFunc(array("商品規格ID", 'product_class_id', INT_LEN), array("EXIST_CHECK", "NUM_CHECK", "MAX_LENGTH_CHECK"));
327        $objErr->doFunc(array("数量", 'quantity', INT_LEN), array("EXIST_CHECK", "ZERO_CHECK", "NUM_CHECK", "MAX_LENGTH_CHECK"));
328
329        return $objErr->arrErr;
330    }
331
332    /**
333     * パラメータの読み込み
334     *
335     * @return void
336     */
337    function lfGetDisplayNum($display_number) {
338        // 表示件数
339        return (SC_Utils_Ex::sfIsInt($display_number))
340            ? $display_number
341            : current(array_keys($this->arrPRODUCTLISTMAX));
342    }
343
344    /**
345     * ページタイトルの設定
346     *
347     * @return str
348     */
349    function lfGetPageTitle($mode, $category_id = 0){
350        if ($mode == 'search') {
351            return "検索結果";
352        } elseif ($category_id == 0) {
353            return "全商品";
354        } else {
355            $arrCat = SC_Helper_DB_Ex::sfGetCat($category_id);
356            return $arrCat['name'];
357        }
358    }
359
360    /**
361     * 表示用検索条件の設定
362     *
363     * @return array
364     */
365    function lfGetSearchConditionDisp($arrSearchData){
366        $objQuery   =& SC_Query_Ex::getSingletonInstance();
367        $arrSearch  = array('category' => '指定なし', 'maker' => '指定なし', 'name' => '指定なし');
368        // カテゴリー検索条件
369        if ($arrSearchData['category_id'] > 0) {
370            $arrSearch['category']  = $objQuery->get('category_name', 'dtb_category', 'category_id = ?', array($arrSearchData['category_id']));
371        }
372
373        // メーカー検索条件
374        if (strlen($arrSearchData['maker_id']) > 0) {
375            $arrSearch['maker']     = $objQuery->get('name', 'dtb_maker', 'maker_id = ?', array($arrSearchData['maker_id']));
376        }
377
378        // 商品名検索条件
379        if (strlen($arrSearchData['name']) > 0) {
380            $arrSearch['name']      = $arrSearchData['name'];
381        }
382        return $arrSearch;
383    }
384
385    /**
386     * 該当件数の取得
387     *
388     * @return int
389     */
390    function lfGetProductAllNum($searchCondition){
391        // 検索結果対象となる商品の数を取得
392        $objQuery   =& SC_Query_Ex::getSingletonInstance();
393        $objQuery->setWhere($searchCondition['where']);
394        $objProduct = new SC_Product_Ex();
395        return $objProduct->findProductCount($objQuery, $searchCondition['arrval']);
396    }
397
398    /**
399     * 検索条件のwhere文とかを取得
400     *
401     * @return array
402     */
403    function lfGetSearchCondition($arrSearchData){
404        $searchCondition = array(
405            'where'             => "",
406            'arrval'            => array(),
407            "where_category"    => "",
408            'arrvalCategory'    => array()
409        );
410
411        // カテゴリからのWHERE文字列取得
412        if ($arrSearchData["category_id"] != 0) {
413            list($searchCondition["where_category"], $searchCondition['arrvalCategory']) = SC_Helper_DB_Ex::sfGetCatWhere($arrSearchData["category_id"]);
414        }
415        // ▼対象商品IDの抽出
416        // 商品検索条件の作成(未削除、表示)
417        $searchCondition['where'] = "alldtl.del_flg = 0 AND alldtl.status = 1 ";
418
419        // 在庫無し商品の非表示
420        if (NOSTOCK_HIDDEN === true) {
421            $searchCondition['where'] .= ' AND (stock >= 1 OR stock_unlimited = 1)';
422        }
423
424        if (strlen($searchCondition["where_category"]) >= 1) {
425            $searchCondition['where'] .= " AND T2.".$searchCondition["where_category"];
426            $searchCondition['arrval'] = array_merge($searchCondition['arrval'], $searchCondition['arrvalCategory']);
427        }
428
429        // 商品名をwhere文に
430        $name = $arrSearchData['name'];
431        $name = str_replace(",", "", $name);
432        // 全角スペースを半角スペースに変換
433        $name = str_replace(' ', ' ', $name);
434        // スペースでキーワードを分割
435        $names = preg_split("/ +/", $name);
436        // 分割したキーワードを一つずつwhere文に追加
437        foreach ($names as $val) {
438            if ( strlen($val) > 0 ) {
439                $searchCondition['where']    .= " AND ( alldtl.name ILIKE ? OR alldtl.comment3 ILIKE ?) ";
440                $searchCondition['arrval'][]  = "%$val%";
441                $searchCondition['arrval'][]  = "%$val%";
442            }
443        }
444
445        // メーカーらのWHERE文字列取得
446        if ($arrSearchData['maker_id']) {
447            $searchCondition['where']   .= " AND alldtl.maker_id = ? ";
448            $searchCondition['arrval'][] = $arrSearchData['maker_id'];
449        }
450        return $searchCondition;
451    }
452
453    /**
454     * カートに入れる商品情報にエラーがあったら戻す
455     *
456     * @return str
457     */
458    function lfSetSelectedData(&$arrProducts, $arrForm, $arrErr, $product_id){
459        $js_fnOnLoad = "";
460        foreach (array_keys($arrProducts) as $key) {
461            if ($arrProducts[$key]['product_id'] == $product_id) {
462
463                $arrProducts[$key]['product_class_id']  = $arrForm['product_class_id'];
464                $arrProducts[$key]['classcategory_id1'] = $arrForm['classcategory_id1'];
465                $arrProducts[$key]['classcategory_id2'] = $arrForm['classcategory_id2'];
466                $arrProducts[$key]['quantity']          = $arrForm['quantity'];
467                $arrProducts[$key]['arrErr']            = $arrErr;
468                $js_fnOnLoad .= "fnSetClassCategories(document.product_form{$arrProducts[$key]['product_id']}, '{$arrForm['classcategory_id2']}');";
469            }
470        }
471        return $js_fnOnLoad;
472    }
473
474    /**
475     * カートに商品を追加
476     *
477     * @return void
478     */
479    function lfAddCart($arrForm, $referer){
480        $product_class_id = $arrForm['product_class_id'];
481        $objCartSess = new SC_CartSession_Ex();
482        $objCartSess->addProduct($product_class_id, $arrForm['quantity']);
483
484        // カート「戻るボタン」用に保持
485        if (SC_Utils_Ex::sfIsInternalDomain($referer)) {
486            //該当メソッドが無いため、$_SESSIONに直接セット
487            $_SESSION['cart_referer_url'] = $referer;
488        }
489    }
490}
491?>
Note: See TracBrowser for help on using the repository browser.