HelperSearch.php 41.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
<?php

namespace Plugin;
use Plugin\Paging;
use LibModels\Web\Product\BrandData;
/**
 * 搜索辅助类
 */
class HelperSearch
{
    /**
     * 请求时用的所有参数
     */
    public static $params = array();

    //当前页
    public static $page = 1;

    /**
     * 选中的条件
     */
    public static $selected = array();

    /**
     * list分类面包屑
     * @var array
     */
    public static $listnav = array();


    //设置导航
    private static function setListNav($option = array())
    {
        $cookieChannel = isset($_COOKIE['_Channel']) ? $_COOKIE['_Channel'] : 'boys';
        if (isset($option['brandName']) && !empty($option['brandName'])) {
            $initNav = $option['brandName'];
        }else{
            $initNav = '列表';
        }
        self::$listnav[0] = array(
            'href' => '',
            'name' => $cookieChannel
        );
        self::$listnav[1] = array(
            'href' => '',
            'name' => $initNav
        );
    }
    /**
     * 组织搜索模板数据
     * @param array $data
     * @param array $options
     * @return array
     */
    public static function getList($data = array() , $options = array())
    {
        $result = array();
        self::$params = $_GET;
        unset(self::$params['/']);
        unset(self::$params['page']);
        unset(self::$params['/sale']);
        unset(self::$params['sale']);
        $filter = $data['data']['filter'];
        //产品列表
        $result['goods'] = self::getProductList($data['data']['product_list'], $options['imgSize']);
        //总页数
        $result['page_total'] = $data['data']['page_total'];
        //当前页
        $result['page'] = $data['data']['page'];
        self::$page = $result['page'];
        //清除选中
        $result['filters']['checkedConditions']['clearUrl'] = self::current();
        //频道
        $result['filters']['channel'] = self::gender($filter);
        //分类
        $result['filters']['sort'] = array();
        //品牌
        $result['filters']['brand'] = self::brand($filter, $options);
        //价格
        $result['filters']['price'] = self::price($filter);
        //颜色
        $result['filters']['color'] = self::color($filter);
        //尺寸
        $result['filters']['size'] = self::size($filter);
        //高级选项
        $result['filters']['seniorChose'] = self::seniorChose($filter);
        //排序方式
        $result['opts']['sortType'] = array( self::orderDefault(),self::orderTime(),self::orderPrice(),self::orderDiscount());
        //特殊:新品、特价、限量
        $result['opts']['checks'] = array(self::isnew(),self::specialoffer(),self::limited());
        //每行显示5个产品
        $result['opts']['fivePerLine'] = true;
        //每行显示6个产品
        $result['opts']['sixPerLineHref'] = true;
        //每页显示的数量
        $result['opts']['countPerPage'] = $options['viewNum'];
        //可选每页显示数量
        $result['opts']['pageCounts'] = self::viewNum();
        $result['opts']['curPage'] = self::$page;
        $result['opts']['pageCount'] = $data['data']['page_total'];
        $result['opts']['nextHref'] = self::next($data['data']['page_total'],$data['data']['filter']);
        $result['hasNextPage'] = array(
            'href' => self::next($data['data']['page_total'],$data['data']['filter']),
            'src' => 'http://img10.static.yhbimg.com/product/2014/01/15/11/01fa01614784f6239760f1b749663016f1.jpg?imageMogr2/thumbnail/235x314/extent/235x314/background/d2hpdGU=/position/center/quality/90'
        );
        //分类
        $result['leftContent'][]['allSort'] = isset($filter['group_sort']) ? self::groupSort($filter['group_sort'], $options) : array();
        //一周新品上架
        $result['leftContent'][]['newSales'] = isset($filter['recent']) ? self::recentShelve($filter['recent']) : array();
        //全部折扣
        $result['leftContent'][]['allDiscount'] = isset($filter['discount']) ? self::getDiscount($filter['discount']) : array();
        //总记录数
        $result['totalCount'] = $data['data']['total'];
        //分页
        $result['pager'] = self::pager($result['totalCount'],$options['viewNum']);
        //选中条件
        $result['filters']['checkedConditions']['conditions'] = self::getSelected();
        $result['pathNav'] = self::$listnav;
        return $result;
    }

