Blame view

helpers/FilterHelper.php 14.7 KB
2f25da09   Yarik   first commit
1
2
3
4
5
6
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
  <?php
      
      namespace artweb\artbox\ecommerce\helpers;
      
      use artweb\artbox\ecommerce\models\BrandLang;
      use artweb\artbox\ecommerce\models\CategoryLang;
      use artweb\artbox\ecommerce\models\Product;
      use artweb\artbox\ecommerce\models\ProductLang;
      use artweb\artbox\ecommerce\models\ProductVariant;
      use artweb\artbox\ecommerce\models\ProductVariantLang;
      use artweb\artbox\ecommerce\models\TaxGroup;
      use yii\base\Object;
      use yii\db\ActiveQuery;
      use yii\db\Query;
      use yii\helpers\ArrayHelper;
      
      class FilterHelper extends Object
      {
          
          public static $optionsList = [];
          
          /**
           * Get TaxGroups
           *
           * @return array
           */
          public static function optionsTemplate()
          {
              if (empty( static::$optionsList )) {
                  return static::$optionsList = ArrayHelper::getColumn(
                      TaxGroup::find()
                              ->joinWith('lang')
                              ->where([ 'is_filter' => 'TRUE' ])
                              ->all(),
                      'lang.alias'
                  );
              } else {
                  return static::$optionsList;
              }
              
          }
          
          /**
           * Return custom filter-option link
           *
           * @param array  $filter
           * @param string $key
           * @param mixed  $value
           * @param bool   $remove
           *
           * @return array
           */
          public static function getFilterForOption(array $filter, string $key, $value, bool $remove = false)
          {
              
              $optionsTemplate = self::optionsTemplate();
              array_unshift($optionsTemplate, "special", "brands");
              
              $result = $filter;
              
              if (is_array($value)) {
                  foreach ($value as $value_key => $value_items) {
                      if (!is_array($value_items)) {
                          $value_items = [ $value_items ];
                      }
                      foreach ($value_items as $value_item) {
                          if ($remove && isset( $result[ $key ] ) && ( $i = array_search(
                                  $value_item,
                                  $result[ $key ][ $value_key ]
                              ) ) !== false
                          ) {
                              unset( $result[ $key ][ $value_key ][ $i ] );
                              if (empty( $result[ $key ][ $value_key ] )) {
                                  unset( $result[ $key ][ $value_key ] );
                              }
                          } else {
                              if (!isset( $result[ $key ][ $value_key ] ) || array_search(
                                      $value_item,
                                      $result[ $key ][ $value_key ]
                                  ) === false
                              ) {
                                  $result[ $key ][ $value_key ][] = $value_item;
                              }
                          }
                      }
                  }
              } else {
                  if ($remove && isset( $result[ $key ] ) && ( $i = array_search($value, $result[ $key ]) ) !== false) {
                      unset( $result[ $key ][ $i ] );
                      if (empty( $result[ $key ] )) {
                          unset( $result[ $key ] );
                      }
                  } else {
                      if (!isset( $result[ $key ] ) || array_search($value, $result[ $key ]) === false) {
                          $result[ $key ][] = $value;
                      }
                  }
              }
              
              $filterView = [];
              
              foreach ($optionsTemplate as $optionKey) {
                  if (isset( $result[ $optionKey ] )) {
                      $filterView[ $optionKey ] = $result[ $optionKey ];
                  }
                  
              }
              
              return $filterView;
          }
ad348101   Administrator   add variantSku
111
112
113
  
  
  
2f25da09   Yarik   first commit
114
115
116
117
118
119
120
121
122
123
124
125
          /**
           * Fill query with filter conditions
           *
           * @param ActiveQuery $query
           * @param array       $params
           */
          public static function setQueryParams(ActiveQuery $query, array $params)
          {
              $last_query = null;
              foreach ($params as $key => $param) {
                  switch ($key) {
                      case 'special':
ad348101   Administrator   add variantSku
126
                          unset($params[$key]);
2f25da09   Yarik   first commit
127
128
129
                          self::filterSpecial($param, $query);
                          break;
                      case 'brands':
ad348101   Administrator   add variantSku
130
                          unset($params[$key]);
2f25da09   Yarik   first commit
131
132
133
                          self::filterBrands($param, $query);
                          break;
                      case 'keywords':
ad348101   Administrator   add variantSku
134
                          unset($params[$key]);
2f25da09   Yarik   first commit
135
136
137
                          self::filterKeywords($param, $query);
                          break;
                      case 'prices':
ad348101   Administrator   add variantSku
138
                          unset($params[$key]);
2f25da09   Yarik   first commit
139
140
                          self::filterPrices($param, $query);
                          break;
2f25da09   Yarik   first commit
141
142
                  }
              }
ad348101   Administrator   add variantSku
143
              $last_query = self::filterOptions($params, $last_query);
2f25da09   Yarik   first commit
144
145
146
147
148
              // If tax option filters were provided filter query with them
              if (!empty( $last_query )) {
                  $query->andWhere([ 'product.id' => $last_query ]);
              }
          }
ad348101   Administrator   add variantSku
149
150
151
152
  
  
  
  
2f25da09   Yarik   first commit
153
154
155
156
157
          /**
           * Tax Option filter
           *
           * @param string[]           $params
           * @param \yii\db\Query|null $last_query
4fd39cf0   Yarik   Query fix in_stoc...
158
           * @param bool               $in_stock
2f25da09   Yarik   first commit
159
           *
4fd39cf0   Yarik   Query fix in_stoc...
160
           * @return \yii\db\Query
2f25da09   Yarik   first commit
161
           */
4fd39cf0   Yarik   Query fix in_stoc...
162
          private static function filterOptions(array $params, Query $last_query = null, bool $in_stock = true): Query
2f25da09   Yarik   first commit
163
164
165
166
167
168
169
          {
              $variant_query = ( new Query() )->distinct()
                                              ->select('product_variant.product_id as products')
                                              ->from('product_variant_option')
                                              ->innerJoin(
                                                  'product_variant',
                                                  'product_variant_option.product_variant_id = product_variant.id'
ad348101   Administrator   add variantSku
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
                                              );
  
                                              foreach ($params as $key=>$param){
                                                  $product_variant_id = ( new Query() )->distinct()->select('product_variant_option.product_variant_id as id')
                                                      ->from('product_variant_option')
                                                      ->innerJoin(
                                                          'tax_option',
                                                          'tax_option.id = product_variant_option.option_id'
                                                      )
                                                      ->innerJoin(
                                                          'tax_option_lang',
                                                          'tax_option_lang.tax_option_id = tax_option.id'
                                                      )
                                                      ->innerJoin(
                                                          'tax_group',
                                                          'tax_group.id = tax_option.tax_group_id'
                                                      )
                                                      ->innerJoin(
                                                          'tax_group_lang',
                                                          'tax_group_lang.tax_group_id = tax_group.id'
                                                      )
                                                      ->where([ 'tax_group_lang.alias' => $key ])
                                                      ->andWhere([ 'tax_option_lang.alias' => $param ]);
                                                  $variant_query->andWhere(['product_variant_option.product_variant_id'=>$product_variant_id]);
  
                                              }
  
  
4fd39cf0   Yarik   Query fix in_stoc...
198
199
200
              if($in_stock) {
                  $variant_query->andWhere(['!=', 'product_variant.stock', 0]);
              }
2f25da09   Yarik   first commit
201
202
              $product_query = ( new Query() )->distinct()
                                              ->select('product_option.product_id as products')
ad348101   Administrator   add variantSku
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
                                              ->from('product_option');
                                             foreach ($params as $key=>$param){
                                                 $product_id = ( new Query() )->distinct()->select('product_option.product_id as id')
                                                     ->from('product_option')
                                                     ->innerJoin(
                                                         'tax_option',
                                                         'tax_option.id = product_option.option_id'
                                                     )
                                                     ->innerJoin(
                                                         'tax_option_lang',
                                                         'tax_option_lang.tax_option_id = tax_option.id'
                                                     )
                                                     ->innerJoin(
                                                         'tax_group',
                                                         'tax_group.id = tax_option.tax_group_id'
                                                     )
                                                     ->innerJoin(
                                                         'tax_group_lang',
                                                         'tax_group_lang.tax_group_id = tax_group.id'
                                                     )
                                                     ->where([ 'tax_group_lang.alias' => $key ])
                                                     ->andWhere([ 'tax_option_lang.alias' => $param ]);
                                                 $product_query->andWhere(['product_option.product_id'=>$product_id]);
  
                                             }
              $product_query->union($variant_query);
2f25da09   Yarik   first commit
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
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
288
289
290
291
292
              $query = ( new Query() )->select('products')
                                      ->from([ 'result_table' => $product_query ]);
              if (!empty( $last_query )) {
                  $query->andWhere([ 'product.id' => $last_query ]);
              }
              return $query;
          }
          
          /**
           * Fill $query with special filters (used in Product)
           *
           * @param array               $params
           * @param \yii\db\ActiveQuery $query
           */
          private static function filterSpecial(array $params, ActiveQuery $query)
          {
              $conditions = [];
              /**
               * @var string $key
               */
              foreach ($params as $key => $param) {
                  $conditions[] = [
                      '=',
                      Product::tableName() . '.' . $key,
                      $param,
                  ];
              }
              /* If 2 or more special conditions get all that satisfy at least one of them. */
              if (count($conditions) > 1) {
                  array_unshift($conditions, 'or');
              } else {
                  $conditions = $conditions[ 0 ];
              }
              $query->andFilterWhere($conditions);
          }
          
          /**
           * Fill query with brands filter
           *
           * @param int[]               $param
           * @param \yii\db\ActiveQuery $query
           */
          private static function filterBrands(array $param, ActiveQuery $query)
          {
              $query->andFilterWhere([ Product::tableName() . '.brand_id' => $param ]);
          }
          
          /**
           * Fill query with keywords filter
           *
           * @param array               $params
           * @param \yii\db\ActiveQuery $query
           */
          private static function filterKeywords(array $params, ActiveQuery $query)
          {
              $conditions = [];
              if (!empty( $params )) {
                  if (!is_array($params)) {
                      $params = [ $params ];
                  }
                  /**
                   * @var string $param Inputed keyword
                   */
                  foreach ($params as $param) {
ac8d2b24   Administrator   add variantSku
293
  
68f06691   Administrator   add variantSku
294
                      if(iconv_strlen($param) >= 3){
ac8d2b24   Administrator   add variantSku
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
                          $conditions[] = [
                              'or',
                              [
                                  'ilike',
                                  ProductLang::tableName() . '.title',
                                  $param,
                              ],
                              [
                                  'ilike',
                                  BrandLang::tableName() . '.title',
                                  $param,
                              ],
                              [
                                  'ilike',
                                  CategoryLang::tableName() . '.title',
                                  $param,
                              ],
                              [
                                  'ilike',
                                  ProductVariantLang::tableName() . '.title',
                                  $param,
                              ],
                              [
                                  'ilike',
                                  ProductVariant::tableName() . '.sku',
                                  $param,
                              ]
  
                          ];
                      }
  
  
2f25da09   Yarik   first commit
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
                  }
              }
              if (count($conditions) > 1) {
                  array_unshift($conditions, 'or');
              } else {
                  $conditions = $conditions[ 0 ];
              }
              $query->andFilterWhere($conditions);
          }
          
          /**
           * Fill query with price limits filter
           *
           * @param array               $params
           * @param \yii\db\ActiveQuery $query
           */
          private static function filterPrices(array $params, ActiveQuery $query)
          {
              $conditions = [];
              if (!empty( $params[ 'min' ] ) && $params[ 'min' ] > 0) {
                  $conditions[] = [
                      '>=',
                      ProductVariant::tableName() . '.price',
                      $params[ 'min' ],
                  ];
              }
              if (!empty( $params[ 'max' ] ) && $params[ 'max' ] > 0) {
                  $conditions[] = [
                      '<=',
                      ProductVariant::tableName() . '.price',
                      $params[ 'max' ],
                  ];
              }
              if (count($conditions) > 1) {
                  array_unshift($conditions, 'and');
              } else {
                  $conditions = $conditions[ 0 ];
              }
              $query->andFilterWhere($conditions);
          }
          
      }