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

Revision 21049, 21.2 KB checked in by 468, 13 years ago (diff)

#1413 スマートフォン版商品一覧用処理(Ajax通信用データ返信)追加

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