    /**
     * TODO 组织商品列表信息
     * @param $product 接口返回商品列表
     * @author sefon 2015-12-21 17:24:04
     * @return array
     */
    public static function getProductList($product,$imgSize){
        $goods = array();
        foreach($product as $key => $val){
            //NEW
            $isNew = $val['is_new'] == 'Y' ? true : false;
            //限量商品
            $isLimit = isset($val['is_limited']) && $val['is_limited'] === 'Y';
            //即将售罄
            $isFew = $val['is_soon_sold_out'] === 'Y' ? true : false;
            //SALE
            //新品节
            //再到着
            //年终大促
            // 年中大促
            foreach($val['goods_list'] as $k => $v){
                $goods_list[$k]['url'] = Helpers::getUrlBySkc($val['product_id'], $v['goods_id'], $val['cn_alphabet']);
            }
            if (!empty($val['default_images'])) {
                $val['default_images'] = Images::getImageUrl($val['default_images'],$imgSize[0],$imgSize[1]);
            }
            $goods[] = array(
                'tags' => array(
                    'isNew' => $isNew,
                    'isLimit' => $isLimit
                ),
                'url' => $goods_list[0]['url'],
                'thumb' => $val['default_images'],
                'name' => $val['product_name'],
                'salePrice' => $val['sales_price'],
                'marketPrice' => $val['market_price'],
                'isFew' => $isFew,
                'skn' => $val['product_skn']
            );
        }
        return $goods;
    }
    /**
     * 版型等其它筛选项
     * @param $filter
     * @return array
     */
    public static function standard($filter) {
        $params = self::$params;
        $result = array();
        if (empty(self::$params)) {
            return $result;
        }
        if (empty($filter) || !is_array($filter)) {
            return array();
        }
        foreach ($filter as $key => $val) {
            if (substr_compare($key, 'parameter', 0, 9) != 0) {
                continue;
            }
            if (!isset($val['sub']) || empty($val['sub'])) {
                continue;
            }

            $sub = array();
            foreach ($val['sub'] as $pval) {
                if (isset(self::$params['parameter_' . $val['standard_id']]) && self::$params['parameter_' . $val['standard_id']] == $pval['standard_id']) {
                    $tmpParams = self::$params;
                    unset($tmpParams['parameter_' . $val['standard_id']]);
                    self::$selected['parameter_' . $val['standard_id']] = array(
                        'name' => $pval['standard_name'],
                        'href' => self::buildurl($tmpParams)
                    );
                }
                $sub[] = array(
                    'name' => $pval['standard_name'],
                    'href' => self::buildurl(array_merge($params, array(
                        'parameter_' . $val['standard_id'] => $pval['standard_id']
                    ))) ,
                    'selected' => isset(self::$params['parameter_' . $val['standard_id']]) && self::$params['parameter_' . $val['standard_id']] == $pval['standard_id'] ? true : false
                );
            }
            if (!empty($sub) && count($sub) > 1) {
                $result[] = array(
                    'name' => $val['standard_name'],
                    'sub' => $sub
                );
            }
        }
        return $result;
    }

    /**
     * 性别
     * @return array
     */
    public static function gender($filter) {
        if (empty($filter) || !isset($filter['gender']) || empty($filter['gender'])) {
            return array();
        }
        $params = self::$params;
        $gender = isset($params['gender']) ? $params['gender'] : '';
        if (isset($params['gender'])) {
            unset($params['gender']);
        }

        if ($gender == '1,3' || $gender == '2,3') {
            self::$selected['gender'] = array(
                'name' => $gender == '1,3' ? 'BOYS' : 'GIRLS',
                'href' => self::buildurl($params)
            );
        }
        $result = array();
        foreach ($filter['gender'] as $key => $val) {
            $result[] = array(
                'name' => $val,
                'href' => self::buildurl(array_merge($params, array(
                    'gender' => $key
                ))) ,
                'selected' => $gender == $key ? true : false
            );
        }
        return $result;
    }

    /**
     * 组织搜索列表页面的左侧分类
     * @param array $sort
     * @param array $option
     * @return array
     */
    public static function groupSort($sort, $option = array()) {
        self::setListNav($option);
        $params = self::$params;
        $gender = isset($params['gender']) ? $params['gender'] : '';

        $query = array();
        if (isset($option['needKeyword']) && $option['needKeyword'] == 'Y') {
            if (isset($params['query']) && !empty($params['query'])) {
                $query['query'] = $params['query'];
            }

            if (isset($params['keyword']) && !empty($params['keyword'])) {
                $query['keyword'] = $params['keyword'];
            }
        }

        if (isset($params['gender']) && !empty($params['gender'])) {
            $query['gender'] = $params['gender'];
        }
        if (isset($option['needDay']) && !empty($option['needDay']) && isset($params['day']) && !empty($params['day'])) {
            $query['day'] = $params['day'];
        }
        if (isset($option['needPd']) && !empty($option['needPd']) && isset($params['p_d']) && !empty($params['p_d'])) {
            $query['p_d'] = $params['p_d'];
        }
        if (isset($option['specialsale_id']) && !empty($option['specialsale_id']) && isset($params['specialsale_id']) && !empty($params['specialsale_id'])) {
            $query['specialsale_id'] = $params['specialsale_id'];
        }

        $result = array(
            'all' => array(
                'sort_id' => 0,
                'name' => '全部品类',
                'href' => self::buildurl($query) ,
//                'num' => 0,
                'active' => ''
            ) ,
            'list' => array()
        );
        foreach ($sort as $val) {
            if (isset($params['msort']) && $params['msort'] == $val['sort_id']) {
                $nav_index = isset($option['brandName']) && $option['brandName'] ? 2 : 1;
                    self::$listnav[$nav_index] = array(
                    'sort_id' => $val['sort_id'],
                    'name' => $val['sort_name'],
                    'href' => self::buildurl(array(
                        'msort' => $val['sort_id'],
                        'gender' => $gender
                    ))
                );
            }
            //$result['all']['num']+= $val['count'];
            $msort = array(
                'sort_id' => $val['sort_id'],
                'name' => $val['sort_name'],
//                'num' => $val['count'],
                'active' => isset($params['msort']) && $params['msort'] == $val['sort_id'] ? true : false,
                'href' => self::buildurl(array_merge($query, array(
                    'msort' => $val['sort_id']
                ))) ,
                'childList' => array()
            );
            $sub = array();
            if (!empty($val['sub'])) {

                $sub[] = array(
                    'sort_id' => $val['sort_id'],
                    'name' => '全部' . $val['sort_name'],
//                    'num' => $val['count'],
                    'childActive' => !isset($params['misort']) && isset($params['msort']) && $params['msort'] == $val['sort_id'] ? true : false,
                    'href' => self::buildurl(array_merge($query, array(
                        'msort' => $val['sort_id']
                    ))) ,
                );
                foreach ($val['sub'] as $mval) {
                    if (isset($params['misort']) && $params['misort'] == $mval['sort_id']) {
                        $nav_index = isset($option['brandName']) && $option['brandName'] ? 3 : 2;
                            self::$listnav[$nav_index] = array(
                            'sort_id' => $mval['sort_id'],
                            'name' => $mval['sort_name'],
                            'href' => ''
                        );
                    }

                    $sub[] = array(
                        'sort_id' => $mval['sort_id'],
                        'name' => $mval['sort_name'],
//                        'num' => $mval['count'],
                        'childActive' => isset($params['misort']) && $params['misort'] == $mval['sort_id'] ? true : false,
                        'href' => self::buildurl(array_merge($query, array(
                            'msort' => $val['sort_id'],
                            'misort' => $mval['sort_id']
                        ))) ,
                    );
                }
                $msort['childList'] = $sub;
            }
            $result['list'][] = $msort;
        }
        return $result;
    }

