Export.php
5.31 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
namespace common\modules\product\models;
use common\modules\language\models\Language;
use common\modules\rubrication\models\TaxOption;
use yii\base\Model;
class Export extends Model
{
public $lang;
public $file;
public $errors = [];
public $output = [];
public function rules()
{
return [
[
'lang',
'integer',
],
[
'lang',
'default',
'value' => Language::getCurrent()->language_id,
],
];
}
public function process($filename = NULL, $from = 0)
{
$limit = 100;
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");
}
$language = Language::findOne(\Yii::$app->session->get('export_lang', Language::getDefaultLanguage()->language_id));
Language::setCurrent($language->url);
/**
* @var Product[] $products
*/
$products = Product::find()
->with('variantsWithFilters', 'brand.lang', 'categories.lang', 'filters')
->joinWith('lang', true, 'INNER JOIN')
->limit($limit)
->offset($from)
->all();
$filesize = Product::find()
->joinWith('lang', true, 'INNER JOIN')
->count();
foreach($products as $product) {
$mods = [];
$filterString = $this->convertFilterToString($product->filters);
foreach($product->variantsWithFilters as $variant) {
/**
* @var ProductVariant $variant
*/
$color = $variant->lang->name;
$mods[] = $variant->sku.$this->generateID($variant->remote_id) . '=' . $this->convertFilterToString($variant->filters) . '=' . $color . '=' . ( ( !empty( $variant->image ) ) ? $variant->image->image : '' ) . '=' . $variant->stock;
}
$fotos = [];
$categories = [];
foreach($product->categories as $value) {
$categories[] = $value->lang->name.$this->generateID($value->remote_id);
}
$categories = implode(',', $categories);
$list = [
$categories,
( ( !empty( $product->brand ) ) ? $product->brand->lang->name.$this->generateID($product->brand->remote_id) : '' ),
$product->lang->name.$this->generateID($product->remote_id),
'',
( ( !empty( $product->lang->description ) ) ? $product->lang->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 );
}
fclose($handle);
$from += $limit;
$end = false;
if($from > $filesize) {
$end = true;
}
$result = [
'end' => $end,
'from' => $from,
'totalsize' => $filesize,
'filename' => $filename,
];
if($end) {
$result = array_merge($result, [
'link' => '/storage/sync/' . $filename,
]);
}
return $result;
}
public function convertFilterToString($filters)
{
$fittersArray = [];
/**
* @var TaxOption[] $filters
*/
foreach($filters as $filter) {
$fittersArray[ $filter->taxGroup->lang->name.$this->generateID($filter->taxGroup->remote_id) ][] = $filter->lang->value.$this->generateID($filter->remote_id);
}
$filterString = [];
foreach($fittersArray as $filterName => $filterRows) {
$row = implode(',', $filterRows);
$filterString[] = "[{$filterName}:{$row}]";
}
return implode('*', $filterString);
}
private function generateID(string $id):string {
return sprintf('(#%s#)', $id);
}
}