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

Revision 21933, 19.6 KB checked in by pineray, 12 years ago (diff)

#1726 モバイルサイト 商品名検索のページングで検索条件が引き継がれない

キーワードをURLエンコーディング.

  • 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-2012 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
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        // モバイルの場合に検索条件をURLの引数に追加
127        if (SC_Display_Ex::detectDevice() === DEVICE_TYPE_MOBILE) {
128            $urlParam .= "&mode={$this->mode}&name=" . urlencode($this->arrSearchData['name']) . "&orderby={$this->orderby}";
129        }
130        $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);
131        $this->arrProducts  = $this->lfGetProductsList($arrSearchCondition, $this->disp_number, $this->objNavi->start_row, $this->tpl_linemax, $objProduct);
132
133        switch ($this->getMode()) {
134
135            case 'json':
136                $this->arrProducts = $this->setStatusDataTo($this->arrProducts, $this->arrSTATUS, $this->arrSTATUS_IMAGE);
137                $this->arrProducts = $objProduct->setPriceTaxTo($this->arrProducts);
138
139                // 一覧メイン画像の指定が無い商品のための処理
140                foreach ($this->arrProducts as $key=>$val) {
141                    $this->arrProducts[$key]['main_list_image'] = SC_Utils_Ex::sfNoImageMainList($val['main_list_image']);
142                }
143
144                echo SC_Utils_Ex::jsonEncode($this->arrProducts);
145                SC_Response_Ex::actionExit();
146                break;
147
148            default:
149
150                //商品一覧の表示処理
151                $strnavi            = $this->objNavi->strnavi;
152                // 表示文字列
153                $this->tpl_strnavi  = empty($strnavi) ? '&nbsp;' : $strnavi;
154
155                // 規格1クラス名
156                $this->tpl_class_name1  = $objProduct->className1;
157
158                // 規格2クラス名
159                $this->tpl_class_name2  = $objProduct->className2;
160
161                // 規格1
162                $this->arrClassCat1     = $objProduct->classCats1;
163
164                // 規格1が設定されている
165                $this->tpl_classcat_find1 = $objProduct->classCat1_find;
166                // 規格2が設定されている
167                $this->tpl_classcat_find2 = $objProduct->classCat2_find;
168
169                $this->tpl_stock_find       = $objProduct->stock_find;
170                $this->tpl_product_class_id = $objProduct->product_class_id;
171                $this->tpl_product_type     = $objProduct->product_type;
172
173                // 商品ステータスを取得
174                $this->productStatus = $this->arrProducts['productStatus'];
175                unset($this->arrProducts['productStatus']);
176                $this->tpl_javascript .= 'var productsClassCategories = ' . SC_Utils_Ex::jsonEncode($objProduct->classCategories) . ';';
177                //onloadスクリプトを設定. 在庫ありの商品のみ出力する
178                foreach ($this->arrProducts as $arrProduct) {
179                    if ($arrProduct['stock_unlimited_max'] || $arrProduct['stock_max'] > 0) {
180                        $js_fnOnLoad .= "fnSetClassCategories(document.product_form{$arrProduct['product_id']});";
181                    }
182                }
183
184                //カート処理
185                $target_product_id = intval($this->arrForm['product_id']);
186                if ($target_product_id > 0) {
187                    // 商品IDの正当性チェック
188                    if (!SC_Utils_Ex::sfIsInt($this->arrForm['product_id'])
189                        || !SC_Helper_DB_Ex::sfIsRecord('dtb_products', 'product_id', $this->arrForm['product_id'], 'del_flg = 0 AND status = 1')) {
190                        SC_Utils_Ex::sfDispSiteError(PRODUCT_NOT_FOUND);
191                    }
192
193                    // 入力内容のチェック
194                    $arrErr = $this->lfCheckError($target_product_id, $this->arrForm, $this->tpl_classcat_find1, $this->tpl_classcat_find2);
195                    if (empty($arrErr)) {
196                        $this->lfAddCart($this->arrForm, $_SERVER['HTTP_REFERER']);
197
198
199                        SC_Response_Ex::sendRedirect(CART_URLPATH);
200                        SC_Response_Ex::actionExit();
201                    }
202                    $js_fnOnLoad .= $this->lfSetSelectedData($this->arrProducts, $this->arrForm, $arrErr, $target_product_id);
203                } else {
204                    // カート「戻るボタン」用に保持
205                    $netURL = new Net_URL();
206                    //該当メソッドが無いため、$_SESSIONに直接セット
207                    $_SESSION['cart_referer_url'] = $netURL->getURL();
208                }
209
210                $this->tpl_javascript   .= 'function fnOnLoad(){' . $js_fnOnLoad . '}';
211                $this->tpl_onload       .= 'fnOnLoad(); ';
212                break;
213        }
214
215        $this->tpl_rnd          = SC_Utils_Ex::sfGetRandomString(3);
216
217
218    }
219
220    /**
221     * デストラクタ.
222     *
223     * @return void
224     */
225    function destroy() {
226        parent::destroy();
227    }
228
229    /**
230     * カテゴリIDの取得
231     *
232     * @return integer カテゴリID
233     */
234    function lfGetCategoryId($category_id) {
235
236        // 指定なしの場合、0 を返す
237        if (empty($category_id)) return 0;
238
239        // 正当性チェック
240        if (!SC_Utils_Ex::sfIsInt($category_id)
241            || SC_Utils_Ex::sfIsZeroFilling($category_id)
242            || !SC_Helper_DB_Ex::sfIsRecord('dtb_category', 'category_id', (array)$category_id, 'del_flg = 0')
243            ) {
244            SC_Utils_Ex::sfDispSiteError(CATEGORY_NOT_FOUND);
245        }
246
247        // 指定されたカテゴリIDを元に正しいカテゴリIDを取得する。
248        $arrCategory_id = SC_Helper_DB_Ex::sfGetCategoryId('', $category_id);
249
250        if (empty($arrCategory_id)) {
251            SC_Utils_Ex::sfDispSiteError(CATEGORY_NOT_FOUND);
252        }
253
254        return $arrCategory_id[0];
255    }
256
257    /* 商品一覧の表示 */
258    function lfGetProductsList($searchCondition, $disp_number, $startno, $linemax, &$objProduct) {
259
260        $arrOrderVal = array();
261
262        $objQuery =& SC_Query_Ex::getSingletonInstance();
263        // 表示順序
264        switch ($this->orderby) {
265            // 販売価格が安い順
266            case 'price':
267                $objProduct->setProductsOrder('price02', 'dtb_products_class', 'ASC');
268                break;
269
270            // 新着順
271            case 'date':
272                $objProduct->setProductsOrder('create_date', 'dtb_products', 'DESC');
273                break;
274
275            default:
276                if (strlen($searchCondition['where_category']) >= 1) {
277                    $dtb_product_categories = '(SELECT * FROM dtb_product_categories WHERE '.$searchCondition['where_category'].')';
278                    $arrOrderVal           = $searchCondition['arrvalCategory'];
279                } else {
280                    $dtb_product_categories = 'dtb_product_categories';
281                }
282                $order = <<< __EOS__
283                    (
284                        SELECT
285                            T3.rank * 2147483648 + T2.rank
286                        FROM
287                            $dtb_product_categories T2
288                            JOIN dtb_category T3
289                              ON T2.category_id = T3.category_id
290                        WHERE T2.product_id = alldtl.product_id
291                        ORDER BY T3.rank DESC, T2.rank DESC
292                        LIMIT 1
293                    ) DESC
294                    ,product_id DESC
295__EOS__;
296                    $objQuery->setOrder($order);
297                break;
298        }
299        // 取得範囲の指定(開始行番号、行数のセット)
300        $objQuery->setLimitOffset($disp_number, $startno);
301        $objQuery->setWhere($searchCondition['where']);
302
303        // 表示すべきIDとそのIDの並び順を一気に取得
304        $arrProductId = $objProduct->findProductIdsOrder($objQuery, array_merge($searchCondition['arrval'], $arrOrderVal));
305
306        $objQuery =& SC_Query_Ex::getSingletonInstance();
307        $arrProducts = $objProduct->getListByProductIds($objQuery, $arrProductId);
308
309        // 規格を設定
310        $objProduct->setProductsClassByProductIds($arrProductId);
311        $arrProducts['productStatus'] = $objProduct->getProductStatus($arrProductId);
312        return $arrProducts;
313    }
314
315    /* 入力内容のチェック */
316    function lfCheckError($product_id, &$arrForm, $tpl_classcat_find1, $tpl_classcat_find2) {
317
318        // 入力データを渡す。
319        $objErr = new SC_CheckError_Ex($arrForm);
320
321        // 複数項目チェック
322        if ($tpl_classcat_find1[$product_id]) {
323            $objErr->doFunc(array('規格1', 'classcategory_id1', INT_LEN), array('EXIST_CHECK', 'NUM_CHECK', 'MAX_LENGTH_CHECK'));
324        }
325        if ($tpl_classcat_find2[$product_id]) {
326            $objErr->doFunc(array('規格2', 'classcategory_id2', INT_LEN), array('EXIST_CHECK', 'NUM_CHECK', 'MAX_LENGTH_CHECK'));
327        }
328
329        $objErr->doFunc(array('商品規格ID', 'product_class_id', INT_LEN), array('EXIST_CHECK', 'NUM_CHECK', 'MAX_LENGTH_CHECK'));
330        $objErr->doFunc(array('数量', 'quantity', INT_LEN), array('EXIST_CHECK', 'ZERO_CHECK', 'NUM_CHECK', 'MAX_LENGTH_CHECK'));
331
332        return $objErr->arrErr;
333    }
334
335    /**
336     * パラメーターの読み込み
337     *
338     * @return void
339     */
340    function lfGetDisplayNum($display_number) {
341        // 表示件数
342        return (SC_Utils_Ex::sfIsInt($display_number))
343            ? $display_number
344            : current(array_keys($this->arrPRODUCTLISTMAX));
345    }
346
347    /**
348     * ページタイトルの設定
349     *
350     * @return str
351     */
352    function lfGetPageTitle($mode, $category_id = 0) {
353        if ($mode == 'search') {
354            return '検索結果';
355        } elseif ($category_id == 0) {
356            return '全商品';
357        } else {
358            $arrCat = SC_Helper_DB_Ex::sfGetCat($category_id);
359            return $arrCat['name'];
360        }
361    }
362
363    /**
364     * 表示用検索条件の設定
365     *
366     * @return array
367     */
368    function lfGetSearchConditionDisp($arrSearchData) {
369        $objQuery   =& SC_Query_Ex::getSingletonInstance();
370        $arrSearch  = array('category' => '指定なし', 'maker' => '指定なし', 'name' => '指定なし');
371        // カテゴリ検索条件
372        if ($arrSearchData['category_id'] > 0) {
373            $arrSearch['category']  = $objQuery->get('category_name', 'dtb_category', 'category_id = ?', array($arrSearchData['category_id']));
374        }
375
376        // メーカー検索条件
377        if (strlen($arrSearchData['maker_id']) > 0) {
378            $arrSearch['maker']     = $objQuery->get('name', 'dtb_maker', 'maker_id = ?', array($arrSearchData['maker_id']));
379        }
380
381        // 商品名検索条件
382        if (strlen($arrSearchData['name']) > 0) {
383            $arrSearch['name']      = $arrSearchData['name'];
384        }
385        return $arrSearch;
386    }
387
388    /**
389     * 該当件数の取得
390     *
391     * @return int
392     */
393    function lfGetProductAllNum($searchCondition) {
394        // 検索結果対象となる商品の数を取得
395        $objQuery   =& SC_Query_Ex::getSingletonInstance();
396        $objQuery->setWhere($searchCondition['where_for_count']);
397        $objProduct = new SC_Product_Ex();
398        return $objProduct->findProductCount($objQuery, $searchCondition['arrval']);
399    }
400
401    /**
402     * 検索条件のwhere文とかを取得
403     *
404     * @return array
405     */
406    function lfGetSearchCondition($arrSearchData) {
407        $searchCondition = array(
408            'where'             => '',
409            'arrval'            => array(),
410            'where_category'    => '',
411            'arrvalCategory'    => array()
412        );
413
414        // カテゴリからのWHERE文字列取得
415        if ($arrSearchData['category_id'] != 0) {
416            list($searchCondition['where_category'], $searchCondition['arrvalCategory']) = SC_Helper_DB_Ex::sfGetCatWhere($arrSearchData['category_id']);
417        }
418        // ▼対象商品IDの抽出
419        // 商品検索条件の作成(未削除、表示)
420        $searchCondition['where'] = 'alldtl.del_flg = 0 AND alldtl.status = 1 ';
421
422        if (strlen($searchCondition['where_category']) >= 1) {
423            $searchCondition['where'] .= ' AND EXISTS (SELECT * FROM dtb_product_categories WHERE ' . $searchCondition['where_category'] . ' AND product_id = alldtl.product_id)';
424            $searchCondition['arrval'] = array_merge($searchCondition['arrval'], $searchCondition['arrvalCategory']);
425        }
426
427        // 商品名をwhere文に
428        $name = $arrSearchData['name'];
429        $name = str_replace(',', '', $name);
430        // 全角スペースを半角スペースに変換
431        $name = str_replace(' ', ' ', $name);
432        // スペースでキーワードを分割
433        $names = preg_split('/ +/', $name);
434        // 分割したキーワードを一つずつwhere文に追加
435        foreach ($names as $val) {
436            if (strlen($val) > 0) {
437                $searchCondition['where']    .= ' AND ( alldtl.name ILIKE ? OR alldtl.comment3 ILIKE ?) ';
438                $searchCondition['arrval'][]  = "%$val%";
439                $searchCondition['arrval'][]  = "%$val%";
440            }
441        }
442
443        // メーカーらのWHERE文字列取得
444        if ($arrSearchData['maker_id']) {
445            $searchCondition['where']   .= ' AND alldtl.maker_id = ? ';
446            $searchCondition['arrval'][] = $arrSearchData['maker_id'];
447        }
448
449        $searchCondition['where_for_count'] = $searchCondition['where'];
450
451        // 在庫無し商品の非表示
452        if (NOSTOCK_HIDDEN) {
453            $searchCondition['where'] .= ' AND (stock >= 1 OR stock_unlimited = 1)';
454            $searchCondition['where_for_count'] .= ' AND EXISTS(SELECT * FROM dtb_products_class WHERE product_id = alldtl.product_id AND del_flg = 0 AND (stock >= 1 OR stock_unlimited = 1))';
455        }
456
457        return $searchCondition;
458    }
459
460    /**
461     * カートに入れる商品情報にエラーがあったら戻す
462     *
463     * @return str
464     */
465    function lfSetSelectedData(&$arrProducts, $arrForm, $arrErr, $product_id) {
466        $js_fnOnLoad = '';
467        foreach (array_keys($arrProducts) as $key) {
468            if ($arrProducts[$key]['product_id'] == $product_id) {
469
470                $arrProducts[$key]['product_class_id']  = $arrForm['product_class_id'];
471                $arrProducts[$key]['classcategory_id1'] = $arrForm['classcategory_id1'];
472                $arrProducts[$key]['classcategory_id2'] = $arrForm['classcategory_id2'];
473                $arrProducts[$key]['quantity']          = $arrForm['quantity'];
474                $arrProducts[$key]['arrErr']            = $arrErr;
475                $js_fnOnLoad .= "fnSetClassCategories(document.product_form{$arrProducts[$key]['product_id']}, '{$arrForm['classcategory_id2']}');";
476            }
477        }
478        return $js_fnOnLoad;
479    }
480
481    /**
482     * カートに商品を追加
483     *
484     * @return void
485     */
486    function lfAddCart($arrForm, $referer) {
487        $product_class_id = $arrForm['product_class_id'];
488        $objCartSess = new SC_CartSession_Ex();
489        $objCartSess->addProduct($product_class_id, $arrForm['quantity']);
490    }
491
492    /**
493     * 商品情報配列に商品ステータス情報を追加する
494     *
495     * @param Array $arrProducts 商品一覧情報
496     * @param Array $arrStatus 商品ステータス配列
497     * @param Array $arrStatusImage スタータス画像配列
498     * @return Array $arrProducts 商品一覧情報
499     */
500    function setStatusDataTo($arrProducts, $arrStatus, $arrStatusImage) {
501
502        foreach ($arrProducts['productStatus'] as $product_id => $arrValues) {
503            for ($i = 0; $i < count($arrValues); $i++) {
504                $product_status_id = $arrValues[$i];
505                if (!empty($product_status_id)) {
506                    $arrProductStatus = array(
507                        'status_cd' => $product_status_id,
508                        'status_name' => $arrStatus[$product_status_id],
509                        'status_image' =>$arrStatusImage[$product_status_id],
510                    );
511                    $arrProducts['productStatus'][$product_id][$i] = $arrProductStatus;
512                }
513            }
514        }
515        return $arrProducts;
516    }
517}
Note: See TracBrowser for help on using the repository browser.