|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dtc\GridBundle\Grid\Source; |
|
4
|
|
|
|
|
5
|
|
|
use Dtc\GridBundle\Grid\Column\AbstractGridColumn; |
|
6
|
|
|
use Dtc\GridBundle\Grid\Pager\GridSourcePager; |
|
7
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
8
|
|
|
|
|
9
|
|
|
abstract class AbstractGridSource implements GridSourceInterface |
|
10
|
|
|
{ |
|
11
|
|
|
protected $limit = 25; |
|
12
|
|
|
protected $offset = 0; |
|
13
|
|
|
protected $filter = array(); |
|
14
|
|
|
protected $orderBy = array(); |
|
15
|
|
|
protected $pager = array(); |
|
16
|
|
|
protected $id = 'grid'; |
|
17
|
|
|
protected $columns; |
|
18
|
|
|
protected $parameters; |
|
19
|
|
|
|
|
20
|
|
|
public function bind(Request $request) |
|
21
|
|
|
{ |
|
22
|
|
|
// Change limit, offset |
|
23
|
|
|
if ($limit = $request->get('limit')) { |
|
24
|
|
|
$this->limit = $limit; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
if ($page = $request->get('page')) { |
|
28
|
|
|
$this->offset = $this->limit * ($page - 1); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
if ($offset = $request->get('offset')) { |
|
32
|
|
|
$this->offset = $offset; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
if ($filter = $request->get('filter')) { |
|
36
|
|
|
$this->filter = $filter; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
if ($sortColumn = $request->get('sort_column')) { |
|
40
|
|
|
$sortOrder = $request->get('sort_order'); |
|
41
|
|
|
$sortOrder = strtoupper($sortOrder); |
|
42
|
|
|
|
|
43
|
|
|
$this->orderBy[$sortColumn] = $sortOrder; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
if ($orderBy = $request->get('order')) { |
|
47
|
|
|
$this->orderBy = $orderBy; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function getId() |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->id; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
private $divId = null; |
|
57
|
|
|
|
|
58
|
|
|
public function getDivId() |
|
59
|
|
|
{ |
|
60
|
|
|
if (!$this->divId) { |
|
61
|
|
|
$this->divId = preg_replace('/[^a-zA-Z0-9\-]/', '', $this->id); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return $this->divId; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function setId($value) |
|
68
|
|
|
{ |
|
69
|
|
|
$this->id = $value; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function getColumns() |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->columns; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function setColumns($columns) |
|
78
|
|
|
{ |
|
79
|
|
|
/** @var AbstractGridColumn $col */ |
|
80
|
|
|
foreach ($columns as $col) { |
|
81
|
|
|
$this->columns[$col->getField()] = $col; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function removeColumn($field) |
|
86
|
|
|
{ |
|
87
|
|
|
$this->removeColumns(func_get_args()); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function selectColumns(array $fields) |
|
91
|
|
|
{ |
|
92
|
|
|
$selectedCols = array(); |
|
93
|
|
|
foreach ($fields as $field) { |
|
94
|
|
|
if (isset($this->columns[$field])) { |
|
95
|
|
|
$selectedCols[$field] = $this->columns[$field]; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$this->columns = $selectedCols; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function removeColumns(array $fields) |
|
103
|
|
|
{ |
|
104
|
|
|
foreach ($fields as $field) { |
|
105
|
|
|
unset($this->columns[$field]); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function getPager() |
|
110
|
|
|
{ |
|
111
|
|
|
if (!$this->pager) { |
|
|
|
|
|
|
112
|
|
|
$this->pager = new GridSourcePager($this); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return $this->pager; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @return $limit |
|
120
|
|
|
*/ |
|
|
|
|
|
|
121
|
|
|
public function getLimit() |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->limit; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param int $limit |
|
128
|
|
|
*/ |
|
129
|
|
|
public function setLimit($limit) |
|
130
|
|
|
{ |
|
131
|
|
|
$this->limit = $limit; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @return $offset |
|
136
|
|
|
*/ |
|
|
|
|
|
|
137
|
|
|
public function getOffset() |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->offset; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param int $offset |
|
144
|
|
|
*/ |
|
145
|
|
|
public function setOffset($offset) |
|
146
|
|
|
{ |
|
147
|
|
|
$this->offset = $offset; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @return $filter |
|
152
|
|
|
*/ |
|
|
|
|
|
|
153
|
|
|
public function getFilter() |
|
154
|
|
|
{ |
|
155
|
|
|
return $this->filter; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @param $filter |
|
160
|
|
|
*/ |
|
161
|
|
|
public function setFilter($filter) |
|
162
|
|
|
{ |
|
163
|
|
|
$this->filter = $filter; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @return $orderBy |
|
168
|
|
|
*/ |
|
|
|
|
|
|
169
|
|
|
public function getOrderBy() |
|
170
|
|
|
{ |
|
171
|
|
|
return $this->orderBy; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @param $orderBy |
|
176
|
|
|
*/ |
|
177
|
|
|
public function setOrderBy($orderBy) |
|
178
|
|
|
{ |
|
179
|
|
|
$this->orderBy = $orderBy; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
public function getLastModified() |
|
183
|
|
|
{ |
|
184
|
|
|
return null; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
public function getParameter($key) |
|
188
|
|
|
{ |
|
189
|
|
|
return $this->parameters[$key]; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
public function setParameter($key, $value) |
|
193
|
|
|
{ |
|
194
|
|
|
$this->parameters[$key] = $value; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* @return $parameters |
|
199
|
|
|
*/ |
|
|
|
|
|
|
200
|
|
|
public function getParameters() |
|
201
|
|
|
{ |
|
202
|
|
|
return $this->parameters; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* @param $parameters |
|
207
|
|
|
*/ |
|
208
|
|
|
public function setParameters($parameters) |
|
209
|
|
|
{ |
|
210
|
|
|
$this->parameters = $parameters; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* @return bool Returns true if this GridSource has an ID or false otherise |
|
215
|
|
|
*/ |
|
216
|
|
|
public function hasIdColumn() |
|
217
|
|
|
{ |
|
218
|
|
|
return false; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* @param $id |
|
223
|
|
|
* |
|
224
|
|
|
* @return null|mixed returns the row identified by Id if found |
|
225
|
|
|
*/ |
|
226
|
|
|
public function find($id) |
|
227
|
|
|
{ |
|
228
|
|
|
return null; |
|
229
|
|
|
} |
|
230
|
|
|
} |
|
231
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.