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