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

Revision 23230, 21.8 KB checked in by m_uehara, 11 years ago (diff)

#2363 r23177, r23181 - r23186, r23188 - r23191, r23194, r23197, r23199 - r23218, r23220, r23223 - r23225 をマージ

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