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

Revision 23378, 11.1 KB checked in by Seasoft, 10 years ago (diff)

#2531 (DBMS に依存したサブクエリー内の LIMIT OFFSET を排除する)

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/**
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'], $tpl_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     * @return array
112     * TODO: LC_Page_Products_List::lfGetProductsList() と共通化
113     */
114    protected function getProductsList($searchCondition, $disp_number, $startno, $linemax, &$objProduct)
115    {
116        $objQuery =& SC_Query_Ex::getSingletonInstance();
117
118        $arrOrderVal = array();
119
120        // 表示順序
121        switch ($searchCondition['orderby']) {
122            // 販売価格が安い順
123            case 'price':
124                $objProduct->setProductsOrder('price02', 'dtb_products_class', 'ASC');
125                break;
126            // 販売価格が高い順
127            case '-price':
128                $objProduct->setProductsOrder('price02', 'dtb_products_class', 'DESC');
129                break;
130
131            // 新着順
132            case 'releasedate':
133            case 'date':
134                $objProduct->setProductsOrder('create_date', 'dtb_products', 'DESC');
135                break;
136
137            // 新着順
138            case 'releasedate':
139            case 'date':
140                $objProduct->setProductsOrder('create_date', 'dtb_products', 'ASC');
141                break;
142
143            default:
144                if (strlen($searchCondition['where_category']) >= 1) {
145                    $dtb_product_categories = '(SELECT * FROM dtb_product_categories WHERE '.$searchCondition['where_category'].')';
146                    $arrOrderVal           = $searchCondition['arrvalCategory'];
147                } else {
148                    $dtb_product_categories = 'dtb_product_categories';
149                }
150                $col = 'T3.rank * 2147483648 + T2.rank';
151                $from = "$dtb_product_categories T2 JOIN dtb_category T3 ON T2.category_id = T3.category_id";
152                $where = 'T2.product_id = alldtl.product_id';
153                $objQuery->setOrder('T3.rank DESC, T2.rank DESC');
154                $objQuery->setLimit(1);
155                $sub_sql = $objQuery->getSqlWithLimitOffset($col, $from, $where);
156
157                $objQuery->setOrder("($sub_sql) DESC ,product_id DESC");
158                break;
159        }
160        // 取得範囲の指定(開始行番号、行数のセット)
161        $objQuery->setLimitOffset($disp_number, $startno);
162        $objQuery->setWhere($searchCondition['where']);
163
164        // 表示すべきIDとそのIDの並び順を一気に取得
165        $arrProductId = $objProduct->findProductIdsOrder($objQuery, array_merge($searchCondition['arrval'], $arrOrderVal));
166
167        $objQuery =& SC_Query_Ex::getSingletonInstance();
168        $arrProducts = $objProduct->getListByProductIds($objQuery, $arrProductId);
169        // 規格を設定
170        $objProduct->setProductsClassByProductIds($arrProductId);
171        $arrProducts['productStatus'] = $objProduct->getProductStatus($arrProductId);
172
173        return $arrProducts;
174    }
175
176    /**
177     * 検索条件のwhere文とかを取得
178     *
179     * @return array
180     * TODO: LC_Page_Products_List:;lfGetSearchCondition() と共通化
181     */
182    protected function getSearchCondition($arrSearchData)
183    {
184        $searchCondition = array(
185            'where'             => '',
186            'arrval'            => array(),
187            'where_category'    => '',
188            'arrvalCategory'    => array(),
189            'orderby'           => ''
190        );
191
192        // カテゴリからのWHERE文字列取得
193        if (!SC_Utils_Ex::isBlank($arrSearchData['category_id'])) {
194            list($searchCondition['where_category'], $searchCondition['arrvalCategory']) = SC_Helper_DB_Ex::sfGetCatWhere($arrSearchData['category_id']);
195        }
196        // ▼対象商品IDの抽出
197        // 商品検索条件の作成(未削除、表示)
198        $searchCondition['where'] = 'alldtl.del_flg = 0 AND alldtl.status = 1 ';
199
200        if (strlen($searchCondition['where_category']) >= 1) {
201            $searchCondition['where'] .= ' AND EXISTS (SELECT * FROM dtb_product_categories WHERE ' . $searchCondition['where_category'] . ' AND product_id = alldtl.product_id)';
202            $searchCondition['arrval'] = array_merge($searchCondition['arrval'], $searchCondition['arrvalCategory']);
203        }
204
205        // 商品名をwhere文に
206        $name = $arrSearchData['name'];
207        $name = str_replace(',', '', $name);
208        // 全角スペースを半角スペースに変換
209        $name = str_replace(' ', ' ', $name);
210        // スペースでキーワードを分割
211        $names = preg_split('/ +/', $name);
212        // 分割したキーワードを一つずつwhere文に追加
213        foreach ($names as $val) {
214            if (strlen($val) > 0) {
215                $searchCondition['where']    .= ' AND ( alldtl.name ILIKE ? OR alldtl.comment3 ILIKE ?) ';
216                $searchCondition['arrval'][]  = "%$val%";
217                $searchCondition['arrval'][]  = "%$val%";
218            }
219        }
220
221        // メーカーらのWHERE文字列取得
222        if ($arrSearchData['maker_id']) {
223            $searchCondition['where']   .= ' AND alldtl.maker_id = ? ';
224            $searchCondition['arrval'][] = $arrSearchData['maker_id'];
225        }
226
227        $searchCondition['where_for_count'] = $searchCondition['where'];
228
229        // 在庫無し商品の非表示
230        if (NOSTOCK_HIDDEN) {
231            $searchCondition['where'] .= ' AND (stock >= 1 OR stock_unlimited = 1)';
232            $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))';
233        }
234
235        // ソート順
236        if (!SC_Utils_Ex::isBlank($arrSearchData['orderby'])) {
237            $searchCondition['orderby'] = $arrSearchData['orderby'];
238        }
239
240        return $searchCondition;
241    }
242
243    /**
244     * 商品情報配列に商品ステータス情報を追加する
245     *
246     * @param  Array $arrProducts    商品一覧情報
247     * @param  Array $arrStatus      商品ステータス配列
248     * @param  Array $arrStatusImage スタータス画像配列
249     * @return Array $arrProducts 商品一覧情報
250     */
251    protected function setStatusDataTo($arrProducts, $arrStatus, $arrStatusImage)
252    {
253        foreach ($arrProducts['productStatus'] as $product_id => $arrValues) {
254            for ($i = 0; $i < count($arrValues); $i++) {
255                $product_status_id = $arrValues[$i];
256                if (!empty($product_status_id)) {
257                    $arrProductStatus = array(
258                        'status_cd' => $product_status_id,
259                        'status_name' => $arrStatus[$product_status_id],
260                        'status_image' =>$arrStatusImage[$product_status_id],
261                    );
262                    $arrProducts['productStatus'][$product_id][$i] = $arrProductStatus;
263                }
264            }
265        }
266
267        return $arrProducts;
268    }
269}
Note: See TracBrowser for help on using the repository browser.