|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace DoctrineDatatable; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class DatatableWithTotal. |
|
9
|
|
|
*/ |
|
10
|
|
|
abstract class DatatableWithTotal extends Datatable |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* ABSTRACT METHODS. |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @return string[] |
|
18
|
|
|
*/ |
|
19
|
|
|
abstract protected function getSumableColumns(): array; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* PRIVATE METHODS. |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @return float[] |
|
27
|
|
|
*/ |
|
28
|
|
|
private function calculTotaux(): array |
|
29
|
|
|
{ |
|
30
|
|
|
$query = clone $this->final_query; |
|
31
|
|
|
|
|
32
|
|
|
$query->resetDQLPart('orderBy') |
|
33
|
|
|
->resetDQLPart('groupBy'); |
|
34
|
|
|
|
|
35
|
|
|
$index = 0; |
|
36
|
|
|
foreach ($this->getSumableColumns() as $alias => $column) { |
|
37
|
|
|
0 == $index ? |
|
38
|
|
|
$query->select("$column as $alias") : |
|
39
|
|
|
$query->addSelect("$column as $alias"); |
|
40
|
|
|
|
|
41
|
|
|
++$index; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$retour = array(); |
|
45
|
|
|
|
|
46
|
|
|
foreach ($query->getQuery()->getResult() as $result) { |
|
47
|
|
|
foreach ($result as $index => $r) { |
|
48
|
|
|
if (!isset($retour['total_'.$index])) { |
|
49
|
|
|
$retour['total_'.$index] = 0; |
|
50
|
|
|
} |
|
51
|
|
|
$retour['total_'.$index] += (float) $r; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return $retour; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* IMPLEMENT METHODS. |
|
60
|
|
|
*/ |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritdoc} |
|
64
|
|
|
*/ |
|
65
|
|
|
public function get(array $filters): array |
|
66
|
|
|
{ |
|
67
|
|
|
$this->createFinalQuery($filters); |
|
68
|
|
|
$data = $this->data($filters); |
|
69
|
|
|
|
|
70
|
|
|
return array( |
|
71
|
|
|
'recordsTotal' => $this->count(), |
|
72
|
|
|
'recordsFiltered' => \count($data), |
|
73
|
|
|
'data' => $data, |
|
74
|
|
|
'totalColonnes' => $this->calculTotaux(), |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* {@inheritdoc} |
|
80
|
|
|
*/ |
|
81
|
|
|
private function count(): int |
|
82
|
|
|
{ |
|
83
|
|
|
$tools = new Tools(); |
|
84
|
|
|
$temp = (clone $this->final_query) |
|
85
|
|
|
->setFirstResult(0) |
|
86
|
|
|
->setMaxResults(null) |
|
87
|
|
|
->getQuery(); |
|
88
|
|
|
|
|
89
|
|
|
/** @var \Doctrine\ORM\Query\ParserResult $parser */ |
|
90
|
|
|
$parser = $tools->callMethod($temp, '_parse'); |
|
91
|
|
|
|
|
92
|
|
|
list($sqlParams, $types) = $tools->callMethod( |
|
93
|
|
|
$temp, |
|
94
|
|
|
'processParameterMappings', |
|
95
|
|
|
array( |
|
96
|
|
|
$parser->getParameterMappings(), |
|
97
|
|
|
) |
|
98
|
|
|
); |
|
99
|
|
|
|
|
100
|
|
|
return (int) ((clone $this->final_query)->getEntityManager() |
|
101
|
|
|
->getConnection() |
|
102
|
|
|
->executeQuery('SELECT COUNT(*) as total FROM ('.$temp->getSQL().') as t', $sqlParams, $types) |
|
|
|
|
|
|
103
|
|
|
->fetch()['total']); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|