1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OpenSkill\Datatable\Queries\Parser; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use OpenSkill\Datatable\Columns\ColumnConfiguration; |
7
|
|
|
use OpenSkill\Datatable\DatatableException; |
8
|
|
|
use OpenSkill\Datatable\Queries\QueryConfiguration; |
9
|
|
|
use OpenSkill\Datatable\Queries\QueryConfigurationBuilder; |
10
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
11
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
13
|
|
|
|
14
|
|
|
class Datatable19QueryParser extends QueryParser |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Method to determine if this parser can handle the query parameters. If so then the parser should return true |
19
|
|
|
* and be able to return a DTQueryConfiguration |
20
|
|
|
* |
21
|
|
|
* @param Request $request The current request, that should be investigated |
22
|
|
|
* @return bool true if the parser is able to parse the query parameters and to return a DTQueryConfiguration |
23
|
|
|
*/ |
24
|
|
|
public function canParse(Request $request) |
25
|
|
|
{ |
26
|
|
|
return $request->query->has("sEcho"); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Method that should parse the request and return a DTQueryConfiguration |
31
|
|
|
* |
32
|
|
|
* @param Request $request The current request that should be investigated |
33
|
|
|
* @param ColumnConfiguration[] $columnConfiguration The configuration of the columns |
34
|
|
|
* @return QueryConfiguration the configuration the provider can use to prepare the data |
35
|
|
|
*/ |
36
|
|
|
public function parse(Request $request, array $columnConfiguration) |
37
|
|
|
{ |
38
|
|
|
$query = $request->query; |
39
|
|
|
$builder = QueryConfigurationBuilder::create(); |
40
|
|
|
|
41
|
|
|
$this->getDrawCall($query, $builder); |
42
|
|
|
|
43
|
|
|
$this->getStart($query, $builder); |
44
|
|
|
|
45
|
|
|
$this->getLength($query, $builder); |
46
|
|
|
|
47
|
|
|
$this->getSearch($query, $builder); |
48
|
|
|
|
49
|
|
|
$this->determineSortableColumns($query, $builder, $columnConfiguration); |
50
|
|
|
|
51
|
|
|
$this->getRegex($query, $builder); |
52
|
|
|
|
53
|
|
|
$this->getSearchColumns($query, $builder, $columnConfiguration); |
54
|
|
|
|
55
|
|
|
return $builder->build(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Helper function that will check if a variable is empty |
60
|
|
|
* @param mixed $string |
61
|
|
|
* @return bool true if empty, false otherwise |
62
|
|
|
*/ |
63
|
|
|
private function isEmpty($string) |
64
|
|
|
{ |
65
|
|
|
return empty($string); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Helper function that will check if a variable has a value |
70
|
|
|
* |
71
|
|
|
* NOTE: (this is almost the opposite of isEmpty, but it is *not* the same) |
72
|
|
|
* |
73
|
|
|
* @param mixed $string |
74
|
|
|
* @return bool true if empty, false otherwise |
75
|
|
|
*/ |
76
|
|
|
private function hasValue($string) |
77
|
|
|
{ |
78
|
|
|
return isset($string) && (strlen($string) > 0); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param ParameterBag $query |
83
|
|
|
* @param QueryConfigurationBuilder $builder |
84
|
|
|
*/ |
85
|
|
|
public function getDrawCall($query, $builder) |
86
|
|
|
{ |
87
|
|
|
if ($query->has('sEcho')) { |
88
|
|
|
$builder->drawCall($query->get('sEcho')); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param ParameterBag $query |
94
|
|
|
* @param QueryConfigurationBuilder $builder |
95
|
|
|
*/ |
96
|
|
|
public function getStart($query, $builder) |
97
|
|
|
{ |
98
|
|
|
if ($query->has('iDisplayStart')) { |
99
|
|
|
$builder->start($query->get('iDisplayStart')); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param ParameterBag $query |
105
|
|
|
* @param QueryConfigurationBuilder $builder |
106
|
|
|
*/ |
107
|
|
|
public function getLength($query, $builder) |
108
|
|
|
{ |
109
|
|
|
if ($query->has('iDisplayLength')) { |
110
|
|
|
$builder->length($query->get('iDisplayLength')); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param ParameterBag $query |
116
|
|
|
* @param QueryConfigurationBuilder $builder |
117
|
|
|
*/ |
118
|
|
|
public function getSearch($query, $builder) |
119
|
|
|
{ |
120
|
|
|
if ($query->has('sSearch') && !$this->isEmpty($query->get('sSearch'))) { |
121
|
|
|
$builder->searchValue($query->get('sSearch')); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param ParameterBag $query |
127
|
|
|
* @param QueryConfigurationBuilder $builder |
128
|
|
|
* @param ColumnConfiguration[] $columnConfiguration |
129
|
|
|
*/ |
130
|
|
|
public function getSearchColumns($query, $builder, array $columnConfiguration) |
131
|
|
|
{ |
132
|
|
|
// for each column we need to see if there is a search value |
133
|
|
|
foreach ($columnConfiguration as $i => $c) { |
134
|
|
|
// check if there is something search related |
135
|
|
|
$this->addColumnSearchToBuilderIfRequested($query, $builder, $c, $i); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param ParameterBag $query |
141
|
|
|
* @param QueryConfigurationBuilder $builder |
142
|
|
|
* @param ColumnConfiguration $column |
143
|
|
|
* @param integer $position position of the column in the columnConfiguration loop |
144
|
|
|
*/ |
145
|
|
|
private function addColumnSearchToBuilderIfRequested($query, $builder, $column, $position) |
146
|
|
|
{ |
147
|
|
|
$query_key = 'sSearch_' . $position; |
148
|
|
|
|
149
|
|
|
if ($column->getSearch()->isSearchable() && $query->has($query_key) && !$this->isEmpty($query->get($query_key))) { |
150
|
|
|
$builder->columnSearch($column->getName(), $query->get($query_key)); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param ParameterBag $query |
156
|
|
|
* @param QueryConfigurationBuilder $builder |
157
|
|
|
* @param ColumnConfiguration[] $columnConfiguration |
158
|
|
|
* @throws DatatableException when a column for sorting is out of bounds |
159
|
|
|
* @return bool success? |
160
|
|
|
*/ |
161
|
|
|
private function determineSortableColumns($query, $builder, array $columnConfiguration) |
162
|
|
|
{ |
163
|
|
|
$columns = $this->getNumberOfSortingColumns($query); |
164
|
|
|
|
165
|
|
|
for ($i = 0; $i < $columns; $i++) { |
166
|
|
|
if ($query->has("iSortCol_" . $i) && $this->hasValue($query->get("iSortCol_" . $i))) { |
167
|
|
|
$item = $query->get("iSortCol_" . $i); |
168
|
|
|
$direction = $query->get("sSortDir_" . $i); |
169
|
|
|
|
170
|
|
|
$this->addColumnForOrdering($builder, $columnConfiguration, $item, $direction); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return true; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Find out how many columns we are sorting by for the sorting loop |
179
|
|
|
* @see determineSortableColumns |
180
|
|
|
* @param ParameterBag $query |
181
|
|
|
* @return int |
182
|
|
|
*/ |
183
|
|
|
private function getNumberOfSortingColumns(ParameterBag $query) |
184
|
|
|
{ |
185
|
|
|
if (!$query->has('iSortingCols')) |
186
|
|
|
return 0; |
187
|
|
|
|
188
|
|
|
return intval($query->get('iSortingCols')); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Add a column for ordering to the QueryConfigurationBuilder |
193
|
|
|
* @see determineSortableColumns |
194
|
|
|
* @param $builder |
195
|
|
|
* @param $columnConfiguration |
196
|
|
|
* @param $item |
197
|
|
|
* @param $direction |
198
|
|
|
* @throws DatatableException |
199
|
|
|
*/ |
200
|
|
|
private function addColumnForOrdering($builder, $columnConfiguration, $item, $direction) |
201
|
|
|
{ |
202
|
|
|
$c = $this->getColumnFromConfiguration($columnConfiguration, $item); |
203
|
|
|
|
204
|
|
|
if ($c->getOrder()->isOrderable()) { |
205
|
|
|
$builder->columnOrder($c->getName(), $direction); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param ColumnConfiguration[] $columnConfiguration |
211
|
|
|
* @param int $item |
212
|
|
|
* @return ColumnConfiguration a specific item from $ColumnConfiguration |
213
|
|
|
* @throws DatatableException |
214
|
|
|
*/ |
215
|
|
|
private function getColumnFromConfiguration(array $columnConfiguration, $item) |
216
|
|
|
{ |
217
|
|
|
$columnPosition = intval($item); |
218
|
|
|
|
219
|
|
|
if (!isset($columnConfiguration[$columnPosition])) { |
220
|
|
|
throw new DatatableException('The column requested for ordering does not exist'); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
return $columnConfiguration[$columnPosition]; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @param ParameterBag $query |
228
|
|
|
* @param QueryConfigurationBuilder $builder |
229
|
|
|
*/ |
230
|
|
|
public function getRegex($query, $builder) |
231
|
|
|
{ |
232
|
|
|
if ($query->has('bRegex')) { |
233
|
|
|
$builder->searchRegex($query->get('bRegex')); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|