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

Revision 21184, 19.8 KB checked in by Seasoft, 13 years ago (diff)

#1435 (ソースコードを読みやすくする)

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