    /**
     * 品牌,可以多选
     * @param array $filter
     * @param array $options
     * @return array
     */
    public static function brand($filter, $options = array()) {
        if (isset($options['brandName'])) {
            return array();
        }
        $params = self::$params;
        $brand_ids = isset($params['brand']) && !empty($params['brand']) ? explode(',', $params['brand']) : array();
        if (isset($params['brand'])) {
            unset($params['brand']);
        }
        $result = array(
            'default' => array() ,
            'brandIndex' => array(
                '0~9',
                'A~G',
                'H~N',
                'O~T',
                'U~Z') ,
            'brandsShow' => array() ,
            'list' => array(
                '0~9' => array() ,
                'A~G' => array() ,
                'H~N' => array() ,
                'O~T' => array() ,
                'U~Z' => array() ,
            )
        );
        $brand_keys = array_reverse(array_keys($result['list']));
        if (isset($filter['brand']) && !empty($filter['brand'])) {
            //有商品结果时
            $brand = $filter['brand'];
            foreach ($brand as $v) {
                if (!isset($v['brand_alif']) && isset($v['brand_name_en'])) {
                    $v['brand_alif'] = substr($v['brand_name_en'], 0, 1);
                }
                else if (!isset($v['brand_alif'])) {
                    $v['brand_alif'] = '';
                }
                if (!isset($v['brand_domain'])) {
                    $v['brand_domain'] = '';
                }
                //选中的筛选条件
                if (in_array($v['id'], $brand_ids)) {
                    $selected_brand = explode(',', self::$params['brand']);
                    $selected_key = array_search($v['id'], $selected_brand);
                    unset($selected_brand[$selected_key]);
                    $selected_params = $params;
                    $selected_params['brand'] = implode(',', $selected_brand);
                    self::$selected['brand'][] = array(
                        'name' => $v['brand_name'],
                        'href' => self::buildurl($selected_params)
                    );
                }
                $selected_brand = isset(self::$params['brand']) && !empty(self::$params['brand']) ? explode(',', self::$params['brand']) : array();
                if (empty($selected_brand)) {
                    $url = self::buildurl(array_merge($params, array(
                        'brand' => $v['id']
                    )));
                }
                else if (in_array($v['id'], $brand_ids)) {
                    $tk = array_search($v['id'], $selected_brand);
                    unset($selected_brand[$tk]);
                    $url = self::buildurl(array_merge($params, array(
                        'brand' => implode(',', $selected_brand)
                    )));
                }
                else {
                    $selected_brand[] = $v['id'];
                    $url = self::buildurl(array_merge($params, array(
                        'brand' => implode(',', $selected_brand)
                    )));
                }
                $this_brand = array(
                    'brand_id' => $v['id'],
                    'name' => $v['brand_name'],
                    'alif' => $v['brand_alif'],
                    'href' => $url,
                    'active' => in_array($v['id'], $brand_ids) ? true : false
                );
                if (count($result['default']) < 10) {
                    $result['default'][] = $this_brand;
                }
                $result['brandsShow'][] = $this_brand;
                foreach ($brand_keys as $range) {
                    if (ord($v['brand_alif']) >= ord($range)) {
                        $_order_key = $v['brand_alif'] . $v['brand_name'];
                        $result['list'][$range][$_order_key] = $this_brand;
                        break;
                    }
                }
            }
            foreach ($result['list'] as $key => $value) {
                ksort($result['list'][$key]);
                $result['list'][$key] = array_values($result['list'][$key]);
                if (empty($value)) {
                    unset($result['list'][$key]);
                }
            }
        }
        else if (isset($filter['paramNames']) && isset($filter['paramNames']['brand'])) {
            //无商品结果时且选择了品牌
            $brand = $filter['paramNames']['brand'];
            foreach ($brand as $v) {
                //选中的筛选条件
                if (in_array($v['id'], $brand_ids)) {
                    $selected_brand = explode(',', self::$params['brand']);
                    $selected_key = array_search($v['id'], $selected_brand);
                    unset($selected_brand[$selected_key]);
                    $selected_params = $params;
                    $selected_params['brand'] = implode(',', $selected_brand);
                    self::$selected['brand'][] = array(
                        'name' => $v['name'],
                        'href' => self::buildurl($selected_params)
                    );
                }
            }
            $result['list'] = array();
        }
        else {
            $result['list'] = array();
        }
        return $result;
    }

    /**
     * 尺码的链接,不能多选
     * @param array $filter
     * @return array
     */
    public static function size($filter) {
        $params = self::$params;
        $size_id = isset($params['size']) && !empty($params['size']) ? $params['size'] : '';
        if (isset($params['size'])) {
            unset($params['size']);
        }
        $result = array();
        if (isset($filter['size']) && !empty($filter['size'])) {
            $size = $filter['size'];
            foreach ($size as $v) {
                if ($v['size_id'] == $size_id) {
                    self::$selected['size'] = array(
                        'name' => $v['size_name'],
                        'href' => self::buildurl($params)
                    );
                }
                $result[] = array(
                    'size_id' => $v['size_id'],
                    'name' => $v['size_name'],
                    'href' => self::buildurl(array_merge($params, array(
                        'size' => $v['size_id']
                    ))) ,
                    'selected' => $size_id == $v['size_id'] ? true : false
                );
            }
        }
        else if (isset($filter['paramNames']) && isset($filter['paramNames']['size'])) {
            $size = $filter['paramNames']['size'];
            foreach ($size as $val) {
                self::$selected['size'] = array(
                    'name' => $val['name'],
                    'href' => self::buildurl($params)
                );
            }
        }

        return $result;
    }

