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

Revision 23124, 11.2 KB checked in by kimoto, 11 years ago (diff)

#2043 typo修正・ソース整形・ソースコメントの改善 for 2.13.0
PHP4的な書き方の修正

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