Sidebar.php 5.67 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';
        Yaf_Loader::import($templatePath);
        //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 = empty($params['specialsale_id']) ? array() : array('specialsale_id' => $params['specialsale_id']);
        }
        if (!empty($params['page'])) {
            unset($params['page']);
        }
        foreach ($param as $key => $value) {
            $is_multi = in_array($key, self::$noSingle) ? 'Y' : 'N';
            //增加参数
            if ($type == 1) {
                if ($is_multi == 'Y') {
                    //多选
                	if(empty($params[$key])){
						$params[$key] = '';
					}
                    $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 = empty($urlInfo['query']) ? '' : $urlInfo['query'];

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

        foreach ($queryArr as $v) {
            $param = explode('=', $v);
            if (empty($param) || empty($param[1])) {
                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(!isset($params[$v]) || empty($params[$v])){
				continue;
			}
			$is_multi = in_array($v, self::$noSingle) ? 'Y' : 'N';
			if($is_multi == 'Y'){
			    $arr = explode(',', $params[$v]);
			    if (empty($arr)) {
			        $result[] = array(
			            'key' => $v,
			            'value' => $params[$v]
			        );
			        continue;
			    }
				$value = array_unique($arr);
				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;
    }
}