    /**
     * 颜色,不能多选
     * @param array $filter
     * @return array
     */
    public static function color($filter) {
        $params = self::$params;
        $color_id = isset($params['color']) && !empty(self::$params['color']) ? self::$params['color'] : '';
        if (isset($params['color'])) {
            unset($params['color']);
        }

        $result = array();
        if (isset($filter['color']) && !empty($filter['color'])) {

            //有商品结果时的颜色
            $color = $filter['color'];
            foreach ($color as $v) {
                if ($v['color_id'] == $color_id) {
                    $background = empty($v['color_value']) ? '#' . $v['color_code'] : "url(" . $v['color_value'] . ")";
                    self::$selected['color'] = array(
                        'color' =>  $background,
                        'href' => self::buildurl($params)
                    );
                }

                $result[] = array(
                    'color_id' => $v['color_id'],
                    'name' => $v['color_name'],
                    'rgb' => empty($v['color_value']) ? '#' . $v['color_code'] : "url({$v['color_value']})",
                    'href' => self::buildurl(array_merge($params, array(
                        'color' => $v['color_id']
                    ))) ,
                    'selected' => $color_id == $v['color_id'] ? true : false
                );
            }
        }
        else if (isset($filter['paramNames']) && isset($filter['paramNames']['color'])) {

            //无商品结果时的颜色
            $color = $filter['paramNames']['color'];
            foreach ($color as $v) {
                if ($v['id'] == $color_id) {
                    $background = empty($v['value']) ? '#' . $v['code'] : "url(" . $v['value'] . ")";
                    self::$selected['color'] = array(
                        'name' => '<em style="background:' . $background . ';">&nbsp;</em>',
                        'href' => self::buildurl($params)
                    );
                }
            }
        }

        return $result;
    }

    /**
     * 获取价格,不能多选
     * @param array $filter
     * @return array
     */
    public static function price($filter) {
        $params = self::$params;
        $price_id = isset($params['price']) && !empty(self::$params['price']) ? self::$params['price'] : '';
        if (isset($params['price'])) {
            unset($params['price']);
        }

        $result = array();

        if (isset($filter['price']) && !empty($filter['price'])) {

            //有商品时
            if (!empty($price_id) && isset($filter['price'][$price_id])) {
                self::$selected['price'] = array(
                    'name' => $filter['price'][$price_id],
                    'href' => self::buildurl($params)
                );
            }
            else if (!empty($price_id)) {
                $_price = explode(',', $price_id);
                if (count($_price) == 2) {
                    self::$selected['price'] = array(
                        'name' => self::$params['price'] == '2000,99999' ? '¥2000以上' : '¥' . (int)$_price[0] . '-' . (int)$_price[1],
                        'href' => self::buildurl($params)
                    );
                }
            }

            foreach ($filter['price'] as $key => $val) {
                $val = trim($val,'¥');
                $result[] = array(
                    'name' => $val,
                    'href' => self::buildurl(array_merge($params, array(
                        'price' => $key
                    ))) ,
                    'selected' => isset(self::$params['price']) && $key == self::$params['price'] ? true : false
                );
            }
        }
        else if (!empty($price_id)) {

            //无商品结果时
            $_price = explode(',', $price_id);
            if (count($_price) == 2) {
                self::$selected['price'] = array(
                    'name' => self::$params['price'] == '2000,99999' ? '¥2000以上' : '¥' . (int)$_price[0] . '-' . (int)$_price[1],
                    'href' => self::buildurl($params)
                );
            }
        }
        return $result;
    }

