1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of vaibhavpandeyvpz/doctrine-datatables package. |
5
|
|
|
* |
6
|
|
|
* (c) Vaibhav Pandey <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE.md. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Doctrine\DataTables; |
13
|
|
|
|
14
|
|
|
use Doctrine\DBAL\Query\QueryBuilder; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Builder |
18
|
|
|
* @package Doctrine\DataTables |
19
|
|
|
*/ |
20
|
|
|
class Builder |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $columnAliases = array(); |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $columnField = 'name'; // or 'data' |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $indexColumn = '*'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var QueryBuilder |
39
|
|
|
*/ |
40
|
|
|
protected $queryBuilder; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
protected $requestParams; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
|
|
public function getData() |
51
|
|
|
{ |
52
|
|
|
$query = $this->getFilteredQuery(); |
53
|
|
|
$columns = &$this->requestParams['columns']; |
54
|
|
|
// Order |
55
|
|
|
if (array_key_exists('order', $this->requestParams)) { |
56
|
|
|
$order = &$this->requestParams['order']; |
57
|
|
|
foreach ($order as $sort) { |
58
|
|
|
$column = &$columns[intval($sort['column'])]; |
59
|
|
|
if (array_key_exists($column[$this->columnField], $this->columnAliases)) { |
60
|
|
|
$column[$this->columnField] = $this->columnAliases[$column[$this->columnField]]; |
61
|
|
|
} |
62
|
|
|
$query->addOrderBy($column[$this->columnField], $sort['dir']); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
// Offset |
66
|
|
|
if (array_key_exists('start', $this->requestParams)) { |
67
|
|
|
$query->setFirstResult(intval($this->requestParams['start'])); |
68
|
|
|
} |
69
|
|
|
// Limit |
70
|
|
|
if (array_key_exists('length', $this->requestParams)) { |
71
|
|
|
$length = intval($this->requestParams['length']); |
72
|
|
|
if ($length > 0) { |
73
|
|
|
$query->setMaxResults($length); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
// Fetch |
77
|
|
|
return $query->execute()->fetchAll(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return QueryBuilder |
82
|
|
|
*/ |
83
|
|
|
public function getFilteredQuery() |
84
|
|
|
{ |
85
|
|
|
$query = clone $this->queryBuilder; |
86
|
|
|
$columns = &$this->requestParams['columns']; |
87
|
|
|
$c = count($columns); |
88
|
|
|
// Search |
89
|
|
|
if (array_key_exists('search', $this->requestParams)) { |
90
|
|
|
if ($value = trim($this->requestParams['search']['value'])) { |
91
|
|
|
$orX = $query->expr()->orX(); |
92
|
|
|
for ($i = 0; $i < $c; $i++) { |
93
|
|
|
$column = &$columns[$i]; |
94
|
|
|
if ($column['searchable'] == 'true') { |
95
|
|
|
if (array_key_exists($column[$this->columnField], $this->columnAliases)) { |
96
|
|
|
$column[$this->columnField] = $this->columnAliases[$column[$this->columnField]]; |
97
|
|
|
} |
98
|
|
|
$orX->add($query->expr()->like($column[$this->columnField], ':search')); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
if ($orX->count() >= 1) { |
102
|
|
|
$query->andWhere($orX) |
103
|
|
|
->setParameter('search', "%{$value}%"); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
// Filter |
108
|
|
|
for ($i = 0; $i < $c; $i++) { |
109
|
|
|
$column = &$columns[$i]; |
110
|
|
|
$andX = $query->expr()->andX(); |
111
|
|
|
if (($column['searchable'] == 'true') && ($value = trim($column['search']['value']))) { |
112
|
|
|
if (array_key_exists($column[$this->columnField], $this->columnAliases)) { |
113
|
|
|
$column[$this->columnField] = $this->columnAliases[$column[$this->columnField]]; |
114
|
|
|
} |
115
|
|
|
$andX->add($query->expr()->eq($column[$this->columnField], ":filter_{$i}")); |
116
|
|
|
$query->setParameter("filter_{$i}", $value); |
117
|
|
|
} |
118
|
|
|
if ($andX->count() >= 1) { |
119
|
|
|
$query->andWhere($andX); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
// Done |
123
|
|
|
return $query; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return int |
128
|
|
|
*/ |
129
|
|
|
public function getRecordsFiltered() |
130
|
|
|
{ |
131
|
|
|
return $this->getFilteredQuery() |
132
|
|
|
->resetQueryPart('select') |
133
|
|
|
->select("count({$this->indexColumn})") |
134
|
|
|
->execute() |
135
|
|
|
->fetchColumn(0); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return int |
140
|
|
|
*/ |
141
|
|
|
public function getRecordsTotal() |
142
|
|
|
{ |
143
|
|
|
$tmp = clone $this->queryBuilder; |
144
|
|
|
return $tmp->resetQueryPart('select') |
145
|
|
|
->select("count({$this->indexColumn})") |
146
|
|
|
->execute() |
147
|
|
|
->fetchColumn(0); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return array |
152
|
|
|
*/ |
153
|
|
|
public function getResponse() |
154
|
|
|
{ |
155
|
|
|
return array( |
156
|
|
|
'data' => $this->getData(), |
157
|
|
|
'draw' => $this->requestParams['draw'], |
158
|
|
|
'recordsFiltered' => $this->getRecordsFiltered(), |
159
|
|
|
'recordsTotal' => $this->getRecordsTotal(), |
160
|
|
|
); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param string $indexColumn |
165
|
|
|
* @return static |
166
|
|
|
*/ |
167
|
|
|
public function withIndexColumn($indexColumn) |
168
|
|
|
{ |
169
|
|
|
$this->indexColumn = $indexColumn; |
170
|
|
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param array $columnAliases |
175
|
|
|
* @return static |
176
|
|
|
*/ |
177
|
|
|
public function withColumnAliases($columnAliases) |
178
|
|
|
{ |
179
|
|
|
$this->columnAliases = $columnAliases; |
180
|
|
|
return $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param array $columnField |
185
|
|
|
* @return static |
186
|
|
|
*/ |
187
|
|
|
public function withColumnField($columnField) |
188
|
|
|
{ |
189
|
|
|
$this->columnField = $columnField; |
|
|
|
|
190
|
|
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @param QueryBuilder $queryBuilder |
195
|
|
|
* @return static |
196
|
|
|
*/ |
197
|
|
|
public function withQueryBuilder(QueryBuilder $queryBuilder) |
198
|
|
|
{ |
199
|
|
|
$this->queryBuilder = $queryBuilder; |
200
|
|
|
return $this; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param array $requestParams |
205
|
|
|
* @return static |
206
|
|
|
*/ |
207
|
|
|
public function withRequestParams($requestParams) |
208
|
|
|
{ |
209
|
|
|
$this->requestParams = $requestParams; |
210
|
|
|
return $this; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..