source: branches/comu-ver2/data/class/pages/products/LC_Page_Products_List.php @ 18277

Revision 18277, 19.8 KB checked in by Seasoft, 15 years ago (diff)
  • 商品規格プルダウンに品切れを表示
  • 商品規格プルダウンの選択によって、関連項目を動的に更新

関連URL:  http://xoops.ec-cube.net/modules/newbb/viewtopic.php?topic_id=3463&forum=10

  • 商品一覧と商品詳細のテンプレートをコードレベルで出来るだけ揃えた
  • 不要な処理を削除
  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id Revision Date
  • 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-2007 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_PATH . "pages/LC_Page.php");
26
27/**
28 * 商品一覧 のページクラス.
29 *
30 * @package Page
31 * @author LOCKON CO.,LTD.
32 * @version $Id:LC_Page_Products_List.php 15532 2007-08-31 14:39:46Z nanasess $
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        $this->lfLoadParam();
87
88        $objView = new SC_SiteView(!$this->inCart);
89        $conn = new SC_DBConn();
90        $objDb = new SC_Helper_DB_Ex();
91
92        // タイトル編集
93        if ($this->mode == 'search') {
94            $this->tpl_subtitle = "検索結果";
95        } elseif ($this->arrSearchData['category_id'] == 0) {
96            $this->tpl_subtitle = "全商品";
97        } else {
98            $arrCat = $objDb->sfGetCat($this->arrSearchData['category_id']);
99            $this->tpl_subtitle = $arrCat['name'];
100        }
101
102        $objQuery = new SC_Query();
103        $count = $objQuery->count("dtb_best_products", "category_id = ?", array($this->arrSearchData['category_id']));
104
105            // 商品一覧の表示処理
106        $this->lfDispProductsList();
107
108            // 検索条件を画面に表示
109            // カテゴリー検索条件
110        if ($this->arrSearchData['category_id'] == 0) {
111            $this->arrSearch['category'] = "指定なし";
112        } else {
113            $arrCat = $conn->getOne("SELECT category_name FROM dtb_category WHERE category_id = ?", array($this->arrSearchData['category_id']));
114            $this->arrSearch['category'] = $arrCat;
115            }
116
117            // メーカー検索条件
118        if (strlen($this->arrSearchData['maker_id']) == 0) {
119            $this->arrSearch['maker'] = "指定なし";
120        } else {
121            $this->arrSearch['maker'] = $name = $conn->getOne("SELECT name FROM dtb_maker WHERE maker_id = ?", $this->arrSearchData['maker_id']);
122            }
123
124            // 商品名検索条件
125        if (strlen($this->arrSearchData['name']) == 0) {
126            $this->arrSearch['name'] = "指定なし";
127        } else {
128            $this->arrSearch['name'] = $this->arrSearchData['name'];
129            }
130
131        // レイアウトデザインを取得
132        $layout = new SC_Helper_PageLayout_Ex();
133        $layout->sfGetPageLayout($this, false, "products/list.php");
134
135        foreach ($this->arrProducts as $arrProduct) {
136            $js_fnOnLoad .= "fnSetClassCategories(document.product_form{$arrProduct['product_id']});\n";
137        }
138       
139        if ($this->inCart) {
140            // 商品IDの正当性チェック
141            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")) {
142                SC_Utils_Ex::sfDispSiteError(PRODUCT_NOT_FOUND);
143            }
144            $product_id = $this->arrForm['product_id'];
145            // 入力内容のチェック
146            $arrErr = $this->lfCheckError($product_id);
147            if (count($arrErr) == 0) {
148                $classcategory_id1 = $this->arrForm['classcategory_id1'];
149                $classcategory_id2 = $this->arrForm['classcategory_id2'];
150                    // 規格1が設定されていない場合
151                if (!$this->tpl_classcat_find1[$product_id]) {
152                        $classcategory_id1 = '0';
153                    }
154                    // 規格2が設定されていない場合
155                if (!$this->tpl_classcat_find2[$product_id]) {
156                        $classcategory_id2 = '0';
157                    }
158                $objCartSess = new SC_CartSession();
159                $objCartSess->addProduct(array($product_id, $classcategory_id1, $classcategory_id2), $this->arrForm['quantity']);
160                    $this->sendRedirect($this->getLocation(URL_CART_TOP));
161                    exit;
162            }
163            foreach (array_keys($this->arrProducts) as $key) {
164                $arrProduct =& $this->arrProducts[$key];
165                if ($arrProduct['product_id'] == $product_id) {
166                    $arrProduct['classcategory_id1'] = $this->arrForm['classcategory_id1'];
167                    $arrProduct['classcategory_id2'] = $this->arrForm['classcategory_id2'];
168                    $arrProduct['quantity'] = $this->arrForm['quantity'];
169                    $arrProduct['arrErr'] = $arrErr;
170                    $js_fnOnLoad .= "fnSetClassCategories(document.product_form{$arrProduct['product_id']}, '{$this->arrForm['classcategory_id2']}');\n";
171                }
172            }
173        }
174        $this->tpl_javascript .= 'function fnOnLoad(){' . $js_fnOnLoad . '}';
175        $this->tpl_onload .= 'fnOnLoad(); ';
176
177        $this->tpl_rnd = SC_Utils_Ex::sfGetRandomString(3);
178
179        $objView->assignobj($this);
180        $objView->display(SITE_FRAME);
181    }
182
183    /**
184     * モバイルページを初期化する.
185     *
186     * @return void
187     */
188    function mobileInit() {
189        $this->init();
190    }
191
192    /**
193     * Page のプロセス(モバイル).
194     *
195     * FIXME スパゲッティ...
196     *
197     * @return void
198     */
199    function mobileProcess() {
200        $this->lfLoadParam();
201
202        $objView = new SC_MobileView();
203        $conn = new SC_DBConn();
204        $objDb = new SC_Helper_DB_Ex();
205
206        // タイトル編集
207        $tpl_search_mode = false;
208
209        if ($this->mode == 'search') {
210            $this->tpl_subtitle = "検索結果";
211            $tpl_search_mode = true;
212        } elseif ($this->arrSearchData['category_id'] == 0) {
213            $this->tpl_subtitle = "全商品";
214        } else {
215            $arrCat = $objDb->sfGetCat($this->arrSearchData['category_id']);
216            $this->tpl_subtitle = $arrCat['name'];
217        }
218
219        $objQuery = new SC_Query();
220        $count = $objQuery->count("dtb_best_products", "category_id = ?", array($this->arrSearchData['category_id']));
221
222            // 商品一覧の表示処理
223        $this->lfDispProductsList();
224
225            // 検索条件を画面に表示
226            // カテゴリー検索条件
227        if ($this->arrSearchData['category_id'] == 0) {
228            $this->arrSearch['category'] = "指定なし";
229        } else {
230                $arrCat = $conn->getOne("SELECT category_name FROM dtb_category WHERE category_id = ?",array($category_id));
231            $this->arrSearch['category'] = $arrCat;
232            }
233
234            // 商品名検索条件
235        if ($this->arrForm['name'] === "") {
236            $this->arrSearch['name'] = "指定なし";
237            } else {
238            $this->arrSearch['name'] = $this->arrForm['name'];
239                    }
240
241        // ページ送り機能用のURLを作成する。
242        $objURL = new Net_URL($_SERVER['PHP_SELF']);
243        foreach ($_REQUEST as $key => $value) {
244            if ($key == session_name() || $key == 'pageno') {
245                continue;
246            }
247            $objURL->addQueryString($key, mb_convert_encoding($value, 'SJIS', CHAR_CODE));
248        }
249
250        if ($this->objNavi->now_page > 1) {
251            $objURL->addQueryString('pageno', $this->objNavi->now_page - 1);
252            $this->tpl_previous_page = $objURL->path . '?' . $objURL->getQueryString();
253        }
254        if ($this->objNavi->now_page < $this->objNavi->max_page) {
255            $objURL->addQueryString('pageno', $this->objNavi->now_page + 1);
256            $this->tpl_next_page = $objURL->path . '?' . $objURL->getQueryString();
257        }
258
259        $this->tpl_search_mode = $tpl_search_mode;
260
261        $this->tpl_mainpage = MOBILE_TEMPLATE_DIR . "products/list.tpl";
262
263        $objView->assignobj($this);
264        $objView->display(SITE_FRAME);
265    }
266
267    /**
268     * デストラクタ.
269     *
270     * @return void
271     */
272    function destroy() {
273        parent::destroy();
274    }
275
276    /**
277     * カテゴリIDの取得
278     *
279     * @return integer カテゴリID
280     */
281    function lfGetCategoryId($category_id) {
282        $objDb = new SC_Helper_DB_Ex();
283       
284        // 指定なしの場合、0 を返す
285        if (
286            strlen($category_id) == 0
287            || (String) $category_id == '0'
288        ) {
289            return 0;
290        }
291       
292        // 正当性チェック
293        if (
294            !SC_Utils_Ex::sfIsInt($category_id)
295                || SC_Utils_Ex::sfIsZeroFilling($category_id)
296            || !$objDb->sfIsRecord('dtb_category', 'category_id', (array)$category_id, 'del_flg = 0')
297        ) {
298            SC_Utils_Ex::sfDispSiteError(CATEGORY_NOT_FOUND);
299    }
300       
301        // 指定されたカテゴリIDを元に正しいカテゴリIDを取得する。
302        $arrCategory_id = $objDb->sfGetCategoryId('', $category_id);
303       
304        if (empty($arrCategory_id)) {
305            SC_Utils_Ex::sfDispSiteError(CATEGORY_NOT_FOUND);
306        }
307       
308        return $arrCategory_id[0];
309    }
310
311    /* 商品一覧の表示 */
312    function lfDispProductsList() {
313
314        $objQuery = new SC_Query();
315        $objDb = new SC_Helper_DB_Ex();
316        $arrval = array();
317        $arrval_order = array();
318        $arrval_category = array();
319       
320        // カテゴリからのWHERE文字列取得
321        if ($this->arrSearchData['category_id'] != 0) {
322            list($where_category, $arrval_category) = $objDb->sfGetCatWhere($this->arrSearchData['category_id']);
323        }
324       
325        // ▼対象商品IDの抽出
326        // 商品検索条件の作成(未削除、表示)
327        $where = "del_flg = 0 AND status = 1 ";
328       
329        // 在庫無し商品の非表示
330        if (NOSTOCK_HIDDEN === true) {
331            $where .= ' AND (stock_max >= 1 OR stock_unlimited_max = 1)';
332        }
333       
334        if (strlen($where_category) >= 1) {
335            $where.= " AND $where_category";
336            $arrval = array_merge($arrval, $arrval_category);
337        }
338
339        // 商品名をwhere文に
340        $name = $this->arrSearchData['name'];
341        $name = ereg_replace(",", "", $name);// XXX
342        // 全角スペースを半角スペースに変換
343        $name = str_replace(' ', ' ', $name);
344        // スペースでキーワードを分割
345        $names = preg_split("/ +/", $name);
346        // 分割したキーワードを一つずつwhere文に追加
347        foreach ($names as $val) {
348            if ( strlen($val) > 0 ) {
349                $where .= " AND ( name ILIKE ? OR comment3 ILIKE ?) ";
350                $ret = SC_Utils_Ex::sfManualEscape($val);
351                $arrval[] = "%$ret%";
352                $arrval[] = "%$ret%";
353            }
354        }
355
356        // メーカーらのWHERE文字列取得
357        if ($this->arrSearchData['maker_id']) {
358            $where .= " AND maker_id = ? ";
359            $arrval[] = $this->arrSearchData['maker_id'];
360        }
361       
362        // 対象商品IDの抽出
363        $arrProduct_id = $objQuery->getCol('vw_products_allclass AS allcls', 'DISTINCT product_id', $where, $arrval);
364       
365        // 行数の取得
366        $linemax = count($arrProduct_id);
367
368        $this->tpl_linemax = $linemax;   // 何件が該当しました。表示用
369
370        // ページ送りの取得
371        $urlParam = "category_id={$this->arrSearchData['category_id']}&pageno=#page#";
372        $this->objNavi = new SC_PageNavi($this->tpl_pageno, $linemax, $this->disp_number, "fnNaviPage", NAVI_PMAX, $urlParam);
373        $strnavi = $this->objNavi->strnavi;
374
375        // 表示文字列
376        $this->tpl_strnavi = empty($strnavi) ? "&nbsp;" : $strnavi;
377        $startno = $this->objNavi->start_row;                 // 開始行
378       
379        // ▼商品詳細取得
380        $col = <<< __EOS__
381             product_id
382            ,product_code_min
383            ,product_code_max
384            ,name
385            ,comment1
386            ,comment2
387            ,comment3
388            ,main_list_comment
389            ,main_image
390            ,main_list_image
391            ,price01_min
392            ,price01_max
393            ,price02_min
394            ,price02_max
395            ,stock_min
396            ,stock_max
397            ,stock_unlimited_min
398            ,stock_unlimited_max
399            ,point_rate
400            ,sale_limit
401            ,deliv_date_id
402            ,deliv_fee
403            ,status
404            ,product_flag
405            ,del_flg
406__EOS__;
407       
408        $from = "vw_products_allclass_detail AS alldtl";
409       
410        // WHERE 句
411        $where = '0=0';
412        if (is_array($arrProduct_id) && !empty($arrProduct_id)) {
413            $where .= ' AND product_id IN (' . implode(',', $arrProduct_id) . ')';
414        } else {
415            // 一致させない
416            $where .= ' AND 0<>0';
417        }
418       
419        // 表示順序
420        switch ($this->orderby) {
421
422            // 販売価格順
423            case 'price':
424                $order = "price02_min, product_id";
425                break;
426
427            // 新着順
428            case 'date':
429                $order = "create_date DESC, product_id";
430                break;
431
432            default:
433                if (strlen($where_category) >= 1) {
434                    $dtb_product_categories = "(SELECT * FROM dtb_product_categories WHERE $where_category)";
435                    $arrval_order = array_merge($arrval_category, $arrval_category);
436                } else {
437                    $dtb_product_categories = 'dtb_product_categories';
438                }
439                $order = <<< __EOS__
440                    (
441                        SELECT
442                             T3.rank
443                        FROM
444                            $dtb_product_categories T2
445                            JOIN dtb_category T3
446                                USING (category_id)
447                        WHERE T2.product_id = alldtl.product_id
448                        ORDER BY T3.rank DESC, T2.rank DESC
449                        LIMIT 1
450                    ) DESC
451                    ,(
452                        SELECT
453                            T2.rank
454                        FROM
455                            $dtb_product_categories T2
456                            JOIN dtb_category T3
457                                USING (category_id)
458                        WHERE T2.product_id = alldtl.product_id
459                        ORDER BY T3.rank DESC, T2.rank DESC
460                        LIMIT 1
461                    ) DESC
462                    ,product_id
463__EOS__;
464                break;
465        }
466       
467        // 取得範囲の指定(開始行番号、行数のセット)
468        $objQuery->setlimitoffset($this->disp_number, $startno);
469        // 表示順序
470        $objQuery->setorder($order);
471       
472        // 検索結果の取得
473        $this->arrProducts = $objQuery->select($col, $from, $where, $arrval_order);
474        // ▲商品詳細取得
475       
476        $arrProductId = array();
477        // 規格セレクトボックス設定
478        foreach ($this->arrProducts as $product) {
479            $arrProductId[] = $product['product_id'];
480            // 購入制限数を取得
481            $this->lfGetSaleLimit($product);
482        }
483       
484        require_once CLASS_PATH . 'SC_Product.php';
485        $objProduct = new SC_Product($arrProductId);
486       
487        // 規格1クラス名
488        $this->tpl_class_name1 = $objProduct->className1;
489
490        // 規格2クラス名
491        $this->tpl_class_name2 = $objProduct->className2;
492       
493        // 規格1
494        $this->arrClassCat1 = $objProduct->classCats1;
495
496        // 規格1が設定されている
497        $this->tpl_classcat_find1 = $objProduct->classCat1_find;
498        // 規格2が設定されている
499        $this->tpl_classcat_find2 = $objProduct->classCat2_find;
500
501        $this->tpl_stock_find = $objProduct->stock_find;
502
503        $productsClassCategories = $objProduct->classCategories;
504       
505        require_once DATA_PATH . 'module/Services/JSON.php';
506        $objJson = new Services_JSON();
507        $this->tpl_javascript .= 'productsClassCategories = ' . $objJson->encode($productsClassCategories) . '; ';
508    }
509
510    /* 入力内容のチェック */
511    function lfCheckError($id) {
512
513        // 入力データを渡す。
514        $objErr = new SC_CheckError($this->arrForm);
515
516        // 複数項目チェック
517        if ($this->tpl_classcat_find1[$id]) {
518            $objErr->doFunc(array("規格1", 'classcategory_id1', INT_LEN), array("EXIST_CHECK", "NUM_CHECK", "MAX_LENGTH_CHECK"));
519        }
520        if ($this->tpl_classcat_find2[$id]) {
521            $objErr->doFunc(array("規格2", 'classcategory_id2', INT_LEN), array("EXIST_CHECK", "NUM_CHECK", "MAX_LENGTH_CHECK"));
522        }
523        $objErr->doFunc(array("数量", 'quantity', INT_LEN), array("EXIST_CHECK", "ZERO_CHECK", "NUM_CHECK", "MAX_LENGTH_CHECK"));
524
525        return $objErr->arrErr;
526    }
527
528    // 購入制限数の設定
529    // XXX 標準実装では「tpl_sale_limit」は利用されていないようである。カスタマイズ用?
530    function lfGetSaleLimit($arrProduct) {
531        //在庫が無限または購入制限値が設定値より大きい場合
532        if (!SC_Utils_Ex::sfIsInt($arrProduct['sale_limit']) || $arrProduct['sale_limit'] > SALE_LIMIT_MAX) {
533            $this->tpl_sale_limit[$arrProduct['product_id']] = SALE_LIMIT_MAX;
534        } else {
535            $this->tpl_sale_limit[$arrProduct['product_id']] = $arrProduct['sale_limit'];
536        }
537    }
538
539    /**
540     * パラメータの読み込み
541     *
542     * @return void
543     */
544    function lfLoadParam() {
545        $this->arrForm = $_GET;
546       
547        $this->mode = $this->arrForm['mode'];
548        $this->arrSearchData['category_id'] = $this->lfGetCategoryId($this->arrForm['category_id']);
549        $this->arrSearchData['maker_id'] = $this->arrForm['maker_id'];
550        $this->arrSearchData['name'] = $this->arrForm['name'];
551        $this->orderby = $this->arrForm['orderby'];
552        // 表示件数
553        if (
554            isset($this->arrForm['disp_number'])
555            && SC_Utils_Ex::sfIsInt($this->arrForm['disp_number'])
556        ) {
557            $this->disp_number = $this->arrForm['disp_number'];
558        } else {
559            //最小表示件数を選択
560            $this->disp_number = current(array_keys($this->arrPRODUCTLISTMAX));
561    }
562        $this->tpl_pageno = $this->arrForm['pageno'];
563        $this->inCart = strlen($this->arrForm['product_id']) >= 1;
564            }
565}
566?>
Note: See TracBrowser for help on using the repository browser.