Blame view

helpers/FilterHelper.php 14.9 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
  
  
  
cb55aee8   Administrator   add variantSku
114
115
116
117
118
119
120
121
122
123
124
125
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
  
  
          public static function setNewQueryParams(ActiveQuery $query, array $params)
          {
              $last_query = null;
              foreach ($params as $key => $param) {
                  switch ($key) {
                      case 'special':
                          self::filterSpecial($param, $query);
                          break;
                      case 'brands':
                          self::filterBrands($param, $query);
                          break;
                      case 'keywords':
                          self::filterKeywords($param, $query);
                          break;
                      case 'prices':
                          self::filterPrices($param, $query);
                          break;
                      default:
                          self::filterNewOptions($key, $param, $query);
                          break;
                  }
              }
          }
  
  
          /**
           * @param $key
           * @param array $param
           * @param ActiveQuery $query
           */
          private static function filterNewOptions($key, array $param, &$query)
          {
              $query->andWhere(
                      'product.id IN (
                          SELECT DISTINCT products
                          FROM (
                              SELECT id AS products FROM product WHERE id IN(
                              SELECT product_id FROM  product_option
                                  INNER JOIN tax_option ON tax_option.id = product_option.option_id
                                  INNER JOIN tax_group ON tax_group.id = tax_option.tax_group_id
                                  INNER JOIN tax_group_lang ON tax_group.id = tax_group_lang.tax_group_id
                                  INNER JOIN tax_option_lang ON tax_option.id = tax_option_lang.tax_option_id
                                  WHERE tax_group_lang.alias = \''. $key .'\' AND tax_option_lang.alias IN (\'' . implode('\',\'', $param) . '\'))
                               OR id IN (
                                (SELECT product_id AS products
                                  FROM product_variant_option
                                  INNER JOIN product_variant ON product_variant_option.product_variant_id = product_variant.id
                                  INNER JOIN tax_option ON tax_option.id = product_variant_option.option_id
                                  INNER JOIN tax_group ON tax_group.id = tax_option.tax_group_id
                                  INNER JOIN tax_group_lang ON tax_group.id = tax_group_lang.tax_group_id
                                  INNER JOIN tax_option_lang ON tax_option.id = tax_option_lang.tax_option_id
                                  WHERE tax_group_lang.alias = \''. $key .'\' AND tax_option_lang.alias IN (\'' . implode('\',\'', $param) . '\'))
                                  )
                              ) AS table_name
                          )'
              );
  
  
          }
  
  
2f25da09   Yarik   first commit
177
178
179
180
181
182
183
184
185
186
187
188
          /**
           * 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':
2f25da09   Yarik   first commit
189
190
191
                          self::filterSpecial($param, $query);
                          break;
                      case 'brands':
2f25da09   Yarik   first commit
192
193
194
                          self::filterBrands($param, $query);
                          break;
                      case 'keywords':
2f25da09   Yarik   first commit
195
196
197
                          self::filterKeywords($param, $query);
                          break;
                      case 'prices':
2f25da09   Yarik   first commit
198
199
                          self::filterPrices($param, $query);
                          break;
cb55aee8   Administrator   add variantSku
200
201
202
                      default:
                          $last_query = self::filterOptions($param, $last_query);
                          break;
2f25da09   Yarik   first commit
203
204
                  }
              }
2f25da09   Yarik   first commit
205
206
207
208
209
              // If tax option filters were provided filter query with them
              if (!empty( $last_query )) {
                  $query->andWhere([ 'product.id' => $last_query ]);
              }
          }
ad348101   Administrator   add variantSku
210
211
  
  
cb55aee8   Administrator   add variantSku
212
          
2f25da09   Yarik   first commit
213
214
215
216
217
          /**
           * Tax Option filter
           *
           * @param string[]           $params
           * @param \yii\db\Query|null $last_query
2f25da09   Yarik   first commit
218
           *
cb55aee8   Administrator   add variantSku
219
           * @return Query
2f25da09   Yarik   first commit
220
           */
cb55aee8   Administrator   add variantSku
221
          private static function filterOptions(array $params, Query $last_query = null): Query
2f25da09   Yarik   first commit
222
          {
cb55aee8   Administrator   add variantSku
223
  
2f25da09   Yarik   first commit
224
225
226
227
228
229
              $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'
cb55aee8   Administrator   add variantSku
230
231
232
233
234
235
236
237
238
239
                                              )
                                              ->innerJoin(
                                                  'tax_option',
                                                  'tax_option.id = product_variant_option.option_id'
                                              )
                                              ->innerJoin(
                                                  'tax_option_lang',
                                                  'tax_option_lang.tax_option_id = tax_option.id'
                                              )
                                              ->where([ 'tax_option_lang.alias' => $params ]);
2f25da09   Yarik   first commit
240
241
              $product_query = ( new Query() )->distinct()
                                              ->select('product_option.product_id as products')
cb55aee8   Administrator   add variantSku
242
243
244
245
246
247
248
249
250
251
                                              ->from('product_option')
                                              ->innerJoin('tax_option', 'product_option.option_id = tax_option.id')
                                              ->innerJoin(
                                                  'tax_option_lang',
                                                  'tax_option_lang.tax_option_id = tax_option.id'
                                              )
                                              ->where(
                                                  [ 'tax_option_lang.alias' => $params ]
                                              )
                                              ->union($variant_query);
2f25da09   Yarik   first commit
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
              $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
316
  
68f06691   Administrator   add variantSku
317
                      if(iconv_strlen($param) >= 3){
ac8d2b24   Administrator   add variantSku
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
                          $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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
                  }
              }
              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);
          }
          
      }