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
|
|
|
public function __construct( |
68
|
|
|
FormFactory $formFactory, |
69
|
|
|
AuthorizationCheckerInterface $checker, |
70
|
|
|
Router $router, |
71
|
|
|
FormatterInterface $formatter, |
72
|
|
|
DataTableLayoutInterface $layout = null |
73
|
|
|
) { |
74
|
|
|
$this->formFactory = $formFactory; |
75
|
|
|
$this->router = $router; |
76
|
|
|
$this->formatter = $formatter; |
|
|
|
|
77
|
|
|
$this->authorizationChecker = $checker; |
|
|
|
|
78
|
|
|
$this->layout = null === $layout ? new Bootstrap() : $layout; |
79
|
|
|
$this->optionsResolver = new OptionsResolver(); |
80
|
|
|
//$this->initColumnsDefinitions(); |
81
|
|
|
$this->setDefaultOptions($this->optionsResolver); |
82
|
|
|
$this->configureOptions($this->optionsResolver); |
83
|
|
|
$this->options = $this->optionsResolver->resolve(); |
84
|
|
|
} |
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
|
|
|
private final function setDefaultOptions(OptionsResolver $resolver) |
|
|
|
|
126
|
|
|
{ |
127
|
|
|
$resolver->setDefaults([ |
128
|
|
|
'layout' => $this->layout, |
129
|
|
|
'client_side_filtering' => false, |
130
|
|
|
'filter_reload_table_on_change' => false, |
131
|
|
|
'no_init_loading' => false, |
132
|
|
|
'template' => 'CrossKnowledgeDataTableBundle::default_table.html.twig', |
133
|
|
|
'data_table_custom_options' => [], |
134
|
|
|
'has_filter_form' => function() { |
|
|
|
|
135
|
|
|
return $this->getFilterForm()->count()>1; |
136
|
|
|
} |
137
|
|
|
]); |
138
|
|
|
} |
139
|
|
|
/** |
140
|
|
|
* Configure the table options |
141
|
|
|
* |
142
|
|
|
* @param OptionsResolver $resolver |
143
|
|
|
*/ |
144
|
|
|
public function configureOptions(OptionsResolver $resolver){} |
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return array |
147
|
|
|
*/ |
148
|
|
|
public function setOptions(array $options) |
149
|
|
|
{ |
150
|
|
|
$this->options = $this->optionsResolver->resolve(array_merge($this->options, $options)); |
151
|
|
|
} |
152
|
|
|
/** |
153
|
|
|
* @return array |
154
|
|
|
*/ |
155
|
|
|
public function getOptions() |
156
|
|
|
{ |
157
|
|
|
return $this->options; |
158
|
|
|
} |
159
|
|
|
/** |
160
|
|
|
* Build the filter form |
161
|
|
|
* |
162
|
|
|
* @param FormBuilder $builder |
163
|
|
|
* |
164
|
|
|
* @return FormBuilder |
165
|
|
|
*/ |
166
|
|
|
public function buildFilterForm(FormBuilder $builder) |
167
|
|
|
{ |
168
|
|
|
return $builder; |
169
|
|
|
} |
170
|
|
|
/** |
171
|
|
|
* @return string |
172
|
|
|
*/ |
173
|
|
|
public function getAjaxAdditionalParameters() |
174
|
|
|
{ |
175
|
|
|
return []; |
176
|
|
|
} |
177
|
|
|
/** |
178
|
|
|
* @return string[] should return the content to insert in the rows key(colname) => value(string / html / any) |
179
|
|
|
*/ |
180
|
|
|
public function getOutputRows($context = self::VIEW_CONTEXT) |
181
|
|
|
{ |
182
|
|
|
$t = []; |
183
|
|
|
foreach ($this->getDataIterator() as $item) { |
184
|
|
|
$formatted = $this->formatter->formatRow($item, $this, $context); |
|
|
|
|
185
|
|
|
$t[] = $formatted; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return $t; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @see getColumns() same as getColumns but filtered for datatable JS API |
193
|
|
|
*/ |
194
|
|
|
public function getClientSideColumns($context = self::VIEW_CONTEXT) |
195
|
|
|
{ |
196
|
|
|
$columns = $this->getColumns($context); |
197
|
|
|
$clientSideCols = []; |
198
|
|
|
foreach ($columns as $colid=>$column) { |
|
|
|
|
199
|
|
|
$clientSideCols[$colid] = $column->getClientSideDefinition(); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
return $clientSideCols; |
203
|
|
|
} |
204
|
|
|
/** |
205
|
|
|
* @param Request $request |
206
|
|
|
*/ |
207
|
|
|
public function handleRequest(Request $request) |
208
|
|
|
{ |
209
|
|
|
$this->currentRequest = PaginateRequest::fromHttpRequest($request, $this); |
210
|
|
|
} |
211
|
|
|
/** |
212
|
|
|
* @return PaginateRequest |
213
|
|
|
*/ |
214
|
|
|
public function getCurrentRequest() |
215
|
|
|
{ |
216
|
|
|
return $this->currentRequest; |
217
|
|
|
} |
218
|
|
|
/** |
219
|
|
|
* @return Form|\Symfony\Component\Form\Form |
220
|
|
|
*/ |
221
|
|
|
public function getFilterForm() |
222
|
|
|
{ |
223
|
|
|
if (null===$this->filterForm) { |
224
|
|
|
$this->filterForm = $this->buildFilterForm( |
|
|
|
|
225
|
|
|
$this->formFactory->createNamedBuilder($this->getTableId().'_filter') |
|
|
|
|
226
|
|
|
->add('dofilter', 'button') |
227
|
|
|
)->getForm(); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
return $this->filterForm; |
231
|
|
|
} |
232
|
|
|
/** |
233
|
|
|
* Sets the formatter |
234
|
|
|
* |
235
|
|
|
* @param FormatterInterface $formatter |
236
|
|
|
* |
237
|
|
|
* @return void |
238
|
|
|
*/ |
239
|
|
|
public function setFormatter(FormatterInterface $formatter) |
240
|
|
|
{ |
241
|
|
|
$this->formatter = $formatter; |
242
|
|
|
} |
243
|
|
|
/** |
244
|
|
|
* @return string a table idenfitier that will be used for ajax requests |
245
|
|
|
*/ |
246
|
|
|
public final function getTableId() |
|
|
|
|
247
|
|
|
{ |
248
|
|
|
return $this->tableId; |
|
|
|
|
249
|
|
|
} |
250
|
|
|
/** |
251
|
|
|
* @return \CrossKnowledge\DataTableBundle\Table\Element\Column[] |
252
|
|
|
*/ |
253
|
|
|
public function getColumns($context = self::VIEW_CONTEXT) |
254
|
|
|
{ |
255
|
|
|
if(!array_key_exists($context, $this->columns)) { |
|
|
|
|
256
|
|
|
$this->initColumnsDefinitions($context); |
257
|
|
|
} |
258
|
|
|
return $this->columns[$context]; |
259
|
|
|
} |
260
|
|
|
/** |
261
|
|
|
* Builds the columns definition |
262
|
|
|
*/ |
263
|
|
|
protected function initColumnsDefinitions($context = self::VIEW_CONTEXT) |
264
|
|
|
{ |
265
|
|
|
$builder = new ColumnBuilder(); |
266
|
|
|
|
267
|
|
|
$this->buildColumns($builder, $context); |
268
|
|
|
|
269
|
|
|
$this->columns[$context] = $builder->getColumns(); |
270
|
|
|
$this->columnsInitialized[$context] = true; |
271
|
|
|
} |
272
|
|
|
/** |
273
|
|
|
* Sets the table identifier |
274
|
|
|
* |
275
|
|
|
* @return null |
276
|
|
|
*/ |
277
|
|
|
public final function setTableId($id) |
|
|
|
|
278
|
|
|
{ |
279
|
|
|
$this->tableId = $id; |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
|
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: