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

Revision 22735, 19.9 KB checked in by h_yoshimoto, 11 years ago (diff)

#2193 ユニットテストチームのコミットをマージ(from camp/camp-2_13-tests)

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