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
|
|
|
|
34
|
|
|
$index = 0; |
35
|
|
|
foreach ($this->getSumableColumns() as $alias => $column) { |
36
|
|
|
0 == $index ? |
37
|
|
|
$query->select("$column as $alias") : |
38
|
|
|
$query->addSelect("$column as $alias"); |
39
|
|
|
|
40
|
|
|
++$index; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$retour = array(); |
44
|
|
|
|
45
|
|
|
foreach ($query->getQuery()->getResult() as $result) { |
46
|
|
|
foreach ($result as $index => $r) { |
47
|
|
|
if (!isset($retour['total_'.$index])) { |
48
|
|
|
$retour['total_'.$index] = 0; |
49
|
|
|
} |
50
|
|
|
$retour['total_'.$index] += (float) $r; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $retour; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* IMPLEMENT METHODS. |
59
|
|
|
*/ |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
public function get(array $filters): array |
65
|
|
|
{ |
66
|
|
|
$this->createFinalQuery($filters); |
67
|
|
|
$data = $this->data($filters); |
68
|
|
|
|
69
|
|
|
return array( |
70
|
|
|
'recordsTotal' => $this->count(), |
71
|
|
|
'recordsFiltered' => \count($data), |
72
|
|
|
'data' => $data, |
73
|
|
|
'totalColonnes' => $this->calculTotaux(), |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
|
|
private function count(): int |
81
|
|
|
{ |
82
|
|
|
$tools = new Tools(); |
83
|
|
|
$temp = (clone $this->final_query) |
84
|
|
|
->setFirstResult(0) |
85
|
|
|
->setMaxResults(null) |
86
|
|
|
->getQuery(); |
87
|
|
|
|
88
|
|
|
/** @var \Doctrine\ORM\Query\ParserResult $parser */ |
89
|
|
|
$parser = $tools->callMethod($temp, '_parse'); |
90
|
|
|
|
91
|
|
|
list($sqlParams, $types) = $tools->callMethod( |
92
|
|
|
$temp, |
93
|
|
|
'processParameterMappings', |
94
|
|
|
array( |
95
|
|
|
$parser->getParameterMappings(), |
96
|
|
|
) |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
return (int) ((clone $this->final_query)->getEntityManager() |
100
|
|
|
->getConnection() |
101
|
|
|
->executeQuery('SELECT COUNT(*) as total FROM ('.$temp->getSQL().') as t', $sqlParams, $types) |
|
|
|
|
102
|
|
|
->fetch()['total']); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|