1 | <?php |
||
29 | 1 | class Export extends Component implements \Nette\Application\IResponse |
|
30 | { |
||
31 | const ID = 'export'; |
||
32 | |||
33 | /** @var int */ |
||
34 | protected $fetchLimit = 100000; |
||
35 | |||
36 | /** @var array */ |
||
37 | protected $header = []; |
||
38 | |||
39 | /** @var callable */ |
||
40 | protected $customData; |
||
41 | |||
42 | /** |
||
43 | * @param Grid $grid |
||
44 | * @param string $label |
||
45 | */ |
||
46 | public function __construct(Grid $grid, $label = NULL) |
||
47 | { |
||
48 | 1 | $this->grid = $grid; |
|
49 | 1 | $this->label = $label; |
|
50 | |||
51 | 1 | $grid->addComponent($this, self::ID); |
|
52 | 1 | } |
|
53 | |||
54 | /** |
||
55 | * @return void |
||
56 | */ |
||
57 | protected function printCsv() |
||
58 | { |
||
59 | 1 | $escape = function($value) { |
|
60 | 1 | return preg_match("~[\"\n,;\t]~", $value) || $value === "" |
|
61 | 1 | ? '"' . str_replace('"', '""', $value) . '"' |
|
62 | 1 | : $value; |
|
63 | 1 | }; |
|
64 | |||
65 | 1 | $print = function(array $row) { |
|
66 | 1 | print implode(',', $row) . "\n"; |
|
67 | 1 | }; |
|
68 | |||
69 | 1 | $columns = $this->grid[Column::ID]->getComponents(); |
|
70 | |||
71 | 1 | $header = []; |
|
72 | 1 | $headerItems = $this->header ? $this->header : $columns; |
|
73 | 1 | foreach ($headerItems as $column) { |
|
74 | 1 | $header[] = $this->header |
|
75 | 1 | ? $escape($column) |
|
76 | 1 | : $escape($column->getLabel()); |
|
77 | 1 | } |
|
78 | |||
79 | 1 | $print($header); |
|
80 | |||
81 | 1 | $datasource = $this->grid->getData(FALSE, FALSE, FALSE); |
|
82 | 1 | $iterations = ceil($datasource->getCount() / $this->fetchLimit); |
|
|
|||
83 | 1 | for ($i = 0; $i < $iterations; $i++) { |
|
84 | 1 | $datasource->limit($i * $this->fetchLimit, $this->fetchLimit); |
|
85 | 1 | $data = $this->customData |
|
86 | 1 | ? call_user_func_array($this->customData, [$datasource]) |
|
87 | 1 | : $datasource->getData(); |
|
88 | |||
89 | 1 | foreach ($data as $items) { |
|
90 | 1 | $row = []; |
|
91 | |||
92 | 1 | $columns = $this->customData |
|
93 | 1 | ? $items |
|
94 | 1 | : $columns; |
|
95 | |||
96 | 1 | foreach ($columns as $column) { |
|
97 | 1 | $row[] = $this->customData |
|
98 | 1 | ? $escape($column) |
|
99 | 1 | : $escape($column->renderExport($items)); |
|
100 | 1 | } |
|
101 | |||
102 | 1 | $print($row); |
|
103 | 1 | } |
|
104 | 1 | } |
|
105 | 1 | } |
|
106 | |||
107 | /** |
||
108 | * Sets a limit which will be used in order to retrieve data from datasource. |
||
109 | * @param int $limit |
||
110 | * @return \Grido\Components\Export |
||
111 | */ |
||
112 | public function setFetchLimit($limit) |
||
113 | { |
||
114 | 1 | $this->fetchLimit = (int) $limit; |
|
115 | 1 | return $this; |
|
116 | } |
||
117 | |||
118 | /** |
||
119 | * @return int |
||
120 | */ |
||
121 | public function getFetchLimit() |
||
125 | |||
126 | /** |
||
127 | * Sets a custom header of result CSV file (list of field names). |
||
128 | * @param array $header |
||
129 | * @return \Grido\Components\Export |
||
130 | */ |
||
131 | public function setHeader(array $header) |
||
136 | |||
137 | /** |
||
138 | * Sets a callback to modify output data. This callback must return a list of items. (array) function($datasource) |
||
139 | * DEBUG? You probably need to comment lines started with $httpResponse->setHeader in Grido\Components\Export.php |
||
140 | * @param callable $callback |
||
141 | * @return \Grido\Components\Export |
||
142 | */ |
||
143 | public function setCustomData($callback) |
||
148 | |||
149 | /** |
||
150 | * @internal |
||
151 | */ |
||
152 | public function handleExport() |
||
157 | |||
158 | /*************************** interface \Nette\Application\IResponse ***************************/ |
||
159 | |||
160 | /** |
||
161 | * Sends response to output. |
||
162 | * @param \Nette\Http\IRequest $httpRequest |
||
163 | * @param \Nette\Http\IResponse $httpResponse |
||
164 | * @return void |
||
165 | */ |
||
166 | public function send(\Nette\Http\IRequest $httpRequest, \Nette\Http\IResponse $httpResponse) |
||
180 | } |
||
181 |
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: