1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Pfilsx\DataGrid\Grid; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Pfilsx\DataGrid\Config\Configuration; |
8
|
|
|
use Pfilsx\DataGrid\Config\ConfigurationInterface; |
9
|
|
|
use Pfilsx\DataGrid\DataGridServiceContainer; |
10
|
|
|
use Pfilsx\DataGrid\Grid\Columns\AbstractColumn; |
11
|
|
|
use Pfilsx\DataGrid\Grid\Columns\ActionColumn; |
12
|
|
|
use Pfilsx\DataGrid\Grid\Columns\DataColumn; |
13
|
|
|
use InvalidArgumentException; |
14
|
|
|
use Pfilsx\DataGrid\Grid\Columns\SerialColumn; |
15
|
|
|
use Pfilsx\DataGrid\Grid\Providers\DataProviderInterface; |
16
|
|
|
|
17
|
|
|
class DataGridBuilder implements DataGridBuilderInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var Pager |
21
|
|
|
*/ |
22
|
|
|
protected $pager; |
23
|
|
|
/** |
24
|
|
|
* @var DataGridServiceContainer |
25
|
|
|
*/ |
26
|
|
|
protected $container; |
27
|
|
|
/** |
28
|
|
|
* @var DataProviderInterface |
29
|
|
|
*/ |
30
|
|
|
protected $provider; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var AbstractColumn[] |
34
|
|
|
*/ |
35
|
|
|
protected $columns = []; |
36
|
|
|
|
37
|
|
|
protected $hasFilters = false; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var Configuration |
41
|
|
|
*/ |
42
|
|
|
protected $configuration; |
43
|
|
|
|
44
|
|
|
protected $instance = 'default'; |
45
|
|
|
|
46
|
|
|
protected $sort = null; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* DataGridBuilder constructor. |
50
|
|
|
* @param DataGridServiceContainer $container |
51
|
|
|
*/ |
52
|
|
|
public function __construct(DataGridServiceContainer $container) |
53
|
|
|
{ |
54
|
|
|
$this->container = $container; |
55
|
|
|
$this->configuration = new Configuration(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $attribute |
60
|
|
|
* @param string $columnClass |
61
|
|
|
* @param array $options |
62
|
|
|
* @return $this |
63
|
|
|
*/ |
64
|
|
|
public function addColumn(string $attribute, string $columnClass = DataColumn::class, array $options = []): DataGridBuilderInterface |
65
|
|
|
{ |
66
|
|
|
if (!is_subclass_of($columnClass, AbstractColumn::class)) { |
67
|
|
|
throw new InvalidArgumentException('Expected subclass of' . AbstractColumn::class); |
68
|
|
|
} |
69
|
|
|
/** |
70
|
|
|
* @var AbstractColumn $column |
71
|
|
|
*/ |
72
|
|
|
$column = new $columnClass($this->container, array_merge($options, ['attribute' => $attribute])); |
73
|
|
|
$this->columns[] = $column; |
74
|
|
|
if ($column->hasFilter() && $column->isVisible()) { |
75
|
|
|
$this->hasFilters = true; |
76
|
|
|
} |
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $attribute |
82
|
|
|
* @param array $options |
83
|
|
|
* @return $this |
84
|
|
|
*/ |
85
|
|
|
public function addDataColumn(string $attribute, array $options = []): DataGridBuilderInterface |
86
|
|
|
{ |
87
|
|
|
return $this->addColumn($attribute, DataColumn::class, $options); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param array $options |
92
|
|
|
* @return $this |
93
|
|
|
*/ |
94
|
|
|
public function addActionColumn(array $options = []): DataGridBuilderInterface |
95
|
|
|
{ |
96
|
|
|
return $this->addColumn('id', ActionColumn::class, $options); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param array $options |
101
|
|
|
* @return $this |
102
|
|
|
*/ |
103
|
|
|
public function addSerialColumn(array $options = []): DataGridBuilderInterface |
104
|
|
|
{ |
105
|
|
|
return $this->addColumn('', SerialColumn::class, $options); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param string $path |
110
|
|
|
* @return $this |
111
|
|
|
*/ |
112
|
|
|
public function setTemplate(string $path): DataGridBuilderInterface |
113
|
|
|
{ |
114
|
|
|
$this->configuration->setTemplate($path); |
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
/** |
118
|
|
|
* @param string $message |
119
|
|
|
* @return $this |
120
|
|
|
*/ |
121
|
|
|
public function setNoDataMessage(string $message): DataGridBuilderInterface |
122
|
|
|
{ |
123
|
|
|
$this->configuration->setNoDataMessage($message); |
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function enablePagination(bool $enabled = true, ?int $limit = null): DataGridBuilderInterface |
128
|
|
|
{ |
129
|
|
|
$this->configuration->setPaginationEnabled($enabled); |
130
|
|
|
if ($limit !== null){ |
131
|
|
|
$this->configuration->setPaginationLimit($limit); |
132
|
|
|
} |
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function setCountFieldName(string $name): DataGridBuilderInterface |
137
|
|
|
{ |
138
|
|
|
$this->provider->setCountFieldName($name); |
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @internal |
144
|
|
|
* @return AbstractColumn[] |
145
|
|
|
*/ |
146
|
|
|
public function getColumns(): array |
147
|
|
|
{ |
148
|
|
|
return $this->columns; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function getProvider(): DataProviderInterface |
152
|
|
|
{ |
153
|
|
|
return $this->provider; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @internal |
158
|
|
|
* @param DataProviderInterface $provider |
159
|
|
|
*/ |
160
|
|
|
public function setProvider(DataProviderInterface $provider): void |
161
|
|
|
{ |
162
|
|
|
$provider->setPager($this->getPager()); |
163
|
|
|
$this->provider = $provider; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param string $attribute |
168
|
|
|
* @param string $direction |
169
|
|
|
*/ |
170
|
|
|
public function setSort(string $attribute, string $direction) |
171
|
|
|
{ |
172
|
|
|
$this->sort = [$attribute, $direction]; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @internal |
177
|
|
|
*/ |
178
|
|
|
public function acquireSort(): void |
179
|
|
|
{ |
180
|
|
|
if (is_array($this->sort) && count($this->sort) === 2) { |
181
|
|
|
$attribute = $this->sort[0]; |
182
|
|
|
$direction = $this->sort[1]; |
183
|
|
|
foreach ($this->columns as $column) { |
184
|
|
|
if ($column instanceof DataColumn && $column->hasSort() && $column->getAttribute() == $attribute) { |
185
|
|
|
$column->setSort($direction); |
186
|
|
|
$this->provider->setSort([$attribute => $direction]); |
187
|
|
|
break; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
|
194
|
|
|
|
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @internal |
198
|
|
|
* @return bool |
199
|
|
|
*/ |
200
|
|
|
public function hasFilters(): bool |
201
|
|
|
{ |
202
|
|
|
return $this->hasFilters; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @internal |
207
|
|
|
* @param array $filters |
208
|
|
|
*/ |
209
|
|
|
public function setFiltersValues(array $filters): void |
210
|
|
|
{ |
211
|
|
|
foreach ($this->columns as $column) { |
212
|
|
|
if ($column instanceof DataColumn && $column->hasFilter() && array_key_exists($column->getAttribute(), $filters)) { |
213
|
|
|
$column->setFilterValue($filters[$column->getAttribute()]); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @internal |
221
|
|
|
* @return Pager |
222
|
|
|
*/ |
223
|
|
|
public function getPager(): Pager |
224
|
|
|
{ |
225
|
|
|
return $this->pager ?? ($this->pager = new Pager()); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @internal |
230
|
|
|
* @return bool |
231
|
|
|
*/ |
232
|
|
|
public function hasPagination(): bool |
233
|
|
|
{ |
234
|
|
|
return $this->getPager()->isEnabled() && is_integer($this->getPager()->getLimit()); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @return Configuration |
239
|
|
|
* @internal |
240
|
|
|
*/ |
241
|
|
|
public function getConfiguration(): ConfigurationInterface |
242
|
|
|
{ |
243
|
|
|
return $this->configuration; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @internal |
248
|
|
|
* @return string |
249
|
|
|
*/ |
250
|
|
|
public function getInstance(): string { |
251
|
|
|
return $this->instance; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @param string $name |
256
|
|
|
* @return DataGridBuilderInterface |
257
|
|
|
*/ |
258
|
|
|
public function setInstance(string $name): DataGridBuilderInterface { |
259
|
|
|
$this->instance = $name; |
260
|
|
|
return $this; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
public function setTranslationDomain(string $domain): DataGridBuilderInterface |
264
|
|
|
{ |
265
|
|
|
$this->configuration->setTranslationDomain($domain); |
266
|
|
|
return $this; |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|