Blame view

yohobuy/m.yohobuy.com/application/controllers/Search.php 10.1 KB
hf authored
1
<?php
2
hf authored
3
use Action\AbstractAction;
4
// use LibModels\Wap\Product\SearchData;
5 6 7
use Plugin\DataProcess\ListProcess;
use Plugin\Helpers;
hf authored
8
/**
hf authored
9
 * 搜索页
hf authored
10
 */
hf authored
11
class SearchController extends AbstractAction
hf authored
12
{
13
14 15 16
    /**
     * 搜索首页
     */
hf authored
17 18
    public function indexAction()
    {
19
        $this->setNavHeader('搜索', true, SITE_MAIN);
hf authored
20 21

        //$this->_view->html('search');
22
        $this->_view->display('index', array(
23
            'search' => array('url' => Helpers::url('', null, 'search')),
24
            'showDownloadApp' => true,
25
            'searchPage' => true,
26 27 28
            'pageFooter' => true
        ));
    }
29
30 31 32 33 34
    /**
     * 搜索列表页
     */
    public function listAction()
    {
35 36 37 38 39 40 41 42 43 44
        // 过滤请求参数
        $condition = filter_input_array(INPUT_GET, array(
            'query' => FILTER_DEFAULT,
            'brand' => FILTER_VALIDATE_INT,
            'sort' => FILTER_DEFAULT,
            'msort' => FILTER_VALIDATE_INT,
            'misort' => FILTER_VALIDATE_INT,
            'color' => FILTER_VALIDATE_INT,
            'size' => FILTER_VALIDATE_INT,
            'price' => FILTER_VALIDATE_INT,
hf authored
45
            'discount' => FILTER_DEFAULT,
46 47
            'gender' => FILTER_DEFAULT,
            'p_d' => FILTER_DEFAULT,), false);
hf authored
48
hf authored
49
        $query = empty($condition['query']) ? null : strtolower(trim($condition['query']));
hf authored
50 51 52 53
        if (isset($condition['discount'])) {
            $condition['p_d'] = rawurldecode($condition['discount']);
            unset($condition['discount']);
        }
hf authored
54
55 56 57 58 59 60
        // 标识用户是否有输入搜索内容
        $haveQuery = $query !== null;
        // 标识用户搜的是不是一级品类
        $isQueryFirstClass = false;
        // 标识用户搜的是不是二级品类
        $isQuerySecondClass = false;
hf authored
61
62 63 64 65
        /* 判断是不是品牌, 是品牌跳到品牌列表页(显示搜索框) */
        if ($haveQuery) {
            $domain = null;
            $brandNames = Product\ListModel::getAllBrandNames();
hf authored
66
67 68 69 70 71 72
            do {
                /* 精确查品牌域名 */
                if (isset($brandNames[$query])) {
                    $domain = $query;
                    break;
                }
hf authored
73
74 75 76 77 78 79
                /* 精确查品牌名称 */
                $domains = array_keys($brandNames, $query, true);
                if (isset($domains[0])) {
                    $domain = $domains[0];
                    break;
                }
hf authored
80
81 82 83 84 85 86 87 88
                /* 模糊查品牌域名 */
                foreach ($brandNames as $key => $domains) {
                    if (strpos($key, $query) !== false) {
                        $domain = $key;
                        break;
                    }
                }
            } while (false);
hf authored
89
90 91
            // 清空变量做释放
            $brandNames = array();
hf authored
92
93 94
            // 跳转到品牌商品列表页
            if ($domain !== null) {
hf authored
95 96
                $url = Helpers::url('', array(
                            'from' => 'search',
97
                            'query' => $query
hf authored
98
                                ), $domain);
99 100
                $this->go($url);
            }
101
        }
hf authored
102
103 104 105
        /* 判断是不是品类, 是品类加导航标题(不显示搜索框) */
        if ($haveQuery) {
            $classNames = Category\ClassModel::getClassNames();
106
107 108 109 110 111 112 113
            do {
                /* 精确查一级品类 */
                $sorts = array_keys($classNames['first'], $query, true);
                if (isset($sorts[0])) {
                    $isQueryFirstClass = true;
                    break;
                }
hf authored
114
115 116 117 118 119 120
                /* 精确查二级品类 */
                $sorts = array_keys($classNames['second'], $query, true);
                if (isset($sorts[0])) {
                    $isQuerySecondClass = true;
                    break;
                }
hf authored
121 122
            } while (false);
123
            $classNames = array();
hf authored
124 125
        } else {
            $condition['query'] = '';
126
        }
hf authored
127
128
        $data = array();
hf authored
129 130 131
        $data['goodListPage'] = true;
        $data['goodList'] = $condition;
        $data['query'] = $query;
132 133 134 135
        // 搜索是一级品类
        if ($isQueryFirstClass) {
            $this->setTitle('全部' . $query);
            $this->setNavHeader('全部' . $query, true, SITE_MAIN);
hf authored
136
        }
137 138 139 140
        // 搜索是二级品类
        elseif ($isQuerySecondClass) {
            $this->setTitle($query);
            $this->setNavHeader($query, true, SITE_MAIN);
hf authored
141
        }
142 143 144 145
        // 搜索其它内容
        else {
            if ($haveQuery) {
                $data['goodList']['search']['default'] = $query;
hf authored
146
                $data['goodList']['search']['url'] = Helpers::url('', null, 'search');
147 148 149 150
            }
            $this->setTitle('搜索');
            $this->setNavHeader('搜索', true, SITE_MAIN);
        }
151
152 153
        // 右下角的购物车链接
        $data['goodList']['cartUrl'] = Helpers::url('/cart/index/index', null);
hf authored
154
hf authored
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
//        // 查询数据
//        if (!isset($condition['query'])) {
//            $data['goodList'] += Product\ListModel::getClassData($condition);
//        } else {
//            $listData = SearchData::searchByCondition($condition);
//            // 处理返回的数据
//            if (!empty($listData['data']['brand'])) {
//                $brandData = $listData['data']['brand'];
//                $data['brandWay'] = array(
//                    'url' => 'http://' . $brandData['brand_domain'] . SUB_DOMAIN,
//                    'thumb' => Helpers::getImageUrl($brandData['brand_ico'], 75, 40),
//                    'name' => $brandData['brand_name']
//                );
//                // 设置品牌默认值
//                $data['goodList']['brand'] = $brandData['id'];
//                $data['goodList'] += ListProcess::getListData($listData['data']);
//            }
//            $listData = array();
//        }
174 175 176

        $this->_view->display('list', $data);
    }
177 178

    /**
179 180 181 182
     * Ajax异步筛选请求
     */
    public function searchAction()
    {
hf authored
183 184
        $data = array();
185 186 187 188 189 190 191
        do {
            /* 判断是不是AJAX请求 */
            if (!$this->isAjax()) {
                break;
            }

            /* 过滤请求参数 */
192 193 194
            $condition = filter_input_array(INPUT_GET, array(
                'query' => FILTER_DEFAULT,
                'brand' => FILTER_VALIDATE_INT,
hf authored
195
                'sort' => FILTER_DEFAULT,
196 197 198 199 200 201 202
                'msort' => FILTER_VALIDATE_INT,
                'misort' => FILTER_VALIDATE_INT,
                'color' => FILTER_VALIDATE_INT,
                'size' => FILTER_VALIDATE_INT,
                'price' => FILTER_VALIDATE_INT,
                'discount' => FILTER_DEFAULT,
                'gender' => FILTER_DEFAULT,
203 204 205
                'p_d' => FILTER_DEFAULT,
                'page' => FILTER_VALIDATE_INT, ), false);
            // 转义分类
hf authored
206 207 208
            if (isset($condition['sort'])) {
                $condition['sort'] = rawurldecode($condition['sort']);
            }
209
            // 转换折扣
hf authored
210 211 212 213
            if (isset($condition['discount'])) {
                $condition['p_d'] = rawurldecode($condition['discount']);
                unset($condition['discount']);
            }
214
            // 转义性别
hf authored
215 216 217
            if (isset($condition['gender'])) {
                $condition['gender'] = rawurldecode($condition['gender']);
            }
218 219
            // 转换排序方式
            $type = $this->get('type', '');
220 221
            $order = $this->get('order', 0);
            $condition['order'] = Helpers::transOrder($order, $type);
222
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
//            /* 模糊搜索关键词 */
//            if (isset($condition['query'])) {
//                $data = Product\SearchModel::getSearchData($condition);
//                break;
//            }
//
//            /* 精确搜索品牌 */
//            if (isset($condition['brand'])) {
//                $title = '';
//                $data = Product\ListModel::getBrandData($condition, $title);
//                break;
//            }
//
//            /* 精确搜索品类 */
//            $data = Product\ListModel::getClassData($condition);
            
            $data = Product\SearchModel::getSearchData($condition);
            
        } while (false);
242
hf authored
243 244 245 246
        if (empty($data['new'])) {
            echo ' ';
        } else {
            $this->_view->display('page', $data);
247
        }
hf authored
248
    }
249 250

    /**
251 252 253 254 255
     * Ajax异步获取筛选数据
     * @return array 筛选数据
     */
    public function filterAction()
    {
hf authored
256 257
        $data = array();
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
        if ($this->isAjax()) {
            // 过滤请求参数
            $condition = filter_input_array(INPUT_GET, array(
                'query' => FILTER_DEFAULT,
                'brand' => FILTER_VALIDATE_INT,
                'sort' => FILTER_DEFAULT,
                'msort' => FILTER_VALIDATE_INT,
                'misort' => FILTER_VALIDATE_INT,
                'color' => FILTER_VALIDATE_INT,
                'size' => FILTER_VALIDATE_INT,
                'price' => FILTER_VALIDATE_INT,
                'discount' => FILTER_DEFAULT,
                'gender' => FILTER_DEFAULT,
                'p_d' => FILTER_DEFAULT,), false);
273
            // 转义分类
274 275 276
            if (isset($condition['sort'])) {
                $condition['sort'] = rawurldecode($condition['sort']);
            }
277
            // 转换折扣
278 279 280 281
            if (isset($condition['discount'])) {
                $condition['p_d'] = rawurldecode($condition['discount']);
                unset($condition['discount']);
            }
282
            // 转义性别
283 284 285 286
            if (isset($condition['gender'])) {
                $condition['gender'] = rawurldecode($condition['gender']);
            }
287
            // 区别各种列表页面的筛选数据
288
            $data = Product\FilterModel::getFilterData($condition);
hf authored
289
        }
290
hf authored
291
        if (empty($data)) {
292
            echo ' ';
hf authored
293 294
        } else {
            $this->_view->display('filter', $data);
295 296 297 298
        }
    }

    /**
299 300 301 302
     * 模糊搜索指定字符
     * 
     * @return array 模糊搜索的结果
     */
303
    /*public function fuzzysearch()
304
    {
305
        if ($this->isAjax()) {
306 307 308 309 310 311
            $keyword = $this->post('keyword', '');

            $result = SearchData::searchFuzzyDatas($keyword);

            $this->echoJson($result);
        }
312
    }*/
313 314

}