Blame view

common/modules/product/models/Export.php 4.46 KB
4253cbec   root   first commit
1
  <?php
7253a9a0   Yarik   Export fix
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
      
      namespace common\modules\product\models;
      
      use yii\base\Model;
      
      class Export extends Model
      {
          
          public $lang;
          
          public $file;
          
          public $errors = [];
          
          public $output = [];
          
          public function process($filename = NULL, $from = 0)
4253cbec   root   first commit
19
          {
7253a9a0   Yarik   Export fix
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
              if(empty( $filename )) {
                  $filename = 'products_' . date('d_m_Y_H_i') . '.csv';
                  $handle = fopen(\Yii::getAlias('@storage/sync/') . $filename, "w");
              } else {
                  $handle = fopen(\Yii::getAlias('@storage/sync/') . $filename, "a");
              }
              
              
              $products = Product::find()
                                 ->joinWith([
                                     'variantsWithFilters',
                                     'brand',
                                     'categories',
                                 ])
                                 ->with('filters')
                                 ->limit(1000)
                                 ->offset($from)
                                 ->all();
              $filesize = Product::find()
                                 ->count();
              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,
                                      $product->brand->name,
                                      $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);
                  fputcsv($handle, $to_write, ';');
                  unset( $product );
4253cbec   root   first commit
78
              }
7253a9a0   Yarik   Export fix
79
80
81
82
83
84
85
      
              fclose($handle);
      
              $from += 1000;
              $end = false;
              if($from > $filesize) {
                  $end = true;
4253cbec   root   first commit
86
              }
7253a9a0   Yarik   Export fix
87
88
89
90
91
92
              
              $result = [
                  'end'       => $end,
                  'from'      => $from,
                  'totalsize' => $filesize,
                  'filename'  => $filename,
4253cbec   root   first commit
93
              ];
7253a9a0   Yarik   Export fix
94
95
96
97
98
              
              if($end) {
                  $result = array_merge($result, [
                      'link' => '/storage/sync/'.$filename,
                  ]);
73028f91   Administrator   big commti
99
              }
7253a9a0   Yarik   Export fix
100
101
102
              
              return $result;
              
4253cbec   root   first commit
103
          }
7253a9a0   Yarik   Export fix
104
105
106
107
108
109
110
111
112
113
114
115
116
117
          
          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);
4253cbec   root   first commit
118
          }
7253a9a0   Yarik   Export fix
119
      }