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 extends Base |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Custom actions for each record |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $customActions = ''; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Custom header actions |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $customHeaderActions = ''; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Name of the template for the empty records list |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $noItemsTemplateName = 'listing-no-items-with-buttons'; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Endpoint for create button |
48
|
|
|
* |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
public $createButtonEndpoint = ''; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Method sets custom actions |
55
|
|
|
* |
56
|
|
|
* @param string $actions |
57
|
|
|
*/ |
58
|
|
|
public function setCustomActions(string $actions): void |
59
|
|
|
{ |
60
|
|
|
$this->customActions = $actions; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Method sets custom header actions |
65
|
|
|
* |
66
|
|
|
* @param string $actions |
67
|
|
|
*/ |
68
|
|
|
public function setCustomHeaderActions(string $actions): void |
69
|
|
|
{ |
70
|
|
|
$this->customHeaderActions = $actions; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Method returns end point for the create page form |
75
|
|
|
* |
76
|
|
|
* @return string Create page endpoint |
77
|
|
|
*/ |
78
|
|
|
private function getCreatePageEndpoint(): string |
79
|
|
|
{ |
80
|
|
|
if ($this->createButtonEndpoint !== '') { |
81
|
|
|
return $this->createButtonEndpoint; |
82
|
|
|
} elseif (isset($_GET['create-page-endpoint'])) { |
83
|
|
|
return $_GET['create-page-endpoint']; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return '../create/'; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Method shows "no records" message instead of listing |
91
|
|
|
* |
92
|
|
|
* @return string Compiled list view |
93
|
|
|
*/ |
94
|
|
|
private function listingNoItems(): string |
95
|
|
|
{ |
96
|
|
|
$content = $this->getNoItemsContent(); |
97
|
|
|
|
98
|
|
|
if (isset($_GET['create-button']) || $this->createButtonEndpoint !== '') { |
99
|
|
|
$content = str_replace('{buttons}', BootstrapWidgets::get('listing-header-buttons'), $content); |
100
|
|
|
$content = str_replace('{create-page-endpoint}', $this->getCreatePageEndpoint(), $content); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $content; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Method displays list of possible buttons |
108
|
|
|
* |
109
|
|
|
* @param int $id |
110
|
|
|
* id of the record |
111
|
|
|
* @return string compiled list buttons |
112
|
|
|
*/ |
113
|
|
|
private function listOfButtons(int $id): string |
114
|
|
|
{ |
115
|
|
|
return str_replace('{id}', (string) $id, BootstrapWidgets::get('list-of-buttons')); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Need to display actions in list |
120
|
|
|
* |
121
|
|
|
* @return bool do we need add actions |
122
|
|
|
*/ |
123
|
|
|
private function needActions(): bool |
124
|
|
|
{ |
125
|
|
|
if (@$_GET['update-button'] == 1 || @$_GET['delete-button'] == 1 || $this->customActions !== '') { |
126
|
|
|
return true; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return false; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Method compiles listing items cells |
134
|
|
|
* |
135
|
|
|
* @param array|object $record |
136
|
|
|
* record data |
137
|
|
|
* @return string compiled row |
138
|
|
|
*/ |
139
|
|
|
private function listingItemsCells($record): string |
140
|
|
|
{ |
141
|
|
|
$content = ''; |
142
|
|
|
|
143
|
|
|
foreach (array_keys($this->getFields()) as $name) { |
144
|
|
|
if ($name == 'domain_id') { |
145
|
|
|
continue; |
146
|
|
|
} |
147
|
|
|
if ($name == 'id') { |
148
|
|
|
$content .= BootstrapWidgets::get('listing-row-centered-cell'); |
149
|
|
|
} else { |
150
|
|
|
$content .= BootstrapWidgets::get('listing-row-cell'); |
151
|
|
|
} |
152
|
|
|
$content = str_replace('{name}', '{' . $name . '}', $content); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
if ($this->needActions()) { |
156
|
|
|
$content .= BootstrapWidgets::get('listing-actions'); |
157
|
|
|
|
158
|
|
|
$content = str_replace( |
159
|
|
|
'{actions}', |
160
|
|
|
$this->customActions === '' ? $this->listOfButtons(Fetcher::getField($record, 'id')) : $this->customActions, |
|
|
|
|
161
|
|
|
$content); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $content; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Method compiles listing items |
169
|
|
|
* |
170
|
|
|
* @param array $records |
171
|
|
|
* listof records |
172
|
|
|
* @return string compiled list items |
173
|
|
|
*/ |
174
|
|
|
private function listingItems(array $records): string |
175
|
|
|
{ |
176
|
|
|
$content = ''; |
177
|
|
|
|
178
|
|
|
foreach ($records as $record) { |
179
|
|
|
$content .= BootstrapWidgets::get('listing-row'); |
180
|
|
|
$content = str_replace('{items}', $this->listingItemsCells($record), $content); |
181
|
|
|
|
182
|
|
|
$record = $this->transformRecord($record); |
183
|
|
|
|
184
|
|
|
$record = $this->getListBuilderAdapter()->preprocessListItem($record); |
185
|
|
|
|
186
|
|
|
$content = TemplateEngine::printRecord($content, $record); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return $content; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Method compiles header cells |
194
|
|
|
* |
195
|
|
|
* @return string compiled header |
196
|
|
|
*/ |
197
|
|
|
private function listingHeaderCells(): string |
198
|
|
|
{ |
199
|
|
|
$content = ''; |
200
|
|
|
|
201
|
|
|
foreach ($this->getFields() as $name => $data) { |
202
|
|
|
if ($name == 'domain_id') { |
203
|
|
|
continue; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
$idStyle = $name == 'id' ? 'style="text-align: center; width:5%;"' : ''; |
207
|
|
|
|
208
|
|
|
$content .= BootstrapWidgets::get('listing-header-cell'); |
209
|
|
|
$content = str_replace([ |
210
|
|
|
'{id-style}', |
211
|
|
|
'{title}' |
212
|
|
|
], [ |
213
|
|
|
$idStyle, |
214
|
|
|
$data['title'] |
215
|
|
|
], $content); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
if ($this->needActions()) { |
219
|
|
|
$content .= BootstrapWidgets::get('listing-header-actions'); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
return $content; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Method returns listing header content |
227
|
|
|
* |
228
|
|
|
* @return string compiled header |
229
|
|
|
*/ |
230
|
|
|
private function listingHeaderContent(): string |
231
|
|
|
{ |
232
|
|
|
if (@$_GET['create-button'] == 1 || $this->createButtonEndpoint !== '') { |
233
|
|
|
$content = BootstrapWidgets::get('listing-header'); |
234
|
|
|
|
235
|
|
|
$content = str_replace('{create-page-endpoint}', $this->getCreatePageEndpoint(), $content); |
236
|
|
|
} else { |
237
|
|
|
$content = BootstrapWidgets::get('simple-listing-header'); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
return str_replace('{header-actions}', $this->customHeaderActions, $content); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Method compiles listing header |
245
|
|
|
* |
246
|
|
|
* @return string compiled header |
247
|
|
|
*/ |
248
|
|
|
private function listingHeader(): string |
249
|
|
|
{ |
250
|
|
|
$content = $this->listingHeaderContent(); |
251
|
|
|
|
252
|
|
|
$content = str_replace([ |
253
|
|
|
'{list-description}', |
254
|
|
|
'{list-title}' |
255
|
|
|
], [ |
256
|
|
|
Request::getParam('list-description', $this->listDescription), |
257
|
|
|
$this->listTitle |
258
|
|
|
], $content); |
259
|
|
|
|
260
|
|
|
return str_replace('{cells}', $this->listingHeaderCells(), $content); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Method compiles listing form |
265
|
|
|
* |
266
|
|
|
* @return string compiled listing form |
267
|
|
|
*/ |
268
|
|
|
public function listingForm(): string |
269
|
|
|
{ |
270
|
|
|
$records = $this->getListBuilderAdapter()->getRecords([ |
271
|
|
|
'field' => 'id', |
272
|
|
|
'order' => 'ASC' |
273
|
|
|
], isset($_GET['from']) ? $_GET['from'] : 0, isset($_GET['limit']) ? $_GET['limit'] : 100); |
274
|
|
|
|
275
|
|
|
if (! empty($records)) { |
276
|
|
|
$header = $this->listingHeader(); |
277
|
|
|
|
278
|
|
|
$items = $this->listingItems($records); |
279
|
|
|
|
280
|
|
|
$footer = BootstrapWidgets::get('listing-footer'); |
281
|
|
|
|
282
|
|
|
return $header . $items . $footer; |
283
|
|
|
} else { |
284
|
|
|
return $this->listingNoItems(); |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
|