Blame view

CatalogUrlManager.php 9.57 KB
fa33d79b   Yarik   Namespaces
1
2
  <?php

      

07d232e4   Administrator   full commit
3
      namespace artweb\artbox\ecommerce;

fa33d79b   Yarik   Namespaces
4
      

07d232e4   Administrator   full commit
5
6
      use artweb\artbox\ecommerce\models\Category;

      use artweb\artbox\ecommerce\models\Product;

07d232e4   Administrator   full commit
7
      use artweb\artbox\ecommerce\models\ProductVariant;

fbd84203   Alexey Boroda   -Url ru - ua hot fix
8
      use yii\helpers\VarDumper;

fa33d79b   Yarik   Namespaces
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
      use yii\web\HttpException;

      use yii\web\UrlRuleInterface;

      

      class CatalogUrlManager implements UrlRuleInterface

      {

          public $route_map = [];

          /**

           * 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

           *

           * @throws HttpException

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

          {

fbd84203   Alexey Boroda   -Url ru - ua hot fix
27
              /**

ff75928d   Alexey Boroda   -Url ru - ua hot ...
28
29
               * Fast url fix.

               * Did not helped with catalog products

fbd84203   Alexey Boroda   -Url ru - ua hot fix
30
               */

ff75928d   Alexey Boroda   -Url ru - ua hot ...
31
32
33
  //            if (!preg_match('@^\/(ru|ua).*@i', $request->url) && $request->url !== '/') {

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

  //            }

fa33d79b   Yarik   Namespaces
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
              

              $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 = Category::find()

60b61e19   Administrator   add variantSku
49
                                          ->joinWith(['lang'])

fa33d79b   Yarik   Namespaces
50
51
52
53
54
55
56
                                          ->where(

                                              [

                                                  'category_lang.alias' => $paths[ 1 ],

                                              ]

                                          )

                                          ->one();

                      if (empty( $category )) {

40300fd0   Alexey Boroda   -Siblings bug
57
                          throw new HttpException(404, 'Page not found');

fa33d79b   Yarik   Namespaces
58
59
                      }

                      $params[ 'category' ] = $category;

6d7217cf   Administrator   add variantSku
60
61
                  } else {

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

fa33d79b   Yarik   Namespaces
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
                  }

                  if (!empty( $paths[ 2 ] )) {

                      // Filter

                      

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

                          if (!isset( $paths[ 3 ] )) {

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

                          } else {

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

                          }

                          

                      } else {

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

                      }

                  }

fa33d79b   Yarik   Namespaces
77
              }

2ad65823   Alexey Boroda   -Added date range...
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
  //            elseif ($paths[ 0 ] == 'product') {

  //

  //                if (!empty( $paths[ 3 ] )) {

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

  //                }

  //                $product = Product::find()

  //                                  ->joinWith('lang')

  //                                  ->where([ 'product_lang.alias' => $paths[ 1 ] ])

  //                                  ->one();

  //                $variant = ProductVariant::find()

  //                                         ->joinWith('lang')

  //                                         ->where([ 'sku' => $paths[ 2 ] ])

  //                                         ->one();

  //

  //                if (empty( $variant->id ) || empty( $product->id )) {

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

  //                }

  //                $route = 'catalog/product';

  //                $params = [

  //                    'product' => $paths[1],

  //                    'variant' => $variant,

  //                ];

  //            }

fa33d79b   Yarik   Namespaces
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
              

              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)

          {

              

c058aa71   Yarik   Namespaces
119
              

fa33d79b   Yarik   Namespaces
120
121
122
123
124
125
126
127
128
129
130
131
              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' ]->lang->alias : strtolower(

                              $params[ 'category' ]

                          );

464d2610   Administrator   add variantSku
132
                          $url = 'catalog/' . $category_alias ;

fa33d79b   Yarik   Namespaces
133
134
                          unset( $params[ 'category' ] );

                      } else {

464d2610   Administrator   add variantSku
135
                          $url = 'catalog';

fa33d79b   Yarik   Namespaces
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
                      }

                      

                      $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':

c058aa71   Yarik   Namespaces
154
                      

4a447a24   Yarik   Catalog begin
155
156
157
                      $product_alias = '';

                      $variant_sku = '';

                      

fa33d79b   Yarik   Namespaces
158
                      if (!empty( $params[ 'product' ] )) {

c058aa71   Yarik   Namespaces
159
                          $product_alias = strtolower($params[ 'product' ]);

fa33d79b   Yarik   Namespaces
160
161
                          unset( $params[ 'product' ] );

                      }

a1759641   Alexey Boroda   -Product variants...
162
  

c058aa71   Yarik   Namespaces
163
164
165
166
                      if (!empty( $params[ 'variant' ] )) {

                          $variant_sku = strtolower($params[ 'variant' ]);

                          unset( $params[ 'variant' ] );

                      }

a1759641   Alexey Boroda   -Product variants...
167
  

c058aa71   Yarik   Namespaces
168
                      $url = 'product/' . $product_alias . '/' . $variant_sku;

a1759641   Alexey Boroda   -Product variants...
169
  

fa33d79b   Yarik   Namespaces
170
171
172
                      if (!empty( $params ) && ( $query = http_build_query($params) ) !== '') {

                          $url .= '?' . $query;

                      }

a1759641   Alexey Boroda   -Product variants...
173
  

fa33d79b   Yarik   Namespaces
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
216
                      return $url;

                      break;

                  

              }

          }

          

          private function option_value_encode($value)

          {

              return str_replace(

                  [

                      ',',

                      '/',

                  ],

                  [

                      '~',

                      '&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 )) {

3bb4c5a7   Administrator   add variantSku
217
                      $url .= '/filters:' . implode(';', $filter);

fa33d79b   Yarik   Namespaces
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
                  }

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

                  $filter_exp = explode('=', $filter_option);

                  if (!empty( $filter_exp[ 1 ] )) {

                      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');

                  }

              }

          }

          

      }