Export.php
3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
78
79
80
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
117
118
119
120
<?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) {
        set_time_limit(0);
        ini_set('max_execution_time', 900);
        ini_set('memory_limit', '1024M');
        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()
            ->joinWith(['variantsWithFilters','brand','categories'])->with('filters')->all();
        foreach ($products as $product)
        {
            /*if ($i>1e2) {
                break;
            }*/
            $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 = [];
//            foreach ($product->images as $image)
//            {
//                $fotos[] = $image->imageUrl;
//            }
//            $filters = $product->properties;
            $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);
            foreach($to_write as &$cell) {
                $cell = iconv("UTF-8", "WINDOWS-1251", $cell);
            }
            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->name;
        }
        $filterString=[];
        foreach($fittersArray as $filterName =>$filterRows ){
            $row = implode(',',$filterRows);
            $filterString[] = "[{$filterName}:{$row}]";
        }
        return implode('*',$filterString);
    }
}