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

Revision 22567, 19.5 KB checked in by shutta, 11 years ago (diff)

#2043 (typo修正・ソース整形・ソースコメントの改善 for 2.12.4)
Zend Framework PHP 標準コーディング規約のコーディングスタイルへ準拠。
classおよびfunctionの開始波括弧「{」のスタイルを修正。

  • 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            $arrSearch['maker']     = $objQuery->get('name', 'dtb_maker', 'maker_id = ?', array($arrSearchData['maker_id']));
318        }
319
320        // 商品名検索条件
321        if (strlen($arrSearchData['name']) > 0) {
322            $arrSearch['name']      = $arrSearchData['name'];
323        }
324        return $arrSearch;
325    }
326
327    /**
328     * 該当件数の取得
329     *
330     * @return int
331     */
332    function lfGetProductAllNum($searchCondition)
333    {
334        // 検索結果対象となる商品の数を取得
335        $objQuery   =& SC_Query_Ex::getSingletonInstance();
336        $objQuery->setWhere($searchCondition['where_for_count']);
337        $objProduct = new SC_Product_Ex();
338        return $objProduct->findProductCount($objQuery, $searchCondition['arrval']);
339    }
340
341    /**
342     * 検索条件のwhere文とかを取得
343     *
344     * @return array
345     */
346    function lfGetSearchCondition($arrSearchData)
347    {
348        $searchCondition = array(
349            'where'             => '',
350            'arrval'            => array(),
351            'where_category'    => '',
352            'arrvalCategory'    => array()
353        );
354
355        // カテゴリからのWHERE文字列取得
356        if ($arrSearchData['category_id'] != 0) {
357            list($searchCondition['where_category'], $searchCondition['arrvalCategory']) = SC_Helper_DB_Ex::sfGetCatWhere($arrSearchData['category_id']);
358        }
359        // ▼対象商品IDの抽出
360        // 商品検索条件の作成(未削除、表示)
361        $searchCondition['where'] = 'alldtl.del_flg = 0 AND alldtl.status = 1 ';
362
363        if (strlen($searchCondition['where_category']) >= 1) {
364            $searchCondition['where'] .= ' AND EXISTS (SELECT * FROM dtb_product_categories WHERE ' . $searchCondition['where_category'] . ' AND product_id = alldtl.product_id)';
365            $searchCondition['arrval'] = array_merge($searchCondition['arrval'], $searchCondition['arrvalCategory']);
366        }
367
368        // 商品名をwhere文に
369        $name = $arrSearchData['name'];
370        $name = str_replace(',', '', $name);
371        // 全角スペースを半角スペースに変換
372        $name = str_replace(' ', ' ', $name);
373        // スペースでキーワードを分割
374        $names = preg_split('/ +/', $name);
375        // 分割したキーワードを一つずつwhere文に追加
376        foreach ($names as $val) {
377            if (strlen($val) > 0) {
378                $searchCondition['where']    .= ' AND ( alldtl.name ILIKE ? OR alldtl.comment3 ILIKE ?) ';
379                $searchCondition['arrval'][]  = "%$val%";
380                $searchCondition['arrval'][]  = "%$val%";
381            }
382        }
383
384        // メーカーらのWHERE文字列取得
385        if ($arrSearchData['maker_id']) {
386            $searchCondition['where']   .= ' AND alldtl.maker_id = ? ';
387            $searchCondition['arrval'][] = $arrSearchData['maker_id'];
388        }
389
390        // 在庫無し商品の非表示
391        if (NOSTOCK_HIDDEN) {
392            $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))';
393        }
394
395        // XXX 一時期内容が異なっていたことがあるので別要素にも格納している。
396        $searchCondition['where_for_count'] = $searchCondition['where'];
397
398        return $searchCondition;
399    }
400
401    /**
402     * カートに入れる商品情報にエラーがあったら戻す
403     *
404     * @return str
405     */
406    function lfSetSelectedData(&$arrProducts, $arrForm, $arrErr, $product_id)
407    {
408        $js_fnOnLoad = '';
409        foreach ($arrProducts as $key => $value) {
410            if ($arrProducts[$key]['product_id'] == $product_id) {
411
412                $arrProducts[$key]['product_class_id']  = $arrForm['product_class_id'];
413                $arrProducts[$key]['classcategory_id1'] = $arrForm['classcategory_id1'];
414                $arrProducts[$key]['classcategory_id2'] = $arrForm['classcategory_id2'];
415                $arrProducts[$key]['quantity']          = $arrForm['quantity'];
416                $arrProducts[$key]['arrErr']            = $arrErr;
417                $js_fnOnLoad .= "fnSetClassCategories(document.product_form{$arrProducts[$key]['product_id']}, '{$arrForm['classcategory_id2']}');";
418            }
419        }
420        return $js_fnOnLoad;
421    }
422
423    /**
424     * カートに商品を追加
425     *
426     * @return void
427     */
428    function lfAddCart($arrForm, $referer)
429    {
430        $product_class_id = $arrForm['product_class_id'];
431        $objCartSess = new SC_CartSession_Ex();
432        $objCartSess->addProduct($product_class_id, $arrForm['quantity']);
433    }
434
435    /**
436     * 商品情報配列に商品ステータス情報を追加する
437     *
438     * @param Array $arrProducts 商品一覧情報
439     * @param Array $arrStatus 商品ステータス配列
440     * @param Array $arrStatusImage スタータス画像配列
441     * @return Array $arrProducts 商品一覧情報
442     */
443    function setStatusDataTo($arrProducts, $arrStatus, $arrStatusImage)
444    {
445
446        foreach ($arrProducts['productStatus'] as $product_id => $arrValues) {
447            for ($i = 0; $i < count($arrValues); $i++) {
448                $product_status_id = $arrValues[$i];
449                if (!empty($product_status_id)) {
450                    $arrProductStatus = array(
451                        'status_cd' => $product_status_id,
452                        'status_name' => $arrStatus[$product_status_id],
453                        'status_image' =>$arrStatusImage[$product_status_id],
454                    );
455                    $arrProducts['productStatus'][$product_id][$i] = $arrProductStatus;
456                }
457            }
458        }
459        return $arrProducts;
460    }
461
462    /**
463     *
464     * @return void
465     */
466    function doJson()
467    {
468        $this->arrProducts = $this->setStatusDataTo($this->arrProducts, $this->arrSTATUS, $this->arrSTATUS_IMAGE);
469        SC_Product_Ex::setPriceTaxTo($this->arrProducts);
470
471        // 一覧メイン画像の指定が無い商品のための処理
472        foreach ($this->arrProducts as $key=>$val) {
473            $this->arrProducts[$key]['main_list_image'] = SC_Utils_Ex::sfNoImageMainList($val['main_list_image']);
474        }
475
476        echo SC_Utils_Ex::jsonEncode($this->arrProducts);
477        SC_Response_Ex::actionExit();
478    }
479
480    /**
481     *
482     * @param type $objProduct
483     * @return void
484     */
485    function doDefault(&$objProduct)
486    {
487        //商品一覧の表示処理
488        $strnavi            = $this->objNavi->strnavi;
489        // 表示文字列
490        $this->tpl_strnavi  = empty($strnavi) ? '&nbsp;' : $strnavi;
491
492        // 規格1クラス名
493        $this->tpl_class_name1  = $objProduct->className1;
494
495        // 規格2クラス名
496        $this->tpl_class_name2  = $objProduct->className2;
497
498        // 規格1
499        $this->arrClassCat1     = $objProduct->classCats1;
500
501        // 規格1が設定されている
502        $this->tpl_classcat_find1 = $objProduct->classCat1_find;
503        // 規格2が設定されている
504        $this->tpl_classcat_find2 = $objProduct->classCat2_find;
505
506        $this->tpl_stock_find       = $objProduct->stock_find;
507        $this->tpl_product_class_id = $objProduct->product_class_id;
508        $this->tpl_product_type     = $objProduct->product_type;
509
510        // 商品ステータスを取得
511        $this->productStatus = $this->arrProducts['productStatus'];
512        unset($this->arrProducts['productStatus']);
513        $this->tpl_javascript .= 'var productsClassCategories = ' . SC_Utils_Ex::jsonEncode($objProduct->classCategories) . ';';
514        //onloadスクリプトを設定. 在庫ありの商品のみ出力する
515        foreach ($this->arrProducts as $arrProduct) {
516            if ($arrProduct['stock_unlimited_max'] || $arrProduct['stock_max'] > 0) {
517                $js_fnOnLoad .= "fnSetClassCategories(document.product_form{$arrProduct['product_id']});";
518            }
519        }
520
521        //カート処理
522        $target_product_id = intval($this->arrForm['product_id']);
523        if ($target_product_id > 0) {
524            // 商品IDの正当性チェック
525            if (!SC_Utils_Ex::sfIsInt($this->arrForm['product_id'])
526                || !SC_Helper_DB_Ex::sfIsRecord('dtb_products', 'product_id', $this->arrForm['product_id'], 'del_flg = 0 AND status = 1')) {
527                SC_Utils_Ex::sfDispSiteError(PRODUCT_NOT_FOUND);
528            }
529
530            // 入力内容のチェック
531            $arrErr = $this->lfCheckError($target_product_id, $this->arrForm, $this->tpl_classcat_find1, $this->tpl_classcat_find2);
532            if (empty($arrErr)) {
533                $this->lfAddCart($this->arrForm, $_SERVER['HTTP_REFERER']);
534
535
536                SC_Response_Ex::sendRedirect(CART_URLPATH);
537                SC_Response_Ex::actionExit();
538            }
539            $js_fnOnLoad .= $this->lfSetSelectedData($this->arrProducts, $this->arrForm, $arrErr, $target_product_id);
540        } else {
541            // カート「戻るボタン」用に保持
542            $netURL = new Net_URL();
543            //該当メソッドが無いため、$_SESSIONに直接セット
544            $_SESSION['cart_referer_url'] = $netURL->getURL();
545        }
546
547        $this->tpl_javascript   .= 'function fnOnLoad()
548        {' . $js_fnOnLoad . '}';
549        $this->tpl_onload       .= 'fnOnLoad(); ';
550    }
551}
Note: See TracBrowser for help on using the repository browser.