Blame view

common/modules/product/models/Export.php 3.29 KB
c7852657   Karnovsky A   -
1
2
3
4
5
6
  <?php
  
  namespace common\modules\product\models;
  
  use common\modules\product\helpers\ProductHelper;
  use yii\base\Model;
96f5a822   Administrator   29.06.16
7
  use yii\helpers\ArrayHelper;
c7852657   Karnovsky A   -
8
9
10
11
12
13
14
15
16
17
18
  
  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");
96f5a822   Administrator   29.06.16
19
20
21
22
          ///$products = Product::find()->joinWith(['variants'])->where(['!=', ProductVariant::tableName() .'.stock', 0])->select('product.product_id')->all();
          $products = Product::find()
              ->with(['variantsWithFilters','brand','categoriesWithName'])->all();
  
c7852657   Karnovsky A   -
23
          $i = 0;
96f5a822   Administrator   29.06.16
24
          foreach ($products as $product)
c7852657   Karnovsky A   -
25
          {
96f5a822   Administrator   29.06.16
26
27
  
  
c7852657   Karnovsky A   -
28
29
30
31
32
33
              $i++;
              /*if ($i>1e2) {
                  break;
              }*/
              $mods = [];
  
96f5a822   Administrator   29.06.16
34
35
36
              $filterString = $this->convertFilterToString($product->getFilters());
  
              foreach ($product->variantsWithFilters as $variant)
c7852657   Karnovsky A   -
37
              {
96f5a822   Administrator   29.06.16
38
39
40
41
42
43
44
45
  
                  $color = $variant->name;
  
                  $mods[] = $variant->sku .
                      '=' . $this->convertFilterToString($variant->filters) .
                      '=' . $color .
                      '=' . ((!empty($variant->image)) ? $variant->image->image: '').
                      '=' . $variant->stock;
c7852657   Karnovsky A   -
46
47
48
49
              }
  
              $fotos = [];
  
96f5a822   Administrator   29.06.16
50
51
52
53
54
55
56
57
58
59
60
  //            foreach ($product->images as $image)
  //            {
  //                $fotos[] = $image->imageUrl;
  //            }
  
  //            $filters = $product->properties;
              $categories = [];
              foreach($product->categoriesWithName as $value){
                  $categorName = ArrayHelper::getColumn($value->categoryNames,'value');
                  $categories[] = $categorName[0];
  
c7852657   Karnovsky A   -
61
62
              }
  
96f5a822   Administrator   29.06.16
63
64
  
              $categories = implode(',',$categories);
c7852657   Karnovsky A   -
65
66
  
              $list = [
96f5a822   Administrator   29.06.16
67
                  $categories,
c7852657   Karnovsky A   -
68
69
70
71
                  $product->brand->name,
                  $product->name,
                  '',
                  ((! empty($product->description)) ? $product->description : ''),
96f5a822   Administrator   29.06.16
72
73
74
                  $filterString,
                  (!empty($product->variant)) ? $product->variant->price_old : '',
                  (!empty($product->variant)) ? $product->variant->price : '',
c7852657   Karnovsky A   -
75
76
77
78
                  intval($product->akciya),
                  '',
                  intval($product->new),
                  intval($product->top),
c7852657   Karnovsky A   -
79
80
81
82
83
84
85
86
87
                  $product->video,
                  implode (',', $fotos),
              ];
  
              $to_write = array_merge ($list, $mods);
              foreach($to_write as &$cell) {
                  $cell = iconv("UTF-8", "WINDOWS-1251", $cell);
              }
  
96f5a822   Administrator   29.06.16
88
  
c7852657   Karnovsky A   -
89
90
91
              fputcsv($handle, $to_write, ';');
          }
  
96f5a822   Administrator   29.06.16
92
93
  
  
c7852657   Karnovsky A   -
94
95
96
97
          fclose ($handle);
  
          return $dirName .'/'. $filename;
      }
96f5a822   Administrator   29.06.16
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
  
  
      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);
      }
c7852657   Karnovsky A   -
114
  }