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

Revision 22897, 21.7 KB checked in by michael_nelson, 11 years ago (diff)

#2273 Merge revisions from 12.2-dev to 2.12-multilang r22861-r22863,r22891-r22892

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