source: branches/version-2_13-dev/data/class/api/operations/ItemSearch.php @ 23605

Revision 23605, 11.2 KB checked in by kimoto, 10 years ago (diff)

#2448 typo修正・ソース整形・ソースコメントの改善 for 2.13.3

Scrutinizer Auto-Fixes

This patch was automatically generated as part of the following inspection:
 https://scrutinizer-ci.com/g/nobuhiko/EC-CUBE/inspections/d8722894-69a6-4b1b-898d-43618035c60d

Enabled analysis tools:

  • PHP Analyzer
  • PHP PDepend
  • PHP Similarity Analyzer
  • PHP Change Tracking Analyzer
Line 
1<?php
2/*
3 * This file is part of EC-CUBE
4 *
5 * Copyright(c) 2000-2014 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/**
25 * APIの基本クラス
26 *
27 * @package Api
28 * @author LOCKON CO.,LTD.
29 * @version $Id$
30 */
31require_once CLASS_EX_REALDIR . 'api_extends/SC_Api_Abstract_Ex.php';
32
33class API_ItemSearch extends SC_Api_Abstract_Ex
34{
35    protected $operation_name = 'ItemSearch';
36    protected $operation_description = '商品検索・商品一覧情報を取得します。';
37    protected $default_auth_types = self::API_AUTH_TYPE_OPEN;
38    protected $default_enable = '1';
39    protected $default_is_log = '0';
40    protected $default_sub_data = '';
41
42    public function doAction($arrParam)
43    {
44        $arrRequest = $this->doInitParam($arrParam);
45        if (!$this->isParamError()) {
46            $masterData                 = new SC_DB_MasterData_Ex();
47            $arrSTATUS            = $masterData->getMasterData('mtb_status');
48            $arrSTATUS_IMAGE      = $masterData->getMasterData('mtb_status_image');
49
50            $objProduct = new SC_Product_Ex();
51            $arrSearchData = array(
52                'category_id' => $arrRequest['BrowseNode'],
53                'maker_name' => $arrRequest['Manufacturer'],
54                'name' => $arrRequest['Keywords'],
55                'orderby' => $arrRequest['Sort'],
56            );
57
58            $arrSearchCondition = $this->getSearchCondition($arrSearchData);
59            $disp_number = 10;
60
61            $objQuery =& SC_Query_Ex::getSingletonInstance();
62            $objQuery->setWhere($arrSearchCondition['where_for_count']);
63            $objProduct = new SC_Product_Ex();
64            $linemax = $objProduct->findProductCount($objQuery, $arrSearchCondition['arrval']);
65            $objNavi = new SC_PageNavi_Ex($arrRequest['ItemPage'], $linemax, $disp_number);
66            $arrProducts = $this->getProductsList($arrSearchCondition, $disp_number, $objNavi->start_row, $linemax, $objProduct);
67
68            if (!SC_Utils_Ex::isBlank($arrProducts)) {
69                $arrProducts = $this->setStatusDataTo($arrProducts, $arrSTATUS, $arrSTATUS_IMAGE);
70                SC_Product_Ex::setPriceTaxTo($arrProducts);
71                foreach ($arrProducts as $key=>$val) {
72                    $arrProducts[$key]['main_list_image'] = SC_Utils_Ex::sfNoImageMainList($val['main_list_image']);
73                }
74
75                $arrData = array();
76                foreach ($arrProducts as $key => $val) {
77                    $arrData[] = array(
78                        'product_id' => $val['product_id'],
79                        'DetailPageURL' => HTTP_URL . 'products/detail.php?product_id=' . $val['product_id'],
80                        'ItemAttributes' => $val
81                        );
82                }
83                $this->setResponse('Item', $arrData);
84
85                return true;
86            } else {
87                $this->addError('ItemSearch.Error', '※ 要求された情報は見つかりませんでした。');
88            }
89        }
90
91        return false;
92    }
93
94    protected function lfInitParam(&$objFormParam)
95    {
96        $objFormParam->addParam('カテゴリID', 'BrowseNode', INT_LEN, 'n', array('NUM_CHECK', 'MAX_LENGTH_CHECK'));
97        $objFormParam->addParam('キーワード', 'Keywords', STEXT_LEN, 'a', array('SPTAB_CHECK', 'MAX_LENGTH_CHECK'));
98        $objFormParam->addParam('メーカー名', 'Manufacturer', STEXT_LEN, 'a', array('SPTAB_CHECK', 'MAX_LENGTH_CHECK'));
99        $objFormParam->addParam('ページ番号', 'ItemPage', INT_LEN, 'n', array('NUM_CHECK', 'MAX_LENGTH_CHECK'));
100        $objFormParam->addParam('ソート', 'Sort', STEXT_LEN, 'a', array('GRAPH_CHECK', 'MAX_LENGTH_CHECK'));
101    }
102
103    public function getResponseGroupName()
104    {
105        return 'Items';
106    }
107
108    /**
109     * 商品一覧の取得
110     *
111     * @param integer $disp_number
112     * @param integer $linemax
113     * @param SC_Product_Ex $objProduct
114     * @return array
115     * TODO: LC_Page_Products_List::lfGetProductsList() と共通化
116     */
117    protected function getProductsList($searchCondition, $disp_number, $startno, $linemax, &$objProduct)
118    {
119        $objQuery =& SC_Query_Ex::getSingletonInstance();
120
121        $arrOrderVal = array();
122
123        // 表示順序
124        switch ($searchCondition['orderby']) {
125            // 販売価格が安い順
126            case 'price':
127                $objProduct->setProductsOrder('price02', 'dtb_products_class', 'ASC');
128                break;
129            // 販売価格が高い順
130            case '-price':
131                $objProduct->setProductsOrder('price02', 'dtb_products_class', 'DESC');
132                break;
133
134            // 新着順
135            case 'releasedate':
136            case 'date':
137                $objProduct->setProductsOrder('create_date', 'dtb_products', 'DESC');
138                break;
139
140            // 新着順
141            case 'releasedate':
142            case 'date':
143                $objProduct->setProductsOrder('create_date', 'dtb_products', 'ASC');
144                break;
145
146            default:
147                if (strlen($searchCondition['where_category']) >= 1) {
148                    $dtb_product_categories = '(SELECT * FROM dtb_product_categories WHERE '.$searchCondition['where_category'].')';
149                    $arrOrderVal           = $searchCondition['arrvalCategory'];
150                } else {
151                    $dtb_product_categories = 'dtb_product_categories';
152                }
153                $col = 'T3.rank * 2147483648 + T2.rank';
154                $from = "$dtb_product_categories T2 JOIN dtb_category T3 ON T2.category_id = T3.category_id";
155                $where = 'T2.product_id = alldtl.product_id';
156                $objQuery->setOrder('T3.rank DESC, T2.rank DESC');
157                $sub_sql = $objQuery->getSql($col, $from, $where);
158                $sub_sql = $objQuery->dbFactory->addLimitOffset($sub_sql, 1);
159
160                $objQuery->setOrder("($sub_sql) DESC ,product_id DESC");
161                break;
162        }
163        // 取得範囲の指定(開始行番号、行数のセット)
164        $objQuery->setLimitOffset($disp_number, $startno);
165        $objQuery->setWhere($searchCondition['where']);
166
167        // 表示すべきIDとそのIDの並び順を一気に取得
168        $arrProductId = $objProduct->findProductIdsOrder($objQuery, array_merge($searchCondition['arrval'], $arrOrderVal));
169
170        $objQuery =& SC_Query_Ex::getSingletonInstance();
171        $arrProducts = $objProduct->getListByProductIds($objQuery, $arrProductId);
172        // 規格を設定
173        $objProduct->setProductsClassByProductIds($arrProductId);
174        $arrProducts['productStatus'] = $objProduct->getProductStatus($arrProductId);
175
176        return $arrProducts;
177    }
178
179    /**
180     * 検索条件のwhere文とかを取得
181     *
182     * @return array
183     * TODO: LC_Page_Products_List:;lfGetSearchCondition() と共通化
184     */
185    protected function getSearchCondition($arrSearchData)
186    {
187        $searchCondition = array(
188            'where'             => '',
189            'arrval'            => array(),
190            'where_category'    => '',
191            'arrvalCategory'    => array(),
192            'orderby'           => ''
193        );
194
195        // カテゴリからのWHERE文字列取得
196        if (!SC_Utils_Ex::isBlank($arrSearchData['category_id'])) {
197            list($searchCondition['where_category'], $searchCondition['arrvalCategory']) = SC_Helper_DB_Ex::sfGetCatWhere($arrSearchData['category_id']);
198        }
199        // ▼対象商品IDの抽出
200        // 商品検索条件の作成(未削除、表示)
201        $searchCondition['where'] = 'alldtl.del_flg = 0 AND alldtl.status = 1 ';
202
203        if (strlen($searchCondition['where_category']) >= 1) {
204            $searchCondition['where'] .= ' AND EXISTS (SELECT * FROM dtb_product_categories WHERE ' . $searchCondition['where_category'] . ' AND product_id = alldtl.product_id)';
205            $searchCondition['arrval'] = array_merge($searchCondition['arrval'], $searchCondition['arrvalCategory']);
206        }
207
208        // 商品名をwhere文に
209        $name = $arrSearchData['name'];
210        $name = str_replace(',', '', $name);
211        // 全角スペースを半角スペースに変換
212        $name = str_replace(' ', ' ', $name);
213        // スペースでキーワードを分割
214        $names = preg_split('/ +/', $name);
215        // 分割したキーワードを一つずつwhere文に追加
216        foreach ($names as $val) {
217            if (strlen($val) > 0) {
218                $searchCondition['where']    .= ' AND ( alldtl.name ILIKE ? OR alldtl.comment3 ILIKE ?) ';
219                $searchCondition['arrval'][]  = "%$val%";
220                $searchCondition['arrval'][]  = "%$val%";
221            }
222        }
223
224        // メーカーらのWHERE文字列取得
225        if ($arrSearchData['maker_id']) {
226            $searchCondition['where']   .= ' AND alldtl.maker_id = ? ';
227            $searchCondition['arrval'][] = $arrSearchData['maker_id'];
228        }
229
230        $searchCondition['where_for_count'] = $searchCondition['where'];
231
232        // 在庫無し商品の非表示
233        if (NOSTOCK_HIDDEN) {
234            $searchCondition['where'] .= ' AND (stock >= 1 OR stock_unlimited = 1)';
235            $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))';
236        }
237
238        // ソート順
239        if (!SC_Utils_Ex::isBlank($arrSearchData['orderby'])) {
240            $searchCondition['orderby'] = $arrSearchData['orderby'];
241        }
242
243        return $searchCondition;
244    }
245
246    /**
247     * 商品情報配列に商品ステータス情報を追加する
248     *
249     * @param  Array $arrProducts    商品一覧情報
250     * @param  Array $arrStatus      商品ステータス配列
251     * @param  Array $arrStatusImage スタータス画像配列
252     * @return Array $arrProducts 商品一覧情報
253     */
254    protected function setStatusDataTo($arrProducts, $arrStatus, $arrStatusImage)
255    {
256        foreach ($arrProducts['productStatus'] as $product_id => $arrValues) {
257            for ($i = 0; $i < count($arrValues); $i++) {
258                $product_status_id = $arrValues[$i];
259                if (!empty($product_status_id)) {
260                    $arrProductStatus = array(
261                        'status_cd' => $product_status_id,
262                        'status_name' => $arrStatus[$product_status_id],
263                        'status_image' =>$arrStatusImage[$product_status_id],
264                    );
265                    $arrProducts['productStatus'][$product_id][$i] = $arrProductStatus;
266                }
267            }
268        }
269
270        return $arrProducts;
271    }
272}
Note: See TracBrowser for help on using the repository browser.