    /**
     * 风格,可以多选
     * @param array $filter
     * @return array
     */
    public static function style($filter) {
        $params = self::$params;
        $style_ids = isset($params['style']) && !empty($params['style']) ? explode(',', $params['style']) : array();
        if (isset($params['style'])) {
            unset($params['style']);
        }
        $result = array();
        if (isset($filter['style']) && !empty($filter['style'])) {
            //有商品结果时
            $style = $filter['style'];
            foreach ($style as $v) {
                //选中的筛选条件
                if (in_array($v['style_id'], $style_ids)) {
                    $selected_style = explode(',', self::$params['style']);
                    $selected_key = array_search($v['style_id'], $selected_style);
                    unset($selected_style[$selected_key]);
                    $selected_params = $params;
                    $selected_params['style'] = implode(',', $selected_style);
                    self::$selected['style'][] = array(
                        'name' => $v['style_name'],
                        'href' => self::buildurl($selected_params)
                    );
                }

                $selected_style = isset(self::$params['style']) && !empty(self::$params['style']) ? explode(',', self::$params['style']) : array();

                if (empty($selected_style)) {
                    $url = self::buildurl(array_merge($params, array(
                        'style' => $v['style_id']
                    )));
                }
                else if (in_array($v['style_id'], $selected_style)) {
                    $tk = array_search($v['style_id'], $selected_style);
                    unset($selected_style[$tk]);
                    $url = self::buildurl(array_merge($params, array(
                        'style' => implode(',', $selected_style)
                    )));
                }
                else {
                    $selected_style[] = $v['style_id'];
                    $url = self::buildurl($params + array(
                            'style' => implode(',', $selected_style)
                        ));
                }
                $result[] = array(
                    'style_id' => $v['style_id'],
                    'name' => $v['style_name'],
                    'href' => $url,
                    'selected' => in_array($v['style_id'], $style_ids) ? true : false
                );
            }
        }
        else if (isset($filter['paramNames']) && isset($filter['paramNames']['style'])) {

            //无商品结果时
            $style = $filter['paramNames']['style'];
            foreach ($style as $v) {

                //选中的筛选条件
                if (in_array($v['id'], $style_ids)) {
                    $selected_style = explode(',', self::$params['style']);
                    $selected_key = array_search($v['id'], $selected_style);
                    unset($selected_style[$selected_key]);
                    $selected_params = $params;
                    $selected_params['style'] = implode(',', $selected_style);
                    self::$selected['style'][] = array(
                        'name' => $v['name'],
                        'href' => self::buildurl($selected_params)
                    );
                }
            }
        }
        $data[0] = array(
            'attr' => 'style',
            'name' => '风格',
            'showMulti' => true,
            'sub' => $result
        );
        return $data;
    }
    /**
     * 高级选项
     */
    public static function seniorChose($filter)
    {
        $style = self::style($filter);
        $other = self::standard($filter);
        $result = array_merge($style,$other);
        return $result;
    }

    /**
     *  每页显示数量
     */
    public static function viewNum()
    {
        $params = self::$params;
        $view_num = array(
            60,
            100,
            200
        );
        $result = array();
        foreach ($view_num as $value) {
            $params['viewNum'] = $value;
            $params['page'] = 1;
            $result[] = array(
                'count' => $value,
                'href' => self::buildurl($params)
            );
        }
        return $result;
    }

    /**
     *  显示行数
     */
    public static function rowNum($num = 5)
    {
        $params = self::$params;

        $params['rowNum'] = $num;

        return self::buildurl($params);
    }

    /**
     * 是否特价
     */
    public static function specialoffer()
    {
        $params = self::$params;
        if (isset($params['specialoffer']) && !empty($params['specialoffer'])) {
            unset($params['specialoffer']);
            $selected = true;
        }
        else {
            $params['specialoffer'] = 1;
            $selected = '';
        }

        return array(
            'name' => '打折',
            'href' => self::buildurl($params) ,
            'checked' => $selected
        );
    }

    /**
     * 是否限量
     */
    public static function limited()
    {
        $params = self::$params;
        if (isset($params['limited']) && !empty($params['limited'])) {
            unset($params['limited']);
            $selected = true;
        }
        else {
            $params['limited'] = 'Y';
            $selected = '';
        }
        return array(
            'name' => '限量',
            'href' => self::buildurl($params) ,
            'checked' => $selected
        );
    }

    /**
     * 是否新品
     */
    public static function isnew()
    {
        $params = self::$params;

        if (isset($params['isNew']) && !empty($params['isNew'])) {
            unset($params['isNew']);
            $selected = true;
        }
        else {
            $params['isNew'] = 'Y';
            $selected = false;
        }
        return array(
            'name' => '新品',
            'href' => self::buildurl($params) ,
            'checked' => $selected
        );
    }

    /**
     *  排序是否最新
     */
    public static function orderTime()
    {
        $params = self::$params;
        if (isset($params['order'])) {
            unset($params['order']);
        }
        $result = array(
            'name' => '最新',
            'href' => isset(self::$params['order']) && self::$params['order'] == 's_t_desc' ? self::buildurl($params) : self::buildurl(array_merge($params, array(
                'order' => 's_t_desc'
            ))) ,
            'selected' => isset(self::$params['order']) && self::$params['order'] == 's_t_desc' ? true : ''
        );
        return $result;
    }

