Blame view

common/modules/product/CatalogUrlManager.php 6.51 KB
4ca21c3e   Alexey Boroda   first commit
1
2
3
4
5
6
7
8
  <?php

  

  namespace common\modules\product;

  

  

  use common\modules\product\models\CategorySearch;

  use common\modules\product\models\ProductSearch;

  use yii\helpers\Url;

4556b430   Alexey Boroda   Changes:
9
  use yii\helpers\VarDumper;

4ca21c3e   Alexey Boroda   first commit
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
  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

       * @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(isset($paths[1])) {

              if(strripos($request->url,'catalog/'.$paths[1].'?') && (!strripos($request->url,'?page')) && (!strripos($request->url,'?sort'))){

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

              }

  

          }

  

  

          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], 'filters:') === 0) {

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

  

                  }

                  else {

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

                  }

              }

          } elseif ($paths[0] == 'product') {

4556b430   Alexey Boroda   Changes:
63
  

4ca21c3e   Alexey Boroda   first commit
64
              $route = 'catalog/product';

4556b430   Alexey Boroda   Changes:
65
  

4ca21c3e   Alexey Boroda   first commit
66
              $params = [

4556b430   Alexey Boroda   Changes:
67
68
                  'product' => $paths[1],

                  'variant' => $paths[2]

4ca21c3e   Alexey Boroda   first commit
69
70
              ];

          }

4ca21c3e   Alexey Boroda   first commit
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
          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 .'/';

                      unset($params['category']);

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

                      unset($params['product']);

                  }

4556b430   Alexey Boroda   Changes:
117
118
119
120
121
122
123
124
125
                  if (!empty($params['variant'])) {

                      $variant_alias = is_object($params['variant']) ? $params['variant']->sku : strtolower($params['variant']);

                      unset($params['variant']);

                  }

  

                  /**

                   * todo refactor this

                   */

                  $url = 'product/'. $product_alias.'/'.$variant_alias;

4ca21c3e   Alexey Boroda   first commit
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
  

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

                      $url .= '?' . $query;

                  }

  

  

  

                  return $url;

                  break;

  

          }

      }

  

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

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

              }

          }

      }

  

  }