Sidebar.php 9.25 KB
<?php

class QLibView_Sidebar
{
    public static $_G = array();
    public static $path = '';
    public static $noSingle = array('brand','style','parameter');

    /**
     * 显示内容
     * @param String $template
     * @param array $params
     * @return string
     */
    public static function show($template, array $params)
    {
        if (empty($template) || empty($params)) {
            return '';
        }
        $templatePath = __DIR__ . '/Template/Sidebar/' . $template . '.phtml';
        include $templatePath;
    }

    /**
     * 获得url
     *
     * @param Array $param array('brand' => 1)
     * @param String $type 1 添加参数 -1 是减少参数
     * @param String $is_clear Y 为清空其他参数 N 为不清空其他参数
     * @param String $is_multi Y 为复选 N 为单选
     * @param String
     */
    public static function getUrl($param, $type, $is_clear = 'N', $url = null, $default = array())
    {
        if ($url == null) {
            $params = self::parseQuery();
            $url = self::$path;
        }
        //清除现有参数
        if ($is_clear == 'Y') {
            $params = array();
        }
        if (!empty($param['num'])) {
            unset($params['page']);
        }
        foreach ($param as $key => $value) {
            $is_multi = in_array($key, self::$noSingle) ? 'Y' : 'N';
            //增加参数
            if ($type == 1) {
                if ($is_multi == 'Y') {
                    //多选
                    $params[$key] .= empty($params[$key]) ? $value : ',' . $value;
                } else {
                    //单选
                    $params = array_merge($params, array($key => $value));
                }
            } else {
                //减少参数
                //多选
                if ($is_multi == 'Y') {
                    $arr = array_unique(explode(',', $params[$key]));
                    //只有一个时直接unset
                    if (count($arr) == 1) {
                        unset($params[$key]);
                    } else {
                        //多于一个时循环处理
                        foreach ($arr as $k => $v) {
                            if ($v == $value) {
                                unset($arr[$k]);
                            }
                        }
                        if (!empty($arr)) {
                            $params[$key] = implode(',', $arr);
                        }
                    }
                } else {
                    //单选
                    unset($params[$key]);
                }
            }
        }

        $query = array();
        foreach ($params as $k => $v) {
            $query[] = $k . '=' . $v;
        }
        return $url . (empty($query) ? '' : '?' . implode('&', $query));
    }

    /**
     * 验证参数是否所选
     *
     * @param unknown_type $key
     * @param unknown_type $value
     * @return boolean
     */
    public static function vail($key, $value)
    {
        $params = self::parseQuery();
        if (empty($params[$key])) {
            return false;
        }
        $is_multi = in_array($key, self::$noSingle) ? 'Y' : 'N';
        if ($is_multi == 'Y') {
            $val = array_unique(explode(',', $params[$key]));
			return in_array($value, $val) ? true : false;
        } else {
            return $params[$key] != $value ? false : true;
        }
    }

    /**
     * 获得url中的参数字符串
     */
    public static function parseQuery()
    {
        if (!empty(self::$_G)) {
            return self::$_G;
        }
        $urlInfo = parse_url($_SERVER['REQUEST_URI']);
        self::$path = $urlInfo['path'];
        $query = $urlInfo['query'];

        if (empty($query)) {
            return array();
        }
        $queryArr = explode('&', $query);
        $result = array();

        foreach ($queryArr as $v) {
            $param = explode('=', $v);
            if (empty($param)) {
                continue;
            }
            $result[$param[0]] = $param[1];
        }
        return self::$_G = $result;
    }

	public static function params($params)
	{
		if(empty($params) || !is_array($params)){
			return array();
		}
		$fields = array('gender','brand','specialsale_id','price','style','p_d','parameter','sort','size', 'color');
		$result = array();
		foreach ($fields as $v){
			if(empty($params[$v])){
				continue;
			}
			$is_multi = in_array($v, self::$noSingle) ? 'Y' : 'N';
			if($is_multi == 'Y'){
				$value = array_unique(explode(',', $params[$v]));
				foreach ($value as $id){
					$result[] = array(
						'key' => $v,
						'value' => $id
					);
				}
			}else{
				$result[] = array('key' => $v, 'value' => $params[$v]);
			}
		}
		return $result;
	}

    static function selectGoodsByColor(array $goodsList, $colorID)
    {
        $_defaultGoods = array();
        foreach ($goodsList as $_key => $_goods) {
            if ($_goods['color_id'] == $colorID) {
                return $_goods;
            }
            if ($_goods['is_default'] == 'Y') {
                $_defaultGoods = $_goods;
            }
        }
        if (empty($_defaultGoods)) {
            $_firstGoods = array_slice($goodsList, 0, 1);
            return $_firstGoods[0];
        }
        return $_defaultGoods;
    }
    
