|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the jquery-datatables-bundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2018 WEBEWEB |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace WBW\Bundle\JQuery\DataTablesBundle\Repository; |
|
13
|
|
|
|
|
14
|
|
|
use Doctrine\ORM\EntityRepository; |
|
15
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
16
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\API\DataTablesWrapper; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Default DataTables repository. |
|
20
|
|
|
* |
|
21
|
|
|
* @author webeweb <https://github.com/webeweb/> |
|
22
|
|
|
* @package WBW\Bundle\JQuery\DataTablesBundle\Repository |
|
23
|
|
|
* @abstract |
|
24
|
|
|
*/ |
|
25
|
|
|
abstract class DefaultDataTablesRepository extends EntityRepository implements DataTablesRepositoryInterface { |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* {@inheritdoc} |
|
29
|
|
|
*/ |
|
30
|
|
|
public function dataTablesCountFiltered(DataTablesWrapper $dtWrapper) { |
|
31
|
|
|
|
|
32
|
|
|
// Get the prefix. |
|
33
|
|
|
$prefix = $dtWrapper->getMapping()->getPrefix(); |
|
34
|
|
|
|
|
35
|
|
|
// Create a query builder. |
|
36
|
|
|
$qb = $this->createQueryBuilder($prefix) |
|
37
|
|
|
->select("COUNT(" . $prefix . ")"); |
|
38
|
|
|
|
|
39
|
|
|
// Build the where clause. |
|
40
|
|
|
self::dataTablesWhere($qb, $dtWrapper); |
|
41
|
|
|
|
|
42
|
|
|
// Return the single scalar result. |
|
43
|
|
|
return intval($qb->getQuery()->getSingleScalarResult()); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* {@inheritdoc} |
|
48
|
|
|
*/ |
|
49
|
|
|
public function dataTablesCountTotal(DataTablesWrapper $dtWrapper) { |
|
50
|
|
|
|
|
51
|
|
|
// Get the prefix. |
|
52
|
|
|
$prefix = $dtWrapper->getMapping()->getPrefix(); |
|
53
|
|
|
|
|
54
|
|
|
// Create a query builder. |
|
55
|
|
|
$qb = $this->createQueryBuilder($prefix) |
|
56
|
|
|
->select("COUNT(" . $prefix . ")"); |
|
57
|
|
|
|
|
58
|
|
|
// Return the single scalar result. |
|
59
|
|
|
return intval($qb->getQuery()->getSingleScalarResult()); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritdoc} |
|
64
|
|
|
*/ |
|
65
|
|
|
public function dataTablesFindAll(DataTablesWrapper $dtWrapper) { |
|
66
|
|
|
|
|
67
|
|
|
// Get the prefix. |
|
68
|
|
|
$prefix = $dtWrapper->getMapping()->getPrefix(); |
|
69
|
|
|
|
|
70
|
|
|
// Create a query builder. |
|
71
|
|
|
$qb = $this->createQueryBuilder($prefix) |
|
72
|
|
|
->setFirstResult($dtWrapper->getRequest()->getStart()) |
|
73
|
|
|
->setMaxResults($dtWrapper->getRequest()->getLength()); |
|
74
|
|
|
|
|
75
|
|
|
// Build the where and order clauses. |
|
76
|
|
|
self::dataTablesWhere($qb, $dtWrapper); |
|
77
|
|
|
self::dataTablesOrder($qb, $dtWrapper); |
|
78
|
|
|
|
|
79
|
|
|
// Return the result. |
|
80
|
|
|
return $qb->getQuery()->getResult(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Build a WHERE operator. |
|
85
|
|
|
* |
|
86
|
|
|
* @param DataTablesWrapper $dtWrapper The DataTables wrapper. |
|
87
|
|
|
* @return string Returns the WHERE operator. |
|
88
|
|
|
*/ |
|
89
|
|
|
protected static function dataTablesOperator(DataTablesWrapper $dtWrapper) { |
|
90
|
|
|
|
|
91
|
|
|
// Check if the DataTables columns defines a search. |
|
92
|
|
|
foreach ($dtWrapper->getRequest()->getColumns() as $dtColumn) { |
|
93
|
|
|
if ("" !== $dtColumn->getSearch()->getValue()) { |
|
94
|
|
|
return "AND"; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
// Check if the DataTables defines a search. |
|
99
|
|
|
if ("" !== $dtWrapper->getRequest()->getSearch()->getValue()) { |
|
100
|
|
|
return "OR"; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
// Return the operator. |
|
104
|
|
|
return null; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Build an ORDER clause. |
|
109
|
|
|
* |
|
110
|
|
|
* @param QueryBuilder $queryBuilder The query builder. |
|
111
|
|
|
* @param DataTablesWrapper $dtWrapper The DataTables wrapper. |
|
112
|
|
|
* @return void |
|
113
|
|
|
*/ |
|
114
|
|
|
public static function dataTablesOrder(QueryBuilder $queryBuilder, DataTablesWrapper $dtWrapper) { |
|
115
|
|
|
|
|
116
|
|
|
// Handle each DataTables order. |
|
117
|
|
|
foreach ($dtWrapper->getRequest()->getOrder() as $dtOrder) { |
|
118
|
|
|
|
|
119
|
|
|
// Get the DataTables column. |
|
120
|
|
|
$dtColumn = array_values($dtWrapper->getColumns())[$dtOrder->getColumn()]; |
|
121
|
|
|
|
|
122
|
|
|
// Check if the DataTables column is orderable. |
|
123
|
|
|
if (false === $dtColumn->getOrderable()) { |
|
124
|
|
|
continue; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
// Add the order by. |
|
128
|
|
|
$queryBuilder->addOrderBy($dtColumn->getMapping()->getAlias(), $dtOrder->getDir()); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Build a WHERE clause. |
|
134
|
|
|
* |
|
135
|
|
|
* @param QueryBuilder $queryBuilder The query builder. |
|
136
|
|
|
* @param DataTablesWrapper $dtWrapper The DataTables wrapper. |
|
137
|
|
|
* @return void |
|
138
|
|
|
*/ |
|
139
|
|
|
public static function dataTablesWhere(QueryBuilder $queryBuilder, DataTablesWrapper $dtWrapper) { |
|
140
|
|
|
|
|
141
|
|
|
// Get and check the operator. |
|
142
|
|
|
$operator = self::dataTablesOperator($dtWrapper); |
|
143
|
|
|
if (null === $operator) { |
|
144
|
|
|
return; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
// Initialize. |
|
148
|
|
|
$wheres = []; |
|
149
|
|
|
$params = []; |
|
150
|
|
|
$values = []; |
|
151
|
|
|
|
|
152
|
|
|
// Handle each DataTables column. |
|
153
|
|
|
foreach ($dtWrapper->getRequest()->getColumns() as $dtColumn) { |
|
154
|
|
|
|
|
155
|
|
|
// Check the DataTables column. |
|
156
|
|
|
if ("OR" === $operator || "" !== $dtColumn->getSearch()->getValue()) { |
|
157
|
|
|
|
|
158
|
|
|
// Get the column mapping. |
|
159
|
|
|
$mapping = $dtColumn->getMapping(); |
|
160
|
|
|
|
|
161
|
|
|
// Add. |
|
162
|
|
|
$wheres[] = $mapping->getAlias() . " LIKE :" . $mapping->getPrefix() . $mapping->getColumn(); |
|
163
|
|
|
$params[] = ":" . $mapping->getPrefix() . $mapping->getColumn(); |
|
164
|
|
|
$values[] = "%" . ("AND" === $operator ? $dtColumn->getSearch()->getValue() : $dtWrapper->getRequest()->getSearch()->getValue()) . "%"; |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
// Set the where. |
|
169
|
|
|
$queryBuilder->andWhere("(" . implode(" " . $operator . " ", $wheres) . ")"); |
|
170
|
|
|
for ($i = count($params) - 1; 0 <= $i; --$i) { |
|
171
|
|
|
$queryBuilder->setParameter($params[$i], $values[$i]); |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
} |
|
176
|
|
|
|