|
1
|
|
|
<?php |
|
2
|
|
|
namespace Nayjest\Grids; |
|
3
|
|
|
|
|
4
|
|
|
use Illuminate\Support\Collection; |
|
5
|
|
|
use Nayjest\Grids\Components\Base\RenderableComponentInterface; |
|
6
|
|
|
use Nayjest\Grids\Components\Base\TComponent; |
|
7
|
|
|
use Nayjest\Grids\Components\Base\TRegistry; |
|
8
|
|
|
use Nayjest\Grids\Components\Base\RegistryInterface; |
|
9
|
|
|
use Nayjest\Grids\Components\TFoot; |
|
10
|
|
|
use Nayjest\Grids\Components\THead; |
|
11
|
|
|
use Nayjest\Grids\Components\Tr; |
|
12
|
|
|
|
|
13
|
|
|
class GridConfig implements RegistryInterface |
|
14
|
|
|
{ |
|
15
|
|
|
use TRegistry; |
|
16
|
|
|
use TComponent; |
|
17
|
|
|
|
|
18
|
|
|
const SECTION_DO_NOT_RENDER = 'not_render'; |
|
19
|
|
|
|
|
20
|
|
|
protected $template = 'grids::default'; |
|
21
|
|
|
|
|
22
|
|
|
/** @var FieldConfig[]|Collection */ |
|
23
|
|
|
protected $columns; |
|
24
|
|
|
|
|
25
|
|
|
/** @var DataProvider $data_provider */ |
|
26
|
|
|
protected $data_provider; |
|
27
|
|
|
|
|
28
|
|
|
protected $page_size = 50; |
|
29
|
|
|
|
|
30
|
|
|
/** @var Collection|FilterConfig[] $filters */ |
|
31
|
|
|
protected $filters; |
|
32
|
|
|
|
|
33
|
|
|
/** @var int */ |
|
34
|
|
|
protected $caching_time = 0; |
|
35
|
|
|
|
|
36
|
|
|
protected $main_template = '*.grid'; |
|
37
|
|
|
|
|
38
|
|
|
protected $row_component; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return RenderableComponentInterface |
|
42
|
|
|
*/ |
|
43
|
|
|
public function getRowComponent() |
|
44
|
|
|
{ |
|
45
|
|
|
if (!$this->row_component) { |
|
46
|
|
|
$this->row_component = (new Tr) |
|
47
|
|
|
->setRenderSection(self::SECTION_DO_NOT_RENDER); |
|
48
|
|
|
if ($this->grid) { |
|
49
|
|
|
$this->row_component->initialize($this->grid); |
|
50
|
|
|
} |
|
51
|
|
|
$this->addComponent($this->row_component); |
|
52
|
|
|
} |
|
53
|
|
|
return $this->row_component; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param RenderableComponentInterface $rowComponent |
|
58
|
|
|
* @return $this |
|
59
|
|
|
*/ |
|
60
|
|
|
public function setRowComponent(RenderableComponentInterface $rowComponent) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->row_component = $rowComponent; |
|
63
|
|
|
$this->addComponent($rowComponent); |
|
64
|
|
|
$rowComponent->setRenderSection(self::SECTION_DO_NOT_RENDER); |
|
65
|
|
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Returns default child components. |
|
70
|
|
|
* |
|
71
|
|
|
* |
|
72
|
|
|
* @return \Illuminate\Support\Collection|Components\Base\ComponentInterface[]|array |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function getDefaultComponents() |
|
75
|
|
|
{ |
|
76
|
|
|
return [ |
|
77
|
|
|
new THead, |
|
78
|
|
|
new TFoot |
|
79
|
|
|
]; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param string $template |
|
84
|
|
|
* @return $this |
|
85
|
|
|
*/ |
|
86
|
|
|
public function setTemplate($template) |
|
87
|
|
|
{ |
|
88
|
|
|
$this->template = $template; |
|
89
|
|
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function setMainTemplate($template) |
|
93
|
|
|
{ |
|
94
|
|
|
$this->main_template = $template; |
|
95
|
|
|
return $this; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function getMainTemplate() |
|
99
|
|
|
{ |
|
100
|
|
|
return str_replace('*.', "$this->template.", $this->main_template); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param Collection|FilterConfig[] $filters |
|
106
|
|
|
* @return $this |
|
107
|
|
|
*/ |
|
108
|
|
|
public function setFilters($filters) |
|
109
|
|
|
{ |
|
110
|
|
|
$this->filters = Collection::make($filters); |
|
111
|
|
|
return $this; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function getFilters() |
|
115
|
|
|
{ |
|
116
|
|
|
if (null === $this->filters) { |
|
117
|
|
|
$this->filters = new Collection(); |
|
118
|
|
|
} |
|
119
|
|
|
return $this->filters; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @return string |
|
124
|
|
|
*/ |
|
125
|
|
|
public function getTemplate() |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->template; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param DataProvider $dataProvider |
|
132
|
|
|
* @return $this |
|
133
|
|
|
*/ |
|
134
|
|
|
public function setDataProvider(DataProvider $dataProvider) |
|
135
|
|
|
{ |
|
136
|
|
|
$this->data_provider = $dataProvider; |
|
137
|
|
|
return $this; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @return DataProvider |
|
142
|
|
|
*/ |
|
143
|
|
|
public function getDataProvider() |
|
144
|
|
|
{ |
|
145
|
|
|
return $this->data_provider; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @param FieldConfig[]|Collection $columns |
|
150
|
|
|
* @return $this |
|
151
|
|
|
*/ |
|
152
|
|
|
public function setColumns($columns) |
|
153
|
|
|
{ |
|
154
|
|
|
$this->columns = Collection::make($columns); |
|
155
|
|
|
return $this; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Returns collection of grid columns. |
|
160
|
|
|
* |
|
161
|
|
|
* @return FieldConfig[]|Collection |
|
162
|
|
|
*/ |
|
163
|
|
|
public function getColumns() |
|
164
|
|
|
{ |
|
165
|
|
|
if (null === $this->columns) { |
|
166
|
|
|
$this->columns = new Collection; |
|
167
|
|
|
} |
|
168
|
|
|
return $this->columns; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Returns column by name. |
|
173
|
|
|
* |
|
174
|
|
|
* @param string $name |
|
175
|
|
|
* @return null|FieldConfig |
|
176
|
|
|
*/ |
|
177
|
|
|
public function getColumn($name) |
|
178
|
|
|
{ |
|
179
|
|
|
foreach ($this->getColumns() as $column) { |
|
180
|
|
|
if ($column->getName() === $name) { |
|
181
|
|
|
return $column; |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Returns cache expiration time in minutes. |
|
189
|
|
|
* |
|
190
|
|
|
* @return int |
|
191
|
|
|
*/ |
|
192
|
|
|
public function getCachingTime() |
|
193
|
|
|
{ |
|
194
|
|
|
return $this->caching_time; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Sets cache expiration time in minutes. |
|
199
|
|
|
* |
|
200
|
|
|
* @param int $minutes |
|
201
|
|
|
* |
|
202
|
|
|
* @return $this |
|
203
|
|
|
*/ |
|
204
|
|
|
public function setCachingTime($minutes) |
|
205
|
|
|
{ |
|
206
|
|
|
$this->caching_time = $minutes; |
|
207
|
|
|
return $this; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Adds column to grid. |
|
212
|
|
|
* |
|
213
|
|
|
* @param FieldConfig $column |
|
214
|
|
|
* @return $this |
|
215
|
|
|
*/ |
|
216
|
|
|
public function addColumn(FieldConfig $column) |
|
217
|
|
|
{ |
|
218
|
|
|
if ($this->columns === null) { |
|
219
|
|
|
$this->setColumns([]); |
|
220
|
|
|
} |
|
221
|
|
|
$this->columns->push($column); |
|
222
|
|
|
return $this; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Sets maximal quantity of rows per page. |
|
227
|
|
|
* |
|
228
|
|
|
* @param int $pageSize |
|
229
|
|
|
* @return $this |
|
230
|
|
|
*/ |
|
231
|
|
|
public function setPageSize($pageSize) |
|
232
|
|
|
{ |
|
233
|
|
|
$this->page_size = (int)$pageSize; |
|
234
|
|
|
return $this; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Returns maximal quantity of rows per page. |
|
239
|
|
|
* |
|
240
|
|
|
* @return int |
|
241
|
|
|
*/ |
|
242
|
|
|
public function getPageSize() |
|
243
|
|
|
{ |
|
244
|
|
|
return $this->page_size; |
|
245
|
|
|
} |
|
246
|
|
|
/** |
|
247
|
|
|
* Returns maximal quantity of rows per page. |
|
248
|
|
|
* |
|
249
|
|
|
* @return int |
|
250
|
|
|
*/ |
|
251
|
|
|
public function getPageSize() |
|
252
|
|
|
{ |
|
253
|
|
|
return $this->page_size; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
public function setDefaultSort($columnName, $direction) |
|
257
|
|
|
{ |
|
258
|
|
|
$this->defaultSort = [$columnName, $direction]; |
|
|
|
|
|
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
public function getDefaultSort() |
|
262
|
|
|
{ |
|
263
|
|
|
return $this->defaultSort; |
|
264
|
|
|
} |
|
265
|
|
|
} |
|
266
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: