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

Revision 21669, 20.1 KB checked in by h_yoshimoto, 12 years ago (diff)

#1692 lc_page_products_list_action_end を複数回実行している箇所を修正

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