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