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

Revision 23606, 21.3 KB checked in by kimoto, 10 years ago (diff)

#2448 typo修正・ソース整形・ソースコメントの改善 for 2.13.3

 https://scrutinizer-ci.com/g/nobuhiko/EC-CUBE/inspections/e0f27994-b3c7-4fc6-ad70-55d3cd63769b/patches

  • 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-2014 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  SC_FormParam_Ex $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     * @param int $category_id
188     * @return integer|void カテゴリID
189     */
190    public function lfGetCategoryId($category_id)
191    {
192        // 指定なしの場合、0 を返す
193        if (empty($category_id)) return 0;
194
195        // 正当性チェック
196        $objCategory = new SC_Helper_Category_Ex();
197        if ($objCategory->isValidCategoryId($category_id)) {
198            return $category_id;
199        } else {
200            SC_Utils_Ex::sfDispSiteError(CATEGORY_NOT_FOUND);
201        }
202    }
203
204    /* 商品一覧の表示 */
205
206    /**
207     * @param SC_Product_Ex $objProduct
208     */
209    public function lfGetProductsList($searchCondition, $disp_number, $startno, &$objProduct)
210    {
211        $objQuery =& SC_Query_Ex::getSingletonInstance();
212
213        $arrOrderVal = array();
214
215        // 表示順序
216        switch ($this->orderby) {
217            // 販売価格が安い順
218            case 'price':
219                $objProduct->setProductsOrder('price02', 'dtb_products_class', 'ASC');
220                break;
221
222            // 新着順
223            case 'date':
224                $objProduct->setProductsOrder('create_date', 'dtb_products', 'DESC');
225                break;
226
227            default:
228                if (strlen($searchCondition['where_category']) >= 1) {
229                    $dtb_product_categories = '(SELECT * FROM dtb_product_categories WHERE '.$searchCondition['where_category'].')';
230                    $arrOrderVal           = $searchCondition['arrvalCategory'];
231                } else {
232                    $dtb_product_categories = 'dtb_product_categories';
233                }
234                $col = 'MAX(T3.rank * 2147483648 + T2.rank)';
235                $from = "$dtb_product_categories T2 JOIN dtb_category T3 ON T2.category_id = T3.category_id";
236                $where = 'T2.product_id = alldtl.product_id';
237                $sub_sql = $objQuery->getSql($col, $from, $where);
238
239                $objQuery->setOrder("($sub_sql) DESC ,product_id DESC");
240                break;
241        }
242        // 取得範囲の指定(開始行番号、行数のセット)
243        $objQuery->setLimitOffset($disp_number, $startno);
244        $objQuery->setWhere($searchCondition['where']);
245
246        // 表示すべきIDとそのIDの並び順を一気に取得
247        $arrProductId = $objProduct->findProductIdsOrder($objQuery, array_merge($searchCondition['arrval'], $arrOrderVal));
248
249        $objQuery =& SC_Query_Ex::getSingletonInstance();
250        $arrProducts = $objProduct->getListByProductIds($objQuery, $arrProductId);
251
252        // 規格を設定
253        $objProduct->setProductsClassByProductIds($arrProductId);
254        $arrProducts['productStatus'] = $objProduct->getProductStatus($arrProductId);
255
256        return $arrProducts;
257    }
258
259    /* 入力内容のチェック */
260    public function lfCheckError($objFormParam)
261    {
262        // 入力データを渡す。
263        $arrForm =  $objFormParam->getHashArray();
264        $objErr = new SC_CheckError_Ex($arrForm);
265        $objErr->arrErr = $objFormParam->checkError();
266
267        // 動的チェック
268        if ($this->tpl_classcat_find1[$arrForm['product_id']]) {
269            $objErr->doFunc(array('規格1', 'classcategory_id1'), array('EXIST_CHECK'));
270        }
271        if ($this->tpl_classcat_find2[$arrForm['product_id']]) {
272            $objErr->doFunc(array('規格2', 'classcategory_id2'), array('EXIST_CHECK'));
273        }
274
275        return $objErr->arrErr;
276    }
277
278    /**
279     * パラメーターの読み込み
280     *
281     * @return void
282     */
283    public function lfGetDisplayNum($display_number)
284    {
285        // 表示件数
286        return (SC_Utils_Ex::sfIsInt($display_number))
287            ? $display_number
288            : current(array_keys($this->arrPRODUCTLISTMAX));
289    }
290
291    /**
292     * ページタイトルの設定
293     *
294     * @param string|null $mode
295     * @return str
296     */
297    public function lfGetPageTitle($mode, $category_id = 0)
298    {
299        if ($mode == 'search') {
300            return '検索結果';
301        } elseif ($category_id == 0) {
302            return '全商品';
303        } else {
304            $objCategory = new SC_Helper_Category_Ex();
305            $arrCat = $objCategory->get($category_id);
306
307            return $arrCat['category_name'];
308        }
309    }
310
311    /**
312     * 表示用検索条件の設定
313     *
314     * @return array
315     */
316    public function lfGetSearchConditionDisp($arrSearchData)
317    {
318        $objQuery   =& SC_Query_Ex::getSingletonInstance();
319        $arrSearch  = array('category' => '指定なし', 'maker' => '指定なし', 'name' => '指定なし');
320        // カテゴリ検索条件
321        if ($arrSearchData['category_id'] > 0) {
322            $arrSearch['category']  = $objQuery->get('category_name', 'dtb_category', 'category_id = ?', array($arrSearchData['category_id']));
323        }
324
325        // メーカー検索条件
326        if (strlen($arrSearchData['maker_id']) > 0) {
327            $objMaker = new SC_Helper_Maker_Ex();
328            $maker = $objMaker->getMaker($arrSearchData['maker_id']);
329            $arrSearch['maker']     = $maker['name'];
330        }
331
332        // 商品名検索条件
333        if (strlen($arrSearchData['name']) > 0) {
334            $arrSearch['name']      = $arrSearchData['name'];
335        }
336
337        return $arrSearch;
338    }
339
340    /**
341     * 該当件数の取得
342     *
343     * @return int
344     */
345    public function lfGetProductAllNum($searchCondition)
346    {
347        // 検索結果対象となる商品の数を取得
348        $objQuery   =& SC_Query_Ex::getSingletonInstance();
349        $objQuery->setWhere($searchCondition['where_for_count']);
350        $objProduct = new SC_Product_Ex();
351
352        return $objProduct->findProductCount($objQuery, $searchCondition['arrval']);
353    }
354
355    /**
356     * 検索条件のwhere文とかを取得
357     *
358     * @return array
359     */
360    public function lfGetSearchCondition($arrSearchData)
361    {
362        $searchCondition = array(
363            'where'             => '',
364            'arrval'            => array(),
365            'where_category'    => '',
366            'arrvalCategory'    => array()
367        );
368
369        // カテゴリからのWHERE文字列取得
370        if ($arrSearchData['category_id'] != 0) {
371            list($searchCondition['where_category'], $searchCondition['arrvalCategory']) = SC_Helper_DB_Ex::sfGetCatWhere($arrSearchData['category_id']);
372        }
373        // ▼対象商品IDの抽出
374        // 商品検索条件の作成(未削除、表示)
375        $searchCondition['where'] = SC_Product_Ex::getProductDispConditions('alldtl');
376
377        if (strlen($searchCondition['where_category']) >= 1) {
378            $searchCondition['where'] .= ' AND EXISTS (SELECT * FROM dtb_product_categories WHERE ' . $searchCondition['where_category'] . ' AND product_id = alldtl.product_id)';
379            $searchCondition['arrval'] = array_merge($searchCondition['arrval'], $searchCondition['arrvalCategory']);
380        }
381
382        // 商品名をwhere文に
383        $name = $arrSearchData['name'];
384        $name = str_replace(',', '', $name);
385        // 全角スペースを半角スペースに変換
386        $name = str_replace(' ', ' ', $name);
387        // スペースでキーワードを分割
388        $names = preg_split('/ +/', $name);
389        // 分割したキーワードを一つずつwhere文に追加
390        foreach ($names as $val) {
391            if (strlen($val) > 0) {
392                $searchCondition['where']    .= ' AND ( alldtl.name ILIKE ? OR alldtl.comment3 ILIKE ?) ';
393                $searchCondition['arrval'][]  = "%$val%";
394                $searchCondition['arrval'][]  = "%$val%";
395            }
396        }
397
398        // メーカーらのWHERE文字列取得
399        if ($arrSearchData['maker_id']) {
400            $searchCondition['where']   .= ' AND alldtl.maker_id = ? ';
401            $searchCondition['arrval'][] = $arrSearchData['maker_id'];
402        }
403
404        // 在庫無し商品の非表示
405        if (NOSTOCK_HIDDEN) {
406            $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))';
407        }
408
409        // XXX 一時期内容が異なっていたことがあるので別要素にも格納している。
410        $searchCondition['where_for_count'] = $searchCondition['where'];
411
412        return $searchCondition;
413    }
414
415    /**
416     * カートに入れる商品情報にエラーがあったら戻す
417     *
418     * @param integer $product_id
419     * @return str
420     */
421    public function lfSetSelectedData(&$arrProducts, $arrForm, $arrErr, $product_id)
422    {
423        $js_fnOnLoad = '';
424        foreach (array_keys($arrProducts) as $key) {
425            if ($arrProducts[$key]['product_id'] == $product_id) {
426                $arrProducts[$key]['product_class_id']  = $arrForm['product_class_id'];
427                $arrProducts[$key]['classcategory_id1'] = $arrForm['classcategory_id1'];
428                $arrProducts[$key]['classcategory_id2'] = $arrForm['classcategory_id2'];
429                $arrProducts[$key]['quantity']          = $arrForm['quantity'];
430                $arrProducts[$key]['arrErr']            = $arrErr;
431                $classcategory_id2 = SC_Utils_Ex::jsonEncode($arrForm['classcategory_id2']);
432                $js_fnOnLoad .= "fnSetClassCategories(document.product_form{$arrProducts[$key]['product_id']}, {$classcategory_id2});";
433            }
434        }
435
436        return $js_fnOnLoad;
437    }
438
439    /**
440     * カートに商品を追加
441     *
442     * @return void
443     */
444    public function lfAddCart($arrForm)
445    {
446        $objCartSess = new SC_CartSession_Ex();
447
448        $product_class_id = $arrForm['product_class_id'];
449        $objCartSess->addProduct($product_class_id, $arrForm['quantity']);
450    }
451
452    /**
453     * 商品情報配列に商品ステータス情報を追加する
454     *
455     * @param  Array $arrProducts    商品一覧情報
456     * @param  Array $arrStatus      商品ステータス配列
457     * @param  Array $arrStatusImage スタータス画像配列
458     * @return Array $arrProducts 商品一覧情報
459     */
460    public function setStatusDataTo($arrProducts, $arrStatus, $arrStatusImage)
461    {
462        foreach ($arrProducts['productStatus'] as $product_id => $arrValues) {
463            for ($i = 0; $i < count($arrValues); $i++) {
464                $product_status_id = $arrValues[$i];
465                if (!empty($product_status_id)) {
466                    $arrProductStatus = array(
467                        'status_cd' => $product_status_id,
468                        'status_name' => $arrStatus[$product_status_id],
469                        'status_image' =>$arrStatusImage[$product_status_id],
470                    );
471                    $arrProducts['productStatus'][$product_id][$i] = $arrProductStatus;
472                }
473            }
474        }
475
476        return $arrProducts;
477    }
478
479    /**
480     *
481     * @return void
482     */
483    public function doJson()
484    {
485        $this->arrProducts = $this->setStatusDataTo($this->arrProducts, $this->arrSTATUS, $this->arrSTATUS_IMAGE);
486        SC_Product_Ex::setPriceTaxTo($this->arrProducts);
487
488        // 一覧メイン画像の指定が無い商品のための処理
489        foreach ($this->arrProducts as $key=>$val) {
490            $this->arrProducts[$key]['main_list_image'] = SC_Utils_Ex::sfNoImageMainList($val['main_list_image']);
491        }
492
493        echo SC_Utils_Ex::jsonEncode($this->arrProducts);
494        SC_Response_Ex::actionExit();
495    }
496
497    /**
498     *
499     * @param  SC_Product_Ex $objProduct
500     * @param SC_FormParam_Ex $objFormParam
501     * @return void
502     */
503    public function doDefault(&$objProduct, &$objFormParam)
504    {
505        //商品一覧の表示処理
506        $strnavi            = $this->objNavi->strnavi;
507        // 表示文字列
508        $this->tpl_strnavi  = empty($strnavi) ? '&nbsp;' : $strnavi;
509
510        // 規格1クラス名
511        $this->tpl_class_name1  = $objProduct->className1;
512
513        // 規格2クラス名
514        $this->tpl_class_name2  = $objProduct->className2;
515
516        // 規格1
517        $this->arrClassCat1     = $objProduct->classCats1;
518
519        // 規格1が設定されている
520        $this->tpl_classcat_find1 = $objProduct->classCat1_find;
521        // 規格2が設定されている
522        $this->tpl_classcat_find2 = $objProduct->classCat2_find;
523
524        $this->tpl_stock_find       = $objProduct->stock_find;
525        $this->tpl_product_class_id = $objProduct->product_class_id;
526        $this->tpl_product_type     = $objProduct->product_type;
527
528        // 商品ステータスを取得
529        $this->productStatus = $this->arrProducts['productStatus'];
530        unset($this->arrProducts['productStatus']);
531        $this->tpl_javascript .= 'eccube.productsClassCategories = ' . SC_Utils_Ex::jsonEncode($objProduct->classCategories) . ';';
532        if (SC_Display_Ex::detectDevice() === DEVICE_TYPE_PC) {
533            //onloadスクリプトを設定. 在庫ありの商品のみ出力する
534            foreach ($this->arrProducts as $arrProduct) {
535                if ($arrProduct['stock_unlimited_max'] || $arrProduct['stock_max'] > 0) {
536                    $js_fnOnLoad .= "fnSetClassCategories(document.product_form{$arrProduct['product_id']});";
537                }
538            }
539        }
540
541        //カート処理
542        $target_product_id = intval($this->arrForm['product_id']);
543        if ($target_product_id > 0) {
544            // 商品IDの正当性チェック
545            if (!SC_Utils_Ex::sfIsInt($this->arrForm['product_id'])
546                || !SC_Helper_DB_Ex::sfIsRecord('dtb_products', 'product_id', $this->arrForm['product_id'], 'del_flg = 0 AND status = 1')) {
547                SC_Utils_Ex::sfDispSiteError(PRODUCT_NOT_FOUND);
548            }
549
550            // 入力内容のチェック
551            $arrErr = $this->lfCheckError($objFormParam);
552            if (empty($arrErr)) {
553                $this->lfAddCart($this->arrForm);
554
555                // 開いているカテゴリーツリーを維持するためのパラメーター
556                $arrQueryString = array(
557                    'category_id' => $this->arrForm['category_id'],
558                );
559
560                SC_Response_Ex::sendRedirect(CART_URL, $arrQueryString);
561                SC_Response_Ex::actionExit();
562            }
563            $js_fnOnLoad .= $this->lfSetSelectedData($this->arrProducts, $this->arrForm, $arrErr, $target_product_id);
564        } else {
565            // カート「戻るボタン」用に保持
566            $netURL = new Net_URL();
567            //該当メソッドが無いため、$_SESSIONに直接セット
568            $_SESSION['cart_referer_url'] = $netURL->getURL();
569        }
570
571        $this->tpl_javascript   .= 'function fnOnLoad() {' . $js_fnOnLoad . '}';
572        $this->tpl_onload       .= 'fnOnLoad(); ';
573    }
574}
Note: See TracBrowser for help on using the repository browser.