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

Revision 21957, 19.7 KB checked in by pineray, 12 years ago (diff)

r21953 を差し戻し.

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