|
1
|
|
|
<?php |
|
2
|
|
|
namespace CrossKnowledge\DataTableBundle\DataTable\Table; |
|
3
|
|
|
|
|
4
|
|
|
use CrossKnowledge\DataTableBundle\DataTable\ColumnBuilder; |
|
5
|
|
|
use CrossKnowledge\DataTableBundle\DataTable\Formatter\FormatterInterface; |
|
6
|
|
|
use CrossKnowledge\DataTableBundle\DataTable\Table\Layout\Bootstrap; |
|
7
|
|
|
use CrossKnowledge\DataTableBundle\DataTable\Table\Layout\DataTableLayoutInterface; |
|
8
|
|
|
use CrossKnowledge\DataTableBundle\Table\Element\Column; |
|
9
|
|
|
use Symfony\Component\Form\FormBuilder; |
|
10
|
|
|
use Symfony\Component\Form\FormFactory; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
12
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
13
|
|
|
use Symfony\Component\Routing\Router; |
|
14
|
|
|
use CrossKnowledge\DataTableBundle\DataTable\Request\PaginateRequest; |
|
15
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker; |
|
16
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
|
17
|
|
|
|
|
18
|
|
|
abstract class AbstractTable |
|
19
|
|
|
{ |
|
20
|
|
|
const VIEW_CONTEXT = 'view'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var Router |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $router; |
|
26
|
|
|
/** |
|
27
|
|
|
* @var PaginateRequest |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $currentRequest; |
|
30
|
|
|
/** |
|
31
|
|
|
* @var FormFactory |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $formFactory; |
|
34
|
|
|
/** |
|
35
|
|
|
* @var Form |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $filterForm; |
|
38
|
|
|
/** |
|
39
|
|
|
* @var AuthorizationChecker |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $authorizationChecker; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var Column[] |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $columns = array(); |
|
47
|
|
|
protected $columnsInitialized = array(); |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var OptionsResolver |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $optionsResolver; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var array Key value array of options |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $options = []; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var DataTableLayoutInterface |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $layout; |
|
63
|
|
|
/** |
|
64
|
|
|
* @param FormFactory $formFactory |
|
65
|
|
|
* @param Router $router |
|
66
|
|
|
*/ |
|
67
|
4 |
|
public function __construct( |
|
68
|
|
|
FormFactory $formFactory, |
|
69
|
|
|
AuthorizationCheckerInterface $checker, |
|
70
|
|
|
Router $router, |
|
71
|
|
|
FormatterInterface $formatter, |
|
72
|
|
|
DataTableLayoutInterface $layout = null |
|
73
|
|
|
) { |
|
74
|
4 |
|
$this->formFactory = $formFactory; |
|
75
|
4 |
|
$this->router = $router; |
|
76
|
4 |
|
$this->formatter = $formatter; |
|
|
|
|
|
|
77
|
4 |
|
$this->authorizationChecker = $checker; |
|
|
|
|
|
|
78
|
4 |
|
$this->layout = null === $layout ? new Bootstrap() : $layout; |
|
79
|
4 |
|
$this->optionsResolver = new OptionsResolver(); |
|
80
|
|
|
//$this->initColumnsDefinitions(); |
|
81
|
4 |
|
$this->setDefaultOptions($this->optionsResolver); |
|
82
|
4 |
|
$this->configureOptions($this->optionsResolver); |
|
83
|
4 |
|
$this->options = $this->optionsResolver->resolve(); |
|
84
|
4 |
|
} |
|
85
|
|
|
/** |
|
86
|
|
|
* |
|
87
|
|
|
* Example implementation |
|
88
|
|
|
|
|
89
|
|
|
public function buildColumns(ColumnBuilder $builder) |
|
90
|
|
|
{ |
|
91
|
|
|
$builder->add('Learner.FirstName', new Column('First name title', ['width' => '20%'])) |
|
92
|
|
|
->add('Learner.Name', new Column('Last name')); |
|
93
|
|
|
} |
|
94
|
|
|
* |
|
95
|
|
|
* @return array key must be the column field name, |
|
96
|
|
|
* value must be an array of options for https://datatables.net/reference/option/columns |
|
97
|
|
|
*/ |
|
98
|
|
|
abstract public function buildColumns(ColumnBuilder $builder, $context = self::VIEW_CONTEXT); |
|
99
|
|
|
/** |
|
100
|
|
|
* Must return a \Traversable a traversable element that must contain for each element an ArrayAccess such as |
|
101
|
|
|
* key(colname) => value(db value) |
|
102
|
|
|
* |
|
103
|
|
|
* The filter should be used there. |
|
104
|
|
|
* |
|
105
|
|
|
* Example of the expected return |
|
106
|
|
|
return [ |
|
107
|
|
|
'first_name' => 'John', |
|
108
|
|
|
'last_name' => 'Doe' |
|
109
|
|
|
]; |
|
110
|
|
|
* Example: |
|
111
|
|
|
return new \PropelCollection(); |
|
112
|
|
|
* |
|
113
|
|
|
* @return \Traversable |
|
114
|
|
|
*/ |
|
115
|
|
|
abstract public function getDataIterator(); |
|
116
|
|
|
/** |
|
117
|
|
|
* @return int the total number of rows regardless of filters |
|
118
|
|
|
*/ |
|
119
|
|
|
abstract public function getUnfilteredCount(); |
|
120
|
|
|
/** |
|
121
|
|
|
* @return int|false if there is no such count |
|
122
|
|
|
*/ |
|
123
|
|
|
abstract public function getFilteredCount(); |
|
124
|
|
|
|
|
125
|
4 |
|
private final function setDefaultOptions(OptionsResolver $resolver) |
|
|
|
|
|
|
126
|
|
|
{ |
|
127
|
4 |
|
$resolver->setDefaults([ |
|
128
|
4 |
|
'layout' => $this->layout, |
|
129
|
4 |
|
'client_side_filtering' => false, |
|
130
|
4 |
|
'filter_reload_table_on_change' => false, |
|
131
|
4 |
|
'template' => 'CrossKnowledgeDataTableBundle::default_table.html.twig', |
|
132
|
4 |
|
'data_table_custom_options' => [], |
|
133
|
4 |
|
'has_filter_form' => function() { |
|
|
|
|
|
|
134
|
|
|
return $this->getFilterForm()->count()>1; |
|
135
|
|
|
} |
|
136
|
4 |
|
]); |
|
137
|
4 |
|
} |
|
138
|
|
|
/** |
|
139
|
|
|
* Configure the table options |
|
140
|
|
|
* |
|
141
|
|
|
* @param OptionsResolver $resolver |
|
142
|
|
|
*/ |
|
143
|
4 |
|
public function configureOptions(OptionsResolver $resolver){} |
|
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @return array |
|
146
|
|
|
*/ |
|
147
|
2 |
|
public function setOptions(array $options) |
|
148
|
|
|
{ |
|
149
|
2 |
|
$this->options = $this->optionsResolver->resolve(array_merge($this->options, $options)); |
|
150
|
2 |
|
} |
|
151
|
|
|
/** |
|
152
|
|
|
* @return array |
|
153
|
|
|
*/ |
|
154
|
2 |
|
public function getOptions() |
|
155
|
|
|
{ |
|
156
|
2 |
|
return $this->options; |
|
157
|
|
|
} |
|
158
|
|
|
/** |
|
159
|
|
|
* Build the filter form |
|
160
|
|
|
* |
|
161
|
|
|
* @param FormBuilder $builder |
|
162
|
|
|
* |
|
163
|
|
|
* @return FormBuilder |
|
164
|
|
|
*/ |
|
165
|
|
|
public function buildFilterForm(FormBuilder $builder) |
|
166
|
|
|
{ |
|
167
|
|
|
return $builder; |
|
168
|
|
|
} |
|
169
|
|
|
/** |
|
170
|
|
|
* @return string |
|
171
|
|
|
*/ |
|
172
|
|
|
public function getAjaxAdditionalParameters() |
|
173
|
|
|
{ |
|
174
|
|
|
return []; |
|
175
|
|
|
} |
|
176
|
|
|
/** |
|
177
|
|
|
* @return string[] should return the content to insert in the rows key(colname) => value(string / html / any) |
|
178
|
|
|
*/ |
|
179
|
3 |
|
public function getOutputRows($context = self::VIEW_CONTEXT) |
|
180
|
|
|
{ |
|
181
|
3 |
|
$t = []; |
|
182
|
3 |
|
foreach ($this->getDataIterator() as $item) { |
|
183
|
3 |
|
$formatted = $this->formatter->formatRow($item, $this, $context); |
|
|
|
|
|
|
184
|
3 |
|
$t[] = $formatted; |
|
185
|
3 |
|
} |
|
186
|
|
|
|
|
187
|
3 |
|
return $t; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* @see getColumns() same as getColumns but filtered for datatable JS API |
|
192
|
|
|
*/ |
|
193
|
2 |
|
public function getClientSideColumns($context = self::VIEW_CONTEXT) |
|
194
|
|
|
{ |
|
195
|
2 |
|
$columns = $this->getColumns($context); |
|
196
|
2 |
|
$clientSideCols = []; |
|
197
|
2 |
|
foreach ($columns as $colid=>$column) { |
|
|
|
|
|
|
198
|
2 |
|
$clientSideCols[$colid] = $column->getClientSideDefinition(); |
|
199
|
2 |
|
} |
|
200
|
|
|
|
|
201
|
2 |
|
return $clientSideCols; |
|
202
|
|
|
} |
|
203
|
|
|
/** |
|
204
|
|
|
* @param Request $request |
|
205
|
|
|
*/ |
|
206
|
1 |
|
public function handleRequest(Request $request) |
|
207
|
|
|
{ |
|
208
|
1 |
|
$this->currentRequest = PaginateRequest::fromHttpRequest($request, $this); |
|
209
|
1 |
|
} |
|
210
|
|
|
/** |
|
211
|
|
|
* @return PaginateRequest |
|
212
|
|
|
*/ |
|
213
|
1 |
|
public function getCurrentRequest() |
|
214
|
|
|
{ |
|
215
|
1 |
|
return $this->currentRequest; |
|
216
|
|
|
} |
|
217
|
|
|
/** |
|
218
|
|
|
* @return Form|\Symfony\Component\Form\Form |
|
219
|
|
|
*/ |
|
220
|
|
|
public function getFilterForm() |
|
221
|
|
|
{ |
|
222
|
|
|
if (null===$this->filterForm) { |
|
223
|
|
|
$this->filterForm = $this->buildFilterForm( |
|
|
|
|
|
|
224
|
|
|
$this->formFactory->createNamedBuilder($this->getTableId().'_filter') |
|
|
|
|
|
|
225
|
|
|
->add('dofilter', 'button') |
|
226
|
|
|
)->getForm(); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
return $this->filterForm; |
|
230
|
|
|
} |
|
231
|
|
|
/** |
|
232
|
|
|
* @return array key value of variables accessible for renderers. |
|
233
|
|
|
*/ |
|
234
|
2 |
|
public function buildView() |
|
235
|
|
|
{ |
|
236
|
|
|
$viewParameters = [ |
|
237
|
2 |
|
'columns' => $this->getClientSideColumns(), |
|
238
|
2 |
|
'data' => $this->getOutputRows(), |
|
239
|
2 |
|
'datatable' => $this, |
|
240
|
2 |
|
'unfilteredRowsCount' => $this->getUnfilteredCount(), |
|
241
|
2 |
|
'filteredRowsCount' => $this->getFilteredCount(), |
|
242
|
2 |
|
]; |
|
243
|
|
|
|
|
244
|
2 |
|
if ($this->getOptions()['has_filter_form']) { |
|
245
|
2 |
|
$viewParameters['filterForm'] = $this->getFilterForm()->createView(); |
|
246
|
2 |
|
} |
|
247
|
|
|
|
|
248
|
2 |
|
return $viewParameters; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* Sets the formatter |
|
253
|
|
|
* |
|
254
|
|
|
* @param FormatterInterface $formatter |
|
255
|
|
|
* |
|
256
|
|
|
* @return void |
|
257
|
|
|
*/ |
|
258
|
|
|
public function setFormatter(FormatterInterface $formatter) |
|
259
|
|
|
{ |
|
260
|
|
|
$this->formatter = $formatter; |
|
261
|
|
|
} |
|
262
|
|
|
/** |
|
263
|
|
|
* @return string a table idenfitier that will be used for ajax requests |
|
264
|
|
|
*/ |
|
265
|
|
|
public final function getTableId() |
|
|
|
|
|
|
266
|
|
|
{ |
|
267
|
|
|
return $this->tableId; |
|
|
|
|
|
|
268
|
|
|
} |
|
269
|
|
|
/** |
|
270
|
|
|
* @return \CrossKnowledge\DataTableBundle\Table\Element\Column[] |
|
271
|
|
|
*/ |
|
272
|
|
|
public function getColumns($context = self::VIEW_CONTEXT) |
|
273
|
|
|
{ |
|
274
|
|
|
if(!array_key_exists($context, $this->columns)) { |
|
|
|
|
|
|
275
|
|
|
$this->initColumnsDefinitions($context); |
|
276
|
|
|
} |
|
277
|
|
|
return $this->columns[$context]; |
|
278
|
|
|
} |
|
279
|
|
|
/** |
|
280
|
|
|
* Builds the columns definition |
|
281
|
|
|
*/ |
|
282
|
|
|
protected function initColumnsDefinitions($context = self::VIEW_CONTEXT) |
|
283
|
|
|
{ |
|
284
|
|
|
$builder = new ColumnBuilder(); |
|
285
|
|
|
|
|
286
|
|
|
$this->buildColumns($builder, $context); |
|
287
|
|
|
|
|
288
|
|
|
$this->columns[$context] = $builder->getColumns(); |
|
289
|
|
|
$this->columnsInitialized[$context] = true; |
|
290
|
|
|
} |
|
291
|
|
|
/** |
|
292
|
|
|
* Sets the table identifier |
|
293
|
|
|
* |
|
294
|
|
|
* @return null |
|
295
|
|
|
*/ |
|
296
|
4 |
|
public final function setTableId($id) |
|
|
|
|
|
|
297
|
|
|
{ |
|
298
|
4 |
|
$this->tableId = $id; |
|
299
|
4 |
|
} |
|
300
|
|
|
} |
|
301
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: