This repository was archived by the owner on Feb 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 239
Expand file tree
/
Copy pathAbstractDatatable.php
More file actions
349 lines (300 loc) · 7.37 KB
/
Copy pathAbstractDatatable.php
File metadata and controls
349 lines (300 loc) · 7.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
/*
* This file is part of the SgDatatablesBundle package.
*
* (c) stwe <https://github.com/stwe/DatatablesBundle>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sg\DatatablesBundle\Datatable;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Sg\DatatablesBundle\Datatable\Column\ColumnBuilder;
use Sg\DatatablesBundle\Response\DatatableQueryBuilder;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessor;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Translation\TranslatorInterface;
use Twig_Environment;
abstract class AbstractDatatable implements DatatableInterface
{
/**
* The AuthorizationChecker service.
*
* @var AuthorizationCheckerInterface
*/
protected $authorizationChecker;
/**
* The SecurityTokenStorage service.
*
* @var TokenStorageInterface
*/
protected $securityToken;
/**
* The Translator service.
*
* @var TranslatorInterface
*/
protected $translator;
/**
* The Router service.
*
* @var RouterInterface
*/
protected $router;
/**
* The doctrine orm entity manager service.
*
* @var EntityManagerInterface
*/
protected $em;
/**
* The Twig Environment.
*
* @var Twig_Environment
*/
protected $twig;
/**
* A ColumnBuilder instance.
*
* @var ColumnBuilder
*/
protected $columnBuilder;
/**
* An Ajax instance.
*
* @var Ajax
*/
protected $ajax;
/**
* An Options instance.
*
* @var Options
*/
protected $options;
/**
* A Features instance.
*
* @var Features
*/
protected $features;
/**
* A Callbacks instance.
*
* @var Callbacks
*/
protected $callbacks;
/**
* A Events instance.
*
* @var Events
*/
protected $events;
/**
* An Extensions instance.
*
* @var Extensions
*/
protected $extensions;
/**
* A Language instance.
*
* @var Language
*/
protected $language;
/**
* The unique id for this instance.
*
* @var int
*/
protected $uniqueId;
/**
* The PropertyAccessor.
*
* @var PropertyAccessor
*/
protected $accessor;
//-------------------------------------------------
/**
* Used to generate unique names.
*
* @var array
*/
protected static $uniqueCounter = [];
/**
* @throws Exception
*/
public function __construct(
AuthorizationCheckerInterface $authorizationChecker,
TokenStorageInterface $securityToken,
TranslatorInterface $translator,
RouterInterface $router,
EntityManagerInterface $em,
Twig_Environment $twig
) {
$this->validateName();
if (isset(self::$uniqueCounter[$this->getName()])) {
$this->uniqueId = ++self::$uniqueCounter[$this->getName()];
} else {
$this->uniqueId = self::$uniqueCounter[$this->getName()] = 1;
}
$this->authorizationChecker = $authorizationChecker;
$this->securityToken = $securityToken;
$this->translator = $translator;
$this->router = $router;
$this->em = $em;
$this->twig = $twig;
$metadata = $em->getClassMetadata($this->getEntity());
$this->columnBuilder = new ColumnBuilder($metadata, $twig, $router, $this->getName(), $em);
$this->ajax = new Ajax();
$this->options = new Options();
$this->features = new Features();
$this->callbacks = new Callbacks();
$this->events = new Events();
$this->extensions = new Extensions();
$this->language = new Language();
$this->accessor = PropertyAccess::createPropertyAccessor();
}
//-------------------------------------------------
// DatatableInterface
//-------------------------------------------------
/**
* {@inheritdoc}
*/
public function getLineFormatter()
{
return null;
}
/**
* {@inheritdoc}
*/
public function getColumnBuilder()
{
return $this->columnBuilder;
}
/**
* {@inheritdoc}
*/
public function getAjax()
{
return $this->ajax;
}
/**
* {@inheritdoc}
*/
public function getOptions()
{
return $this->options;
}
/**
* {@inheritdoc}
*/
public function getFeatures()
{
return $this->features;
}
/**
* {@inheritdoc}
*/
public function getCallbacks()
{
return $this->callbacks;
}
/**
* {@inheritdoc}
*/
public function getEvents()
{
return $this->events;
}
/**
* {@inheritdoc}
*/
public function getExtensions()
{
return $this->extensions;
}
/**
* {@inheritdoc}
*/
public function getLanguage()
{
return $this->language;
}
/**
* {@inheritdoc}
*/
public function getEntityManager()
{
return $this->em;
}
/**
* {@inheritdoc}
*/
public function getOptionsArrayFromEntities($entities, $keyFrom = 'id', $valueFrom = 'name')
{
$options = [];
foreach ($entities as $entity) {
if (true === $this->accessor->isReadable($entity, $keyFrom) && true === $this->accessor->isReadable($entity, $valueFrom)) {
$options[$this->accessor->getValue($entity, $keyFrom)] = $this->accessor->getValue($entity, $valueFrom);
}
}
return $options;
}
/**
* {@inheritdoc}
*/
public function getUniqueId()
{
return $this->uniqueId;
}
/**
* {@inheritdoc}
*/
public function getUniqueName()
{
return $this->getName().($this->getUniqueId() > 1 ? '-'.$this->getUniqueId() : '');
}
/**
* {@inheritdoc}
*/
public function getDisplayTotals()
{
foreach ($this->getColumnBuilder()->getColumns() as $column) {
if ($column->getComputeTotal()) {
return true;
}
}
return false;
}
/**
* {@inheritdoc}
*/
public function computeTotals()
{
if ($this->getDisplayTotals()) {
$datatableQueryBuilder = new DatatableQueryBuilder(array(), $this);
$totals = $datatableQueryBuilder->getComputedTotals();
foreach ($totals as $key => $value) {
$this->getColumnBuilder()->setColumnTotal($key, $value);
}
}
}
//-------------------------------------------------
// Private
//-------------------------------------------------
/**
* Checks the name only contains letters, numbers, underscores or dashes.
*
* @throws Exception
*/
private function validateName()
{
if (1 !== preg_match(self::NAME_REGEX, $this->getName())) {
throw new Exception('AbstractDatatable::validateName(): The result of the getName method can only contain letters, numbers, underscore and dashes.');
}
}
}