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

Revision 23607, 21.4 KB checked in by kimoto, 10 years ago (diff)

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

 https://scrutinizer-ci.com/g/nobuhiko/EC-CUBE/inspections/e168461a-0ee4-40cd-8b59-bbf58965f139/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
261    /**
262     * @param SC_FormParam_Ex $objFormParam
263     */
264    public function lfCheckError($objFormParam)
265    {
266        // 入力データを渡す。
267        $arrForm =  $objFormParam->getHashArray();
268        $objErr = new SC_CheckError_Ex($arrForm);
269        $objErr->arrErr = $objFormParam->checkError();
270
271        // 動的チェック
272        if ($this->tpl_classcat_find1[$arrForm['product_id']]) {
273            $objErr->doFunc(array('規格1', 'classcategory_id1'), array('EXIST_CHECK'));
274        }
275        if ($this->tpl_classcat_find2[$arrForm['product_id']]) {
276            $objErr->doFunc(array('規格2', 'classcategory_id2'), array('EXIST_CHECK'));
277        }
278
279        return $objErr->arrErr;
280    }
281
282    /**
283     * パラメーターの読み込み
284     *
285     * @return void
286     */
287    public function lfGetDisplayNum($display_number)
288    {
289        // 表示件数
290        return (SC_Utils_Ex::sfIsInt($display_number))
291            ? $display_number
292            : current(array_keys($this->arrPRODUCTLISTMAX));
293    }
294
295    /**
296     * ページタイトルの設定
297     *
298     * @param string|null $mode
299     * @return str
300     */
301    public function lfGetPageTitle($mode, $category_id = 0)
302    {
303        if ($mode == 'search') {
304            return '検索結果';
305        } elseif ($category_id == 0) {
306            return '全商品';
307        } else {
308            $objCategory = new SC_Helper_Category_Ex();
309            $arrCat = $objCategory->get($category_id);
310
311            return $arrCat['category_name'];
312        }
313    }
314
315    /**
316     * 表示用検索条件の設定
317     *
318     * @return array
319     */
320    public function lfGetSearchConditionDisp($arrSearchData)
321    {
322        $objQuery   =& SC_Query_Ex::getSingletonInstance();
323        $arrSearch  = array('category' => '指定なし', 'maker' => '指定なし', 'name' => '指定なし');
324        // カテゴリ検索条件
325        if ($arrSearchData['category_id'] > 0) {
326            $arrSearch['category']  = $objQuery->get('category_name', 'dtb_category', 'category_id = ?', array($arrSearchData['category_id']));
327        }
328
329        // メーカー検索条件
330        if (strlen($arrSearchData['maker_id']) > 0) {
331            $objMaker = new SC_Helper_Maker_Ex();
332            $maker = $objMaker->getMaker($arrSearchData['maker_id']);
333            $arrSearch['maker']     = $maker['name'];
334        }
335
336        // 商品名検索条件
337        if (strlen($arrSearchData['name']) > 0) {
338            $arrSearch['name']      = $arrSearchData['name'];
339        }
340
341        return $arrSearch;
342    }
343
344    /**
345     * 該当件数の取得
346     *
347     * @return int
348     */
349    public function lfGetProductAllNum($searchCondition)
350    {
351        // 検索結果対象となる商品の数を取得
352        $objQuery   =& SC_Query_Ex::getSingletonInstance();
353        $objQuery->setWhere($searchCondition['where_for_count']);
354        $objProduct = new SC_Product_Ex();
355
356        return $objProduct->findProductCount($objQuery, $searchCondition['arrval']);
357    }
358
359    /**
360     * 検索条件のwhere文とかを取得
361     *
362     * @return array
363     */
364    public function lfGetSearchCondition($arrSearchData)
365    {
366        $searchCondition = array(
367            'where'             => '',
368            'arrval'            => array(),
369            'where_category'    => '',
370            'arrvalCategory'    => array()
371        );
372
373        // カテゴリからのWHERE文字列取得
374        if ($arrSearchData['category_id'] != 0) {
375            list($searchCondition['where_category'], $searchCondition['arrvalCategory']) = SC_Helper_DB_Ex::sfGetCatWhere($arrSearchData['category_id']);
376        }
377        // ▼対象商品IDの抽出
378        // 商品検索条件の作成(未削除、表示)
379        $searchCondition['where'] = SC_Product_Ex::getProductDispConditions('alldtl');
380
381        if (strlen($searchCondition['where_category']) >= 1) {
382            $searchCondition['where'] .= ' AND EXISTS (SELECT * FROM dtb_product_categories WHERE ' . $searchCondition['where_category'] . ' AND product_id = alldtl.product_id)';
383            $searchCondition['arrval'] = array_merge($searchCondition['arrval'], $searchCondition['arrvalCategory']);
384        }
385
386        // 商品名をwhere文に
387        $name = $arrSearchData['name'];
388        $name = str_replace(',', '', $name);
389        // 全角スペースを半角スペースに変換
390        $name = str_replace(' ', ' ', $name);
391        // スペースでキーワードを分割
392        $names = preg_split('/ +/', $name);
393        // 分割したキーワードを一つずつwhere文に追加
394        foreach ($names as $val) {
395            if (strlen($val) > 0) {
396                $searchCondition['where']    .= ' AND ( alldtl.name ILIKE ? OR alldtl.comment3 ILIKE ?) ';
397                $searchCondition['arrval'][]  = "%$val%";
398                $searchCondition['arrval'][]  = "%$val%";
399            }
400        }
401
402        // メーカーらのWHERE文字列取得
403        if ($arrSearchData['maker_id']) {
404            $searchCondition['where']   .= ' AND alldtl.maker_id = ? ';
405            $searchCondition['arrval'][] = $arrSearchData['maker_id'];
406        }
407
408        // 在庫無し商品の非表示
409        if (NOSTOCK_HIDDEN) {
410            $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))';
411        }
412
413        // XXX 一時期内容が異なっていたことがあるので別要素にも格納している。
414        $searchCondition['where_for_count'] = $searchCondition['where'];
415
416        return $searchCondition;
417    }
418
419    /**
420     * カートに入れる商品情報にエラーがあったら戻す
421     *
422     * @param integer $product_id
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  SC_Product_Ex $objProduct
504     * @param SC_FormParam_Ex $objFormParam
505     * @return void
506     */
507    public function doDefault(&$objProduct, &$objFormParam)
508    {
509        //商品一覧の表示処理
510        $strnavi            = $this->objNavi->strnavi;
511        // 表示文字列
512        $this->tpl_strnavi  = empty($strnavi) ? '&nbsp;' : $strnavi;
513
514        // 規格1クラス名
515        $this->tpl_class_name1  = $objProduct->className1;
516
517        // 規格2クラス名
518        $this->tpl_class_name2  = $objProduct->className2;
519
520        // 規格1
521        $this->arrClassCat1     = $objProduct->classCats1;
522
523        // 規格1が設定されている
524        $this->tpl_classcat_find1 = $objProduct->classCat1_find;
525        // 規格2が設定されている
526        $this->tpl_classcat_find2 = $objProduct->classCat2_find;
527
528        $this->tpl_stock_find       = $objProduct->stock_find;
529        $this->tpl_product_class_id = $objProduct->product_class_id;
530        $this->tpl_product_type     = $objProduct->product_type;
531
532        // 商品ステータスを取得
533        $this->productStatus = $this->arrProducts['productStatus'];
534        unset($this->arrProducts['productStatus']);
535        $this->tpl_javascript .= 'eccube.productsClassCategories = ' . SC_Utils_Ex::jsonEncode($objProduct->classCategories) . ';';
536        if (SC_Display_Ex::detectDevice() === DEVICE_TYPE_PC) {
537            //onloadスクリプトを設定. 在庫ありの商品のみ出力する
538            foreach ($this->arrProducts as $arrProduct) {
539                if ($arrProduct['stock_unlimited_max'] || $arrProduct['stock_max'] > 0) {
540                    $js_fnOnLoad .= "fnSetClassCategories(document.product_form{$arrProduct['product_id']});";
541                }
542            }
543        }
544
545        //カート処理
546        $target_product_id = intval($this->arrForm['product_id']);
547        if ($target_product_id > 0) {
548            // 商品IDの正当性チェック
549            if (!SC_Utils_Ex::sfIsInt($this->arrForm['product_id'])
550                || !SC_Helper_DB_Ex::sfIsRecord('dtb_products', 'product_id', $this->arrForm['product_id'], 'del_flg = 0 AND status = 1')) {
551                SC_Utils_Ex::sfDispSiteError(PRODUCT_NOT_FOUND);
552            }
553
554            // 入力内容のチェック
555            $arrErr = $this->lfCheckError($objFormParam);
556            if (empty($arrErr)) {
557                $this->lfAddCart($this->arrForm);
558
559                // 開いているカテゴリーツリーを維持するためのパラメーター
560                $arrQueryString = array(
561                    'category_id' => $this->arrForm['category_id'],
562                );
563
564                SC_Response_Ex::sendRedirect(CART_URL, $arrQueryString);
565                SC_Response_Ex::actionExit();
566            }
567            $js_fnOnLoad .= $this->lfSetSelectedData($this->arrProducts, $this->arrForm, $arrErr, $target_product_id);
568        } else {
569            // カート「戻るボタン」用に保持
570            $netURL = new Net_URL();
571            //該当メソッドが無いため、$_SESSIONに直接セット
572            $_SESSION['cart_referer_url'] = $netURL->getURL();
573        }
574
575        $this->tpl_javascript   .= 'function fnOnLoad() {' . $js_fnOnLoad . '}';
576        $this->tpl_onload       .= 'fnOnLoad(); ';
577    }
578}
Note: See TracBrowser for help on using the repository browser.