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

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

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

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