source: branches/version-2_13_2/data/class/pages/products/LC_Page_Products_List.php @ 23420

Revision 23420, 21.6 KB checked in by m_uehara, 10 years ago (diff)

#2547 (商品一覧の「新着順」「価格順」をクリックすると、ページングが解除されてしまう)
修正内容をマージいたします

  • 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-2013 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
24require_once CLASS_EX_REALDIR . 'page_extends/LC_Page_Ex.php';
25
26/**
27 * 商品一覧 のページクラス.
28 *
29 * @package Page
30 * @author LOCKON CO.,LTD.
31 * @version $Id$
32 */
33class LC_Page_Products_List extends LC_Page_Ex
34{
35    /** テンプレートクラス名1 */
36    public $tpl_class_name1 = array();
37
38    /** テンプレートクラス名2 */
39    public $tpl_class_name2 = array();
40
41    /** JavaScript テンプレート */
42    public $tpl_javascript;
43
44    public $orderby;
45
46    public $mode;
47
48    /** 検索条件(内部データ) */
49    public $arrSearchData = array();
50
51    /** 検索条件(表示用) */
52    public $arrSearch = array();
53
54    public $tpl_subtitle = '';
55
56    /** ランダム文字列 **/
57    public $tpl_rnd = '';
58
59    /**
60     * Page を初期化する.
61     *
62     * @return void
63     */
64    public function init()
65    {
66        parent::init();
67
68        $masterData                 = new SC_DB_MasterData_Ex();
69        $this->arrSTATUS            = $masterData->getMasterData('mtb_status');
70        $this->arrSTATUS_IMAGE      = $masterData->getMasterData('mtb_status_image');
71        $this->arrDELIVERYDATE      = $masterData->getMasterData('mtb_delivery_date');
72        $this->arrPRODUCTLISTMAX    = $masterData->getMasterData('mtb_product_list_max');
73    }
74
75    /**
76     * Page のプロセス.
77     *
78     * @return void
79     */
80    public function process()
81    {
82        parent::process();
83        $this->action();
84        $this->sendResponse();
85    }
86
87    /**
88     * Page のAction.
89     *
90     * @return void
91     */
92    public function action()
93    {
94        //決済処理中ステータスのロールバック
95        $objPurchase = new SC_Helper_Purchase_Ex();
96        $objPurchase->cancelPendingOrder(PENDING_ORDER_CANCEL_FLAG);
97
98        $objProduct = new SC_Product_Ex();
99        // パラメーター管理クラス
100        $objFormParam = new SC_FormParam_Ex();
101
102        // パラメーター情報の初期化
103        $this->lfInitParam($objFormParam);
104
105        // 値の設定
106        $objFormParam->setParam($_REQUEST);
107
108        // 入力値の変換
109        $objFormParam->convParam();
110
111        // 値の取得
112        $this->arrForm = $objFormParam->getHashArray();
113
114        //modeの取得
115        $this->mode = $this->getMode();
116
117        //表示条件の取得
118        $this->arrSearchData = array(
119            'category_id'   => $this->lfGetCategoryId(intval($this->arrForm['category_id'])),
120            'maker_id'      => intval($this->arrForm['maker_id']),
121            'name'          => $this->arrForm['name']
122        );
123        $this->orderby = $this->arrForm['orderby'];
124
125        //ページング設定
126        $this->tpl_pageno   = $this->arrForm['pageno'];
127        $this->disp_number  = $this->lfGetDisplayNum($this->arrForm['disp_number']);
128
129        // 画面に表示するサブタイトルの設定
130        $this->tpl_subtitle = $this->lfGetPageTitle($this->mode, $this->arrSearchData['category_id']);
131
132        // 画面に表示する検索条件を設定
133        $this->arrSearch    = $this->lfGetSearchConditionDisp($this->arrSearchData);
134
135        // 商品一覧データの取得
136        $arrSearchCondition = $this->lfGetSearchCondition($this->arrSearchData);
137        $this->tpl_linemax  = $this->lfGetProductAllNum($arrSearchCondition);
138        $urlParam           = "category_id={$this->arrSearchData['category_id']}&pageno=#page#";
139        // モバイルの場合に検索条件をURLの引数に追加
140        if (SC_Display_Ex::detectDevice() === DEVICE_TYPE_MOBILE) {
141            $searchNameUrl = urlencode(mb_convert_encoding($this->arrSearchData['name'], 'SJIS-win', 'UTF-8'));
142            $urlParam .= "&mode={$this->mode}&name={$searchNameUrl}&orderby={$this->orderby}";
143        }
144        $this->objNavi      = new SC_PageNavi_Ex($this->tpl_pageno, $this->tpl_linemax, $this->disp_number, 'eccube.movePage', NAVI_PMAX, $urlParam, SC_Display_Ex::detectDevice() !== DEVICE_TYPE_MOBILE);
145        $this->arrProducts  = $this->lfGetProductsList($arrSearchCondition, $this->disp_number, $this->objNavi->start_row, $objProduct);
146
147        switch ($this->getMode()) {
148            case 'json':
149                $this->doJson($objProduct);
150                break;
151
152            default:
153                $this->doDefault($objProduct, $objFormParam);
154                break;
155        }
156
157        $this->tpl_rnd = SC_Utils_Ex::sfGetRandomString(3);
158    }
159
160    /**
161     * パラメーター情報の初期化
162     *
163     * @param  array $objFormParam フォームパラメータークラス
164     * @return void
165     */
166    public function lfInitParam(&$objFormParam)
167    {
168        // 抽出条件
169        // XXX カートインしていない場合、チェックしていない
170        $objFormParam->addParam('カテゴリID', 'category_id', INT_LEN, 'n', array('NUM_CHECK', 'MAX_LENGTH_CHECK'));
171        $objFormParam->addParam('メーカーID', 'maker_id', INT_LEN, 'n', array('NUM_CHECK', 'MAX_LENGTH_CHECK'));
172        $objFormParam->addParam('商品名', 'name', STEXT_LEN, 'KVa', array('MAX_LENGTH_CHECK'));
173        $objFormParam->addParam('表示順序', 'orderby', STEXT_LEN, 'KVa', array('MAX_LENGTH_CHECK'));
174        $objFormParam->addParam('ページ番号', 'pageno', INT_LEN, 'n', array('NUM_CHECK', 'MAX_LENGTH_CHECK'));
175        $objFormParam->addParam('表示件数', 'disp_number', INT_LEN, 'n', array('NUM_CHECK', 'MAX_LENGTH_CHECK'));
176        // カートイン
177        $objFormParam->addParam('規格1', 'classcategory_id1', INT_LEN, 'n', array('NUM_CHECK', 'MAX_LENGTH_CHECK'));
178        $objFormParam->addParam('規格2', 'classcategory_id2', INT_LEN, 'n', array('NUM_CHECK', 'MAX_LENGTH_CHECK'));
179        $objFormParam->addParam('数量', 'quantity', INT_LEN, 'n', array('EXIST_CHECK', 'ZERO_CHECK', 'NUM_CHECK', 'MAX_LENGTH_CHECK'));
180        $objFormParam->addParam('商品ID', 'product_id', INT_LEN, 'n', array('ZERO_CHECK', 'NUM_CHECK', 'MAX_LENGTH_CHECK'));
181        $objFormParam->addParam('商品規格ID', 'product_class_id', INT_LEN, 'n', array('EXIST_CHECK', 'NUM_CHECK', 'MAX_LENGTH_CHECK'));
182    }
183
184    /**
185     * カテゴリIDの取得
186     *
187     * @return integer カテゴリID
188     */
189    public function lfGetCategoryId($category_id)
190    {
191        // 指定なしの場合、0 を返す
192        if (empty($category_id)) return 0;
193
194        // 正当性チェック
195        if (!SC_Utils_Ex::sfIsInt($category_id)
196            || SC_Utils_Ex::sfIsZeroFilling($category_id)
197            || !SC_Helper_DB_Ex::sfIsRecord('dtb_category', 'category_id', (array) $category_id, 'del_flg = 0')
198            ) {
199            SC_Utils_Ex::sfDispSiteError(CATEGORY_NOT_FOUND);
200        }
201
202        // 指定されたカテゴリIDを元に正しいカテゴリIDを取得する。
203        $arrCategory_id = SC_Helper_DB_Ex::sfGetCategoryId('', $category_id);
204
205        if (empty($arrCategory_id)) {
206            SC_Utils_Ex::sfDispSiteError(CATEGORY_NOT_FOUND);
207        }
208
209        return $arrCategory_id[0];
210    }
211
212    /* 商品一覧の表示 */
213    public function lfGetProductsList($searchCondition, $disp_number, $startno, &$objProduct)
214    {
215        $objQuery =& SC_Query_Ex::getSingletonInstance();
216
217        $arrOrderVal = array();
218
219        // 表示順序
220        switch ($this->orderby) {
221            // 販売価格が安い順
222            case 'price':
223                $objProduct->setProductsOrder('price02', 'dtb_products_class', 'ASC');
224                break;
225
226            // 新着順
227            case 'date':
228                $objProduct->setProductsOrder('create_date', 'dtb_products', 'DESC');
229                break;
230
231            default:
232                if (strlen($searchCondition['where_category']) >= 1) {
233                    $dtb_product_categories = '(SELECT * FROM dtb_product_categories WHERE '.$searchCondition['where_category'].')';
234                    $arrOrderVal           = $searchCondition['arrvalCategory'];
235                } else {
236                    $dtb_product_categories = 'dtb_product_categories';
237                }
238                $col = 'T3.rank * 2147483648 + T2.rank';
239                $from = "$dtb_product_categories T2 JOIN dtb_category T3 ON T2.category_id = T3.category_id";
240                $where = 'T2.product_id = alldtl.product_id';
241                $objQuery->setOrder('T3.rank DESC, T2.rank DESC');
242                $sub_sql = $objQuery->getSql($col, $from, $where);
243                $sub_sql = $objQuery->dbFactory->addLimitOffset($sub_sql, 1);
244
245                $objQuery->setOrder("($sub_sql) DESC ,product_id DESC");
246                break;
247        }
248        // 取得範囲の指定(開始行番号、行数のセット)
249        $objQuery->setLimitOffset($disp_number, $startno);
250        $objQuery->setWhere($searchCondition['where']);
251
252        // 表示すべきIDとそのIDの並び順を一気に取得
253        $arrProductId = $objProduct->findProductIdsOrder($objQuery, array_merge($searchCondition['arrval'], $arrOrderVal));
254
255        $objQuery =& SC_Query_Ex::getSingletonInstance();
256        $arrProducts = $objProduct->getListByProductIds($objQuery, $arrProductId);
257
258        // 規格を設定
259        $objProduct->setProductsClassByProductIds($arrProductId);
260        $arrProducts['productStatus'] = $objProduct->getProductStatus($arrProductId);
261
262        return $arrProducts;
263    }
264
265    /* 入力内容のチェック */
266    public function lfCheckError($objFormParam)
267    {
268        // 入力データを渡す。
269        $arrForm =  $objFormParam->getHashArray();
270        $objErr = new SC_CheckError_Ex($arrForm);
271        $objErr->arrErr = $objFormParam->checkError();
272
273        // 動的チェック
274        if ($this->tpl_classcat_find1[$arrForm['product_id']]) {
275            $objErr->doFunc(array('規格1', 'classcategory_id1'), array('EXIST_CHECK'));
276        }
277        if ($this->tpl_classcat_find2[$arrForm['product_id']]) {
278            $objErr->doFunc(array('規格2', 'classcategory_id2'), array('EXIST_CHECK'));
279        }
280
281        return $objErr->arrErr;
282    }
283
284    /**
285     * パラメーターの読み込み
286     *
287     * @return void
288     */
289    public function lfGetDisplayNum($display_number)
290    {
291        // 表示件数
292        return (SC_Utils_Ex::sfIsInt($display_number))
293            ? $display_number
294            : current(array_keys($this->arrPRODUCTLISTMAX));
295    }
296
297    /**
298     * ページタイトルの設定
299     *
300     * @return str
301     */
302    public function lfGetPageTitle($mode, $category_id = 0)
303    {
304        if ($mode == 'search') {
305            return '検索結果';
306        } elseif ($category_id == 0) {
307            return '全商品';
308        } else {
309            $objCategory = new SC_Helper_Category_Ex();
310            $arrCat = $objCategory->get($category_id);
311
312            return $arrCat['category_name'];
313        }
314    }
315
316    /**
317     * 表示用検索条件の設定
318     *
319     * @return array
320     */
321    public function lfGetSearchConditionDisp($arrSearchData)
322    {
323        $objQuery   =& SC_Query_Ex::getSingletonInstance();
324        $arrSearch  = array('category' => '指定なし', 'maker' => '指定なし', 'name' => '指定なし');
325        // カテゴリ検索条件
326        if ($arrSearchData['category_id'] > 0) {
327            $arrSearch['category']  = $objQuery->get('category_name', 'dtb_category', 'category_id = ?', array($arrSearchData['category_id']));
328        }
329
330        // メーカー検索条件
331        if (strlen($arrSearchData['maker_id']) > 0) {
332            $objMaker = new SC_Helper_Maker_Ex();
333            $maker = $objMaker->getMaker($arrSearchData['maker_id']);
334            $arrSearch['maker']     = $maker['name'];
335        }
336
337        // 商品名検索条件
338        if (strlen($arrSearchData['name']) > 0) {
339            $arrSearch['name']      = $arrSearchData['name'];
340        }
341
342        return $arrSearch;
343    }
344
345    /**
346     * 該当件数の取得
347     *
348     * @return int
349     */
350    public function lfGetProductAllNum($searchCondition)
351    {
352        // 検索結果対象となる商品の数を取得
353        $objQuery   =& SC_Query_Ex::getSingletonInstance();
354        $objQuery->setWhere($searchCondition['where_for_count']);
355        $objProduct = new SC_Product_Ex();
356
357        return $objProduct->findProductCount($objQuery, $searchCondition['arrval']);
358    }
359
360    /**
361     * 検索条件のwhere文とかを取得
362     *
363     * @return array
364     */
365    public function lfGetSearchCondition($arrSearchData)
366    {
367        $searchCondition = array(
368            'where'             => '',
369            'arrval'            => array(),
370            'where_category'    => '',
371            'arrvalCategory'    => array()
372        );
373
374        // カテゴリからのWHERE文字列取得
375        if ($arrSearchData['category_id'] != 0) {
376            list($searchCondition['where_category'], $searchCondition['arrvalCategory']) = SC_Helper_DB_Ex::sfGetCatWhere($arrSearchData['category_id']);
377        }
378        // ▼対象商品IDの抽出
379        // 商品検索条件の作成(未削除、表示)
380        $searchCondition['where'] = SC_Product_Ex::getProductDispConditions('alldtl');
381
382        if (strlen($searchCondition['where_category']) >= 1) {
383            $searchCondition['where'] .= ' AND EXISTS (SELECT * FROM dtb_product_categories WHERE ' . $searchCondition['where_category'] . ' AND product_id = alldtl.product_id)';
384            $searchCondition['arrval'] = array_merge($searchCondition['arrval'], $searchCondition['arrvalCategory']);
385        }
386
387        // 商品名をwhere文に
388        $name = $arrSearchData['name'];
389        $name = str_replace(',', '', $name);
390        // 全角スペースを半角スペースに変換
391        $name = str_replace(' ', ' ', $name);
392        // スペースでキーワードを分割
393        $names = preg_split('/ +/', $name);
394        // 分割したキーワードを一つずつwhere文に追加
395        foreach ($names as $val) {
396            if (strlen($val) > 0) {
397                $searchCondition['where']    .= ' AND ( alldtl.name ILIKE ? OR alldtl.comment3 ILIKE ?) ';
398                $searchCondition['arrval'][]  = "%$val%";
399                $searchCondition['arrval'][]  = "%$val%";
400            }
401        }
402
403        // メーカーらのWHERE文字列取得
404        if ($arrSearchData['maker_id']) {
405            $searchCondition['where']   .= ' AND alldtl.maker_id = ? ';
406            $searchCondition['arrval'][] = $arrSearchData['maker_id'];
407        }
408
409        // 在庫無し商品の非表示
410        if (NOSTOCK_HIDDEN) {
411            $searchCondition['where'] .= ' AND EXISTS(SELECT * FROM dtb_products_class WHERE product_id = alldtl.product_id AND del_flg = 0 AND (stock >= 1 OR stock_unlimited = 1))';
412        }
413
414        // XXX 一時期内容が異なっていたことがあるので別要素にも格納している。
415        $searchCondition['where_for_count'] = $searchCondition['where'];
416
417        return $searchCondition;
418    }
419
420    /**
421     * カートに入れる商品情報にエラーがあったら戻す
422     *
423     * @return str
424     */
425    public function lfSetSelectedData(&$arrProducts, $arrForm, $arrErr, $product_id)
426    {
427        $js_fnOnLoad = '';
428        foreach (array_keys($arrProducts) as $key) {
429            if ($arrProducts[$key]['product_id'] == $product_id) {
430                $arrProducts[$key]['product_class_id']  = $arrForm['product_class_id'];
431                $arrProducts[$key]['classcategory_id1'] = $arrForm['classcategory_id1'];
432                $arrProducts[$key]['classcategory_id2'] = $arrForm['classcategory_id2'];
433                $arrProducts[$key]['quantity']          = $arrForm['quantity'];
434                $arrProducts[$key]['arrErr']            = $arrErr;
435                $classcategory_id2 = SC_Utils_Ex::jsonEncode($arrForm['classcategory_id2']);
436                $js_fnOnLoad .= "fnSetClassCategories(document.product_form{$arrProducts[$key]['product_id']}, {$classcategory_id2});";
437            }
438        }
439
440        return $js_fnOnLoad;
441    }
442
443    /**
444     * カートに商品を追加
445     *
446     * @return void
447     */
448    public function lfAddCart($arrForm)
449    {
450        $objCartSess = new SC_CartSession_Ex();
451
452        $product_class_id = $arrForm['product_class_id'];
453        $objCartSess->addProduct($product_class_id, $arrForm['quantity']);
454    }
455
456    /**
457     * 商品情報配列に商品ステータス情報を追加する
458     *
459     * @param  Array $arrProducts    商品一覧情報
460     * @param  Array $arrStatus      商品ステータス配列
461     * @param  Array $arrStatusImage スタータス画像配列
462     * @return Array $arrProducts 商品一覧情報
463     */
464    public function setStatusDataTo($arrProducts, $arrStatus, $arrStatusImage)
465    {
466        foreach ($arrProducts['productStatus'] as $product_id => $arrValues) {
467            for ($i = 0; $i < count($arrValues); $i++) {
468                $product_status_id = $arrValues[$i];
469                if (!empty($product_status_id)) {
470                    $arrProductStatus = array(
471                        'status_cd' => $product_status_id,
472                        'status_name' => $arrStatus[$product_status_id],
473                        'status_image' =>$arrStatusImage[$product_status_id],
474                    );
475                    $arrProducts['productStatus'][$product_id][$i] = $arrProductStatus;
476                }
477            }
478        }
479
480        return $arrProducts;
481    }
482
483    /**
484     *
485     * @return void
486     */
487    public function doJson()
488    {
489        $this->arrProducts = $this->setStatusDataTo($this->arrProducts, $this->arrSTATUS, $this->arrSTATUS_IMAGE);
490        SC_Product_Ex::setPriceTaxTo($this->arrProducts);
491
492        // 一覧メイン画像の指定が無い商品のための処理
493        foreach ($this->arrProducts as $key=>$val) {
494            $this->arrProducts[$key]['main_list_image'] = SC_Utils_Ex::sfNoImageMainList($val['main_list_image']);
495        }
496
497        echo SC_Utils_Ex::jsonEncode($this->arrProducts);
498        SC_Response_Ex::actionExit();
499    }
500
501    /**
502     *
503     * @param  type $objProduct
504     * @return void
505     */
506    public function doDefault(&$objProduct, &$objFormParam)
507    {
508        //商品一覧の表示処理
509        $strnavi            = $this->objNavi->strnavi;
510        // 表示文字列
511        $this->tpl_strnavi  = empty($strnavi) ? '&nbsp;' : $strnavi;
512
513        // 規格1クラス名
514        $this->tpl_class_name1  = $objProduct->className1;
515
516        // 規格2クラス名
517        $this->tpl_class_name2  = $objProduct->className2;
518
519        // 規格1
520        $this->arrClassCat1     = $objProduct->classCats1;
521
522        // 規格1が設定されている
523        $this->tpl_classcat_find1 = $objProduct->classCat1_find;
524        // 規格2が設定されている
525        $this->tpl_classcat_find2 = $objProduct->classCat2_find;
526
527        $this->tpl_stock_find       = $objProduct->stock_find;
528        $this->tpl_product_class_id = $objProduct->product_class_id;
529        $this->tpl_product_type     = $objProduct->product_type;
530
531        // 商品ステータスを取得
532        $this->productStatus = $this->arrProducts['productStatus'];
533        unset($this->arrProducts['productStatus']);
534        $this->tpl_javascript .= 'eccube.productsClassCategories = ' . SC_Utils_Ex::jsonEncode($objProduct->classCategories) . ';';
535        if (SC_Display_Ex::detectDevice() === DEVICE_TYPE_PC) {
536            //onloadスクリプトを設定. 在庫ありの商品のみ出力する
537            foreach ($this->arrProducts as $arrProduct) {
538                if ($arrProduct['stock_unlimited_max'] || $arrProduct['stock_max'] > 0) {
539                    $js_fnOnLoad .= "fnSetClassCategories(document.product_form{$arrProduct['product_id']});";
540                }
541            }
542        }
543
544        //カート処理
545        $target_product_id = intval($this->arrForm['product_id']);
546        if ($target_product_id > 0) {
547            // 商品IDの正当性チェック
548            if (!SC_Utils_Ex::sfIsInt($this->arrForm['product_id'])
549                || !SC_Helper_DB_Ex::sfIsRecord('dtb_products', 'product_id', $this->arrForm['product_id'], 'del_flg = 0 AND status = 1')) {
550                SC_Utils_Ex::sfDispSiteError(PRODUCT_NOT_FOUND);
551            }
552
553            // 入力内容のチェック
554            $arrErr = $this->lfCheckError($objFormParam);
555            if (empty($arrErr)) {
556                $this->lfAddCart($this->arrForm);
557
558                // 開いているカテゴリーツリーを維持するためのパラメーター
559                $arrQueryString = array(
560                    'category_id' => $this->arrForm['category_id'],
561                );
562
563                SC_Response_Ex::sendRedirect(CART_URL, $arrQueryString);
564                SC_Response_Ex::actionExit();
565            }
566            $js_fnOnLoad .= $this->lfSetSelectedData($this->arrProducts, $this->arrForm, $arrErr, $target_product_id);
567        } else {
568            // カート「戻るボタン」用に保持
569            $netURL = new Net_URL();
570            //該当メソッドが無いため、$_SESSIONに直接セット
571            $_SESSION['cart_referer_url'] = $netURL->getURL();
572        }
573
574        $this->tpl_javascript   .= 'function fnOnLoad() {' . $js_fnOnLoad . '}';
575        $this->tpl_onload       .= 'fnOnLoad(); ';
576    }
577}
Note: See TracBrowser for help on using the repository browser.