Blame view

common/modules/product/models/Import.php 19.8 KB
8724ec1f   Karnovsky A   -
1
2
3
4
  <?php
  
  namespace common\modules\product\models;
  
c7852657   Karnovsky A   -
5
  use common\modules\product\helpers\ProductHelper;
8724ec1f   Karnovsky A   -
6
7
8
9
  use common\modules\product\models\Category;
  use common\modules\product\models\CategoryName;
  use common\modules\product\models\ProductImage;
  use common\modules\product\models\ProductVariantType;
1c02ab59   Administrator   29.06.16
10
  use common\modules\rubrication\models\TaxGroup;
8724ec1f   Karnovsky A   -
11
12
13
14
15
16
17
18
19
  use common\modules\rubrication\models\TaxOption;
  use common\modules\rubrication\models\TaxValueString;
  use Yii;
  use common\modules\product\models\Brand;
  use common\modules\product\models\BrandName;
  use common\modules\product\models\Product;
  use common\modules\product\models\ProductVariant;
  use common\modules\product\models\RemoteProducts;
  use yii\base\Model;
9495311a   Administrator   14.06.16
20
  use yii\helpers\ArrayHelper;
8724ec1f   Karnovsky A   -
21
22
23
  
  class Import extends Model {
      public $file;
5ee9ab1f   Karnovsky A   -
24
      public $type;
8724ec1f   Karnovsky A   -
25
26
27
28
29
30
31
32
33
34
  
      public $errors = [];
      public $output = [];
  
      /**
       * @inheritdoc
       */
      public function rules()
      {
          return [
5ee9ab1f   Karnovsky A   -
35
36
              [['type'], 'required'],
              [['type'], 'string'],
10e0f427   Karnovsky A   -
37
  //            [['file'], 'safe'],
5ee9ab1f   Karnovsky A   -
38
              [['file'], 'file', 'extensions' => 'csv'],
8724ec1f   Karnovsky A   -
39
40
41
42
43
44
45
46
47
48
49
50
51
          ];
      }
  
      /**
       * @inheritdoc
       */
      public function attributeLabels()
      {
          return [
              'file' => Yii::t('product', 'File'),
          ];
      }
  
5ee9ab1f   Karnovsky A   -
52
53
54
55
56
57
      public function getType() {
          if (!$this->type) {
              $this->type = 'products';
          }
          return $this->type;
      }
8724ec1f   Karnovsky A   -
58
  
37c2918b   Karnovsky A   -
59
60
61
62
63
64
65
66
67
      public function goPrices($from = 0, $limit = null) {
          set_time_limit(0);
          $new_products = $linked_products = 0;
  
          if ( !($handle = $this->getProductsFile('uploadFilePrices')) ) {
              $this->errors[] = 'File not found';
              return FALSE;
          }
  
f7f95504   Karnovsky A   -
68
          $filesize = filesize(Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@uploadFilePrices'));
37c2918b   Karnovsky A   -
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
          if ($from) {
              fseek($handle, $from);
          }
  
          $j = 0;
  
          $is_utf = (preg_match('//u', file_get_contents(Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@uploadFilePrices'), null, null, null, 1000000)));
  
          while (($data = fgetcsv ($handle, 10000, ";")) !== FALSE && (empty($limit) || $j++ < $limit))
          {
              foreach ($data as &$value)
              {
                  if (!$is_utf) {
                      $value = iconv ('windows-1251', "UTF-8//TRANSLIT//IGNORE", $value);
                  }
                  $value = trim ($value);
              }
  
              // данные строк
              $modification_code = @$data[0];
              $price             = floatval(@$data[1]);
              $price_promo       = floatval(@$data[2]);
              $count             = intval(@$data[3]);
              $city_name         = @$data[4];
              $product_title     = @$data[5];
  
              if (empty ($modification_code)) {
49c47c76   Karnovsky A   -
96
                  continue;
37c2918b   Karnovsky A   -
97
98
99
100
              }
              // товары в пути
              if (empty ($city_name))
              {
f7f95504   Karnovsky A   -
101
102
103
104
  //                $this->saveNotFoundRecord (
  //                    [$modification_code, $product_title],
  //                    Yii::getAlias('@uploadFilePricesAway')
  //                );
37c2918b   Karnovsky A   -
105
106
107
  
                  $this->output[] = 'Товар '. $product_title . ' в пути';
  
49c47c76   Karnovsky A   -
108
                  continue;
37c2918b   Karnovsky A   -
109
110
              }
  
f7f95504   Karnovsky A   -
111
              if ( ($productVariant = ProductVariant::find()->filterWhere(['sku' => $modification_code])->one()) === null ) {
37c2918b   Karnovsky A   -
112
                  // 'Нет даной модификации в базе';
f7f95504   Karnovsky A   -
113
114
115
116
  //                $this->saveNotFoundRecord (
  //                    [$modification_code, $product_title],
  //                    Yii::getAlias('@uploadFilePricesNoVariant')
  //                );
37c2918b   Karnovsky A   -
117
118
119
  
                  $this->output[] = 'Для товара '. $product_title . ' не найдено соотвествие';
  
49c47c76   Karnovsky A   -
120
                  continue;
37c2918b   Karnovsky A   -
121
122
              }
  
f7f95504   Karnovsky A   -
123
  
9495311a   Administrator   14.06.16
124
  
37c2918b   Karnovsky A   -
125
126
127
  
              // ===== Set stock ====
              if ( $city_name ) {
19a7a33f   Administrator   14.06.16
128
                  if ( ($stock = Stock::find()->filterWhere(['name' => trim($city_name)])->one()) === null ) {
86301a92   Administrator   14.06.16
129
  
37c2918b   Karnovsky A   -
130
131
132
133
134
135
                      // Create stock
                      $stock = new Stock();
                      $stock->name = trim($city_name);
                      $stock->save();
                  }
  
02a42412   Administrator   14.06.16
136
137
138
139
140
141
142
                  $productStock = ProductStock::find()->where(['product_variant_id' => $productVariant->product_variant_id, 'stock_id' => $stock->stock_id])->one();
                  if(!$productStock instanceof ProductStock) {
                      $productStock = new ProductStock;
                      $productStock->product_variant_id = $productVariant->product_variant_id;
                      $productStock->stock_id = $stock->stock_id;
                      $productStock->product_id = $productVariant->product_id;
                  }
9495311a   Administrator   14.06.16
143
                  $productStock->quantity = $count;
9495311a   Administrator   14.06.16
144
  
86301a92   Administrator   14.06.16
145
                  $productStock->save();
02a42412   Administrator   14.06.16
146
                  $productStocks = ProductStock::find()->where(['product_variant_id' => $productVariant->product_variant_id])->andWhere(['<>', 'stock_id', $stock->stock_id])->all();
9495311a   Administrator   14.06.16
147
148
149
150
  
                  $quantity = array_sum(ArrayHelper::getColumn($productStocks, 'quantity')) + $count;
              } else {
  
02a42412   Administrator   14.06.16
151
                  $productStocks = ProductStock::find()->where(['product_variant_id' => $productVariant->product_variant_id])->all();
9495311a   Administrator   14.06.16
152
153
154
155
156
157
158
  
                  if($productStocks instanceof  ProductStock){
                      $quantity = array_sum(ArrayHelper::getColumn($productStocks, 'quantity')) + $count;
                  } else {
                      $quantity = 0;
                  }
  
37c2918b   Karnovsky A   -
159
160
              }
  
49c47c76   Karnovsky A   -
161
162
163
164
165
166
167
168
              if ($price_promo) {
                  $productVariant->price_old = $price;
                  $productVariant->price = $price_promo;
              } else {
                  $productVariant->price = $price;
                  $productVariant->price_old = $price_promo;
              }
  
37c2918b   Karnovsky A   -
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
              $productVariant->stock = $quantity;
  
              $productVariant->save();
  
              $this->output[] = '<font style="color:blue">Товар '. $product_title .' успешно сохранен</font>';
          }
  
          $result = [
              'end' => feof($handle),
              'from' => ftell($handle),
              'totalsize' => $filesize,
              'items' => $this->output,
  
          ];
  
          fclose ($handle);
  
          if ($result['end']) {
              unlink(Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@uploadFilePrices'));
37c2918b   Karnovsky A   -
188
189
190
191
192
193
          }
  
          return $result;
      }
  
      public function goProducts($from = 0, $limit = null) {
1c02ab59   Administrator   29.06.16
194
  
5ee9ab1f   Karnovsky A   -
195
          set_time_limit(0);
8724ec1f   Karnovsky A   -
196
197
          $new_products = $linked_products = 0;
  
5ee9ab1f   Karnovsky A   -
198
          if ( !($handle = $this->getProductsFile('uploadFileProducts')) ) {
8724ec1f   Karnovsky A   -
199
200
201
202
              $this->errors[] = 'File not found';
              return FALSE;
          }
  
37c2918b   Karnovsky A   -
203
204
          $filesize = filesize(Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@uploadFileProducts'));
  
1c02ab59   Administrator   29.06.16
205
  
37c2918b   Karnovsky A   -
206
207
208
209
          if ($from) {
              fseek($handle, $from);
          }
  
8724ec1f   Karnovsky A   -
210
211
          $j = 0;
  
c7852657   Karnovsky A   -
212
213
          $is_utf = (preg_match('//u', file_get_contents(Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@uploadFileProducts'), null, null, null, 1000000)));
  
37c2918b   Karnovsky A   -
214
          $result_items = [];
8724ec1f   Karnovsky A   -
215
  
37c2918b   Karnovsky A   -
216
217
          while (($data = fgetcsv ($handle, 10000, ";")) !== FALSE && (empty($limit) || $j++ < $limit))
          {
1c02ab59   Administrator   29.06.16
218
219
  
  
8724ec1f   Karnovsky A   -
220
221
              foreach ($data as &$value)
              {
c7852657   Karnovsky A   -
222
223
224
                  if (!$is_utf) {
                      $value = iconv ('windows-1251', "UTF-8//TRANSLIT//IGNORE", $value);
                  }
8724ec1f   Karnovsky A   -
225
226
227
228
229
230
231
232
233
234
235
236
                  $value = trim ($value);
              }
  
              // будет всегда 19 элементов
              for ($i = 0; $i <= 18; $i++)
              {
                  if (! isset ($data[$i]))
                  {
                      $data[$i] = null;
                  }
              }
  
1c02ab59   Administrator   29.06.16
237
  
8724ec1f   Karnovsky A   -
238
              // 1  Группа (категория)
1c02ab59   Administrator   29.06.16
239
240
              $catalog_names = explode(',',$data[0]);
              if (empty ($catalog_names))
8724ec1f   Karnovsky A   -
241
              {
37c2918b   Karnovsky A   -
242
                  $result_items[] = "Не указана категория (строка $j)";
49c47c76   Karnovsky A   -
243
                  continue;
8724ec1f   Karnovsky A   -
244
245
246
247
248
249
              }
  
              // 2  Бренд
              $brand_name = $data[1];
              if (empty ($brand_name))
              {
37c2918b   Karnovsky A   -
250
                  $result_items[] = "Не указан бренд (строка $j)";
49c47c76   Karnovsky A   -
251
                  continue;
8724ec1f   Karnovsky A   -
252
253
254
255
256
257
              }
  
              // 3  Название товара
              $product_name = $data[2];
              if (empty ($product_name))
              {
37c2918b   Karnovsky A   -
258
                  $result_items[] = "Не указано наименование товара (строка $j)";
49c47c76   Karnovsky A   -
259
                  continue;
8724ec1f   Karnovsky A   -
260
261
262
263
264
265
266
267
              }
  
              // 4  Описание Укр
              $product_body_uk = $data[3];
  
              // 5  Описание Рус
              $product_body_ru = $data[4];
  
1c02ab59   Administrator   29.06.16
268
269
              // 6  Фильтр (['pol'='мужской']*['god' = '2013']*['volume'='25 л']*['size'='49 x 30 x 20см']*['composition'='600D полиэстер'])
              $filters = explode ('*', $data[5]);
8724ec1f   Karnovsky A   -
270
  
49c47c76   Karnovsky A   -
271
              // 11 Цена акция
1c02ab59   Administrator   29.06.16
272
              $product_cost_old = floatval($data[7]);
8724ec1f   Karnovsky A   -
273
  
49c47c76   Karnovsky A   -
274
275
              // 10 Цена
              if ($product_cost_old) {
1c02ab59   Administrator   29.06.16
276
277
                  $product_cost_old = floatval($data[6]);
                  $product_cost = floatval($data[7]);
49c47c76   Karnovsky A   -
278
              }
8724ec1f   Karnovsky A   -
279
280
  
              // 12 Акция
1c02ab59   Administrator   29.06.16
281
              $product_akciya = (bool)$data[8];
8724ec1f   Karnovsky A   -
282
283
  
              // 13 Сопуд. Тов.
1c02ab59   Administrator   29.06.16
284
              $similar = explode (',', $data[9]);
8724ec1f   Karnovsky A   -
285
286
  
              // 14 Новинки
1c02ab59   Administrator   29.06.16
287
              $product_new = (bool)$data[10];
8724ec1f   Karnovsky A   -
288
289
  
              // 15 Топ продаж
1c02ab59   Administrator   29.06.16
290
              $product_top = (bool)$data[11];
8724ec1f   Karnovsky A   -
291
  
8724ec1f   Karnovsky A   -
292
293
  
              // 17 ВИДЕО КОД
1c02ab59   Administrator   29.06.16
294
              $product_video = $data[12];
8724ec1f   Karnovsky A   -
295
296
  
              // 18 Галлерея фото
1c02ab59   Administrator   29.06.16
297
298
              if (trim($data[13])) {
                  $fotos = explode (',', trim($data[13]));
37c2918b   Karnovsky A   -
299
              }
8724ec1f   Karnovsky A   -
300
301
302
303
  
              // 19 Штрих код товара.
              // расшифровал - это модификации товара!
  
1c02ab59   Administrator   29.06.16
304
              $product_image = explode ('=', $data[14]);
8724ec1f   Karnovsky A   -
305
306
307
308
309
310
311
312
              $product_image = @$product_image[3];
  
              if ( ($_product = Product::find()->filterWhere(['ilike', 'name', trim($product_name)])->one()) === null ) {
                  $_product = new Product();
              }
  
              $is_new_product = empty($_product->product_id);
  
1c02ab59   Administrator   29.06.16
313
314
315
316
317
318
319
320
321
322
              foreach($catalog_names as $catalog_name){
                  // ==== Set category ====
                  if ( ($category = CategoryName::find()->filterWhere(['ilike', 'value', trim($catalog_name)])->one()) === null ) {
                      // Create category
                      $category = new Category();
                      $category->name = trim($catalog_name);
                      $category->save();
                  }
  
                  $category_id[] = $category->category_id;
8724ec1f   Karnovsky A   -
323
              }
1c02ab59   Administrator   29.06.16
324
325
326
  
  
              $_product->categories = $category_id;
8724ec1f   Karnovsky A   -
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
  
              // ===== Set brand ====
              if ( $brand_name ) {
                  if ( ($brand = BrandName::find()->filterWhere(['ilike', 'value', trim($brand_name)])->one()) !== null ) {
                      $_product->brand_id = $brand->brand_id;
                  } else {
                      // Create brand
                      $brand = new Brand();
                      $brand->name = trim($brand_name);
                      $brand->save();
                      $_product->brand_id = $brand->brand_id;
                  }
              }
  
              $_product->name = $product_name;
              $_product->video = $product_video;
              $_product->description = $product_body_ru;
              $_product->is_top = $product_top;
              $_product->akciya = $product_akciya;
              $_product->is_new = $product_new;
  
              if (!$_product->save()) {
37c2918b   Karnovsky A   -
349
                  $result_items[] = 'Product #'. $_product->name .' not saved' . " (строка $j)";
8724ec1f   Karnovsky A   -
350
351
352
                  continue;
              }
  
1c02ab59   Administrator   29.06.16
353
354
  
  
37c2918b   Karnovsky A   -
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
              if (!empty($fotos)) {
                  foreach($fotos as $foto) {
                      $source_image = Yii::getAlias('@uploadDir') . '/product_images/'. urlencode($foto);
  
                      if (file_exists($source_image)) {
                          if (($productImage = ProductImage::find()->andFilterWhere(['ilike', 'image', $foto])->andFilterWhere(['product_id' => $_product->product_id])->one()) === null) {
                              copy($source_image, Yii::getAlias('@productsDir') . "/" . $foto);
                              $productImage = new ProductImage();
                              $productImage->product_id = $_product->product_id;
                              $productImage->image = $foto;
                              $productImage->save();
                          }
                      }
                  }
              }
  
1c02ab59   Administrator   29.06.16
371
  
8724ec1f   Karnovsky A   -
372
373
374
              // нужно для проставления характеристик относящихся к модификациям
              $MOD_ARRAY = [];
  
1c02ab59   Administrator   29.06.16
375
              for ($i = 14; $i < count ($data); $i ++)
8724ec1f   Karnovsky A   -
376
377
378
379
380
              {
                  if (! empty ($data[$i]))
                  {
                      $mod_arr    = explode ('=', $data[$i]);
                      $mod_art    = $mod_arr[0];
1c02ab59   Administrator   29.06.16
381
                      $variant_filters   = explode ('*', $mod_arr[1]);
8724ec1f   Karnovsky A   -
382
383
                      $mod_color  = $mod_arr[2];
                      $mod_image  = $mod_arr[3];
1c02ab59   Administrator   29.06.16
384
                      $mod_stock  = isset($mod_arr[4]) ?$mod_arr[4]:1;
8724ec1f   Karnovsky A   -
385
386
387
388
389
390
391
392
393
394
395
396
397
                      $mod_cost   = floatval($product_cost);
                      $mod_old_cost = floatval($product_cost_old);
  
                      // Check product variant
                      if ( ($_productVariant = ProductVariant::find()->andFilterWhere(['ilike', 'sku', $mod_art])->andFilterWhere(['product_id' => $_product->product_id])->one()) === null ) {
                          $_productVariant = new ProductVariant();
                          $_productVariant->product_id = $_product->product_id;
                      }
                      $_productVariant->product_unit_id = 1;
  
                      $_productVariant->sku = $mod_art;
                      $_productVariant->price = $mod_cost;
                      $_productVariant->price_old = $mod_old_cost;
1c02ab59   Administrator   29.06.16
398
                      $_productVariant->stock = $mod_stock;
8724ec1f   Karnovsky A   -
399
400
401
402
403
404
  
                      $product_variant_type_name = '';
                      if (! empty ($mod_color)) {
                          $product_variant_type_name = 'Цвет';
                          $_productVariant->name = $mod_color;
                      }
1c02ab59   Administrator   29.06.16
405
406
407
408
409
410
411
412
413
414
  
                      if (! empty ($variant_filters)) {
  
                          $variants_options = $this->saveFilters($variant_filters,1,$category_id);
  
                      }
  
  
                      if (isset($variants_options) && !empty($variants_options)) {
                          $_productVariant->options = $variants_options;
8724ec1f   Karnovsky A   -
415
416
                      }
  
1c02ab59   Administrator   29.06.16
417
  
8724ec1f   Karnovsky A   -
418
419
420
421
422
423
424
425
426
427
428
                      // ===== Set variant type ====
                      if ( $product_variant_type_name ) {
                          if ( ($product_variant_type = ProductVariantType::find()->filterWhere(['ilike', 'name', $product_variant_type_name])->one()) !== null ) {
                              $_productVariant->product_variant_type_id = $product_variant_type->product_variant_type_id;
                          } else {
                              $product_variant_type = new ProductVariantType();
                              $product_variant_type->name = $product_variant_type_name;
                              $product_variant_type->save();
                              $_productVariant->product_variant_type_id = $product_variant_type->product_variant_type_id;
                          }
                      }
1c02ab59   Administrator   29.06.16
429
  
8724ec1f   Karnovsky A   -
430
431
432
433
434
                      $_productVariant->save(false);
  
                      $MOD_ARRAY[] = $_productVariant->product_variant_id;
  
                      if ($mod_image) {
37c2918b   Karnovsky A   -
435
436
                          $source_image = Yii::getAlias('@uploadDir') . '/product_images/'. urlencode($mod_image);
                          if (file_exists($source_image)) {
8724ec1f   Karnovsky A   -
437
                              if (($variantImage = ProductImage::find()->andFilterWhere(['ilike', 'image', $mod_image])->andFilterWhere(['product_variant_id' => $_productVariant->product_variant_id])->one()) === null) {
37c2918b   Karnovsky A   -
438
                                  copy($source_image, Yii::getAlias('@productsDir') . "/" . $mod_image);
8724ec1f   Karnovsky A   -
439
440
441
442
443
444
445
446
447
448
449
                                  $variantImage = new ProductImage();
                                  $variantImage->product_id = $_product->product_id;
                                  $variantImage->product_variant_id = $_productVariant->product_variant_id;
                                  $variantImage->image = $mod_image;
                                  $variantImage->save();
                              }
                          }
                      }
                  }
              }
  
1c02ab59   Administrator   29.06.16
450
  
8724ec1f   Karnovsky A   -
451
452
  
              if (! empty ($filters)) {
8724ec1f   Karnovsky A   -
453
  
1c02ab59   Administrator   29.06.16
454
                  $options = $this->saveFilters($filters,0,$category_id);
8724ec1f   Karnovsky A   -
455
  
8724ec1f   Karnovsky A   -
456
457
              }
  
1c02ab59   Administrator   29.06.16
458
459
  
              if (isset($options) && !empty($options)) {
8724ec1f   Karnovsky A   -
460
461
462
463
464
                  $_product->options = $options;
              }
  
              $_product->save();
  
37c2918b   Karnovsky A   -
465
              $result_items[] = "Product {$_product->name} #{$_product->product_id} saved (". ($is_new_product ? 'new product' : 'exists product') .")" . " (строка $j)";
8724ec1f   Karnovsky A   -
466
467
          }
  
37c2918b   Karnovsky A   -
468
469
470
471
472
          $result = [
              'end' => feof($handle),
              'from' => ftell($handle),
              'totalsize' => $filesize,
              'items' => $result_items,
8724ec1f   Karnovsky A   -
473
  
37c2918b   Karnovsky A   -
474
          ];
8724ec1f   Karnovsky A   -
475
  
37c2918b   Karnovsky A   -
476
          fclose ($handle);
5ee9ab1f   Karnovsky A   -
477
  
87451675   Administrator   29.06.16
478
479
480
          if ($result['end']) {
              unlink(Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@uploadFileProducts'));
          }
5ee9ab1f   Karnovsky A   -
481
  
37c2918b   Karnovsky A   -
482
          return $result;
5ee9ab1f   Karnovsky A   -
483
484
485
486
      }
  
      private function getProductsFile($file_type) {
          $filename = Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@'. $file_type);
8724ec1f   Karnovsky A   -
487
          if (!is_file($filename)) {
5ee9ab1f   Karnovsky A   -
488
              $this->errors[] = "File $filename not found";
8724ec1f   Karnovsky A   -
489
490
491
492
              return FALSE;
          }
          return fopen ($filename, 'r');
      }
5ee9ab1f   Karnovsky A   -
493
494
495
496
497
498
499
500
501
502
  
      private function saveNotFoundRecord (array $line, $filename)
      {
          $str = implode (';', $line)."\n";
          $str = iconv ("UTF-8//TRANSLIT//IGNORE", "windows-1251", $str);
  
          $fg = fopen (Yii::getAlias('@uploadDir') .'/'. $filename, 'a+');
          fputs ($fg, $str);
          fclose ($fg);
      }
1c02ab59   Administrator   29.06.16
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
  
  
      /**
       * @param $filters array of filters like [['pol'='мужской'],['god' = '2013'],['volume'='25 л']*['size'='49 x 30 x 20см'],['composition'='600D полиэстер']]
       * @param $level 0 for products and 1 for product variant
       * @param $catalog_names array catalogs id
       * @return array
       */
      private function saveFilters($filters, $level,$catalog_names){
          $options = [];
          foreach($filters as $filter) {
  
              preg_match_all('/\[(.*):(.*)\]/',$filter,$filter);
  
              if (empty($filter[1][0])) {
                  continue;
              }
              $filter_name = trim($filter[1][0]);
  
              $taxGroup = TaxGroup::find()->where(['alias'=>$filter_name])->one();
              if(!$taxGroup instanceof TaxGroup){
                  $taxGroup = new TaxGroup();
                  $taxGroup->alias = $filter_name;
                  $taxGroup->level = $level;
                  $taxGroup->name = $filter_name;
                  $taxGroup->module = 'string';
                  $taxGroup->hierarchical = FALSE;
                  $taxGroup->group_to_category = $catalog_names;
                  $taxGroup->is_filter = FALSE;
                  $taxGroup->save();
              }
  
              $filters_options = explode(',',$filter[2][0]);
  
              foreach($filters_options as $filter_options){
                  $value = TaxValueString::find()->innerJoinWith('taxOption')->andFilterWhere(['ilike', 'value', $filter_options])->andFilterWhere(['tax_option.tax_group_id' => $taxGroup->tax_group_id])->one();
  
                  if (!$value instanceof TaxValueString) {
                      // Create option
                      $option = new TaxOption();
                      $option->tax_group_id = $taxGroup->tax_group_id;
96f5a822   Administrator   29.06.16
544
                      $option->name = $filter_options;
1c02ab59   Administrator   29.06.16
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
                      $option->save();
  
                      $value = new TaxValueString();
                      $value->tax_option_id = $option->tax_option_id;
                      $value->value = $filter_options;
                      $value->save();
  
                      $option->default_value = $value->tax_value_id;
                      $option->save();
                  }
                  $options[] = $value->tax_option_id;
  
              }
  
  
          }
  
          return $options;
      }
8724ec1f   Karnovsky A   -
564
  }