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

Revision 19684, 19.3 KB checked in by nanasess, 13 years ago (diff)

#635 処理の単純化

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