|
1
|
|
|
<?php |
|
2
|
|
|
namespace Nayjest\Grids; |
|
3
|
|
|
|
|
4
|
|
|
use Request; |
|
5
|
|
|
use Request; |
|
|
|
|
|
|
6
|
|
|
use Form; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class GridInputProcessor |
|
10
|
|
|
* |
|
11
|
|
|
* This class manages input processing for grid. |
|
12
|
|
|
* |
|
13
|
|
|
* @package Nayjest\Grids |
|
14
|
|
|
*/ |
|
15
|
|
|
class GridInputProcessor |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var Grid |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $grid; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $input; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Constructor. |
|
29
|
|
|
* |
|
30
|
|
|
* @param Grid $grid |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(Grid $grid) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->grid = $grid; |
|
35
|
|
|
$this->loadInput(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
protected function loadInput() |
|
39
|
|
|
{ |
|
40
|
|
|
$this->input = Request::get($this->getKey(), []); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Returns input related to grid. |
|
45
|
|
|
* |
|
46
|
|
|
* @return array |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getInput() |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->input; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Returns input key for grid parameters. |
|
55
|
|
|
* |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getKey() |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->grid->getConfig()->getName(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Returns sorting parameters passed to input. |
|
65
|
|
|
* |
|
66
|
|
|
* @return mixed |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getSorting() |
|
69
|
|
|
{ |
|
70
|
|
|
return $_ =& $this->input['sort']; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function getSortingHiddenInputsHtml() |
|
74
|
|
|
{ |
|
75
|
|
|
$html = ''; |
|
76
|
|
|
|
|
77
|
|
|
$key = $this->getKey(); |
|
78
|
|
|
if (isset($this->input['sort'])) { |
|
79
|
|
|
foreach ($this->input['sort'] as $field => $direction) { |
|
80
|
|
|
$html .= Form::hidden("{$key}[sort][$field]", $direction); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
return $html; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Returns UID for current grid state. |
|
88
|
|
|
* |
|
89
|
|
|
* Currently used as key for caching. |
|
90
|
|
|
* |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getUniqueRequestId() |
|
94
|
|
|
{ |
|
95
|
|
|
$cookies_str = ''; |
|
96
|
|
|
foreach ($_COOKIE as $key => $val) { |
|
97
|
|
|
if (strpos($key, $this->getKey()) !== false) { |
|
98
|
|
|
$cookies_str .= $key . json_encode($val); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return md5($cookies_str . $this->getKey() . json_encode($this->getInput())); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param FieldConfig $column |
|
107
|
|
|
* @param $direction |
|
108
|
|
|
* @return $this |
|
109
|
|
|
*/ |
|
110
|
|
|
public function setSorting(FieldConfig $column, $direction) |
|
111
|
|
|
{ |
|
112
|
|
|
$this->input['sort'] = [ |
|
113
|
|
|
$column->getName() => $direction |
|
114
|
|
|
]; |
|
115
|
|
|
return $this; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Returns input value for filter. |
|
120
|
|
|
* |
|
121
|
|
|
* @param string $filterName |
|
122
|
|
|
* @return mixed |
|
123
|
|
|
*/ |
|
124
|
|
|
public function getFilterValue($filterName) |
|
125
|
|
|
{ |
|
126
|
|
|
if (isset($this->input['filters'][$filterName])) { |
|
127
|
|
|
return $this->input['filters'][$filterName]; |
|
128
|
|
|
} else { |
|
129
|
|
|
return null; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Returns value of input parameter related to grid. |
|
135
|
|
|
* |
|
136
|
|
|
* @param string $key |
|
137
|
|
|
* @param $default |
|
138
|
|
|
* @return mixed |
|
139
|
|
|
*/ |
|
140
|
|
|
public function getValue($key, $default = null) |
|
141
|
|
|
{ |
|
142
|
|
|
if (isset($this->input[$key])) { |
|
143
|
|
|
return $this->input[$key]; |
|
144
|
|
|
} else { |
|
145
|
|
|
return $default; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @param string $key |
|
151
|
|
|
* @param mixed $value |
|
152
|
|
|
* @return $this |
|
153
|
|
|
*/ |
|
154
|
|
|
public function setValue($key, $value) |
|
155
|
|
|
{ |
|
156
|
|
|
$this->input[$key] = $value; |
|
157
|
|
|
return $this; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Returns current query string extended by specified GET parameters. |
|
162
|
|
|
* |
|
163
|
|
|
* @param array $new_params |
|
164
|
|
|
* @return string |
|
165
|
|
|
*/ |
|
166
|
|
|
public function getQueryString(array $new_params = []) |
|
167
|
|
|
{ |
|
168
|
|
|
$params = $_GET; |
|
169
|
|
|
if (!empty($this->input)) { |
|
170
|
|
|
$params[$this->getKey()] = $this->input; |
|
171
|
|
|
} |
|
172
|
|
|
if (!empty($new_params)) { |
|
173
|
|
|
if (empty($params[$this->getKey()])) { |
|
174
|
|
|
$params[$this->getKey()] = []; |
|
175
|
|
|
} |
|
176
|
|
|
foreach ($new_params as $key => $value) { |
|
177
|
|
|
$params[$this->getKey()][$key] = $value; |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
return http_build_query($params); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Returns current URL extended by specified GET parameters. |
|
185
|
|
|
* |
|
186
|
|
|
* @param array $new_params |
|
187
|
|
|
* @return string |
|
188
|
|
|
*/ |
|
189
|
|
|
public function getUrl(array $new_params = []) |
|
190
|
|
|
{ |
|
191
|
|
|
if (null !== $query_string = $this->getQueryString($new_params)) { |
|
192
|
|
|
$query_string = '?' . $query_string; |
|
193
|
|
|
} |
|
194
|
|
|
$request = Request::instance(); |
|
195
|
|
|
return $request->getSchemeAndHttpHost() |
|
196
|
|
|
. $request->getBaseUrl() |
|
197
|
|
|
. $request->getPathInfo() |
|
198
|
|
|
. $query_string; |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|