1
|
|
|
<?php |
2
|
|
|
namespace Mezon\Gui\ListBuilder; |
3
|
|
|
|
4
|
|
|
use Mezon\Functional\Fetcher; |
5
|
|
|
use Mezon\Gui\WidgetsRegistry\BootstrapWidgets; |
6
|
|
|
use Mezon\TemplateEngine\TemplateEngine; |
7
|
|
|
use Mezon\Transport\Request; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class ListBuilder |
11
|
|
|
* |
12
|
|
|
* @package CrudService |
13
|
|
|
* @subpackage ListBuilder |
14
|
|
|
* @author Dodonov A.A. |
15
|
|
|
* @version v.1.0 (2019/08/12) |
16
|
|
|
* @copyright Copyright (c) 2019, aeon.org |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class constructs grids |
21
|
|
|
*/ |
22
|
|
|
class Common |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Fields |
27
|
|
|
* |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
private $fields = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Service logic adapter |
34
|
|
|
* |
35
|
|
|
* @var \Mezon\Gui\ListBuilder\ListBuilderAdapter |
36
|
|
|
*/ |
37
|
|
|
private $listBuilderAdapter = false; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* List item transformation callback |
41
|
|
|
* |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
private $recordTransformer = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Custom actions for each record |
48
|
|
|
* |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
private $customActions = null; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Custom header actions |
55
|
|
|
* |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
private $customHeaderActions = ''; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* List title |
62
|
|
|
* |
63
|
|
|
* @var string |
64
|
|
|
*/ |
65
|
|
|
public $listTitle = 'Список записей'; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* List description |
69
|
|
|
* |
70
|
|
|
* @var string |
71
|
|
|
*/ |
72
|
|
|
public $listDescription = 'Выберите необходимое действие'; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Constructor |
76
|
|
|
* |
77
|
|
|
* @param array $fields |
78
|
|
|
* List of fields |
79
|
|
|
* @param \Mezon\Gui\ListBuilder\ListBuilderAdapter $listBuilderAdapter |
80
|
|
|
* Adapter for the data source |
81
|
|
|
*/ |
82
|
|
|
public function __construct(array $fields, ListBuilderAdapter $listBuilderAdapter) |
83
|
|
|
{ |
84
|
|
|
$transformedFields = []; |
85
|
|
|
|
86
|
|
|
foreach ($fields as $i => $field) { |
87
|
|
|
$key = is_array($field) ? $i : $field; |
88
|
|
|
$transformedFields[$key] = is_array($field) ? $field : [ |
89
|
|
|
'title' => $field |
90
|
|
|
]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$this->fields = $transformedFields; |
94
|
|
|
|
95
|
|
|
$this->listBuilderAdapter = $listBuilderAdapter; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Method sets custom actions |
100
|
|
|
* |
101
|
|
|
* @param string $actions |
102
|
|
|
*/ |
103
|
|
|
public function setCustomActions(string $actions): void |
104
|
|
|
{ |
105
|
|
|
$this->customActions = $actions; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Method sets custom header actions |
110
|
|
|
* |
111
|
|
|
* @param string $actions |
112
|
|
|
*/ |
113
|
|
|
public function setCustomHeaderActions(string $actions): void |
114
|
|
|
{ |
115
|
|
|
$this->customHeaderActions = $actions; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Setting record transformer |
120
|
|
|
* |
121
|
|
|
* @param mixed $recordTransformer |
122
|
|
|
* callable record transformer |
123
|
|
|
* @codeCoverageIgnore |
124
|
|
|
*/ |
125
|
|
|
public function setRecordTransformer($recordTransformer): void |
126
|
|
|
{ |
127
|
|
|
$this->recordTransformer = $recordTransformer; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Method returns end point for the create page form |
132
|
|
|
* |
133
|
|
|
* @return string Create page endpoint |
134
|
|
|
*/ |
135
|
|
|
private function getCreatePageEndpoint(): string |
136
|
|
|
{ |
137
|
|
|
if (isset($_GET['create-page-endpoint'])) { |
138
|
|
|
return $_GET['create-page-endpoint']; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return '../create/'; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Method shows "no records" message instead of listing |
146
|
|
|
* |
147
|
|
|
* @return string Compiled list view |
148
|
|
|
*/ |
149
|
|
|
private function listingNoItems(): string |
150
|
|
|
{ |
151
|
|
|
$content = BootstrapWidgets::get('listing-no-items'); |
152
|
|
|
|
153
|
|
|
return str_replace('{create-page-endpoint}', $this->getCreatePageEndpoint(), $content); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Method displays list of possible buttons |
158
|
|
|
* |
159
|
|
|
* @param int $id |
160
|
|
|
* Id of the record |
161
|
|
|
* @return string Compiled list buttons |
162
|
|
|
*/ |
163
|
|
|
private function listOfButtons(int $id): string |
164
|
|
|
{ |
165
|
|
|
$content = BootstrapWidgets::get('list-of-buttons'); |
166
|
|
|
|
167
|
|
|
return str_replace('{id}', $id, $content); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Need to display actions in list |
172
|
|
|
* |
173
|
|
|
* @return bool Do we need add actions |
174
|
|
|
*/ |
175
|
|
|
private function needActions(): bool |
176
|
|
|
{ |
177
|
|
|
if (@$_GET['update-button'] == 1 || @$_GET['delete-button'] == 1 || $this->customActions !== null) { |
178
|
|
|
return true; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return false; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Method compiles listing items cells |
186
|
|
|
* |
187
|
|
|
* @param array|object $record |
188
|
|
|
* record data |
189
|
|
|
* @return string Compiled row |
190
|
|
|
*/ |
191
|
|
|
private function listingItemsCells($record): string |
192
|
|
|
{ |
193
|
|
|
$content = ''; |
194
|
|
|
|
195
|
|
|
foreach (array_keys($this->fields) as $name) { |
196
|
|
|
if ($name == 'domain_id') { |
197
|
|
|
continue; |
198
|
|
|
} |
199
|
|
|
if ($name == 'id') { |
200
|
|
|
$content .= BootstrapWidgets::get('listing-row-centered-cell'); |
201
|
|
|
} else { |
202
|
|
|
$content .= BootstrapWidgets::get('listing-row-cell'); |
203
|
|
|
} |
204
|
|
|
$content = str_replace('{name}', '{' . $name . '}', $content); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
if ($this->needActions()) { |
208
|
|
|
$content .= BootstrapWidgets::get('listing-actions'); |
209
|
|
|
|
210
|
|
|
$content = str_replace( |
211
|
|
|
'{actions}', |
212
|
|
|
$this->customActions === null ? $this->listOfButtons(Fetcher::getField($record, 'id')) : $this->customActions, |
|
|
|
|
213
|
|
|
$content); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
return $content; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Method transforms database record |
221
|
|
|
* |
222
|
|
|
* @param array $record |
223
|
|
|
* Transforming record |
224
|
|
|
* @return object Transformed record |
225
|
|
|
*/ |
226
|
|
|
private function transformRecord(object $record): object |
227
|
|
|
{ |
228
|
|
|
// here we assume that we get from service |
229
|
|
|
// already transformed |
230
|
|
|
// and here we provide only additional transformations |
231
|
|
|
if (is_callable($this->recordTransformer)) { |
232
|
|
|
$record = call_user_func($this->recordTransformer, $record); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
return $record; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Method compiles listing items |
240
|
|
|
* |
241
|
|
|
* @param array $records |
242
|
|
|
* Listof records |
243
|
|
|
* @return string Compiled list items |
244
|
|
|
*/ |
245
|
|
|
private function listingItems(array $records): string |
246
|
|
|
{ |
247
|
|
|
$content = ''; |
248
|
|
|
|
249
|
|
|
foreach ($records as $record) { |
250
|
|
|
$content .= BootstrapWidgets::get('listing-row'); |
251
|
|
|
$content = str_replace('{items}', $this->listingItemsCells($record), $content); |
252
|
|
|
|
253
|
|
|
$record = $this->transformRecord($record); |
254
|
|
|
|
255
|
|
|
$record = $this->listBuilderAdapter->preprocessListItem($record); |
256
|
|
|
|
257
|
|
|
$content = TemplateEngine::printRecord($content, $record); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
return $content; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Method compiles header cells |
265
|
|
|
* |
266
|
|
|
* @return string Compiled header |
267
|
|
|
*/ |
268
|
|
|
private function listingHeaderCells(): string |
269
|
|
|
{ |
270
|
|
|
$content = ''; |
271
|
|
|
|
272
|
|
|
foreach ($this->fields as $name => $data) { |
273
|
|
|
if ($name == 'domain_id') { |
274
|
|
|
continue; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
$idStyle = $name == 'id' ? 'style="text-align: center; width:5%;"' : ''; |
278
|
|
|
|
279
|
|
|
$content .= BootstrapWidgets::get('listing-header-cell'); |
280
|
|
|
$content = str_replace([ |
281
|
|
|
'{id-style}', |
282
|
|
|
'{title}' |
283
|
|
|
], [ |
284
|
|
|
$idStyle, |
285
|
|
|
$data['title'] |
286
|
|
|
], $content); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
if ($this->needActions()) { |
290
|
|
|
$content .= BootstrapWidgets::get('listing-header-actions'); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
return $content; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Method returns listing header content |
298
|
|
|
* |
299
|
|
|
* @param |
300
|
|
|
* string Compiled header |
301
|
|
|
*/ |
302
|
|
|
private function listingHeaderContent(): string |
303
|
|
|
{ |
304
|
|
|
if (@$_GET['create-button'] == 1) { |
305
|
|
|
$content = BootstrapWidgets::get('listing-header'); |
306
|
|
|
|
307
|
|
|
$content = str_replace('{create-page-endpoint}', $this->getCreatePageEndpoint(), $content); |
308
|
|
|
} else { |
309
|
|
|
$content = BootstrapWidgets::get('simple-listing-header'); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
return str_replace('{header-actions}', $this->customHeaderActions, $content); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Method compiles listing header |
317
|
|
|
* |
318
|
|
|
* @return string Compiled header |
319
|
|
|
*/ |
320
|
|
|
private function listingHeader(): string |
321
|
|
|
{ |
322
|
|
|
$content = $this->listingHeaderContent(); |
323
|
|
|
|
324
|
|
|
$content = str_replace([ |
325
|
|
|
'{list-description}', |
326
|
|
|
'{list-title}' |
327
|
|
|
], [ |
328
|
|
|
Request::getParam('list-description', $this->listDescription), |
329
|
|
|
$this->listTitle |
330
|
|
|
], $content); |
331
|
|
|
|
332
|
|
|
return str_replace('{cells}', $this->listingHeaderCells(), $content); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* Method compiles listing form |
337
|
|
|
* |
338
|
|
|
* @return string Compiled listing form |
339
|
|
|
*/ |
340
|
|
|
public function listingForm(): string |
341
|
|
|
{ |
342
|
|
|
$records = $this->listBuilderAdapter->getRecords([ |
343
|
|
|
'field' => 'id', |
344
|
|
|
'order' => 'ASC' |
345
|
|
|
], isset($_GET['from']) ? $_GET['from'] : 0, isset($_GET['limit']) ? $_GET['limit'] : 100); |
346
|
|
|
|
347
|
|
|
if (! empty($records)) { |
348
|
|
|
$header = $this->listingHeader(); |
349
|
|
|
|
350
|
|
|
$items = $this->listingItems($records); |
351
|
|
|
|
352
|
|
|
$footer = BootstrapWidgets::get('listing-footer'); |
353
|
|
|
|
354
|
|
|
return $header . $items . $footer; |
355
|
|
|
} else { |
356
|
|
|
return $this->listingNoItems(); |
357
|
|
|
} |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* Method returns fields of the list |
362
|
|
|
* |
363
|
|
|
* @return array fields list |
364
|
|
|
*/ |
365
|
|
|
public function getFields(): array |
366
|
|
|
{ |
367
|
|
|
return $this->fields; |
368
|
|
|
} |
369
|
|
|
} |
370
|
|
|
|