ExportController.php
6.68 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
namespace artbox\catalog\controllers;
use artbox\catalog\models\Product;
use common\models\ExportLog;
use yii\db\ActiveQuery;
use yii\filters\AccessControl;
use yii\web\Controller;
use PHPExcel;
use PHPExcel_Writer_Excel2007;
/**
* Class ExportController is web wrapper for console ExportXmlController
*/
class ExportController extends Controller
{
/**
* @inheritdoc
*/
public function getViewPath()
{
return '@artbox/catalog/views/export';
}
public function actionTest()
{
$exportLog = ExportLog::findOne(1);
$products = Product::find()
->with(
[
'variant.lang',
'lang',
'brand.lang',
'category.lang',
'productOptionExcls' => function (ActiveQuery $query) {
$query->with(
[
'lang',
'group.lang',
]
);
},
]
);
$data = [];
$j = 0;
$exportLog->message = 'Staeted';
$exportLog->percent = '';
$exportLog->is_running = true;
foreach ($products->batch(1000) as $productsBatch) {
foreach ($productsBatch as $product) {
$j++;
if (empty($product->variant) || empty($product->category) || empty($product->brand)) {
continue;
}
$filters = [];
foreach ($product->productOptionExcls as $optionExcl) {
$filters[] = trim($optionExcl->group->lang->title, ':') . ':' . $optionExcl->lang->value;
}
$data[] = [
'A' => $product->category->lang->title,
//1
'B' => $product->brand->lang->title,
//2
'C' => $product->lang->title,
//3
'D' => $product->variant->sku,
//4
'E' => $product->lang->description,
//5
'F' => $product->variant->price,
//6
'G' => $product->variant->price_old,
//7
'H' => '',
//8
'I' => '',
//9
'J' => '',
//10
'K' => '',
//11
'L' => '',
//12
'M' => $product->variant->stock,
//13
'N' => implode('*', $filters),
//14
];
}
}
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()
->setCreator("Runnable.com");
$objPHPExcel->getProperties()
->setLastModifiedBy("Runnable.com");
$objPHPExcel->getProperties()
->setTitle("Office 2007 XLSX Test Document");
$objPHPExcel->getProperties()
->setSubject("Office 2007 XLSX Test Document");
$objPHPExcel->getProperties()
->setDescription(
"Test document for Office 2007 XLSX, generated using PHP classes."
);
$objPHPExcel->setActiveSheetIndex(0);
$i = 1;
foreach ($data as $values) {
foreach ($values as $key => $value) {
$objPHPExcel->getActiveSheet()
->SetCellValue($key . $i, $value);
}
$i++;
}
$objPHPExcel->getActiveSheet()
->setTitle('Simple');
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save(\Yii::getAlias('@storage/test2.xlsx'));
}
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => [
'login',
'error',
],
'allow' => true,
],
[
'allow' => true,
'roles' => [ '@' ],
],
],
],
];
}
public function actionIndex()
{
return $this->render('index');
}
public function actionHotline()
{
$response = \Yii::$app->response;
$response->format = $response::FORMAT_JSON;
$rootDir = dirname(\Yii::getAlias('@common'));
exec("cd $rootDir && php yii export-xml/hotline", $output);
if (count($output) === 1) {
return [
'error' => true,
'output' => $output[ 0 ],
];
} else {
return [
'success' => true,
'output' => $output,
];
}
}
public function actionNadavi()
{
$response = \Yii::$app->response;
$response->format = $response::FORMAT_JSON;
$rootDir = dirname(\Yii::getAlias('@common'));
exec("cd $rootDir && php yii export-xml/nadavi", $output);
if (count($output) === 1) {
return [
'error' => true,
'output' => $output[ 0 ],
];
} else {
return [
'success' => true,
'output' => $output,
];
}
}
}