3f2bc3d0
Administrator
first commit
|
1
2
3
4
5
|
<?php
namespace common\modules\product;
use common\modules\product\models\Brand;
|
38828295
Karnovsky A
-
|
6
|
use common\modules\product\models\BrandSearch;
|
3f2bc3d0
Administrator
first commit
|
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
|
use common\modules\product\models\CategorySearch;
use common\modules\product\models\ProductSearch;
use common\modules\rubrication\models\TaxOption;
use Yii;
use yii\helpers\Url;
use yii\web\UrlRuleInterface;
class CatalogUrlManager implements UrlRuleInterface {
public $route_map = [];
public $option_prefix = 'o:';
/**
* Parses the given request and returns the corresponding route and parameters.
* @param \yii\web\UrlManager $manager the URL manager
* @param \yii\web\Request $request the request component
* @return array|boolean the parsing result. The route and the parameters are returned as an array.
* If false, it means this rule cannot be used to parse this path info.
*/
public function parseRequest($manager, $request)
{
$pathInfo = $request->getPathInfo();
$paths = explode('/', $pathInfo);
if (!array_key_exists($paths[0], $this->route_map)) {
return false;
}
$params = [];
if ($paths[0] == 'catalog') {
$route = 'catalog/category';
// Category
if (!empty($paths[1])) {
$category = CategorySearch::findByAlias($paths[1]);
if (empty($category)) {
http_redirect(Url::to(['/']));
}
$params['category'] = $category;
}
if (!empty($paths[2])) {
// Filter
if (strpos($paths[2], 'filter:') === 0) {
|
868680ca
Karnovsky A
-
|
48
|
$this->parseFilter($paths[2], $params);
|
8724ec1f
Karnovsky A
-
|
49
|
}/* elseif (strpos($paths[2], 'word:') === 0) {
|
3f2bc3d0
Administrator
first commit
|
50
|
$params['word'] = substr($paths[2], 5);
|
8724ec1f
Karnovsky A
-
|
51
|
}*/
|
3f2bc3d0
Administrator
first commit
|
52
53
54
55
56
57
58
59
60
61
|
}
} elseif ($paths[0] == 'product') {
$product = ProductSearch::findByAlias($paths[1]);
if (empty($product->product_id)) {
throw new HttpException(404 ,'Page not found');
}
$route = 'catalog/product';
$params = [
'product' => $product,
];
|
38828295
Karnovsky A
-
|
62
63
64
65
|
} elseif ($paths[0] == 'brands') {
if (empty($paths[1]) || $paths[1] == 'index') {
$route = 'catalog/brands';
} elseif (($brand = BrandSearch::findByAlias($paths[1]))) {
|
868680ca
Karnovsky A
-
|
66
|
$route = 'catalog/brand';
|
38828295
Karnovsky A
-
|
67
|
$params['brand'] = $brand;
|
868680ca
Karnovsky A
-
|
68
69
70
71
72
73
|
if (!empty($paths[2])) {
// Filter
if (strpos($paths[2], 'filter:') === 0) {
$this->parseFilter($paths[2], $params);
}
}
|
38828295
Karnovsky A
-
|
74
75
76
|
} else {
// @todo redirect or return FALSE
}
|
3f2bc3d0
Administrator
first commit
|
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
|
}
return [$route, $params];
}
/**
* Creates a URL according to the given route and parameters.
* @param \yii\web\UrlManager $manager the URL manager
* @param string $route the route. It should not have slashes at the beginning or the end.
* @param array $params the parameters
* @return string|boolean the created URL, or false if this rule cannot be used for creating this URL.
*/
public function createUrl($manager, $route, $params)
{
if (!in_array($route, $this->route_map)) {
return false;
}
switch($route) {
case 'catalog/category':
if (!empty($params['category'])) {
$category_alias = is_object($params['category']) ? $params['category']->alias : strtolower($params['category']);
$url = 'catalog/'. $category_alias .'/';
} else {
$url = 'catalog/';
}
|
8724ec1f
Karnovsky A
-
|
102
|
/*if (!empty($params['word'])) {
|
3f2bc3d0
Administrator
first commit
|
103
104
105
106
|
if (!is_array($params['word'])) {
$params['word'] = [$params['word']];
}
$url .= 'word:'. implode(';', $params['word']);
|
c34b3aa0
Karnovsky A
-
|
107
108
|
}
if (isset($params['word'])) {
|
ccc7a9d3
Karnovsky A
Karnovsky 12052016
|
109
|
unset($params['word']);
|
8724ec1f
Karnovsky A
-
|
110
|
}*/
|
3f2bc3d0
Administrator
first commit
|
111
|
|
868680ca
Karnovsky A
-
|
112
|
$this->setFilterUrl($params, $url);
|
3f2bc3d0
Administrator
first commit
|
113
|
|
ccc7a9d3
Karnovsky A
Karnovsky 12052016
|
114
115
116
117
|
if (!empty($params) && ($query = http_build_query($params)) !== '') {
$url .= '?' . $query;
}
|
3f2bc3d0
Administrator
first commit
|
118
119
120
121
122
123
124
125
|
return $url;
break;
case 'catalog/product':
if (!empty($params['product'])) {
$product_alias = is_object($params['product']) ? $params['product']->alias : strtolower($params['product']);
}
$url = 'product/'. $product_alias;
|
ccc7a9d3
Karnovsky A
Karnovsky 12052016
|
126
127
128
129
130
|
if (!empty($params) && ($query = http_build_query($params)) !== '') {
$url .= '?' . $query;
}
|
3f2bc3d0
Administrator
first commit
|
131
132
|
return $url;
break;
|
38828295
Karnovsky A
-
|
133
134
135
136
137
138
139
|
case 'catalog/brands':
if (empty($params['brand'])) {
return 'brands';
} else {
$brand_alias = is_object($params['brand']) ? $params['brand']->alias : strtolower($params['brand']);
}
|
868680ca
Karnovsky A
-
|
140
141
142
|
$url = 'brands/'. $brand_alias .'/';
$this->setFilterUrl($params, $url);
|
38828295
Karnovsky A
-
|
143
144
145
146
147
148
149
|
if (!empty($params) && ($query = http_build_query($params)) !== '') {
$url .= '?' . $query;
}
return $url;
break;
|
3f2bc3d0
Administrator
first commit
|
150
151
152
153
154
155
|
}
}
private function option_value_encode($value) {
return str_replace(array(',', '/'), array('~', '&s;'), $value);
}
|
868680ca
Karnovsky A
-
|
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
|
private function setFilterUrl(&$params, &$url) {
$filter = [];
if (!empty($params['filter'])) {
foreach ($params['filter'] as $key => $values) {
switch($key) {
case 'prices':
$filter[] = $key .'='. implode(':', $values);
break;
case 'options':
foreach($values as $group => &$value_items) {
foreach($value_items as &$value_item) {
$value_item = $this->option_value_encode($value_item);
if (empty($value_item)) {
unset($value_item);
}
}
$filter[] = $this->option_prefix. $group .'='. implode(',', $value_items);
}
break;
default:
foreach($values as &$value) {
$value = $this->option_value_encode($value);
if (empty($value)) {
unset($value);
}
}
$filter[] = $key .'='. implode(',', $values);
break;
}
}
$url .= 'filter:'. implode(';', $filter);
unset($params['filter']);
}
}
private function parseFilter($paths, &$params) {
$params['filter'] = [];
$filter_str = substr($paths, 7);
$filter_options = explode(';', $filter_str);
foreach ($filter_options as $filter_option) {
if (empty($filter_option)) {
continue;
}
list($filter_key, $filter_option) = explode('=', $filter_option);
if($filter_key == 'prices') { // price-interval section
$prices = explode(':', $filter_option);
$params['filter'][$filter_key] = [
'min' => floatval($prices[0]),
'max' => floatval($prices[1]),
];
} elseif (strpos($filter_key, $this->option_prefix) === 0) { // options section
$params['filter']['options'][substr($filter_key, 2)] = explode(',', $filter_option);
} else { // brands and other sections
$params['filter'][$filter_key] = explode(',', $filter_option);
}
}
}
|
3f2bc3d0
Administrator
first commit
|
216
|
}
|