Sidebar.php
5.67 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
<?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;
}
}