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
|
|
|
/** |
47
|
|
|
* DataGridBuilder constructor. |
48
|
|
|
* @param DataGridServiceContainer $container |
49
|
|
|
*/ |
50
|
|
|
public function __construct(DataGridServiceContainer $container) |
51
|
|
|
{ |
52
|
|
|
$this->container = $container; |
53
|
|
|
$this->configuration = new Configuration(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $attribute |
58
|
|
|
* @param string $columnClass |
59
|
|
|
* @param array $options |
60
|
|
|
* @return $this |
61
|
|
|
*/ |
62
|
|
|
public function addColumn(string $attribute, string $columnClass = DataColumn::class, array $options = []): DataGridBuilderInterface |
63
|
|
|
{ |
64
|
|
|
if (!is_subclass_of($columnClass, AbstractColumn::class)) { |
65
|
|
|
throw new InvalidArgumentException('Expected subclass of' . AbstractColumn::class); |
66
|
|
|
} |
67
|
|
|
/** |
68
|
|
|
* @var AbstractColumn $column |
69
|
|
|
*/ |
70
|
|
|
$column = new $columnClass($this->container, array_merge($options, ['attribute' => $attribute])); |
71
|
|
|
$this->columns[] = $column; |
72
|
|
|
if ($column->hasFilter() && $column->isVisible()) { |
73
|
|
|
$this->hasFilters = true; |
74
|
|
|
} |
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $attribute |
80
|
|
|
* @param array $options |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
|
|
public function addDataColumn(string $attribute, array $options = []): DataGridBuilderInterface |
84
|
|
|
{ |
85
|
|
|
return $this->addColumn($attribute, DataColumn::class, $options); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param array $options |
90
|
|
|
* @return $this |
91
|
|
|
*/ |
92
|
|
|
public function addActionColumn(array $options = []): DataGridBuilderInterface |
93
|
|
|
{ |
94
|
|
|
return $this->addColumn('id', ActionColumn::class, $options); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param array $options |
99
|
|
|
* @return $this |
100
|
|
|
*/ |
101
|
|
|
public function addSerialColumn(array $options = []): DataGridBuilderInterface |
102
|
|
|
{ |
103
|
|
|
return $this->addColumn('', SerialColumn::class, $options); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $path |
108
|
|
|
* @return $this |
109
|
|
|
*/ |
110
|
|
|
public function setTemplate(string $path): DataGridBuilderInterface |
111
|
|
|
{ |
112
|
|
|
$this->configuration->setTemplate($path); |
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
/** |
116
|
|
|
* @param string $message |
117
|
|
|
* @return $this |
118
|
|
|
*/ |
119
|
|
|
public function setNoDataMessage(string $message): DataGridBuilderInterface |
120
|
|
|
{ |
121
|
|
|
$this->configuration->setNoDataMessage($message); |
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function enablePagination(bool $enabled = true, ?int $limit = null): DataGridBuilderInterface |
126
|
|
|
{ |
127
|
|
|
$this->configuration->setPaginationEnabled($enabled); |
128
|
|
|
if ($limit !== null){ |
129
|
|
|
$this->configuration->setPaginationLimit($limit); |
130
|
|
|
} |
131
|
|
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function setCountFieldName(string $name): DataGridBuilderInterface |
135
|
|
|
{ |
136
|
|
|
$this->provider->setCountFieldName($name); |
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @internal |
142
|
|
|
* @return AbstractColumn[] |
143
|
|
|
*/ |
144
|
|
|
public function getColumns(): array |
145
|
|
|
{ |
146
|
|
|
return $this->columns; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function getProvider(): DataProviderInterface |
150
|
|
|
{ |
151
|
|
|
return $this->provider; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @internal |
156
|
|
|
* @param DataProviderInterface $provider |
157
|
|
|
*/ |
158
|
|
|
public function setProvider(DataProviderInterface $provider): void |
159
|
|
|
{ |
160
|
|
|
$provider->setPager($this->getPager()); |
161
|
|
|
$this->provider = $provider; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function setSort(string $attribute, string $direction) |
165
|
|
|
{ |
166
|
|
|
foreach ($this->columns as $column) { |
167
|
|
|
if ($column instanceof DataColumn && $column->hasSort() && $column->getAttribute() == $attribute) { |
168
|
|
|
$column->setSort($direction); |
169
|
|
|
$this->provider->setSort([$attribute => $direction]); |
170
|
|
|
break; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @internal |
177
|
|
|
* @return bool |
178
|
|
|
*/ |
179
|
|
|
public function hasFilters(): bool |
180
|
|
|
{ |
181
|
|
|
return $this->hasFilters; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @internal |
186
|
|
|
* @param array $filters |
187
|
|
|
*/ |
188
|
|
|
public function setFiltersValues(array $filters): void |
189
|
|
|
{ |
190
|
|
|
foreach ($this->columns as $column) { |
191
|
|
|
if ($column instanceof DataColumn && $column->hasFilter() && array_key_exists($column->getAttribute(), $filters)) { |
192
|
|
|
$column->setFilterValue($filters[$column->getAttribute()]); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @internal |
200
|
|
|
* @return Pager |
201
|
|
|
*/ |
202
|
|
|
public function getPager(): Pager |
203
|
|
|
{ |
204
|
|
|
return $this->pager ?? ($this->pager = new Pager()); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @internal |
209
|
|
|
* @return bool |
210
|
|
|
*/ |
211
|
|
|
public function hasPagination(): bool |
212
|
|
|
{ |
213
|
|
|
return $this->getPager()->isEnabled() && is_integer($this->getPager()->getLimit()); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @return Configuration |
218
|
|
|
* @internal |
219
|
|
|
*/ |
220
|
|
|
public function getConfiguration(): ConfigurationInterface |
221
|
|
|
{ |
222
|
|
|
return $this->configuration; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @internal |
227
|
|
|
* @return string |
228
|
|
|
*/ |
229
|
|
|
public function getInstance(): string { |
230
|
|
|
return $this->instance; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @param string $name |
235
|
|
|
* @return DataGridBuilderInterface |
236
|
|
|
*/ |
237
|
|
|
public function setInstance(string $name): DataGridBuilderInterface { |
238
|
|
|
$this->instance = $name; |
239
|
|
|
return $this; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
public function setTranslationDomain(string $domain): DataGridBuilderInterface |
243
|
|
|
{ |
244
|
|
|
$this->configuration->setTranslationDomain($domain); |
245
|
|
|
return $this; |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|