|
1
|
|
|
<?php |
|
2
|
|
|
namespace Nayjest\Grids; |
|
3
|
|
|
|
|
4
|
|
|
use Event; |
|
5
|
|
|
use Cache; |
|
6
|
|
|
use Nayjest\Grids\Components\TFoot; |
|
7
|
|
|
use Nayjest\Grids\Components\THead; |
|
8
|
|
|
use View; |
|
9
|
|
|
|
|
10
|
|
|
class Grid |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
const SORT_ASC = 'ASC'; |
|
14
|
|
|
const SORT_DESC = 'DESC'; |
|
15
|
|
|
|
|
16
|
|
|
const EVENT_PREPARE = 'grid.prepare'; |
|
17
|
|
|
const EVENT_CREATE = 'grid.create'; |
|
18
|
|
|
|
|
19
|
|
|
/** @var GridConfig */ |
|
20
|
|
|
protected $config; |
|
21
|
|
|
|
|
22
|
|
|
/** @var bool */ |
|
23
|
|
|
protected $prepared = false; |
|
24
|
|
|
|
|
25
|
|
|
/** @var Sorter */ |
|
26
|
|
|
protected $sorter; |
|
27
|
|
|
|
|
28
|
|
|
/** @var GridInputProcessor */ |
|
29
|
|
|
protected $input_processor; |
|
30
|
|
|
|
|
31
|
|
|
protected $filtering; |
|
32
|
|
|
|
|
33
|
|
|
public function __construct(GridConfig $config) |
|
34
|
|
|
{ |
|
35
|
|
|
|
|
36
|
|
|
$this->config = $config; |
|
37
|
|
|
if ($config->getName() === null) { |
|
38
|
|
|
$this->provideName(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$this->initializeComponents(); |
|
42
|
|
|
Event::fire(self::EVENT_CREATE, $this); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return string |
|
47
|
|
|
*/ |
|
48
|
|
|
protected function getMainTemplate() |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->config->getMainTemplate(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
public function prepare() |
|
55
|
|
|
{ |
|
56
|
|
|
if ($this->prepared === true) { |
|
57
|
|
|
return; |
|
58
|
|
|
} |
|
59
|
|
|
$cfg = $this->config; |
|
60
|
|
|
$cfg->getDataProvider() |
|
61
|
|
|
->setPageSize( |
|
62
|
|
|
$cfg->getPageSize() |
|
63
|
|
|
) |
|
64
|
|
|
->setCurrentPage( |
|
65
|
|
|
$this->getInputProcessor()->getValue('page', 1) |
|
66
|
|
|
); |
|
67
|
|
|
$this->getConfig()->prepare(); |
|
68
|
|
|
$this->getFiltering()->apply(); |
|
69
|
|
|
$this->prepareColumns(); |
|
70
|
|
|
$this->getSorter()->apply(); |
|
71
|
|
|
Event::fire(self::EVENT_PREPARE, $this); |
|
72
|
|
|
$this->prepared = true; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
protected function initializeComponents() |
|
76
|
|
|
{ |
|
77
|
|
|
$this->getConfig()->initialize($this); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
protected function prepareColumns() |
|
81
|
|
|
{ |
|
82
|
|
|
if ($this->needToSortColumns()) { |
|
83
|
|
|
$this->sortColumns(); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Provides unique name for each grid on the page |
|
89
|
|
|
* |
|
90
|
|
|
* @return null |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function provideName() |
|
93
|
|
|
{ |
|
94
|
|
|
$bt_len = 10; |
|
95
|
|
|
$backtrace = debug_backtrace(null, $bt_len); |
|
96
|
|
|
$str = ''; |
|
97
|
|
|
for ($id = 2; $id < $bt_len; $id++) { |
|
98
|
|
|
$trace = isset($backtrace[$id]) ? $backtrace[$id] : []; |
|
99
|
|
|
if (empty($trace['class']) || !$this instanceof $trace['class']) { |
|
100
|
|
|
# may be closure |
|
101
|
|
|
if (isset($trace['file'], $trace['line'])) { |
|
102
|
|
|
$str .= $trace['file'] . $trace['line']; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
$this->config->setName(substr(md5($str), 0, 16)); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Returns true if columns must be sorted. |
|
111
|
|
|
* |
|
112
|
|
|
* @return bool |
|
113
|
|
|
*/ |
|
114
|
|
|
protected function needToSortColumns() |
|
115
|
|
|
{ |
|
116
|
|
|
foreach ($this->config->getColumns() as $column) { |
|
117
|
|
|
if ($column->getOrder() !== 0) { |
|
118
|
|
|
return true; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
return false; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
private function sorting(FieldConfig $a, FieldConfig $b) |
|
|
|
|
|
|
125
|
|
|
{ |
|
126
|
|
|
return $a->getOrder() > $b->getOrder(); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Sorts columns according to its order. |
|
131
|
|
|
*/ |
|
132
|
|
|
protected function sortColumns() |
|
133
|
|
|
{ |
|
134
|
|
|
$this->config->getColumns()->sort($this->sorting); |
|
|
|
|
|
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Returns data sorting manager. |
|
139
|
|
|
* |
|
140
|
|
|
* @return Sorter |
|
141
|
|
|
*/ |
|
142
|
|
|
public function getSorter() |
|
143
|
|
|
{ |
|
144
|
|
|
if (null === $this->sorter) { |
|
145
|
|
|
$this->sorter = new Sorter($this); |
|
146
|
|
|
} |
|
147
|
|
|
return $this->sorter; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Returns instance of GridInputProcessor. |
|
152
|
|
|
* |
|
153
|
|
|
* @return GridInputProcessor |
|
154
|
|
|
*/ |
|
155
|
|
|
public function getInputProcessor() |
|
156
|
|
|
{ |
|
157
|
|
|
if (null === $this->input_processor) { |
|
158
|
|
|
$this->input_processor = new GridInputProcessor($this); |
|
159
|
|
|
} |
|
160
|
|
|
return $this->input_processor; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @return GridConfig |
|
165
|
|
|
*/ |
|
166
|
|
|
public function getConfig() |
|
167
|
|
|
{ |
|
168
|
|
|
return $this->config; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
public function getViewData() |
|
172
|
|
|
{ |
|
173
|
|
|
return [ |
|
174
|
|
|
'grid' => $this, |
|
175
|
|
|
'data' => $this->config->getDataProvider(), |
|
176
|
|
|
'template' => $this->config->getTemplate(), |
|
177
|
|
|
'columns' => $this->config->getColumns() |
|
178
|
|
|
]; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Renders grid. |
|
183
|
|
|
* |
|
184
|
|
|
* @return View|string |
|
185
|
|
|
*/ |
|
186
|
|
|
public function render() |
|
187
|
|
|
{ |
|
188
|
|
|
$key = $this->getInputProcessor()->getUniqueRequestId(); |
|
189
|
|
|
$caching_time = $this->config->getCachingTime(); |
|
190
|
|
|
if ($caching_time && ($output = Cache::get($key))) { |
|
191
|
|
|
return $output; |
|
192
|
|
|
} else { |
|
193
|
|
|
$this->prepare(); |
|
194
|
|
|
$provider = $this->config->getDataProvider(); |
|
195
|
|
|
$provider->reset(); |
|
196
|
|
|
$output = View::make( |
|
197
|
|
|
$this->getMainTemplate(), |
|
198
|
|
|
$this->getViewData() |
|
199
|
|
|
)->render(); |
|
200
|
|
|
if ($caching_time) { |
|
201
|
|
|
Cache::put($key, $output, $caching_time); |
|
202
|
|
|
} |
|
203
|
|
|
return $output; |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Returns footer component. |
|
209
|
|
|
* |
|
210
|
|
|
* @return TFoot|null |
|
211
|
|
|
*/ |
|
212
|
|
|
public function footer() |
|
213
|
|
|
{ |
|
214
|
|
|
return $this->getConfig()->getComponentByName('tfoot'); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Returns header component. |
|
219
|
|
|
* |
|
220
|
|
|
* @return THead|null |
|
221
|
|
|
*/ |
|
222
|
|
|
public function header() |
|
223
|
|
|
{ |
|
224
|
|
|
return $this->getConfig()->getComponentByName('thead'); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Returns data filtering manager. |
|
229
|
|
|
* |
|
230
|
|
|
* @return Filtering |
|
231
|
|
|
*/ |
|
232
|
|
|
public function getFiltering() |
|
233
|
|
|
{ |
|
234
|
|
|
if ($this->filtering === null) { |
|
235
|
|
|
$this->filtering = new Filtering($this); |
|
236
|
|
|
} |
|
237
|
|
|
return $this->filtering; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Renders grid object when it is treated like a string. |
|
242
|
|
|
* |
|
243
|
|
|
* @return string |
|
244
|
|
|
*/ |
|
245
|
|
|
public function __toString() |
|
246
|
|
|
{ |
|
247
|
|
|
return (string)$this->render(); |
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
|