1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Pfilsx\DataGrid\Grid; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Collections\Criteria; |
8
|
|
|
use Pfilsx\DataGrid\Config\ConfigurationContainerInterface; |
9
|
|
|
use Pfilsx\DataGrid\Config\ConfigurationInterface; |
10
|
|
|
use Pfilsx\DataGrid\DataGridServiceContainer; |
11
|
|
|
use Pfilsx\DataGrid\Grid\Columns\AbstractColumn; |
12
|
|
|
use Pfilsx\DataGrid\Grid\Providers\DataProviderInterface; |
13
|
|
|
use Twig\Template; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class DataGrid |
17
|
|
|
* @package Pfilsx\DataGrid\Grid |
18
|
|
|
* @internal |
19
|
|
|
*/ |
20
|
|
|
class DataGrid implements DataGridInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var Template |
24
|
|
|
*/ |
25
|
|
|
protected $template; |
26
|
|
|
/** |
27
|
|
|
* @var DataGridServiceContainer |
28
|
|
|
*/ |
29
|
|
|
protected $container; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ConfigurationInterface |
33
|
|
|
*/ |
34
|
|
|
protected $configuration; |
35
|
|
|
/** |
36
|
|
|
* @var Criteria |
37
|
|
|
*/ |
38
|
|
|
protected $filtersCriteria; |
39
|
|
|
/** |
40
|
|
|
* @var AbstractGridType |
41
|
|
|
*/ |
42
|
|
|
protected $gridType; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var DataGridBuilderInterface |
46
|
|
|
*/ |
47
|
|
|
protected $builder; |
48
|
|
|
/** |
49
|
|
|
* @var DataGridFiltersBuilderInterface |
50
|
|
|
*/ |
51
|
|
|
protected $filterBuilder; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* DataGrid constructor. |
55
|
|
|
* @param AbstractGridType $type |
56
|
|
|
* @param DataProviderInterface $dataProvider |
57
|
|
|
* @param ConfigurationContainerInterface $defaultConfiguration |
58
|
|
|
* @param DataGridServiceContainer $container |
59
|
|
|
* @internal |
60
|
|
|
*/ |
61
|
|
|
public function __construct( |
62
|
|
|
AbstractGridType $type, |
63
|
|
|
DataProviderInterface $dataProvider, |
64
|
|
|
ConfigurationContainerInterface $defaultConfiguration, |
65
|
|
|
DataGridServiceContainer $container |
66
|
|
|
) |
67
|
|
|
{ |
68
|
|
|
$this->gridType = $type; |
69
|
|
|
$this->container = $container; |
70
|
|
|
|
71
|
|
|
$this->configureBuilders($dataProvider); |
72
|
|
|
|
73
|
|
|
$this->configuration = $defaultConfiguration->getInstance($this->builder->getInstance()) |
74
|
|
|
->merge($this->builder->getConfiguration()); |
75
|
|
|
|
76
|
|
|
$this->setTemplate($this->configuration->getTemplate()); |
|
|
|
|
77
|
|
|
$this->setTranslationDomain($this->configuration->getTranslationDomain()); |
78
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
protected function configureBuilders(DataProviderInterface $dataProvider) |
82
|
|
|
{ |
83
|
|
|
$this->builder = new DataGridBuilder($this->container); |
84
|
|
|
$this->builder->setProvider($dataProvider); |
85
|
|
|
$this->gridType->buildGrid($this->builder); |
86
|
|
|
|
87
|
|
|
$this->filterBuilder = new DataGridFiltersBuilder(); |
88
|
|
|
$this->filterBuilder->setProvider($dataProvider); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @internal |
93
|
|
|
*/ |
94
|
|
|
protected function configurePagerOptions() |
95
|
|
|
{ |
96
|
|
|
$pager = $this->builder->getProvider()->getPager(); |
97
|
|
|
$pager->setLimit($this->configuration->getPaginationLimit()); |
98
|
|
|
if ($this->configuration->getPaginationEnabled()) { |
99
|
|
|
$pager->enable(); |
100
|
|
|
$pager->setTotalCount($this->getProvider()->getTotalCount()); |
101
|
|
|
$pager->rebuildPaginationOptions(); |
102
|
|
|
} else { |
103
|
|
|
$pager->disable(); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return Providers\DataProviderInterface |
109
|
|
|
* @internal |
110
|
|
|
*/ |
111
|
|
|
public function getProvider(): DataProviderInterface |
112
|
|
|
{ |
113
|
|
|
return $this->builder->getProvider(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return AbstractColumn[] |
118
|
|
|
* @internal |
119
|
|
|
*/ |
120
|
|
|
public function getColumns(): array |
121
|
|
|
{ |
122
|
|
|
return $this->builder->getColumns(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return bool |
127
|
|
|
* @internal |
128
|
|
|
*/ |
129
|
|
|
public function hasFilters() |
130
|
|
|
{ |
131
|
|
|
return $this->builder->hasFilters(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return array |
136
|
|
|
* @internal |
137
|
|
|
*/ |
138
|
|
|
public function getData() |
139
|
|
|
{ |
140
|
|
|
return $this->getProvider()->getItems(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return Template |
145
|
|
|
* @internal |
146
|
|
|
*/ |
147
|
|
|
public function getTemplate() |
148
|
|
|
{ |
149
|
|
|
return $this->template; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return string |
154
|
|
|
* @internal |
155
|
|
|
*/ |
156
|
|
|
public function getNoDataMessage() |
157
|
|
|
{ |
158
|
|
|
return $this->container->getTranslator() !== null |
159
|
|
|
? $this->container->getTranslator()->trans($this->configuration->getNoDataMessage(), [], $this->configuration->getTranslationDomain()) |
160
|
|
|
: ucfirst($this->configuration->getNoDataMessage()); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return bool |
165
|
|
|
* @internal |
166
|
|
|
*/ |
167
|
|
|
public function hasPagination() |
168
|
|
|
{ |
169
|
|
|
return $this->builder->hasPagination(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @return array |
174
|
|
|
* @internal |
175
|
|
|
*/ |
176
|
|
|
public function getPaginationOptions() |
177
|
|
|
{ |
178
|
|
|
return $this->builder->getPager()->getPaginationOptions(); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @return DataGridView |
183
|
|
|
*/ |
184
|
|
|
public function createView(): DataGridView |
185
|
|
|
{ |
186
|
|
|
$this->handleRequest(); |
187
|
|
|
$this->configurePagerOptions(); |
188
|
|
|
return new DataGridView($this, $this->container); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param string $path |
193
|
|
|
*/ |
194
|
|
|
protected function setTemplate(string $path) |
195
|
|
|
{ |
196
|
|
|
$this->template = $this->container->getTwig()->loadTemplate($path); |
197
|
|
|
foreach ($this->builder->getColumns() as $column) { |
198
|
|
|
$column->setTemplate($this->template); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param string $domain |
204
|
|
|
*/ |
205
|
|
|
protected function setTranslationDomain(?string $domain) |
206
|
|
|
{ |
207
|
|
|
foreach ($this->builder->getColumns() as $column) { |
208
|
|
|
$column->setTranslationDomain($domain); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
protected function handleRequest(): void |
213
|
|
|
{ |
214
|
|
|
$request = $this->container->getRequest()->getCurrentRequest(); |
215
|
|
|
$queryParams = $request !== null ? $request->query->get('data_grid', []) : []; |
216
|
|
|
|
217
|
|
|
$this->handleSorting($queryParams); |
218
|
|
|
|
219
|
|
|
$this->handlePagination($queryParams); |
220
|
|
|
|
221
|
|
|
$this->handleFilters($queryParams); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
protected function handleSorting(array &$queryParams) |
225
|
|
|
{ |
226
|
|
|
if (array_key_exists('sortBy', $queryParams)) { |
227
|
|
|
$this->setSort($queryParams['sortBy']); |
228
|
|
|
unset($queryParams['sortBy']); |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
protected function handlePagination(array &$queryParams) |
233
|
|
|
{ |
234
|
|
|
if (array_key_exists('page', $queryParams)) { |
235
|
|
|
$this->setPage($queryParams['page']); |
236
|
|
|
unset($queryParams['page']); |
237
|
|
|
} else { |
238
|
|
|
$this->setPage(1); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
protected function handleFilters(array $queryParams) |
243
|
|
|
{ |
244
|
|
|
$this->builder->setFiltersValues($queryParams); |
245
|
|
|
$this->filterBuilder->setParams($queryParams); |
246
|
|
|
$this->gridType->handleFilters($this->filterBuilder, $queryParams); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
protected function setSort($attribute) |
250
|
|
|
{ |
251
|
|
|
$first = substr($attribute, 0, 1); |
252
|
|
|
if ($first == '-') { |
253
|
|
|
$this->builder->setSort(substr($attribute, 1), 'DESC'); |
254
|
|
|
} else { |
255
|
|
|
$this->builder->setSort($attribute, 'ASC'); |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
protected function setPage($page) |
260
|
|
|
{ |
261
|
|
|
$this->builder->getPager()->setPage(is_numeric($page) ? (int)$page : 1); |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|