Blame view

common/modules/product/models/Export.php 3.2 KB
4253cbec   root   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  <?php
  
  namespace common\modules\product\models;
  
  use common\modules\product\helpers\ProductHelper;
  use yii\base\Model;
  use yii\helpers\ArrayHelper;
  
  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';
          }
          setlocale(LC_ALL, 'ru_RU.CP1251');
          $handle = fopen($dirName .'/'. $filename, "w");
          ///$products = Product::find()->joinWith(['variants'])->where(['!=', ProductVariant::tableName() .'.stock', 0])->select('product.product_id')->all();
          $products = Product::find()
aefe93aa   Administrator   big commti
21
              ->with(['variantsWithFilters','brand','categories'])->all();
4253cbec   root   first commit
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
  
          $i = 0;
          foreach ($products as $product)
          {
  
  
              $i++;
              /*if ($i>1e2) {
                  break;
              }*/
              $mods = [];
  
              $filterString = $this->convertFilterToString($product->getFilters());
  
              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 = [];
  
  //            foreach ($product->images as $image)
  //            {
  //                $fotos[] = $image->imageUrl;
  //            }
  
  //            $filters = $product->properties;
              $categories = [];
aefe93aa   Administrator   big commti
58
59
60
              foreach($product->categories as $value){
  
                  $categories[] = $value->name;
4253cbec   root   first commit
61
62
63
64
65
66
67
  
              }
  
  
              $categories = implode(',',$categories);
  
              $list = [
aefe93aa   Administrator   big commti
68
  
4253cbec   root   first commit
69
70
71
72
73
74
75
76
77
78
                  $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),
                  '',
aefe93aa   Administrator   big commti
79
80
                  intval($product->is_new),
                  intval($product->is_top),
4253cbec   root   first commit
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
111
112
113
114
115
116
                  $product->video,
                  implode (',', $fotos),
              ];
  
              $to_write = array_merge ($list, $mods);
              foreach($to_write as &$cell) {
                  $cell = iconv("UTF-8", "WINDOWS-1251", $cell);
              }
  
  
              fputcsv($handle, $to_write, ';');
          }
  
  
  
          fclose ($handle);
  
          return $dirName .'/'. $filename;
      }
  
  
      public function convertFilterToString($filters){
          $fittersArray = [];
          foreach($filters as $filter){
              $fittersArray[$filter->taxGroup->alias][] =  $filter->name;
          }
          $filterString=[];
  
          foreach($fittersArray as $filterName =>$filterRows ){
              $row = implode(',',$filterRows);
              $filterString[] = "[{$filterName}:{$row}]";
  
          }
          return implode('*',$filterString);
      }
  }