Blame view

common/modules/product/CatalogUrlManager.php 11.8 KB
4253cbec   root   first commit
1
2
3
4
  <?php

  

  namespace common\modules\product;

  

ddf2d8f9   Administrator   big commti
5
  

4253cbec   root   first commit
6
7
  use common\modules\product\models\CategorySearch;

  use common\modules\product\models\ProductSearch;

ddf2d8f9   Administrator   big commti
8
  

bc07fb47   Administrator   big commti
9
  use common\modules\product\models\ProductVariant;

4253cbec   root   first commit
10
11
12
13
14
15
16
  use Yii;

  use yii\helpers\Url;

  use yii\web\HttpException;

  use yii\web\UrlRuleInterface;

  

  class CatalogUrlManager implements UrlRuleInterface {

      public $route_map = [];

4253cbec   root   first commit
17
18
19
20
21
22
23
24
25
      /**

       * 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)

      {

bc07fb47   Administrator   big commti
26
27
  

  

4253cbec   root   first commit
28
29
          $pathInfo = $request->getPathInfo();

          $paths = explode('/', $pathInfo);

bc07fb47   Administrator   big commti
30
31
32
33
34
35
36
37
          /**

           * linija-svitla.ua only

           */

          $pattern = '/^(.+)\.htm$/';

          if(isset($paths[0])){

              preg_match($pattern,$paths[0], $match );

              if(!empty($match)){

                  $old_item_name = explode('-',$paths[0] );

c5c80cc0   Administrator   big commti
38
39
                  array_pop($old_item_name);

                  $count = count($old_item_name)-1;

3d33f4f5   Administrator   big commti
40
  

c5c80cc0   Administrator   big commti
41
42
43
                  if(isset($old_item_name[$count]) && $old_item_name[$count] == 'dis' ){

  

                      $sku = $old_item_name[$count-3].'/'.$old_item_name[$count-2].'/'.$old_item_name[$count-1].'_'.$old_item_name[$count];

3d33f4f5   Administrator   big commti
44
45
46
47
48
                      $product_variant = ProductVariant::find()->where(['sku'=>$sku])->one();

                      if($product_variant instanceof ProductVariant){

  

                          $link = 'product'.'/'. $product_variant->product->alias;

                          header("Status: 301");

c8ee17e4   Administrator   big commti
49
                          header("Location: http://www.linija-svitla.ua/".$link);

3d33f4f5   Administrator   big commti
50
51
52
53
54
                          die();

                      }

                  }

  

  

c5c80cc0   Administrator   big commti
55
56
57
                  if(isset($old_item_name[$count-3]) && isset($old_item_name[$count-2]) && isset($old_item_name[$count-1]) && isset($old_item_name[$count])){

  

                      $sku = $old_item_name[$count-3].'/'.$old_item_name[$count-2].'/'.$old_item_name[$count-1].'/'.$old_item_name[$count];

3d33f4f5   Administrator   big commti
58
59
60
61
62
                      $product_variant = ProductVariant::find()->where(['sku'=>$sku])->one();

                      if($product_variant instanceof ProductVariant){

  

                          $link = 'product'.'/'. $product_variant->product->alias;

                          header("Status: 301");

c8ee17e4   Administrator   big commti
63
                          header("Location: http://www.linija-svitla.ua/".$link);

3d33f4f5   Administrator   big commti
64
65
66
67
                          die();

                      }

                  }

  

c5c80cc0   Administrator   big commti
68
69
70
71
72
73
74
75
                  if(isset($old_item_name[$count-2]) && isset($old_item_name[$count-1]) && isset($old_item_name[$count])){

                      $sku = $old_item_name[$count-2].'/'.$old_item_name[$count-1].'/'.$old_item_name[$count];

                      $product_variant = ProductVariant::find()->where(['sku'=>$sku])->one();

  

                      if($product_variant instanceof ProductVariant){

  

                          $link = 'product'.'/'. $product_variant->product->alias;

                          header("Status: 301");

c8ee17e4   Administrator   big commti
76
                          header("Location: http://www.linija-svitla.ua/".$link);

c5c80cc0   Administrator   big commti
77
78
79
80
81
82
83
84
                          die();

                      }

                      $sku = $old_item_name[$count-2].'/'.$old_item_name[$count-1].' '.$old_item_name[$count];

                      $product_variant = ProductVariant::find()->where(['sku'=>$sku])->one();

  

                      if($product_variant instanceof ProductVariant){

                          $link = 'product'.'/'. $product_variant->product->alias;

                          header("Status: 301");

c8ee17e4   Administrator   big commti
85
                          header("Location: http://www.linija-svitla.ua/".$link);

c5c80cc0   Administrator   big commti
86
87
88
89
90
91
92
93
94
                          die();

                      }

  

                      $sku = $old_item_name[$count-2].'/'.$old_item_name[$count-1].'-'.$old_item_name[$count];

                      $product_variant = ProductVariant::find()->where(['sku'=>$sku])->one();

  

                      if($product_variant instanceof ProductVariant){

                          $link = 'product'.'/'. $product_variant->product->alias;

                          header("Status: 301");

c8ee17e4   Administrator   big commti
95
                          header("Location: http://www.linija-svitla.ua/".$link);

c5c80cc0   Administrator   big commti
96
97
98
99
                          die();

                      }

  

                      $sku = $old_item_name[$count-2].'/'.$old_item_name[$count-1].'_'.$old_item_name[$count];

bc07fb47   Administrator   big commti
100
                      $product_variant = ProductVariant::find()->where(['sku'=>$sku])->one();

3d33f4f5   Administrator   big commti
101
102
  

                      if($product_variant instanceof ProductVariant){

c5c80cc0   Administrator   big commti
103
104
                          $link = 'product'.'/'. $product_variant->product->alias;

                          header("Status: 301");

c8ee17e4   Administrator   big commti
105
                          header("Location: http://www.linija-svitla.ua/".$link);

c5c80cc0   Administrator   big commti
106
107
108
109
110
111
112
113
114
                          die();

                      }

  

                  }

  

                  if(isset($old_item_name[$count-1]) && isset($old_item_name[$count])){

                      $sku = $old_item_name[$count-1].'/'.$old_item_name[$count];

                      $product_variant = ProductVariant::find()->where(['sku'=>$sku])->one();

                      if($product_variant instanceof ProductVariant){

3d33f4f5   Administrator   big commti
115
116
117
  

                          $link = 'product'.'/'. $product_variant->product->alias;

                          header("Status: 301");

c8ee17e4   Administrator   big commti
118
                          header("Location: http://www.linija-svitla.ua/".$link);

3d33f4f5   Administrator   big commti
119
120
121
122
                          die();

                      }

                  }

  

c5c80cc0   Administrator   big commti
123
124
                  if(isset($old_item_name[$count])){

                      $sku = $old_item_name[$count];

3d33f4f5   Administrator   big commti
125
                      $product_variant = ProductVariant::find()->where(['sku'=>$sku])->one();

bc07fb47   Administrator   big commti
126
127
128
129
                      if($product_variant instanceof ProductVariant){

  

                          $link = 'product'.'/'. $product_variant->product->alias;

                          header("Status: 301");

c8ee17e4   Administrator   big commti
130
                          header("Location: http://www.linija-svitla.ua/".$link);

bc07fb47   Administrator   big commti
131
132
133
134
135
136
                          die();

                      }

                  }

              }

          }

  

c5c80cc0   Administrator   big commti
137
138
139
          /**

           * end linija-svitla.ua only

           */

bc07fb47   Administrator   big commti
140
141
  

  

4253cbec   root   first commit
142
  

4d5dbae3   Administrator   big commti
143
144
  

  

4253cbec   root   first commit
145
146
147
148
149
150
151
          if (!array_key_exists($paths[0], $this->route_map)) {

              return false;

          }

  

          $params = [];

          if ($paths[0] == 'catalog') {

              $route = 'catalog/category';

e337d04a   Administrator   liniya first commit
152
  

4d5dbae3   Administrator   big commti
153
              // Category

4253cbec   root   first commit
154
155
156
157
158
159
160
              if (!empty($paths[1])) {

                  $category = CategorySearch::findByAlias($paths[1]);

                  if (empty($category)) {

                      http_redirect(Url::to(['/']));

                  }

                  $params['category'] = $category;

              }

4253cbec   root   first commit
161
162
163
164
              if (!empty($paths[2])) {

                  // Filter

  

                  if (strpos($paths[2], 'filters:') === 0) {

950470c1   Administrator   big commti
165
                      if(!isset($paths[3]) ){

8cfcee73   Administrator   big commti
166
167
168
169
170
                          $this->parseFilter($paths[2], $params);

                      } else {

                          throw new HttpException(404 ,'Page not found');

                      }

  

4253cbec   root   first commit
171
172
  

                  }

4253cbec   root   first commit
173
174
175
176
                  else {

                      throw new HttpException(404 ,'Page not found');

                  }

              }

4253cbec   root   first commit
177
          } elseif ($paths[0] == 'product') {

bc07fb47   Administrator   big commti
178
  

4253cbec   root   first commit
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
              if (!empty($paths[2])) {

                  throw new HttpException(404 ,'Page not found');

              }

              $product = ProductSearch::findByAlias($paths[1]);

              if (empty($product->product_id)) {

                  throw new HttpException(404 ,'Page not found');

              }

              $route = 'catalog/product';

              $params = [

                  'product' => $product,

              ];

          }

  

          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 .'/';

ddf2d8f9   Administrator   big commti
213
                      unset($params['category']);

4253cbec   root   first commit
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
                  } else {

                      $url = 'catalog/';

                  }

  

                  $this->setFilterUrl($params, $url);

  

                  foreach ($params as $key => $param) {

                      if (empty($params[$key])) {

                          unset($params[$key]);

                      }

                  }

  

                  if (!empty($params) && ($query = http_build_query($params)) !== '') {

                      $url .= '?' . $query;

                  }

  

                  return $url;

                  break;

  

              case 'catalog/product':

                  if (!empty($params['product'])) {

                      $product_alias = is_object($params['product']) ? $params['product']->alias : strtolower($params['product']);

ddf2d8f9   Administrator   big commti
236
                      unset($params['product']);

4253cbec   root   first commit
237
238
239
240
241
242
243
244
245
246
247
248
                  }

                  $url = 'product/'. $product_alias;

  

                  if (!empty($params) && ($query = http_build_query($params)) !== '') {

                      $url .= '?' . $query;

                  }

  

  

  

                  return $url;

                  break;

  

