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

Revision 23065, 21.7 KB checked in by pineray, 11 years ago (diff)

#2342 JavaScript?のグローバルな宣言を減らす
商品関連.

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