1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Disclaimer: This source code is protected by copyright law and by |
5
|
|
|
* international conventions. |
6
|
|
|
* |
7
|
|
|
* Any reproduction or partial or total distribution of the source code, by any |
8
|
|
|
* means whatsoever, is strictly forbidden. |
9
|
|
|
* |
10
|
|
|
* Anyone not complying with these provisions will be guilty of the offense of |
11
|
|
|
* infringement and the penal sanctions provided for by law. |
12
|
|
|
* |
13
|
|
|
* © 2018 All rights reserved. |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace WBW\Bundle\JQuery\DataTablesBundle\Helper; |
17
|
|
|
|
18
|
|
|
use Doctrine\ORM\QueryBuilder; |
19
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\API\DataTablesWrapper; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* DataTables repository helper. |
23
|
|
|
* |
24
|
|
|
* @author webeweb <https://github.com/webeweb/> |
25
|
|
|
* @package WBW\Bundle\JQuery\DataTablesBundle\Helper |
26
|
|
|
*/ |
27
|
|
|
class DataTablesRepositoryHelper { |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Append a DataTables ORDER clause. |
31
|
|
|
* |
32
|
|
|
* @param QueryBuilder $queryBuilder The query builder. |
33
|
|
|
* @param DataTablesWrapper $dtWrapper The DataTables wrapper. |
34
|
|
|
* @return void |
35
|
|
|
*/ |
36
|
|
|
public static function appendOrder(QueryBuilder $queryBuilder, DataTablesWrapper $dtWrapper) { |
37
|
|
|
|
38
|
|
|
// Handle each DataTables order. |
39
|
|
|
foreach ($dtWrapper->getRequest()->getOrder() as $dtOrder) { |
40
|
|
|
|
41
|
|
|
// Get the DataTables column. |
42
|
|
|
$dtColumn = array_values($dtWrapper->getColumns())[$dtOrder->getColumn()]; |
43
|
|
|
|
44
|
|
|
// Check if the DataTables column is orderable. |
45
|
|
|
if (false === $dtColumn->getOrderable()) { |
46
|
|
|
continue; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
// Add the order by. |
50
|
|
|
$queryBuilder->addOrderBy($dtColumn->getMapping()->getAlias(), $dtOrder->getDir()); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Append a DataTables WHERE clause. |
56
|
|
|
* |
57
|
|
|
* @param QueryBuilder $queryBuilder The query builder. |
58
|
|
|
* @param DataTablesWrapper $dtWrapper The DataTables wrapper. |
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
|
|
public static function appendWhere(QueryBuilder $queryBuilder, DataTablesWrapper $dtWrapper) { |
62
|
|
|
|
63
|
|
|
// Determines and check the operator. |
64
|
|
|
$operator = self::determineOperator($dtWrapper); |
65
|
|
|
if (null === $operator) { |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// Initialize. |
70
|
|
|
$wheres = []; |
71
|
|
|
$params = []; |
72
|
|
|
$values = []; |
73
|
|
|
|
74
|
|
|
// Handle each DataTables column. |
75
|
|
|
foreach ($dtWrapper->getRequest()->getColumns() as $dtColumn) { |
76
|
|
|
|
77
|
|
|
// Check the DataTables column. |
78
|
|
|
if ("OR" === $operator || "" !== $dtColumn->getSearch()->getValue()) { |
79
|
|
|
|
80
|
|
|
// Get the DataTables mapping. |
81
|
|
|
$mapping = $dtColumn->getMapping(); |
82
|
|
|
|
83
|
|
|
// Add. |
84
|
|
|
$wheres[] = $mapping->getAlias() . " LIKE :" . $mapping->getPrefix() . $mapping->getColumn(); |
85
|
|
|
$params[] = ":" . $mapping->getPrefix() . $mapping->getColumn(); |
86
|
|
|
$values[] = "%" . ("AND" === $operator ? $dtColumn->getSearch()->getValue() : $dtWrapper->getRequest()->getSearch()->getValue()) . "%"; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// Set the where clause. |
91
|
|
|
$queryBuilder->andWhere("(" . implode(" " . $operator . " ", $wheres) . ")"); |
92
|
|
|
for ($i = count($params) - 1; 0 <= $i; --$i) { |
93
|
|
|
$queryBuilder->setParameter($params[$i], $values[$i]); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Determines a DataTables operator. |
99
|
|
|
* |
100
|
|
|
* @param DataTablesWrapper $dtWrapper The DataTables wrapper. |
101
|
|
|
* @return string Returns the DataTables operator. |
102
|
|
|
*/ |
103
|
|
|
public static function determineOperator(DataTablesWrapper $dtWrapper) { |
104
|
|
|
|
105
|
|
|
// Check if the DataTables columns defines a search. |
106
|
|
|
foreach ($dtWrapper->getRequest()->getColumns() as $dtColumn) { |
107
|
|
|
if ("" !== $dtColumn->getSearch()->getValue()) { |
108
|
|
|
return "AND"; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// Check if the DataTables defines a search. |
113
|
|
|
if ("" !== $dtWrapper->getRequest()->getSearch()->getValue()) { |
114
|
|
|
return "OR"; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
// Return the operator. |
118
|
|
|
return null; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
} |
122
|
|
|
|