4253cbec   root   first commit
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
          }

      }

  

      private function option_value_encode($value) {

          return str_replace(array(',', '/'), array('~', '&s;'), $value);

      }

  

      private function setFilterUrl(&$params, &$url) {

          $filter = [];

          if (!empty($params['filters'])) {

              foreach ($params['filters'] as $key => $values) {

                  switch($key) {

                      case 'prices':

                          $filter[] = $key .'='. implode(':', $values);

                          break;

  

                      default:

                          foreach($values as &$value) {

                              $value = $this->option_value_encode($value);

                              if (empty($value)) {

                                  unset($value);

                              }

                          }

                          $filter[] = $key .'='. implode(',', $values);

                          break;

                  }

              }

              if (!empty($filter)) {

                  $url .= 'filters:'. implode(';', $filter);

              }

              unset($params['filters']);

          }

      }

  

      private function parseFilter($paths, &$params) {

          $params['filters'] = [];

          $filter_str = substr($paths, 8);

          $filter_options = explode(';', $filter_str);

          foreach ($filter_options as $filter_option) {

b905419d   Administrator   big commti
288
              $filter_exp = explode('=', $filter_option);

63e867f2   Administrator   big commti
289
              if(!empty($filter_exp[1])){

b905419d   Administrator   big commti
290
291
292
293
294
295
296
297
298
299
300
301
302
303
                  list($filter_key, $filter_option) = explode('=', $filter_option);

                  if($filter_key == 'prices') { // price-interval section

                      $prices = explode(':', $filter_option);

                      $params['filters'][$filter_key] = [

                          'min' => floatval($prices[0]),

                          'max' => floatval($prices[1]),

                      ];

                  }

                  else { // brands and other sections

                      $params['filters'][$filter_key] = explode(',', $filter_option);

                  }

              } else {

                  throw new HttpException(404 ,'Page not found');

              }

b905419d   Administrator   big commti
304
305
306
          }

      }

  

4253cbec   root   first commit
307
  }