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

Revision 20197, 17.9 KB checked in by tao, 12 years ago (diff)

refs #982 とりあえず商品が表示出来る様に修正

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