1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nayjest\Grids\Components; |
4
|
|
|
|
5
|
|
|
use App\Components\Grid\ExtendedTableRow; |
6
|
|
|
use App\Components\Grid\GridsToComponents; |
7
|
|
|
use App\Http\Controllers\SupportersController; |
8
|
|
|
use App\Topic; |
9
|
|
|
use App\User; |
10
|
|
|
use Event; |
11
|
|
|
use Illuminate\Pagination\Paginator; |
12
|
|
|
use Illuminate\Foundation\Application; |
13
|
|
|
use Illuminate\Support\Facades\DB; |
14
|
|
|
use Maatwebsite\Excel\Classes\LaravelExcelWorksheet; |
15
|
|
|
use Maatwebsite\Excel\Excel; |
16
|
|
|
use Maatwebsite\Excel\Writers\LaravelExcelWriter; |
17
|
|
|
use Nayjest\Grids\Components\Base\RenderableComponent; |
18
|
|
|
use Nayjest\Grids\Components\Base\RenderableRegistry; |
19
|
|
|
use Nayjest\Grids\DataProvider; |
20
|
|
|
use Nayjest\Grids\DataRow; |
21
|
|
|
use Nayjest\Grids\FieldConfig; |
22
|
|
|
use Nayjest\Grids\Grid; |
23
|
|
|
use Illuminate\Support\Facades\Storage; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class ExcelExport |
28
|
|
|
* |
29
|
|
|
* The component provides control for exporting data to excel. |
30
|
|
|
* |
31
|
|
|
* @author: Alexander Hofmeister |
32
|
|
|
* @package Nayjest\Grids\Components |
33
|
|
|
*/ |
34
|
|
|
class ExcelDispatchJobService extends RenderableComponent |
35
|
|
|
{ |
36
|
|
|
const NAME = 'excel_export'; |
37
|
|
|
const INPUT_PARAM = 'xlsx'; |
38
|
|
|
const DEFAULT_ROWS_LIMIT = 50000; |
39
|
|
|
|
40
|
|
|
protected $template = '*.components.excel_export'; |
41
|
|
|
protected $name = ExcelDispatchJobService::NAME; |
42
|
|
|
protected $render_section = RenderableRegistry::SECTION_END; |
43
|
|
|
protected $rows_limit = self::DEFAULT_ROWS_LIMIT; |
44
|
|
|
protected $extension = 'xlsx'; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
protected $output; |
50
|
|
|
|
51
|
|
|
protected $data; |
52
|
|
|
|
53
|
|
|
protected $userId; |
54
|
|
|
|
55
|
|
|
protected $className; |
56
|
|
|
|
57
|
|
|
protected $config; |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var string |
63
|
|
|
*/ |
64
|
|
|
protected $fileName; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var string |
68
|
|
|
*/ |
69
|
|
|
protected $sheetName; |
70
|
|
|
|
71
|
|
|
protected $ignored_columns = []; |
72
|
|
|
|
73
|
|
|
protected $is_hidden_columns_exported = false; |
74
|
|
|
|
75
|
|
|
protected $on_file_create; |
76
|
|
|
|
77
|
|
|
protected $on_sheet_create; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return mixed |
81
|
|
|
*/ |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
public function getConfig() |
85
|
|
|
{ |
86
|
|
|
return $this->config; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param mixed $config |
91
|
|
|
*/ |
92
|
|
|
public function setConfig($config) |
93
|
|
|
{ |
94
|
|
|
$this->config = $config; |
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
/** |
98
|
|
|
* @return mixed |
99
|
|
|
*/ |
100
|
|
|
public function getClassName() |
101
|
|
|
{ |
102
|
|
|
return $this->className; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param mixed $config |
|
|
|
|
107
|
|
|
*/ |
108
|
|
|
public function setClassName($className) |
109
|
|
|
{ |
110
|
|
|
$this->className = $className; |
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return mixed |
116
|
|
|
*/ |
117
|
|
|
public function getUserId() |
118
|
|
|
{ |
119
|
|
|
return $this->userId; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param mixed $config |
|
|
|
|
124
|
|
|
*/ |
125
|
|
|
public function setUserId($userId) |
126
|
|
|
{ |
127
|
|
|
$this->userId = $userId; |
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function __construct($userId, $showId, $sheetName, $extension, $ignored_columns, $is_hidden_columns_exported, $rows_limit) |
132
|
|
|
{ |
133
|
|
|
$this->setUserId($userId); |
134
|
|
|
$this->setSheetName($sheetName); |
135
|
|
|
$this->setExtension($extension); |
136
|
|
|
$this->setIgnoredColumns($ignored_columns); |
137
|
|
|
$this->setHiddenColumnsExported($is_hidden_columns_exported); |
138
|
|
|
$this->setRowsLimit($rows_limit); |
139
|
|
|
|
140
|
|
|
$user = User::find($this->userId); |
141
|
|
|
|
142
|
|
|
$controllers = GridsToComponents::getGridsControllers(); |
143
|
|
|
$className = 'App\\Http\\Controllers\\' . $controllers[$sheetName]; |
144
|
|
|
|
145
|
|
|
if($sheetName == 'topicQuestionsGrid') { |
146
|
|
|
$topic = Topic::find($showId); |
147
|
|
|
$this->config = $className::getQuestionsGridConfig($topic); |
148
|
|
|
} |
149
|
|
|
else{ |
150
|
|
|
$this->config = $className::getGridConfig($user); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$grid = (new Grid($this->config->setRowComponent(new ExtendedTableRow(function ($model) { |
|
|
|
|
154
|
|
|
return $className::getRowAttributes($model); |
|
|
|
|
155
|
|
|
})))); |
156
|
|
|
|
157
|
|
|
$this->renderExcel(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param Grid $grid |
162
|
|
|
* @return null|void |
163
|
|
|
*/ |
164
|
|
|
public function initialize(Grid $grid) |
165
|
|
|
{ |
166
|
|
|
$this->renderExcel(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Sets name of exported file. |
171
|
|
|
* |
172
|
|
|
* @param string $name |
173
|
|
|
* @return $this |
174
|
|
|
*/ |
175
|
|
|
public function setFileName($name) |
176
|
|
|
{ |
177
|
|
|
$this->fileName = $name; |
178
|
|
|
return $this; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Returns name of exported file. |
183
|
|
|
* |
184
|
|
|
* @return string |
185
|
|
|
*/ |
186
|
|
|
public function getFileName() |
187
|
|
|
{ |
188
|
|
|
return $this->fileName ?: $this->getConfig()->getName(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param string $name |
193
|
|
|
* @return $this |
194
|
|
|
*/ |
195
|
|
|
public function setSheetName($name) |
196
|
|
|
{ |
197
|
|
|
$this->sheetName = $name; |
198
|
|
|
return $this; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @return string |
203
|
|
|
*/ |
204
|
|
|
public function getSheetName() |
205
|
|
|
{ |
206
|
|
|
return $this->sheetName; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param string $name |
211
|
|
|
* @return $this |
212
|
|
|
*/ |
213
|
|
|
public function setExtension($name) |
214
|
|
|
{ |
215
|
|
|
$this->extension = $name; |
216
|
|
|
return $this; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @return string |
221
|
|
|
*/ |
222
|
|
|
public function getExtension() |
223
|
|
|
{ |
224
|
|
|
return $this->extension; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @return int |
229
|
|
|
*/ |
230
|
|
|
public function getRowsLimit() |
231
|
|
|
{ |
232
|
|
|
return $this->rows_limit; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @param int $limit |
237
|
|
|
* |
238
|
|
|
* @return $this |
239
|
|
|
*/ |
240
|
|
|
public function setRowsLimit($limit = 50000) |
241
|
|
|
{ |
242
|
|
|
$this->rows_limit = $limit; |
243
|
|
|
return $this; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
View Code Duplication |
protected function resetPagination(DataProvider $provider) |
|
|
|
|
247
|
|
|
{ |
248
|
|
|
if (version_compare(Application::VERSION, '5.0.0', '<')) { |
249
|
|
|
$provider->getPaginationFactory()->setPageName('page_unused'); |
250
|
|
|
} else { |
251
|
|
|
Paginator::currentPageResolver(function () { |
252
|
|
|
return 1; |
253
|
|
|
}); |
254
|
|
|
} |
255
|
|
|
$provider->setPageSize($this->getRowsLimit()); |
256
|
|
|
$provider->setCurrentPage(1); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @param FieldConfig $column |
261
|
|
|
* @return bool |
262
|
|
|
*/ |
263
|
|
|
protected function isColumnExported(FieldConfig $column) |
264
|
|
|
{ |
265
|
|
|
return !in_array($column->getName(), $this->getIgnoredColumns()) |
266
|
|
|
&& ($this->isHiddenColumnsExported() || !$column->isHidden()); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @internal |
271
|
|
|
* @return array |
272
|
|
|
*/ |
273
|
|
|
public function getData() |
274
|
|
|
{ |
275
|
|
|
// Build array |
276
|
|
|
$exportData = []; |
277
|
|
|
/** @var $provider DataProvider */ |
278
|
|
|
$provider = $this->getConfig()->getDataProvider(); |
279
|
|
|
|
280
|
|
|
$exportData[] = $this->getHeaderRow(); |
281
|
|
|
|
282
|
|
|
$this->resetPagination($provider); |
283
|
|
|
$provider->reset(); |
284
|
|
|
|
285
|
|
|
/** @var DataRow $row */ |
286
|
|
|
while ($row = $provider->getRow()) { |
287
|
|
|
$output = []; |
288
|
|
View Code Duplication |
foreach ($this->getConfig()->getColumns() as $column) { |
|
|
|
|
289
|
|
|
if ($this->isColumnExported($column)) { |
290
|
|
|
$output[] = $this->escapeString($column->getValue($row)); |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
$exportData[] = $output; |
294
|
|
|
} |
295
|
|
|
return $exportData; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
protected function renderExcel() |
299
|
|
|
{ |
300
|
|
|
/** @var Excel $excel */ |
301
|
|
|
$excel = app('excel'); |
302
|
|
|
$excel |
303
|
|
|
->create($this->getFileName(), $this->getOnFileCreate()) |
304
|
|
|
->store('xlsx',Storage::disk('local')->getDriver()->getAdapter()->getPathPrefix() . 'public/excels/'); |
|
|
|
|
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
protected function escapeString($str) |
308
|
|
|
{ |
309
|
|
|
return str_replace('"', '\'', strip_tags(html_entity_decode($str))); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
protected function getHeaderRow() |
313
|
|
|
{ |
314
|
|
|
$output = []; |
315
|
|
View Code Duplication |
foreach ($this->getConfig()->getColumns() as $column) { |
|
|
|
|
316
|
|
|
if ($this->isColumnExported($column)) { |
317
|
|
|
$output[] = $this->escapeString($column->getLabel()); |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
return $output; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* @return string[] |
325
|
|
|
*/ |
326
|
|
|
public function getIgnoredColumns() |
327
|
|
|
{ |
328
|
|
|
return $this->ignored_columns; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* @param string[] $ignoredColumns |
333
|
|
|
* @return $this |
334
|
|
|
*/ |
335
|
|
|
public function setIgnoredColumns(array $ignoredColumns) |
336
|
|
|
{ |
337
|
|
|
$this->ignored_columns = $ignoredColumns; |
338
|
|
|
return $this; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* @return boolean |
343
|
|
|
*/ |
344
|
|
|
public function isHiddenColumnsExported() |
345
|
|
|
{ |
346
|
|
|
return $this->is_hidden_columns_exported; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* @param bool $isHiddenColumnsExported |
351
|
|
|
* @return $this |
352
|
|
|
*/ |
353
|
|
|
public function setHiddenColumnsExported($isHiddenColumnsExported) |
354
|
|
|
{ |
355
|
|
|
$this->is_hidden_columns_exported = $isHiddenColumnsExported; |
356
|
|
|
return $this; |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* @return mixed |
361
|
|
|
*/ |
362
|
|
|
public function getOnFileCreate() |
363
|
|
|
{ |
364
|
|
|
if ($this->on_file_create === null) { |
365
|
|
|
$this->on_file_create = function (LaravelExcelWriter $excel) { |
366
|
|
|
$excel->sheet($this->getSheetName(), $this->getOnSheetCreate()); |
367
|
|
|
|
368
|
|
|
}; |
369
|
|
|
} |
370
|
|
|
return $this->on_file_create; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* @param callable $onFileCreate |
375
|
|
|
* |
376
|
|
|
* @return $this |
377
|
|
|
*/ |
378
|
|
|
public function setOnFileCreate($onFileCreate) |
379
|
|
|
{ |
380
|
|
|
$this->on_file_create = $onFileCreate; |
381
|
|
|
return $this; |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* @return callable |
386
|
|
|
*/ |
387
|
|
|
public function getOnSheetCreate() |
388
|
|
|
{ |
389
|
|
|
if ($this->on_sheet_create === null) { |
390
|
|
|
$this->on_sheet_create = function (LaravelExcelWorksheet $sheet) { |
391
|
|
|
// for($i = 0; $i < count($data); $i++) { |
392
|
|
|
// for ($j = 0; $j < count($data[$i]); $j++) { |
393
|
|
|
// $sheet->appendRow(($i * 1000) + $j + 1, $data[$i][$j]); |
394
|
|
|
// } |
395
|
|
|
//$sheet->fromArray($data, null, 'A1', false, false); |
396
|
|
|
// $data = $this->getData(); |
397
|
|
|
// $data->chunk(100, function($rows) use ($sheet) |
398
|
|
|
// { |
399
|
|
|
|
400
|
|
|
$sheet->fromArray($this->getData(), null, 'A1', false, false); |
401
|
|
|
// }); |
402
|
|
|
}; |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
return $this->on_sheet_create; |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
/** |
409
|
|
|
* @param callable $onSheetCreate |
410
|
|
|
* |
411
|
|
|
* @return $this |
412
|
|
|
*/ |
413
|
|
|
public function setOnSheetCreate($onSheetCreate) |
414
|
|
|
{ |
415
|
|
|
$this->on_sheet_create = $onSheetCreate; |
416
|
|
|
return $this; |
417
|
|
|
} |
418
|
|
|
} |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.