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

Revision 22578, 19.6 KB checked in by pineray, 11 years ago (diff)

#2156 pageクラスからdtb_makerテーブルを直接指定している箇所をなくす
メイカー管理のページクラス内の処理を他と統一する

  • 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-2013 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
37    // {{{ properties
38
39    /** テンプレートクラス名1 */
40    var $tpl_class_name1 = array();
41
42    /** テンプレートクラス名2 */
43    var $tpl_class_name2 = array();
44
45    /** JavaScript テンプレート */
46    var $tpl_javascript;
47
48    var $orderby;
49
50    var $mode;
51
52    /** 検索条件(内部データ) */
53    var $arrSearchData = array();
54
55    /** 検索条件(表示用) */
56    var $arrSearch = array();
57
58    var $tpl_subtitle = '';
59
60    /** ランダム文字列 **/
61    var $tpl_rnd = '';
62
63    // }}}
64    // {{{ functions
65
66    /**
67     * Page を初期化する.
68     *
69     * @return void
70     */
71    function init()
72    {
73        parent::init();
74
75        $masterData                 = new SC_DB_MasterData_Ex();
76        $this->arrSTATUS            = $masterData->getMasterData('mtb_status');
77        $this->arrSTATUS_IMAGE      = $masterData->getMasterData('mtb_status_image');
78        $this->arrDELIVERYDATE      = $masterData->getMasterData('mtb_delivery_date');
79        $this->arrPRODUCTLISTMAX    = $masterData->getMasterData('mtb_product_list_max');
80    }
81
82    /**
83     * Page のプロセス.
84     *
85     * @return void
86     */
87    function process()
88    {
89        parent::process();
90        $this->action();
91        $this->sendResponse();
92    }
93
94    /**
95     * Page のAction.
96     *
97     * @return void
98     */
99    function action()
100    {
101
102        $objProduct = new SC_Product_Ex();
103
104        $this->arrForm = $_REQUEST;//時間が無いのでコレで勘弁してください。 tao_s
105        //modeの取得
106        $this->mode = $this->getMode();
107
108        //表示条件の取得
109        $this->arrSearchData = array(
110            'category_id'   => $this->lfGetCategoryId(intval($this->arrForm['category_id'])),
111            'maker_id'      => intval($this->arrForm['maker_id']),
112            'name'          => $this->arrForm['name']
113        );
114        $this->orderby = $this->arrForm['orderby'];
115
116        //ページング設定
117        $this->tpl_pageno   = $this->arrForm['pageno'];
118        $this->disp_number  = $this->lfGetDisplayNum($this->arrForm['disp_number']);
119
120        // 画面に表示するサブタイトルの設定
121        $this->tpl_subtitle = $this->lfGetPageTitle($this->mode, $this->arrSearchData['category_id']);
122
123        // 画面に表示する検索条件を設定
124        $this->arrSearch    = $this->lfGetSearchConditionDisp($this->arrSearchData);
125
126        // 商品一覧データの取得
127        $arrSearchCondition = $this->lfGetSearchCondition($this->arrSearchData);
128        $this->tpl_linemax  = $this->lfGetProductAllNum($arrSearchCondition);
129        $urlParam           = "category_id={$this->arrSearchData['category_id']}&pageno=#page#";
130        // モバイルの場合に検索条件をURLの引数に追加
131        if (SC_Display_Ex::detectDevice() === DEVICE_TYPE_MOBILE) {
132            $searchNameUrl = urlencode(mb_convert_encoding($this->arrSearchData['name'], 'SJIS-win', 'UTF-8'));
133            $urlParam .= "&mode={$this->mode}&name={$searchNameUrl}&orderby={$this->orderby}";
134        }
135        $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);
136        $this->arrProducts  = $this->lfGetProductsList($arrSearchCondition, $this->disp_number, $this->objNavi->start_row, $this->tpl_linemax, $objProduct);
137
138        switch ($this->getMode()) {
139
140            case 'json':
141                $this->doJson($objProduct);
142                break;
143
144            default:
145                $this->doDefault($objProduct);
146                break;
147        }
148
149        $this->tpl_rnd = SC_Utils_Ex::sfGetRandomString(3);
150    }
151
152    /**
153     * デストラクタ.
154     *
155     * @return void
156     */
157    function destroy()
158    {
159        parent::destroy();
160    }
161
162    /**
163     * カテゴリIDの取得
164     *
165     * @return integer カテゴリID
166     */
167    function lfGetCategoryId($category_id)
168    {
169
170        // 指定なしの場合、0 を返す
171        if (empty($category_id)) return 0;
172
173        // 正当性チェック
174        if (!SC_Utils_Ex::sfIsInt($category_id)
175            || SC_Utils_Ex::sfIsZeroFilling($category_id)
176            || !SC_Helper_DB_Ex::sfIsRecord('dtb_category', 'category_id', (array)$category_id, 'del_flg = 0')
177            ) {
178            SC_Utils_Ex::sfDispSiteError(CATEGORY_NOT_FOUND);
179        }
180
181        // 指定されたカテゴリIDを元に正しいカテゴリIDを取得する。
182        $arrCategory_id = SC_Helper_DB_Ex::sfGetCategoryId('', $category_id);
183
184        if (empty($arrCategory_id)) {
185            SC_Utils_Ex::sfDispSiteError(CATEGORY_NOT_FOUND);
186        }
187
188        return $arrCategory_id[0];
189    }
190
191    /* 商品一覧の表示 */
192    function lfGetProductsList($searchCondition, $disp_number, $startno, $linemax, &$objProduct)
193    {
194
195        $arrOrderVal = array();
196
197        $objQuery =& SC_Query_Ex::getSingletonInstance();
198        // 表示順序
199        switch ($this->orderby) {
200            // 販売価格が安い順
201            case 'price':
202                $objProduct->setProductsOrder('price02', 'dtb_products_class', 'ASC');
203                break;
204
205            // 新着順
206            case 'date':
207                $objProduct->setProductsOrder('create_date', 'dtb_products', 'DESC');
208                break;
209
210            default:
211                if (strlen($searchCondition['where_category']) >= 1) {
212                    $dtb_product_categories = '(SELECT * FROM dtb_product_categories WHERE '.$searchCondition['where_category'].')';
213                    $arrOrderVal           = $searchCondition['arrvalCategory'];
214                } else {
215                    $dtb_product_categories = 'dtb_product_categories';
216                }
217                $order = <<< __EOS__
218                    (
219                        SELECT
220                            T3.rank * 2147483648 + T2.rank
221                        FROM
222                            $dtb_product_categories T2
223                            JOIN dtb_category T3
224                              ON T2.category_id = T3.category_id
225                        WHERE T2.product_id = alldtl.product_id
226                        ORDER BY T3.rank DESC, T2.rank DESC
227                        LIMIT 1
228                    ) DESC
229                    ,product_id DESC
230__EOS__;
231                $objQuery->setOrder($order);
232                break;
233        }
234        // 取得範囲の指定(開始行番号、行数のセット)
235        $objQuery->setLimitOffset($disp_number, $startno);
236        $objQuery->setWhere($searchCondition['where']);
237
238        // 表示すべきIDとそのIDの並び順を一気に取得
239        $arrProductId = $objProduct->findProductIdsOrder($objQuery, array_merge($searchCondition['arrval'], $arrOrderVal));
240
241        $objQuery =& SC_Query_Ex::getSingletonInstance();
242        $arrProducts = $objProduct->getListByProductIds($objQuery, $arrProductId);
243
244        // 規格を設定
245        $objProduct->setProductsClassByProductIds($arrProductId);
246        $arrProducts['productStatus'] = $objProduct->getProductStatus($arrProductId);
247        return $arrProducts;
248    }
249
250    /* 入力内容のチェック */
251    function lfCheckError($product_id, &$arrForm, $tpl_classcat_find1, $tpl_classcat_find2)
252    {
253
254        // 入力データを渡す。
255        $objErr = new SC_CheckError_Ex($arrForm);
256
257        // 複数項目チェック
258        if ($tpl_classcat_find1[$product_id]) {
259            $objErr->doFunc(array('規格1', 'classcategory_id1', INT_LEN), array('EXIST_CHECK', 'NUM_CHECK', 'MAX_LENGTH_CHECK'));
260        }
261        if ($tpl_classcat_find2[$product_id]) {
262            $objErr->doFunc(array('規格2', 'classcategory_id2', INT_LEN), array('EXIST_CHECK', 'NUM_CHECK', 'MAX_LENGTH_CHECK'));
263        }
264
265        $objErr->doFunc(array('商品規格ID', 'product_class_id', INT_LEN), array('EXIST_CHECK', 'NUM_CHECK', 'MAX_LENGTH_CHECK'));
266        $objErr->doFunc(array('数量', 'quantity', INT_LEN), array('EXIST_CHECK', 'ZERO_CHECK', 'NUM_CHECK', 'MAX_LENGTH_CHECK'));
267
268        return $objErr->arrErr;
269    }
270
271    /**
272     * パラメーターの読み込み
273     *
274     * @return void
275     */
276    function lfGetDisplayNum($display_number)
277    {
278        // 表示件数
279        return (SC_Utils_Ex::sfIsInt($display_number))
280            ? $display_number
281            : current(array_keys($this->arrPRODUCTLISTMAX));
282    }
283
284    /**
285     * ページタイトルの設定
286     *
287     * @return str
288     */
289    function lfGetPageTitle($mode, $category_id = 0)
290    {
291        if ($mode == 'search') {
292            return '検索結果';
293        } elseif ($category_id == 0) {
294            return '全商品';
295        } else {
296            $arrCat = SC_Helper_DB_Ex::sfGetCat($category_id);
297            return $arrCat['name'];
298        }
299    }
300
301    /**
302     * 表示用検索条件の設定
303     *
304     * @return array
305     */
306    function lfGetSearchConditionDisp($arrSearchData)
307    {
308        $objQuery   =& SC_Query_Ex::getSingletonInstance();
309        $arrSearch  = array('category' => '指定なし', 'maker' => '指定なし', 'name' => '指定なし');
310        // カテゴリ検索条件
311        if ($arrSearchData['category_id'] > 0) {
312            $arrSearch['category']  = $objQuery->get('category_name', 'dtb_category', 'category_id = ?', array($arrSearchData['category_id']));
313        }
314
315        // メーカー検索条件
316        if (strlen($arrSearchData['maker_id']) > 0) {
317            $objMaker = new SC_Helper_Maker_Ex();
318            $maker = $objMaker->get($arrSearchData['maker_id']);
319            $arrSearch['maker']     = $maker['name'];
320        }
321
322        // 商品名検索条件
323        if (strlen($arrSearchData['name']) > 0) {
324            $arrSearch['name']      = $arrSearchData['name'];
325        }
326        return $arrSearch;
327    }
328
329    /**
330     * 該当件数の取得
331     *
332     * @return int
333     */
334    function lfGetProductAllNum($searchCondition)
335    {
336        // 検索結果対象となる商品の数を取得
337        $objQuery   =& SC_Query_Ex::getSingletonInstance();
338        $objQuery->setWhere($searchCondition['where_for_count']);
339        $objProduct = new SC_Product_Ex();
340        return $objProduct->findProductCount($objQuery, $searchCondition['arrval']);
341    }
342
343    /**
344     * 検索条件のwhere文とかを取得
345     *
346     * @return array
347     */
348    function lfGetSearchCondition($arrSearchData)
349    {
350        $searchCondition = array(
351            'where'             => '',
352            'arrval'            => array(),
353            'where_category'    => '',
354            'arrvalCategory'    => array()
355        );
356
357        // カテゴリからのWHERE文字列取得
358        if ($arrSearchData['category_id'] != 0) {
359            list($searchCondition['where_category'], $searchCondition['arrvalCategory']) = SC_Helper_DB_Ex::sfGetCatWhere($arrSearchData['category_id']);
360        }
361        // ▼対象商品IDの抽出
362        // 商品検索条件の作成(未削除、表示)
363        $searchCondition['where'] = 'alldtl.del_flg = 0 AND alldtl.status = 1 ';
364
365        if (strlen($searchCondition['where_category']) >= 1) {
366            $searchCondition['where'] .= ' AND EXISTS (SELECT * FROM dtb_product_categories WHERE ' . $searchCondition['where_category'] . ' AND product_id = alldtl.product_id)';
367            $searchCondition['arrval'] = array_merge($searchCondition['arrval'], $searchCondition['arrvalCategory']);
368        }
369
370        // 商品名をwhere文に
371        $name = $arrSearchData['name'];
372        $name = str_replace(',', '', $name);
373        // 全角スペースを半角スペースに変換
374        $name = str_replace(' ', ' ', $name);
375        // スペースでキーワードを分割
376        $names = preg_split('/ +/', $name);
377        // 分割したキーワードを一つずつwhere文に追加
378        foreach ($names as $val) {
379            if (strlen($val) > 0) {
380                $searchCondition['where']    .= ' AND ( alldtl.name ILIKE ? OR alldtl.comment3 ILIKE ?) ';
381                $searchCondition['arrval'][]  = "%$val%";
382                $searchCondition['arrval'][]  = "%$val%";
383            }
384        }
385
386        // メーカーらのWHERE文字列取得
387        if ($arrSearchData['maker_id']) {
388            $searchCondition['where']   .= ' AND alldtl.maker_id = ? ';
389            $searchCondition['arrval'][] = $arrSearchData['maker_id'];
390        }
391
392        // 在庫無し商品の非表示
393        if (NOSTOCK_HIDDEN) {
394            $searchCondition['where'] .= ' AND EXISTS(SELECT * FROM dtb_products_class WHERE product_id = alldtl.product_id AND del_flg = 0 AND (stock >= 1 OR stock_unlimited = 1))';
395        }
396
397        // XXX 一時期内容が異なっていたことがあるので別要素にも格納している。
398        $searchCondition['where_for_count'] = $searchCondition['where'];
399
400        return $searchCondition;
401    }
402
403    /**
404     * カートに入れる商品情報にエラーがあったら戻す
405     *
406     * @return str
407     */
408    function lfSetSelectedData(&$arrProducts, $arrForm, $arrErr, $product_id)
409    {
410        $js_fnOnLoad = '';
411        foreach ($arrProducts as $key => $value) {
412            if ($arrProducts[$key]['product_id'] == $product_id) {
413
414                $arrProducts[$key]['product_class_id']  = $arrForm['product_class_id'];
415                $arrProducts[$key]['classcategory_id1'] = $arrForm['classcategory_id1'];
416                $arrProducts[$key]['classcategory_id2'] = $arrForm['classcategory_id2'];
417                $arrProducts[$key]['quantity']          = $arrForm['quantity'];
418                $arrProducts[$key]['arrErr']            = $arrErr;
419                $js_fnOnLoad .= "fnSetClassCategories(document.product_form{$arrProducts[$key]['product_id']}, '{$arrForm['classcategory_id2']}');";
420            }
421        }
422        return $js_fnOnLoad;
423    }
424
425    /**
426     * カートに商品を追加
427     *
428     * @return void
429     */
430    function lfAddCart($arrForm, $referer)
431    {
432        $product_class_id = $arrForm['product_class_id'];
433        $objCartSess = new SC_CartSession_Ex();
434        $objCartSess->addProduct($product_class_id, $arrForm['quantity']);
435    }
436
437    /**
438     * 商品情報配列に商品ステータス情報を追加する
439     *
440     * @param Array $arrProducts 商品一覧情報
441     * @param Array $arrStatus 商品ステータス配列
442     * @param Array $arrStatusImage スタータス画像配列
443     * @return Array $arrProducts 商品一覧情報
444     */
445    function setStatusDataTo($arrProducts, $arrStatus, $arrStatusImage)
446    {
447
448        foreach ($arrProducts['productStatus'] as $product_id => $arrValues) {
449            for ($i = 0; $i < count($arrValues); $i++) {
450                $product_status_id = $arrValues[$i];
451                if (!empty($product_status_id)) {
452                    $arrProductStatus = array(
453                        'status_cd' => $product_status_id,
454                        'status_name' => $arrStatus[$product_status_id],
455                        'status_image' =>$arrStatusImage[$product_status_id],
456                    );
457                    $arrProducts['productStatus'][$product_id][$i] = $arrProductStatus;
458                }
459            }
460        }
461        return $arrProducts;
462    }
463
464    /**
465     *
466     * @return void
467     */
468    function doJson()
469    {
470        $this->arrProducts = $this->setStatusDataTo($this->arrProducts, $this->arrSTATUS, $this->arrSTATUS_IMAGE);
471        SC_Product_Ex::setPriceTaxTo($this->arrProducts);
472
473        // 一覧メイン画像の指定が無い商品のための処理
474        foreach ($this->arrProducts as $key=>$val) {
475            $this->arrProducts[$key]['main_list_image'] = SC_Utils_Ex::sfNoImageMainList($val['main_list_image']);
476        }
477
478        echo SC_Utils_Ex::jsonEncode($this->arrProducts);
479        SC_Response_Ex::actionExit();
480    }
481
482    /**
483     *
484     * @param type $objProduct
485     * @return void
486     */
487    function doDefault(&$objProduct)
488    {
489        //商品一覧の表示処理
490        $strnavi            = $this->objNavi->strnavi;
491        // 表示文字列
492        $this->tpl_strnavi  = empty($strnavi) ? '&nbsp;' : $strnavi;
493
494        // 規格1クラス名
495        $this->tpl_class_name1  = $objProduct->className1;
496
497        // 規格2クラス名
498        $this->tpl_class_name2  = $objProduct->className2;
499
500        // 規格1
501        $this->arrClassCat1     = $objProduct->classCats1;
502
503        // 規格1が設定されている
504        $this->tpl_classcat_find1 = $objProduct->classCat1_find;
505        // 規格2が設定されている
506        $this->tpl_classcat_find2 = $objProduct->classCat2_find;
507
508        $this->tpl_stock_find       = $objProduct->stock_find;
509        $this->tpl_product_class_id = $objProduct->product_class_id;
510        $this->tpl_product_type     = $objProduct->product_type;
511
512        // 商品ステータスを取得
513        $this->productStatus = $this->arrProducts['productStatus'];
514        unset($this->arrProducts['productStatus']);
515        $this->tpl_javascript .= 'var productsClassCategories = ' . SC_Utils_Ex::jsonEncode($objProduct->classCategories) . ';';
516        //onloadスクリプトを設定. 在庫ありの商品のみ出力する
517        foreach ($this->arrProducts as $arrProduct) {
518            if ($arrProduct['stock_unlimited_max'] || $arrProduct['stock_max'] > 0) {
519                $js_fnOnLoad .= "fnSetClassCategories(document.product_form{$arrProduct['product_id']});";
520            }
521        }
522
523        //カート処理
524        $target_product_id = intval($this->arrForm['product_id']);
525        if ($target_product_id > 0) {
526            // 商品IDの正当性チェック
527            if (!SC_Utils_Ex::sfIsInt($this->arrForm['product_id'])
528                || !SC_Helper_DB_Ex::sfIsRecord('dtb_products', 'product_id', $this->arrForm['product_id'], 'del_flg = 0 AND status = 1')) {
529                SC_Utils_Ex::sfDispSiteError(PRODUCT_NOT_FOUND);
530            }
531
532            // 入力内容のチェック
533            $arrErr = $this->lfCheckError($target_product_id, $this->arrForm, $this->tpl_classcat_find1, $this->tpl_classcat_find2);
534            if (empty($arrErr)) {
535                $this->lfAddCart($this->arrForm, $_SERVER['HTTP_REFERER']);
536
537
538                SC_Response_Ex::sendRedirect(CART_URLPATH);
539                SC_Response_Ex::actionExit();
540            }
541            $js_fnOnLoad .= $this->lfSetSelectedData($this->arrProducts, $this->arrForm, $arrErr, $target_product_id);
542        } else {
543            // カート「戻るボタン」用に保持
544            $netURL = new Net_URL();
545            //該当メソッドが無いため、$_SESSIONに直接セット
546            $_SESSION['cart_referer_url'] = $netURL->getURL();
547        }
548
549        $this->tpl_javascript   .= 'function fnOnLoad()
550        {' . $js_fnOnLoad . '}';
551        $this->tpl_onload       .= 'fnOnLoad(); ';
552    }
553}
Note: See TracBrowser for help on using the repository browser.