    /**
     *  排序是否默认
     */
    public static function orderDefault()
    {
        $params = self::$params;
        if (isset($params['order'])) {
            unset($params['order']);
        }
        $result = array(
            'name' => '默认',
            'href' => self::buildurl($params) ,
            'selected' => !isset(self::$params['order']) || empty(self::$params['order']) ? true : ''
        );
        return $result;
    }

    /**
     *  价格排序
     */
    public static function orderPrice()
    {
        $params = self::$params;
        if (isset($params['order'])) {
            unset($params['order']);
        }
        if (!isset(self::$params['order']) || empty(self::$params['order']) || !in_array(self::$params['order'], array(
                's_p_asc',
                's_p_desc'
            ))) {
            $url = self::buildurl(array_merge($params, array(
                'order' => 's_p_asc'
            )));
            $asc = '';
            $desc = '';
        }
        else if (self::$params['order'] == 's_p_asc') {
            $url = self::buildurl(array_merge($params, array(
                'order' => 's_p_desc'
            )));
            $asc = true;
            $desc = '';
        }
        else {
            $url = self::buildurl(array_merge($params, array(
                'order' => 's_p_asc'
            )));
            $asc = '';
            $desc = true;
        }
        $result = array(
            'name' => '价格',
            'href' => $url,
            'asc' => $asc,
            'desc' => $desc
        );

        return $result;
    }

    /**
     *  折扣排序
     */
    public static function orderDiscount()
    {
        $params = self::$params;
        if (isset($params['order'])) {
            unset($params['order']);
        }
        if (!isset(self::$params['order']) || empty(self::$params['order']) || !in_array(self::$params['order'], array(
                'p_d_asc',
                'p_d_desc'
            ))) {
            $url = self::buildurl(array_merge($params, array(
                'order' => 'p_d_asc'
            )));
            $asc = '';
            $desc = '';
        }
        else if (self::$params['order'] == 'p_d_asc') {
            $url = self::buildurl(array_merge($params, array(
                'order' => 'p_d_desc'
            )));
            $asc = true;
            $desc = '';
        }
        else {
            $url = self::buildurl(array_merge($params, array(
                'order' => 'p_d_asc'
            )));
            $asc = '';
            $desc = true;
        }
        $result = array(
            'name' => '折扣',
            'href' => $url,
            'asc' => $asc,
            'desc' => $desc
        );

        return $result;
    }

    /**
     * 获取自定义价格要提交的地址
     */
    public static function priceSubmit()
    {
        $params = self::$params;
        $price = array();
        if (isset($params['price'])) {
            $price = explode(',', $params['price']);
            unset($params['price']);
        }

        $result = array();
        foreach ($params as $key => $value) {
            $result[] = '<input type="hidden" name="' . $key . '" value="' . $value . '">';
        }

        if (count($price) == 2 && !in_array(self::$params['price'], array(
                '0,300',
                '300,600',
                '600,1000',
                '1000,2000',
                '2000,99999'
            ))) {
            return array(
                'start' => $price[0],
                'end' => $price[1],
                'selected' => true,
                'form' => implode('', $result)
            );
        }
        else {
            return array(
                'start' => '',
                'end' => '',
                'selected' => false,
                'form' => implode('', $result)
            );
        }
    }

    /**
     * 组织参数
     * @param array $params
     * @return string
     */
    private static function buildurl($params)
    {
        if (empty($params)) {
            return self::current();
        }
        $queryPath = explode('?', $_SERVER['REQUEST_URI']);

        $params_arr = array();
        foreach ($params as $key => $val) {
            if (is_array($val) || $val === '') {
                continue;
            }
            $params_arr[] = $key . '=' . $val;
        }
        if (empty($params_arr)) {
            return self::current();
        }
        else {
            return (empty($queryPath[0]) ? '/' : $queryPath[0]) . '?' . implode('&', $params_arr);
        }
    }

    /**
     * 获取选中的条件
     */
    public static function getSelected()
    {
        $result = array();
        $is_array_key = array(
            'brand',
            'style'
        );
        foreach (self::$selected as $key => $val) {
            if (in_array($key, $is_array_key)) {
                foreach ($val as $k => $v) {
                    $result[] = $v;
                }
            }
            else {
                $result[] = $val;
            }
        }
        return $result;
    }