    public static function getPriceUrlbak($params)
    {
    	$ppam = $params;
    	if (isset($ppam['q'])) {
    		unset($ppam['q']);
    	}
    	$localurl = http_build_query($ppam, '', '&');
    	$cssClass = '';
    	if (empty($ppam['order']) || $ppam['order'] == 's_t_desc') {
    		$ppam['order'] = 's_p_desc';
    	} else if ($ppam['order'] == 's_p_desc') {
    		$cssClass = 'down';
    		$ppam['order'] = 's_p_asc';
    	} else if ($ppam['order'] == 's_p_asc') {
    		$cssClass = 'up';
    		$ppam['order'] = 's_p_desc';
    	}
    	
    	$data = http_build_query($ppam, '', '&');
    	$priceurl = '?' . $data;
    	$ppam['order'] = 's_t_desc';
    	$data = http_build_query($ppam, '', '&');
    	$newurl = '?' . $data;
    	return array(
    		'priceUrl' => $priceurl,
    		'newUrl' => $newurl,
    		'localUrl' => $localurl,
    		'cssClass' => $cssClass
    	);
    }
    
    public static function getPriceUrl($params)
    {
    	$ppam = $params;
    	if (isset($ppam['q'])) {
    		unset($ppam['q']);
    	}
    	$localurl = http_build_query($ppam, '', '&');
  
    	 $ppam['order'] = 's_p_desc';
    	 $data = http_build_query($ppam, '', '&');
    	 $down_price_url = '?' . $data;
    	 $ppam['order'] = 's_p_asc';
    	 $data = http_build_query($ppam, '', '&');
    	 $up_price_url = '?' . $data;
    	 $ppam['order'] = 's_t_desc';
    	 $data = http_build_query($ppam, '', '&');
    	 $new_url = '?' . $data;
    	 $ppam['order'] = 's_n_desc';
    	 $data = http_build_query($ppam, '', '&');
    	 $sale_url = '?' . $data;
    	 

    	return array(
    			'down_price_url' => $down_price_url,
    			'up_price_url' => $up_price_url,
    			'new_url' => $new_url,
    			'sale_url' => $sale_url,
    			'localurl' => $localurl
    	);
    }
    
    
    
    public static function getUrlParams($params)
    {
    	$ppam = $params;
    	if (isset($ppam['q'])) {
    		unset($ppam['q']);
    	}
    	$localurl = http_build_query($ppam, '', '&');
    	$cssClass = '';
    	if (empty($ppam['order'])) {
    		$ppam['order'] = 's_p_desc';
    	}
    	
    	$data = http_build_query($ppam, '', '&');
    	$priceurl = '?' . $data;
    	$ppam['order'] = 's_t_desc';
    	$data = http_build_query($ppam, '', '&');
    	$newurl = '?' . $data;
    	return array(
    		'priceUrl' => $priceurl,
    		'newUrl' => $newurl,
    		'localUrl' => $localurl,
    		'cssClass' => $cssClass
    	);    }
    
    
    /**
     * 获取YOHO币类型
     * @param json $params
     * @param int $type
     * @return string
     */
    public static function getYOHOCoin($params, $type)
    {
    	$params = json_decode($params, true);
    	if (!empty($params['cms_order_id'])) {
    		$order_code = $params['cms_order_id'];
    	} else if (!empty($params['order_code'])) {
    		$order_code = $params['order_code'];
    	} else {
    		$order_code = '';
    	}
    	if($type == 1) {
    		return 'YOHO币充值';
    	} else if($type == 2) {
    		return '取消订单号为'.$order_code.'的订单返还';
    	} else if($type == 3) {
    		return '取消订单号为'.$order_code.'的订单返还';
    	} else if($type == 4) {
    		return '使用YOHO币支付订单';
    	} else if($type ==5) {
    		return '订单号为' . $order_code .'的订单退货';
    	} else if($type == 6){
    		return '老系统导入';
    	} else if($type == 7) {
    		return '完善用户信息';
    	} else if($type == 8) {
    		return '晒物送YOHO币';
    	}else if($type == 9){
    		return '订单' . $order_code . '在线支付,返运费。';
    	}else if($type == 10){
    		return '订单' . $order_code . '成功购买返币活动返YOHO币';
    	}else if ($type == 11){
    		return 'visible商品降价补差价';
    	}else if ($type == 12){
    		return '订单' . $order_code . '退货退运费';
    	}else if ($type == 13){
    		return '订单' . $order_code . '发货延迟返50%YOHO币';
    	}else if ($type == 14){
    		return '订单' . $order_code . '退货应退金额不退钱退YOHO币';
    	}else if ($type == 15){
    		return '先锋人物奖励';
    	}else if ($type == 16){
    		return '其他';
    	}
    }
}