|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OpenSkill\Datatable\Queries\Parser; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use OpenSkill\Datatable\Columns\ColumnConfiguration; |
|
7
|
|
|
use OpenSkill\Datatable\Queries\QueryConfiguration; |
|
8
|
|
|
use OpenSkill\Datatable\Queries\QueryConfigurationBuilder; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
12
|
|
|
|
|
13
|
|
|
class Datatable110QueryParser extends QueryParser |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Method to determine if this parser can handle the query parameters. If so then the parser should return true |
|
18
|
|
|
* and be able to return a DTQueryConfiguration |
|
19
|
|
|
* |
|
20
|
|
|
* @param Request $request The current request, that should be investigated |
|
21
|
|
|
* @return bool true if the parser is able to parse the query parameters and to return a DTQueryConfiguration |
|
22
|
|
|
*/ |
|
23
|
|
|
public function canParse(Request $request) |
|
24
|
|
|
{ |
|
25
|
|
|
return $request->query->has("draw"); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Method that should parse the request and return a DTQueryConfiguration |
|
30
|
|
|
* |
|
31
|
|
|
* @param Request $request The current request that should be investigated |
|
32
|
|
|
* @param ColumnConfiguration[] $columnConfiguration The configuration of the columns |
|
33
|
|
|
* @return QueryConfiguration the configuration the provider can use to prepare the data |
|
34
|
|
|
*/ |
|
35
|
|
|
public function parse(Request $request, array $columnConfiguration) |
|
36
|
|
|
{ |
|
37
|
|
|
$query = $request->query; |
|
38
|
|
|
$builder = QueryConfigurationBuilder::create(); |
|
39
|
|
|
|
|
40
|
|
|
$this->getDrawCall($query, $builder); |
|
41
|
|
|
|
|
42
|
|
|
$this->getStart($query, $builder); |
|
43
|
|
|
|
|
44
|
|
|
$this->getLength($query, $builder); |
|
45
|
|
|
|
|
46
|
|
|
$this->getSearch($query, $builder); |
|
47
|
|
|
|
|
48
|
|
|
$this->getRegex($query, $builder); |
|
49
|
|
|
|
|
50
|
|
|
$this->getOrder($query, $builder, $columnConfiguration); |
|
51
|
|
|
|
|
52
|
|
|
// for each column we need to see if there is a search value |
|
53
|
|
|
foreach ($columnConfiguration as $i => $c) { |
|
54
|
|
|
// check if there is something search related |
|
55
|
|
|
if ($c->getSearch()->isSearchable() && |
|
56
|
|
|
!$this->isEmpty($query->get("columns[" . $i . "][search][value]", null, true))) { |
|
57
|
|
|
// search for this column is available |
|
58
|
|
|
$builder->columnSearch($c->getName(), $query->get("columns[" . $i . "][search][value]", null, true)); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $builder->build(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Helper function that will check if a variable is empty |
|
67
|
|
|
* @param mixed $string |
|
68
|
|
|
* @return bool true if empty, false otherwise |
|
69
|
|
|
*/ |
|
70
|
|
|
private function isEmpty($string) |
|
71
|
|
|
{ |
|
72
|
|
|
return empty($string); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param ParameterBag $query |
|
77
|
|
|
* @param QueryConfigurationBuilder $builder |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getDrawCall($query, $builder) |
|
80
|
|
|
{ |
|
81
|
|
|
if ($query->has('draw')) { |
|
82
|
|
|
$builder->drawCall($query->get('draw')); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param ParameterBag $query |
|
88
|
|
|
* @param QueryConfigurationBuilder $builder |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getStart($query, $builder) |
|
91
|
|
|
{ |
|
92
|
|
|
if ($query->has('start')) { |
|
93
|
|
|
$builder->start($query->get('start')); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param ParameterBag $query |
|
99
|
|
|
* @param QueryConfigurationBuilder $builder |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getLength($query, $builder) |
|
102
|
|
|
{ |
|
103
|
|
|
if ($query->has('length')) { |
|
104
|
|
|
$builder->length($query->get('length')); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param ParameterBag $query |
|
110
|
|
|
* @param QueryConfigurationBuilder $builder |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getSearch($query, $builder) |
|
113
|
|
|
{ |
|
114
|
|
|
if (!$this->isEmpty($query->get('search[value]', null, true))) { |
|
115
|
|
|
$builder->searchValue($query->get('search[value]', null, true)); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param ParameterBag $query |
|
121
|
|
|
* @param QueryConfigurationBuilder $builder |
|
122
|
|
|
*/ |
|
123
|
|
|
public function getRegex($query, $builder) |
|
124
|
|
|
{ |
|
125
|
|
|
if (!$this->isEmpty($query->get('search[regex]', null, true))) { |
|
126
|
|
|
$builder->searchRegex($query->get('search[regex]', null, true)); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param ParameterBag $query |
|
132
|
|
|
* @param QueryConfigurationBuilder $builder |
|
133
|
|
|
* @param ColumnConfiguration[] $columnConfiguration |
|
134
|
|
|
*/ |
|
135
|
|
|
private function getOrder(ParameterBag $query, QueryConfigurationBuilder $builder, array $columnConfiguration) |
|
136
|
|
|
{ |
|
137
|
|
|
//loop over the order |
|
138
|
|
|
if(($query->has('order'))) { |
|
139
|
|
|
$order = $query->get('order'); |
|
140
|
|
|
foreach($order as $i => $config) { |
|
141
|
|
|
if(array_key_exists($config['column'], $columnConfiguration)) { |
|
142
|
|
|
$column = $columnConfiguration[$config['column']]; |
|
143
|
|
|
if($column->getOrder()->isOrderable()) { |
|
144
|
|
|
$builder->columnOrder($column->getName(), $config['dir']); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
} |