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

Revision 22920, 21.6 KB checked in by Seasoft, 11 years ago (diff)

#2278 (商品一覧の値取得処理の変更)

  • 過去に実装していたものがあるので統合します。

#2043 (typo修正・ソース整形・ソースコメントの改善 for 2.13.0)

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