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

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