1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Grido\Components\Exports; |
4
|
|
|
|
5
|
|
|
use Grido\Components\Columns\Column; |
6
|
|
|
use Nette\Http\IResponse; |
7
|
|
|
|
8
|
|
|
class CsvExport extends BaseExport |
9
|
1 |
|
{ |
10
|
|
|
|
11
|
|
|
/** @deprecated */ |
12
|
|
|
const CSV_ID = 'csv'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @return void |
16
|
|
|
*/ |
17
|
|
|
protected function printData() |
18
|
|
|
{ |
19
|
1 |
|
$escape = function($value) { |
20
|
1 |
|
return preg_match("~[\"\n,;\t]~", $value) || $value === "" |
21
|
1 |
|
? '"' . str_replace('"', '""', $value) . '"' |
22
|
1 |
|
: $value; |
23
|
1 |
|
}; |
24
|
|
|
|
25
|
1 |
|
$print = function(array $row) { |
26
|
1 |
|
print implode(',', $row) . "\n"; |
27
|
1 |
|
}; |
28
|
|
|
|
29
|
1 |
|
$columns = $this->grid[Column::ID]->getComponents(); |
30
|
|
|
|
31
|
1 |
|
$header = []; |
32
|
1 |
|
$headerItems = $this->header ? $this->header : $columns; |
33
|
1 |
|
foreach ($headerItems as $column) { |
34
|
1 |
|
$header[] = $this->header |
35
|
1 |
|
? $escape($column) |
36
|
1 |
|
: $escape($column->getLabel()); |
37
|
1 |
|
} |
38
|
|
|
|
39
|
1 |
|
$print($header); |
40
|
|
|
|
41
|
1 |
|
$datasource = $this->grid->getData(FALSE, FALSE, FALSE); |
42
|
1 |
|
$iterations = ceil($datasource->getCount() / $this->fetchLimit); |
|
|
|
|
43
|
1 |
|
for ($i = 0; $i < $iterations; $i++) { |
44
|
1 |
|
$datasource->limit($i * $this->fetchLimit, $this->fetchLimit); |
45
|
1 |
|
$data = $this->customData |
46
|
1 |
|
? call_user_func_array($this->customData, [$datasource]) |
47
|
1 |
|
: $datasource->getData(); |
|
|
|
|
48
|
|
|
|
49
|
1 |
|
foreach ($data as $items) { |
50
|
1 |
|
$row = []; |
51
|
|
|
|
52
|
1 |
|
$columns = $this->customData |
53
|
1 |
|
? $items |
54
|
1 |
|
: $columns; |
55
|
|
|
|
56
|
1 |
|
foreach ($columns as $column) { |
57
|
1 |
|
$row[] = $this->customData |
58
|
1 |
|
? $escape($column) |
59
|
1 |
|
: $escape($column->renderExport($items)); |
60
|
1 |
|
} |
61
|
|
|
|
62
|
1 |
|
$print($row); |
63
|
1 |
|
} |
64
|
1 |
|
} |
65
|
1 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param IResponse $httpResponse |
69
|
|
|
* @param string $label |
70
|
|
|
*/ |
71
|
|
|
protected function setHttpHeaders(IResponse $httpResponse, $label) |
72
|
|
|
{ |
73
|
1 |
|
$encoding = 'utf-8'; |
74
|
1 |
|
$httpResponse->setHeader('Content-Encoding', $encoding); |
75
|
1 |
|
$httpResponse->setHeader('Content-Type', "text/csv; charset=$encoding"); |
76
|
1 |
|
$httpResponse->setHeader('Content-Disposition', "attachment; filename=\"$label.csv\""); |
77
|
1 |
|
} |
78
|
|
|
} |
79
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: