|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Grido\Components\Exports; |
|
4
|
|
|
|
|
5
|
|
|
use Grido\Components\Component; |
|
6
|
|
|
use Grido\Grid; |
|
7
|
|
|
use Nette\Application\IResponse; |
|
8
|
|
|
use Nette\Utils\Strings; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Exporting data. |
|
12
|
|
|
* |
|
13
|
|
|
* @package Grido |
|
14
|
|
|
* @subpackage Components |
|
15
|
|
|
* |
|
16
|
|
|
* @property int $fetchLimit |
|
17
|
|
|
* @property-write array $header |
|
18
|
|
|
* @property-write callable $customData |
|
19
|
|
|
*/ |
|
20
|
1 |
|
abstract class BaseExport extends Component implements IResponse |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
const ID = 'export'; |
|
24
|
|
|
|
|
25
|
|
|
/** @var int */ |
|
26
|
|
|
protected $fetchLimit = 100000; |
|
27
|
|
|
|
|
28
|
|
|
/** @var array */ |
|
29
|
1 |
|
protected $header = []; |
|
30
|
|
|
|
|
31
|
|
|
/** @var callable */ |
|
32
|
|
|
protected $customData; |
|
33
|
1 |
|
|
|
34
|
|
|
/** @var string */ |
|
35
|
|
|
private $title; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param string $label |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct($label = NULL) |
|
41
|
|
|
{ |
|
42
|
1 |
|
$this->label = $label; |
|
43
|
1 |
|
$this->monitor('Grido\Grid'); |
|
44
|
1 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
protected function attached($presenter) |
|
47
|
|
|
{ |
|
48
|
1 |
|
parent::attached($presenter); |
|
49
|
1 |
|
if ($presenter instanceof Grid) { |
|
50
|
1 |
|
$this->grid = $presenter; |
|
51
|
1 |
|
} |
|
52
|
1 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return void |
|
56
|
1 |
|
*/ |
|
57
|
|
|
abstract protected function printData(); |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param \Nette\Http\IResponse $httpResponse |
|
61
|
|
|
* @param string $label |
|
62
|
|
|
* @return void |
|
63
|
|
|
*/ |
|
64
|
|
|
abstract protected function setHttpHeaders(\Nette\Http\IResponse $httpResponse, $label); |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param string $title |
|
68
|
|
|
* @return self |
|
69
|
|
|
*/ |
|
70
|
|
|
public function setTitle($title) |
|
71
|
|
|
{ |
|
72
|
|
|
$this->title = $title; |
|
73
|
|
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return string |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getTitle() |
|
80
|
|
|
{ |
|
81
|
1 |
|
return $this->title; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Sets a limit which will be used in order to retrieve data from datasource. |
|
86
|
|
|
* @param int $limit |
|
87
|
|
|
* @return \Grido\Components\Export |
|
88
|
|
|
*/ |
|
89
|
|
|
public function setFetchLimit($limit) |
|
90
|
|
|
{ |
|
91
|
1 |
|
$this->fetchLimit = (int) $limit; |
|
92
|
1 |
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return int |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getFetchLimit() |
|
99
|
|
|
{ |
|
100
|
1 |
|
return $this->fetchLimit; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Sets a custom header of result CSV file (list of field names). |
|
105
|
|
|
* @param array $header |
|
106
|
|
|
* @return \Grido\Components\Export |
|
107
|
|
|
*/ |
|
108
|
|
|
public function setHeader(array $header) |
|
109
|
|
|
{ |
|
110
|
1 |
|
$this->header = $header; |
|
111
|
1 |
|
return $this; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Sets a callback to modify output data. This callback must return a list of items. (array) function($datasource) |
|
116
|
|
|
* DEBUG? You probably need to comment lines started with $httpResponse->setHeader in Grido\Components\Export.php |
|
117
|
|
|
* @param callable $callback |
|
118
|
|
|
* @return \Grido\Components\Export |
|
119
|
|
|
*/ |
|
120
|
|
|
public function setCustomData($callback) |
|
121
|
|
|
{ |
|
122
|
1 |
|
$this->customData = $callback; |
|
123
|
1 |
|
return $this; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @internal |
|
128
|
|
|
*/ |
|
129
|
|
|
public function handleExport() |
|
130
|
|
|
{ |
|
131
|
1 |
|
!empty($this->grid->onRegistered) && $this->grid->onRegistered($this->grid); |
|
132
|
1 |
|
$this->grid->presenter->sendResponse($this); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/*************************** interface \Nette\Application\IResponse ***************************/ |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Sends response to output. |
|
139
|
|
|
* @param \Nette\Http\IRequest $httpRequest |
|
140
|
|
|
* @param \Nette\Http\IResponse $httpResponse |
|
141
|
|
|
* @return void |
|
142
|
|
|
*/ |
|
143
|
|
|
public function send(\Nette\Http\IRequest $httpRequest, \Nette\Http\IResponse $httpResponse) |
|
144
|
|
|
{ |
|
145
|
1 |
|
$label = $this->label |
|
146
|
1 |
|
? ucfirst(Strings::webalize($this->label)) |
|
147
|
1 |
|
: ucfirst($this->grid->name); |
|
|
|
|
|
|
148
|
|
|
|
|
149
|
1 |
|
$this->setHttpHeaders($httpResponse, $label); |
|
150
|
|
|
|
|
151
|
1 |
|
print chr(0xEF) . chr(0xBB) . chr(0xBF); //UTF-8 BOM |
|
152
|
1 |
|
$this->printData(); |
|
153
|
1 |
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.