-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTable.php
More file actions
executable file
·532 lines (454 loc) · 12.6 KB
/
Copy pathTable.php
File metadata and controls
executable file
·532 lines (454 loc) · 12.6 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
<?php
namespace NeuroSYS\DoctrineDatatables;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\Tools\Pagination\Paginator;
use NeuroSYS\DoctrineDatatables\Field\Entity;
use NeuroSYS\DoctrineDatatables\Field\AbstractField;
use NeuroSYS\DoctrineDatatables\Renderer\PhpRenderer;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessor;
class Table extends Entity
{
const HYDRATE_ARRAY = 'array';
const HYDRATE_ENTITY = 'entity';
/**
* @var EntityManager
*/
private $em;
/**
* @var int
*/
private $maxResults;
/**
* @var int
*/
private $firstResult;
/**
* @var Request
*/
private $request;
/**
* @var QueryBuilder
*/
private $queryBuilder;
/**
* @var array
*/
private $hints;
/**
* @var array
*/
private $orders = array();
/**
* @var RendererInterface
*/
private $renderer;
/**
* @var Entity[]
*/
private $entities = array();
private $basePropertyPath;
/**
* @param Table $name
* @param array $alias
* @param EntityManager $em
* @param Request $request
* @param RendererInterface|null $renderer
*
* @throws \Exception
*/
public function __construct($name, $alias, EntityManager $em, Request $request, RendererInterface $renderer = null)
{
if (!$alias) {
throw new \Exception("Alias is required");
}
parent::__construct($this, $name, $alias);
if (!$renderer) {
$renderer = new PhpRenderer();
}
$this->entities[$alias] = $this;
$this->em = $em;
$this->request = $request;
$this->setRenderer($renderer);
$this->setMaxResults($request->get('iDisplayLength'));
$this->setFirstResult($request->get('iDisplayStart'));
$this->hints = array();
}
public function addEntity(Entity $entity)
{
$this->entities[$entity->getAlias()] = $entity;
}
/**
* @param $alias
* @return Entity|null
*/
public function getEntity($alias)
{
if ($this->getAlias() == $alias) {
return $this;
}
if (isset($this->entities[$alias])) {
return $this->entities[$alias];
}
return null;
}
public function setRenderer(RendererInterface $renderer = null)
{
$this->renderer = $renderer;
}
/**
* @param array[] $orders
*/
public function setOrders(array $orders)
{
$this->orders = $orders;
return $this;
}
/**
* @param $maxResults
* @return $this
*/
public function setMaxResults($maxResults)
{
$this->maxResults = $maxResults;
return $this;
}
/**
* @return int
*/
public function getMaxResults()
{
return $this->maxResults;
}
/**
* @param $firstResult
* @return $this
*/
public function setFirstResult($firstResult)
{
$this->firstResult = $firstResult;
return $this;
}
/**
* @return int
*/
public function getFirstResult()
{
return $this->firstResult;
}
/**
* @param $name Hint name
* @param $value Hint value
* @return $this
*/
public function setHint($name, $value)
{
$this->hints[$name] = $value;
return $this;
}
/**
*
*/
public function getHints()
{
return $this->hints;
}
/**
* @param QueryBuilder $qb
* @return $this
*/
public function setQueryBuilder(QueryBuilder $qb)
{
$this->queryBuilder = $qb;
return $this;
}
/**
* @return QueryBuilder Original query builder clone
*/
public function getQueryBuilder()
{
if (!$this->queryBuilder) {
$this->queryBuilder = $this->em->createQueryBuilder();
}
return $this->queryBuilder;
}
/**
* @return RendererInterface
*/
public function getRenderer()
{
return $this->renderer;
}
public function getResult()
{
return $this->getData('entity');
}
public function getArrayResult()
{
return $this->getData('array');
}
public function getData($hydrate = 'array')
{
$query = $this->getResultQueryBuilder()->getQuery();
foreach ($this->getHints() as $name => $value) {
$query->setHint($name, $value);
}
if ($hydrate == 'array') {
$results = $query->getArrayResult();
} else {
$results = $query->getResult();
}
foreach ($results as $i => $result) {
$results[$i] = $this->formatResult($result, $hydrate);
}
return $results;
}
/**
* @param mixed $values
* @return array
*/
public function formatResult($values, $hydration)
{
$accessor = PropertyAccess::createPropertyAccessor();
$results = array();
foreach ($this->getPrimaryKeys() as $key) {
$results[$key] = $this->getValue($values, ($this->basePropertyPath ? $this->basePropertyPath . '.' : '') . $key, $accessor, $hydration);
}
foreach ($this->getFields() as $name => $field) {
if ($context = $field->getContext()) {
$value = $this->getValue($values, $context, $accessor, $hydration);
} else {
$value = array();
$selectPaths = $field->getSelectPaths();
if (count($selectPaths) == 1) {
try {
foreach ($field->getSelectPaths() as $selectPath) {
$value[] = $this->getValue($values, ($this->basePropertyPath ? $this->basePropertyPath . '.' : '') . implode('.', $selectPath), $accessor, $hydration);
}
} catch (\Exception $ex) {
$value = $values;
}
} else {
$value = $values;
}
if (is_array($value) && count($value) == 1) {
$value = $value[0];
}
}
$results[$name] = $field->format($values, $value);
}
return $results;
}
public function getManager()
{
return $this->em;
}
protected function addJoin(QueryBuilder $qb)
{
foreach ($this->entities as $entity) {
if ($entity != $this) {
switch ($entity->getJoinType()) {
case 'LEFT':
$method = "leftJoin";
break;
case 'INNER':
$method = "innerJoin";
break;
default:
throw new \Exception("Invalid join type: " . $entity->getJoinType());
}
$qb->$method($entity->getFullName(), $entity->getAlias(), $entity->getJoinConditionType(), $entity->getJoinCondition());
}
}
return $this;
}
protected function addFilterJoin(QueryBuilder $qb)
{
foreach ($this->entities as $entity) {
if ($entity != $this) {
$qb->leftJoin($entity->getFilterName(), $entity->getFilterAlias());
}
}
return $this;
}
/**
* @return array Field path
*/
public function getPath()
{
// root entity has empty path
return array();
}
protected function getValue($object, $path, $accessor, $hydration)
{
if ($hydration == self::HYDRATE_ARRAY) {
$path = '['.str_replace('.', '][', $path).']';
}
return $accessor->getValue($object, $path);
}
/**
* @return QueryBuilder
*/
public function getResultQueryBuilder()
{
$qb = clone $this->getQueryBuilder();
$this
->select($qb)
->addFrom($qb)
->addJoin($qb)
->addFilter($qb)
->limit($qb)
->offset($qb)
->addOrder($qb);
return $qb;
}
/**
* @return int Total query results before searches/filtering
*/
public function getCountAllResults()
{
$rootEntityIdentifier = $this->getPrimaryKeys()[0]; // FIXME: add support for compound primary keys
$qb = clone $this->getQueryBuilder();
$qb->select('COUNT(DISTINCT ' . $this->getAlias() . '.' . $rootEntityIdentifier . ')');
$this
->addFrom($qb)
->addJoin($qb)
;
$qb->resetDQLPart('orderBy');
$qb->resetDQLPart('groupBy');
$query =$qb->getQuery();
foreach ($this->getHints() as $hint) {
$query->setHint($hint['name'], $hint['value']);
}
return (int) $query->getSingleScalarResult();
}
/**
* @return int Total query results after searches/filtering
*/
public function getCountFilteredResults()
{
$rootEntityIdentifier = $this->getPrimaryKeys()[0]; // FIXME: add support for compound primary keys
$qb = clone $this->getQueryBuilder();
$qb->select('COUNT(DISTINCT ' . $this->getAlias() . '.' . $rootEntityIdentifier . ')');
$this
->addFrom($qb)
->addJoin($qb)
->addFilter($qb)
;
$qb->resetDQLPart('orderBy');
$qb->resetDQLPart('groupBy');
$query =$qb->getQuery();
foreach ($this->getHints() as $hint) {
$query->setHint($hint['name'], $hint['value']);
}
return (int) $query->getSingleScalarResult();
}
public function getResponseArray($hydration = self::HYDRATE_ARRAY)
{
return array(
'sEcho' => $this->request->get('sEcho'),
'aaData' => $this->getData($hydration),
"iTotalRecords" => $this->getCountAllResults(),
"iTotalDisplayRecords" => $this->getCountFilteredResults()
);
}
/**
* @param QueryBuilder $qb
* @return Table
*/
public function from($className, $alias)
{
$this->setName($className);
$this->setAlias($alias);
return $this;
}
/**
* @param QueryBuilder $qb
* @return Table
*/
protected function addFrom(QueryBuilder $qb)
{
if (count($qb->getDQLPart('from')) == 0) {
$qb->from($this->getName(), $this->getAlias());
}
return $this;
}
/**
* @param QueryBuilder $qb
* @return Table
*/
protected function addFilter(QueryBuilder $qb)
{
$andx = $qb->expr()->andX();
foreach ($this->getFields() as $field) {
if ($field->isSearch()) {
$andx->add($field->filter($qb));
}
}
if ($andx->count() > 0) {
$qb->andWhere($andx);
}
return $this;
}
/**
* @param QueryBuilder $qb
* @return Table
*/
public function select(QueryBuilder $qb)
{
$select = $this->getSelect();
foreach ($select as $alias => $names) {
if (!is_array($names)) {
$names = array($names);
}
if (empty($alias)) {
$qb->addSelect($names);
$this->basePropertyPath = '[0]'; // dirty fix for custom fields being fetched
} else {
array_unshift($names, $this->getEntity($alias)->getPrimaryKeys()[0]);
$qb->addSelect('partial ' . $alias . '.{' . implode(',', array_unique($names)) . '}');
}
}
return $this;
}
public function getTable()
{
return $this;
}
/**
* @param QueryBuilder $qb
* @return Table
*/
protected function limit(QueryBuilder $qb)
{
$qb->setMaxResults($this->getMaxResults());
return $this;
}
protected function offset(QueryBuilder $qb)
{
$qb->setFirstResult($this->getFirstResult());
return $this;
}
public function addOrder(QueryBuilder $qb)
{
foreach ($this->getFields() as $field) {
if ($field->isSortable()) {
$field->order($qb);
}
}
return $this;
}
public function getRequest()
{
return $this->request;
}
public function __toString()
{
$str = '[';
foreach ($this->getFields() as $field) {
$str .= $field . "\n";
}
return $str . "]";
}
}