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

Revision 22918, 21.8 KB checked in by shutta, 11 years ago (diff)

#2278 (商品一覧の値取得処理の変更)
r22892 を2_13-devにもコミット。

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