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

Revision 22856, 19.9 KB checked in by Seasoft, 11 years ago (diff)

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