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

Revision 21386, 19.7 KB checked in by Seasoft, 12 years ago (diff)

#1591 (商品登録CSVでカテゴリを指定せず登録すると、商品検索で抽出されない)

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