    /**
     * 打折区间
     * @param array $list
     * @return array
     */
    public static function getDiscount(array $list = array())
    {
        $params = self::$params;

        if (empty($list)) {
            return array();
        }
        $params = $_GET;
        $query = array();
        if (isset($params['gender'])) {
            $query['gender'] = $params['gender'];
        }
        if (isset($params['msort'])) {
            $query['msort'] = $params['msort'];
        }
        if (isset($params['misort'])) {
            $query['misort'] = $params['misort'];
        }
        $total = 0;
        foreach ($list as $k => $v) {
            $total+= $v['count'];
            $query['p_d'] = $k;
            $list[$k]['href'] = self::buildurl($query);
            //$list[$k]['num'] = $total;
            $list[$k]['active'] = isset($params['p_d']) && $params['p_d'] == $k ? true : false;
        }
        $result = array(
            //'updateNum' => $total,
            'list' => array_values($list)
        );
        return $result;
    }

    /**
     * 最新上架
     */
    public static function recentShelve($list = array())
    {
        if (empty($list)) {
            return array();
        }
        $params = self::$params;
        $query = array();
        if (isset($params['gender'])) {
            $query['gender'] = $params['gender'];
        }
        if (isset($params['msort'])) {
            $query['msort'] = $params['msort'];
        }
        if (isset($params['misort'])) {
            $query['misort'] = $params['misort'];
        }
        $data = array();
        $i = 0;
        foreach ($list as $k => $v) {
            $star_time = strtotime($k);
            $end_time = $star_time+60*60*24;
            $query['shelve_time'] = '';
            $query['shelve_time'] = $star_time.','.$end_time;
            if (isset($params['shelve_time']) && $query['shelve_time'] == $params['shelve_time']) {
                $data[$i]['active'] = true;
            }
            @$data[$i]['href'] = self::buildurl($query);
            $data[$i]['name'] = $k;
            $i++;
        }
        $result = array(
            //'updateNum' => $total,
            'list' => $data
        );
        return $result;
    }
    /**
     * 当前页地址
     */
    public static function current(){
        $url =  explode('?', $_SERVER['REQUEST_URI']);
        self::$listnav[0]['href'] = $url[0];
        return $url[0];
    }
    /**
     * 下一页地址
     */
    public static function next($total,$filter){
        $page = 1;
        $param = self::$params;
        $page = self::$page;
        if($page < $total){
            $page ++;
            $param['page'] = $page;
        }
        return self::buildurl($param);
    }

    /**
     * 组织产品的图片
     * @param $product
     * @param $options
     * @return array
     */
    public static function getProductPic($product, $options)
    {
        $result = array();
        if (isset($product[0]['goods_list']) && !empty($product[0]['goods_list'])){
            foreach ($product[0]['goods_list'] as $key => $val){
                $result[$key]['coverImg'] = Images::getImageUrl($val['images_url'], $options['imgSize'][0], $options['imgSize'][1]);
                $result[$key]['url'] = Helpers::getUrlBySkc($product[0]['product_id'], $val['goods_id'], $product[0]['cn_alphabet']);
                $result[$key]['src'] = Images::getImageUrl($val['images_url'], $options['imgSize'][0], $options['imgSize'][1]);
            }
        }
        return $result;
    }
    //分页
    public static function pager($total,$view_num){
        $Paging = new Paging('Yoho');
        return $Paging->setTotal($total)->setSize($view_num)->view(0);
    }
    
    //组织静态资源数据格式
    public static function formatNodeContent($code){
        $nodeContent = BrandData::getByNodeContent($code);
            if (isset($nodeContent['code']) && $nodeContent['code'] === 200) {
                $result = array();
                
                $string = $nodeContent['data'];
                $hrefPatten="/<a href=[\'\"]?([^\'\" ]+).*?>/";
                preg_match_all($hrefPatten, $string, $href);

                $pattern = "/[img|IMG].*?src=['|\"](.*?(?:[.gif|.jpg]))['|\"].*?[\/]?>/";
                preg_match_all($pattern,$string,$img);
                
                foreach($href[1] as $key=>$vo){
                        $result[$key]['href'] = $vo;
                        $result[$key]['src'] = $img[1][$key];
                }
               return $result;
            }
    }
}