Blame view

helpers/FilterHelper.php 14.1 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
          /**
2f5c8d11   Administrator   add variantSku
115
116
117
118
119
           * select options for product variants with selected category
           *
           * @param integer $categoryId
           * @param integer $langId
           * @return mixed
cb55aee8   Administrator   add variantSku
120
           */
2f5c8d11   Administrator   add variantSku
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
          public static function getProductVariantOptions($categoryId,$langId){
              $result = \Yii::$app->db->cache(function () use($categoryId,$langId) {
                  return ( new Query() )->distinct()->select('tax_group_lang.alias')
                  ->from('product_variant_option')
                  ->innerJoin(
                      'tax_option',
                      'product_variant_option.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'
                  )
                  ->innerJoin(
                      'tax_group_to_category',
                      'tax_group_to_category.tax_group_id = tax_group.id'
                  )
                  ->where([
                      'tax_group_lang.language_id' => $langId,
                      'tax_group_to_category.category_id' => $categoryId,
                      'tax_group.is_filter' => true
                  ])->all();
              },60*60*24);
              return ArrayHelper::getColumn($result,'alias');
cb55aee8   Administrator   add variantSku
148
149
150
          }
  
  
2f25da09   Yarik   first commit
151
152
153
154
155
          /**
           * Fill query with filter conditions
           *
           * @param ActiveQuery $query
           * @param array       $params
2f5c8d11   Administrator   add variantSku
156
157
           * @param integer       $categoryId
           * @param integer       $langId
2f25da09   Yarik   first commit
158
           */
2f5c8d11   Administrator   add variantSku
159
          public static function setQueryParams(ActiveQuery $query, array $params, $categoryId, $langId)
2f25da09   Yarik   first commit
160
161
          {
              $last_query = null;
2f5c8d11   Administrator   add variantSku
162
              $productVariantOptions = self::getProductVariantOptions($categoryId,$langId);
2f25da09   Yarik   first commit
163
164
165
              foreach ($params as $key => $param) {
                  switch ($key) {
                      case 'special':
2f5c8d11   Administrator   add variantSku
166
                          unset($params[$key]);
2f25da09   Yarik   first commit
167
168
169
                          self::filterSpecial($param, $query);
                          break;
                      case 'brands':
2f5c8d11   Administrator   add variantSku
170
                          unset($params[$key]);
2f25da09   Yarik   first commit
171
172
173
                          self::filterBrands($param, $query);
                          break;
                      case 'keywords':
2f5c8d11   Administrator   add variantSku
174
                          unset($params[$key]);
2f25da09   Yarik   first commit
175
176
177
                          self::filterKeywords($param, $query);
                          break;
                      case 'prices':
2f5c8d11   Administrator   add variantSku
178
                          unset($params[$key]);
2f25da09   Yarik   first commit
179
180
                          self::filterPrices($param, $query);
                          break;
2f5c8d11   Administrator   add variantSku
181
  
2f25da09   Yarik   first commit
182
183
                  }
              }
2f5c8d11   Administrator   add variantSku
184
              self::filterOptions($params, $productVariantOptions, $query);
2f25da09   Yarik   first commit
185
          }
ad348101   Administrator   add variantSku
186
187
  
  
2f5c8d11   Administrator   add variantSku
188
  
2f25da09   Yarik   first commit
189
190
191
192
          /**
           * Tax Option filter
           *
           * @param string[]           $params
2f5c8d11   Administrator   add variantSku
193
194
           * @param string[]           $productVariantOptions
           * @param \yii\db\Query|null $query
879394e3   Administrator   add variantSku
195
           * @param bool               $in_stock
2f25da09   Yarik   first commit
196
           *
879394e3   Administrator   add variantSku
197
           * @return \yii\db\Query
2f25da09   Yarik   first commit
198
           */
2f5c8d11   Administrator   add variantSku
199
          private static function filterOptions(array $params,$productVariantOptions, Query &$query = null, bool $in_stock = true)
2f25da09   Yarik   first commit
200
          {
2f25da09   Yarik   first commit
201
              $variant_query = ( new Query() )->distinct()
2f5c8d11   Administrator   add variantSku
202
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
229
230
231
232
233
                  ->select('product_variant.product_id as products')
                  ->from('product_variant');
  
              foreach ($params as $key=>$param){
                  if(in_array($key, $productVariantOptions)){
                      unset($params[$key]);
                      $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.id'=>$product_variant_id]);
                  }
  
              }
  
  
879394e3   Administrator   add variantSku
234
235
236
              if($in_stock) {
                  $variant_query->andWhere(['!=', 'product_variant.stock', 0]);
              }
2f25da09   Yarik   first commit
237
              $product_query = ( new Query() )->distinct()
2f5c8d11   Administrator   add variantSku
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
                  ->select('product_option.product_id as products')
                  ->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'
                  );
              foreach ($params as $key=>$param){
                  $product_query
                      ->where([ 'tax_group_lang.alias' => $key ])
                      ->andWhere([ 'tax_option_lang.alias' => $param ]);
  
2f25da09   Yarik   first commit
261
              }
2f5c8d11   Administrator   add variantSku
262
263
264
265
  
              $query->andWhere([ 'product.id' => $product_query ]);
              $query->andWhere([ 'product.id' => $variant_query ]);
  
2f25da09   Yarik   first commit
266
          }
2f5c8d11   Administrator   add variantSku
267
268
  
  
2f25da09   Yarik   first commit
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
316
317
318
319
320
321
322
323
324
325
          
          /**
           * 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
326
  
68f06691   Administrator   add variantSku
327
                      if(iconv_strlen($param) >= 3){
ac8d2b24   Administrator   add variantSku
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
                          $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
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
393
394
395
396
397
398
399
400
401
402
                  }
              }
              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);
          }
          
      }