Blame view

common/modules/product/models/Export.php 3.24 KB
4ca21c3e   Alexey Boroda   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  <?php
      
      namespace common\modules\product\models;
      
      use yii\base\Model;
      
      class Export extends Model
      {
          
          public $errors = [];
          
          public $output = [];
          
          public function process($dirName, $filename = NULL, $use_not_enables = false)
          {
              if(is_null($filename)) {
                  $filename = 'products_' . date('d_m_Y_H_i') . '.csv';
              }
0a24a6ac   Alexey Boroda   -Comments breadcr...
19
  //            setlocale(LC_ALL, 'ru_RU.UTF-8');
4ca21c3e   Alexey Boroda   first commit
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
              $handle = fopen($dirName . '/' . $filename, "w");
              $products = Product::find()
                                 ->joinWith([
                                     'variantsWithFilters',
                                     'brand',
                                     'categories',
                                 ])
                                 ->with('filters')
                                 ->all();
              
              foreach($products as $product) {
                  $mods = [];
                  $filterString = $this->convertFilterToString($product->filters);
                  
                  foreach($product->variantsWithFilters as $variant) {
                      $color = $variant->name;
                      $mods[] = $variant->sku . '=' . $this->convertFilterToString($variant->filters) . '=' . $color . '=' . ( ( !empty( $variant->image ) ) ? $variant->image->image : '' ) . '=' . $variant->stock;
                  }
                  
                  $fotos = [];
                  
                  $categories = [];
                  foreach($product->categories as $value) {
                      $categories[] = $value->name;
                  }
                  
                  $categories = implode(',', $categories);
                  
                  $list = [
                      $categories,
0a24a6ac   Alexey Boroda   -Comments breadcr...
50
                      (!empty($product->brand->name) ? $product->brand->name : ''),
4ca21c3e   Alexey Boroda   first commit
51
52
53
54
55
56
57
58
59
60
61
62
63
64
                      $product->name,
                      '',
                      ( ( !empty( $product->description ) ) ? $product->description : '' ),
                      $filterString,
                      ( !empty( $product->variant ) ) ? $product->variant->price_old : '',
                      ( !empty( $product->variant ) ) ? $product->variant->price : '',
                      intval($product->akciya),
                      '',
                      intval($product->is_new),
                      intval($product->is_top),
                      $product->video,
                      implode(',', $fotos),
                  ];
                  $to_write = array_merge($list, $mods);
0a24a6ac   Alexey Boroda   -Comments breadcr...
65
                 
4ca21c3e   Alexey Boroda   first commit
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
                  fputcsv($handle, $to_write, ';');
                  unset( $product );
              }
              
              fclose($handle);
              
              return $dirName . '/' . $filename;
          }
          
          public function convertFilterToString($filters)
          {
              $fittersArray = [];
              foreach($filters as $filter) {
                  $fittersArray[ $filter->taxGroup->alias ][] = $filter->value;
              }
              $filterString = [];
              
              foreach($fittersArray as $filterName => $filterRows) {
                  $row = implode(',', $filterRows);
                  $filterString[] = "[{$filterName}:{$row}]";
              }
              return implode('*', $filterString);
          }
      }