Blame view

common/modules/product/CatalogUrlManager.php 8.91 KB
3f2bc3d0   Administrator   first commit
1
2
3
4
  <?php

  

  namespace common\modules\product;

  

1b898c16   Administrator   20.07.16
5
  use common\modules\product\helpers\FilterHelper;

3f2bc3d0   Administrator   first commit
6
  use common\modules\product\models\Brand;

38828295   Karnovsky A   -
7
  use common\modules\product\models\BrandSearch;

3f2bc3d0   Administrator   first commit
8
9
10
11
12
  use common\modules\product\models\CategorySearch;

  use common\modules\product\models\ProductSearch;

  use common\modules\rubrication\models\TaxOption;

  use Yii;

  use yii\helpers\Url;

3918480b   Karnovsky A   UrlManager
13
  use yii\web\HttpException;

3f2bc3d0   Administrator   first commit
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  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);

  

f27a7bd1   Administrator   20.07.16
32
  

3f2bc3d0   Administrator   first commit
33
34
35
36
37
38
39
          if (!array_key_exists($paths[0], $this->route_map)) {

              return false;

          }

  

          $params = [];

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

              $route = 'catalog/category';

f27a7bd1   Administrator   20.07.16
40
  

3f2bc3d0   Administrator   first commit
41
42
43
44
45
46
47
48
49
50
              // 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

1b898c16   Administrator   20.07.16
51
52
  

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

868680ca   Karnovsky A   -
53
                      $this->parseFilter($paths[2], $params);

ac8ab861   Administrator   20.07.16
54
  

0f852b51   Administrator   29.06.16
55
                  }

1b898c16   Administrator   20.07.16
56
57
58
                  else if(strpos($paths[2], 'filter:') === 0){

                      $this->parseOldFilter($paths[2], $params);

                      //['catalog/category', 'category' => $category, 'filters' =>$params['filter']]

ed62b6ee   Administrator   20.07.16
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  

                      $optionsTemplate = FilterHelper::optionsTemplate();

                      array_unshift($optionsTemplate, "special", "brands");

                      $filterView = [];

                      foreach($optionsTemplate as $optionKey){

                          if(isset($params['filter'][$optionKey])){

                              $filterView[$optionKey] = $params['filter'][$optionKey];

                          }

  

  

                      }

  

  

                      Yii::$app->response->redirect(['catalog/category', 'category' => $category, 'filters' =>$filterView],301);

1b898c16   Administrator   20.07.16
73
                  }

a3b7905e   Karnovsky A   UrlManager
74
75
76
                  else {

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

                  }

3f2bc3d0   Administrator   first commit
77
              }

079f016c   Administrator   14.09.16
78
79
80
              if(isset($paths[1]) && empty($paths[1])){

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

              }

3f2bc3d0   Administrator   first commit
81
          } elseif ($paths[0] == 'product') {

4365261e   Karnovsky A   UrlManager
82
83
84
              if (!empty($paths[2])) {

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

              }

3f2bc3d0   Administrator   first commit
85
86
87
88
89
90
91
92
              $product = ProductSearch::findByAlias($paths[1]);

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

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

              }

              $route = 'catalog/product';

              $params = [

                  'product' => $product,

              ];

3f2bc3d0   Administrator   first commit
93
94
95
96
97
98
99
100
101
102
103
104
105
          }

  

          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)

      {

ac8ab861   Administrator   20.07.16
106
  

ea07e7cb   Alex Savenko   canonical update2.0
107
108
  

  

3f2bc3d0   Administrator   first commit
109
110
111
112
113
114
115
116
          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']);

62cd1755   Administrator   14.09.16
117
                      $url = 'catalog/'. $category_alias ;

3f2bc3d0   Administrator   first commit
118
119
120
                  } else {

                      $url = 'catalog/';

                  }

3f2bc3d0   Administrator   first commit
121
  

868680ca   Karnovsky A   -
122
                  $this->setFilterUrl($params, $url);

3f2bc3d0   Administrator   first commit
123
  

9d539ac3   Karnovsky A   Brands and option...
124
125
126
127
128
129
                  foreach ($params as $key => $param) {

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

                          unset($params[$key]);

                      }

                  }

  

ccc7a9d3   Karnovsky A   Karnovsky 12052016
130
131
132
133
                  if (!empty($params) && ($query = http_build_query($params)) !== '') {

                      $url .= '?' . $query;

                  }

  

3f2bc3d0   Administrator   first commit
134
135
136
137
                  return $url;

                  break;

  

              case 'catalog/product':

ea07e7cb   Alex Savenko   canonical update2.0
138
  

3f2bc3d0   Administrator   first commit
139
140
                  if (!empty($params['product'])) {

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

ccc7a9d3   Karnovsky A   Karnovsky 12052016
141
  

b7c30426   Administrator   14.09.16
142
143
                  }

                  $url = 'product/'. $product_alias;

ccc7a9d3   Karnovsky A   Karnovsky 12052016
144
145
146
147
                  if (!empty($params) && ($query = http_build_query($params)) !== '') {

                      $url .= '?' . $query;

                  }

  

38828295   Karnovsky A   -
148
  

38828295   Karnovsky A   -
149
150
151
  

                  return $url;

                  break;

ac8ab861   Administrator   20.07.16
152
153
154
155
156
157
158
159
160
  

  //            case 'catalog/brands':

  //                if (empty($params['brand'])) {

  //                    return 'brands';

  //                } else {

  //

  //                    $brand_alias = is_object($params['brand']) ? $params['brand']->alias : strtolower($params['brand']);

  //                }

  //                $url = 'brands/'. $brand_alias .'/';

26f2d2d0   Administrator   20.07.16
161
  //

ac8ab861   Administrator   20.07.16
162
163
164
165
166
167
168
169
  //                $this->setFilterUrl($params, $url);

  //

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

  //                    $url .= '?' . $query;

  //                }

  //

  //                return $url;

  //                break;

3f2bc3d0   Administrator   first commit
170
171
172
173
174
175
          }

      }

  

      private function option_value_encode($value) {

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

      }

868680ca   Karnovsky A   -
176
177
178
  

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

          $filter = [];

1b898c16   Administrator   20.07.16
179
180
          if (!empty($params['filters'])) {

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

868680ca   Karnovsky A   -
181
182
183
184
185
                  switch($key) {

                      case 'prices':

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

                          break;

  

868680ca   Karnovsky A   -
186
187
188
189
190
191
192
193
194
195
196
                      default:

                          foreach($values as &$value) {

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

                              if (empty($value)) {

                                  unset($value);

                              }

                          }

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

                          break;

                  }

              }

9d539ac3   Karnovsky A   Brands and option...
197
              if (!empty($filter)) {

62cd1755   Administrator   14.09.16
198
                  $url .= '/filters:'. implode(';', $filter);

9d539ac3   Karnovsky A   Brands and option...
199
              }

1b898c16   Administrator   20.07.16
200
              unset($params['filters']);

868680ca   Karnovsky A   -
201
202
203
204
          }

      }

  

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

1b898c16   Administrator   20.07.16
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
          $params['filters'] = [];

          $filter_str = substr($paths, 8);

          $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['filters'][$filter_key] = [

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

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

                  ];

              }

              else { // brands and other sections

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

              }

          }

      }

  

  

  

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

868680ca   Karnovsky A   -
229
230
231
232
233
234
235
236
237
238
239
240
241
242
          $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]),

                  ];

1b898c16   Administrator   20.07.16
243
244
245
246
247
              }

              elseif (strpos($filter_key, $this->option_prefix) === 0) { // options section

                  $params['filter'][substr($filter_key, 2)] = explode(',', $filter_option);

              }

              else { // brands and other sections

868680ca   Karnovsky A   -
248
249
250
251
                  $params['filter'][$filter_key] = explode(',', $filter_option);

              }

          }

      }

3f2bc3d0   Administrator   first commit
252
  }