|
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
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
/** @deprecated */ |
|
12
|
|
|
const CSV_ID = 'csv'; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @return void |
|
16
|
|
|
*/ |
|
17
|
|
|
protected function printData() |
|
18
|
|
|
{ |
|
19
|
|
|
$escape = function($value) { |
|
20
|
|
|
return preg_match("~[\"\n,;\t]~", $value) || $value === "" |
|
21
|
|
|
? '"' . str_replace('"', '""', $value) . '"' |
|
22
|
|
|
: $value; |
|
23
|
|
|
}; |
|
24
|
|
|
|
|
25
|
|
|
$print = function(array $row) { |
|
26
|
|
|
print implode(',', $row) . "\n"; |
|
27
|
|
|
}; |
|
28
|
|
|
|
|
29
|
|
|
$columns = $this->grid[Column::ID]->getComponents(); |
|
30
|
|
|
|
|
31
|
|
|
$header = []; |
|
32
|
|
|
$headerItems = $this->header ? $this->header : $columns; |
|
33
|
|
|
foreach ($headerItems as $column) { |
|
34
|
|
|
$header[] = $this->header |
|
35
|
|
|
? $escape($column) |
|
36
|
|
|
: $escape($column->getLabel()); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$print($header); |
|
40
|
|
|
|
|
41
|
|
|
$datasource = $this->grid->getData(FALSE, FALSE, FALSE); |
|
42
|
|
|
$iterations = ceil($datasource->getCount() / $this->fetchLimit); |
|
|
|
|
|
|
43
|
|
|
for ($i = 0; $i < $iterations; $i++) { |
|
44
|
|
|
$datasource->limit($i * $this->fetchLimit, $this->fetchLimit); |
|
45
|
|
|
$data = $this->customData |
|
46
|
|
|
? call_user_func_array($this->customData, [$datasource]) |
|
47
|
|
|
: $datasource->getData(); |
|
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
foreach ($data as $items) { |
|
50
|
|
|
$row = []; |
|
51
|
|
|
|
|
52
|
|
|
$columns = $this->customData |
|
53
|
|
|
? $items |
|
54
|
|
|
: $columns; |
|
55
|
|
|
|
|
56
|
|
|
foreach ($columns as $column) { |
|
57
|
|
|
$row[] = $this->customData |
|
58
|
|
|
? $escape($column) |
|
59
|
|
|
: $escape($column->renderExport($items)); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$print($row); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param IResponse $httpResponse |
|
69
|
|
|
* @param string $label |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function setHttpHeaders(IResponse $httpResponse, $label) |
|
72
|
|
|
{ |
|
73
|
|
|
$encoding = 'utf-8'; |
|
74
|
|
|
$httpResponse->setHeader('Content-Encoding', $encoding); |
|
75
|
|
|
$httpResponse->setHeader('Content-Type', "text/csv; charset=$encoding"); |
|
76
|
|
|
$httpResponse->setHeader('Content-Disposition', "attachment; filename=\"$label.csv\""); |
|
77
|
|
|
} |
|
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: