|
1
|
|
|
<?php |
|
2
|
|
|
namespace Mezon\Gui\ListBuilder; |
|
3
|
|
|
|
|
4
|
|
|
use Mezon\Gui\WidgetsRegistry\BootstrapWidgets; |
|
5
|
|
|
use Mezon\TemplateEngine\TemplateEngine; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class Simple |
|
9
|
|
|
* |
|
10
|
|
|
* @package GUI |
|
11
|
|
|
* @subpackage ListBuilder |
|
12
|
|
|
* @author Dodonov A.A. |
|
13
|
|
|
* @version v.1.0 (2020/12/19) |
|
14
|
|
|
* @copyright Copyright (c) 2020, aeon.org |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class constructs grids |
|
19
|
|
|
*/ |
|
20
|
|
|
class Simple |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Fields |
|
25
|
|
|
* |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
private $fields = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Service logic adapter |
|
32
|
|
|
* |
|
33
|
|
|
* @var \Mezon\Gui\ListBuilder\ListBuilderAdapter |
|
34
|
|
|
*/ |
|
35
|
|
|
private $listBuilderAdapter = false; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* List item transformation callback |
|
39
|
|
|
* |
|
40
|
|
|
* @var array |
|
41
|
|
|
*/ |
|
42
|
|
|
private $recordTransformer = []; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Constructor |
|
46
|
|
|
* |
|
47
|
|
|
* @param array $fields |
|
48
|
|
|
* List of fields |
|
49
|
|
|
* @param \Mezon\Gui\ListBuilder\ListBuilderAdapter $listBuilderAdapter |
|
50
|
|
|
* Adapter for the data source |
|
51
|
|
|
*/ |
|
52
|
|
|
public function __construct(array $fields, ListBuilderAdapter $listBuilderAdapter) |
|
53
|
|
|
{ |
|
54
|
|
|
$transformedFields = []; |
|
55
|
|
|
|
|
56
|
|
|
foreach ($fields as $i => $field) { |
|
57
|
|
|
$key = is_array($field) ? $i : $field; |
|
58
|
|
|
$transformedFields[$key] = is_array($field) ? $field : [ |
|
59
|
|
|
'title' => $field |
|
60
|
|
|
]; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$this->fields = $transformedFields; |
|
64
|
|
|
|
|
65
|
|
|
$this->listBuilderAdapter = $listBuilderAdapter; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Setting record transformer |
|
70
|
|
|
* |
|
71
|
|
|
* @param mixed $recordTransformer |
|
72
|
|
|
* callable record transformer |
|
73
|
|
|
* @codeCoverageIgnore |
|
74
|
|
|
*/ |
|
75
|
|
|
public function setRecordTransformer($recordTransformer): void |
|
76
|
|
|
{ |
|
77
|
|
|
$this->recordTransformer = $recordTransformer; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Method compiles listing items cells |
|
82
|
|
|
* |
|
83
|
|
|
* @return string Compiled row |
|
84
|
|
|
*/ |
|
85
|
|
|
private function listingItemsCells(): string |
|
86
|
|
|
{ |
|
87
|
|
|
$content = ''; |
|
88
|
|
|
|
|
89
|
|
|
foreach (array_keys($this->fields) as $name) { |
|
90
|
|
|
if ($name == 'domain_id') { |
|
91
|
|
|
continue; |
|
92
|
|
|
} |
|
93
|
|
|
if ($name == 'id') { |
|
94
|
|
|
$content .= BootstrapWidgets::get('listing-row-centered-cell'); |
|
95
|
|
|
} else { |
|
96
|
|
|
$content .= BootstrapWidgets::get('listing-row-cell'); |
|
97
|
|
|
} |
|
98
|
|
|
$content = str_replace('{name}', '{' . $name . '}', $content); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $content; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Method transforms database record |
|
106
|
|
|
* |
|
107
|
|
|
* @param array $record |
|
108
|
|
|
* Transforming record |
|
109
|
|
|
* @return object Transformed record |
|
110
|
|
|
*/ |
|
111
|
|
|
private function transformRecord(object $record): object |
|
112
|
|
|
{ |
|
113
|
|
|
// here we assume that we get from service |
|
114
|
|
|
// already transformed |
|
115
|
|
|
// and here we provide only additional transformations |
|
116
|
|
|
if (is_callable($this->recordTransformer)) { |
|
117
|
|
|
$record = call_user_func($this->recordTransformer, $record); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return $record; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Method compiles header cells |
|
125
|
|
|
* |
|
126
|
|
|
* @return string Compiled header |
|
127
|
|
|
*/ |
|
128
|
|
|
private function listingHeaderCells(): string |
|
129
|
|
|
{ |
|
130
|
|
|
$content = ''; |
|
131
|
|
|
|
|
132
|
|
|
foreach ($this->fields as $name => $data) { |
|
133
|
|
|
if ($name == 'domain_id') { |
|
134
|
|
|
continue; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
$idStyle = $name == 'id' ? 'style="text-align: center; width:5%;"' : ''; |
|
138
|
|
|
|
|
139
|
|
|
$content .= BootstrapWidgets::get('listing-header-cell'); |
|
140
|
|
|
$content = str_replace([ |
|
141
|
|
|
'{id-style}', |
|
142
|
|
|
'{title}' |
|
143
|
|
|
], [ |
|
144
|
|
|
$idStyle, |
|
145
|
|
|
$data['title'] |
|
146
|
|
|
], $content); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return $content; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Method compiles listing header |
|
154
|
|
|
* |
|
155
|
|
|
* @return string Compiled header |
|
156
|
|
|
*/ |
|
157
|
|
|
private function simpleListingHeader(): string |
|
158
|
|
|
{ |
|
159
|
|
|
$content = BootstrapWidgets::get('simple-listing-header'); |
|
160
|
|
|
|
|
161
|
|
|
// TODO use Request |
|
162
|
|
|
$content = str_replace( |
|
163
|
|
|
'{description}', |
|
164
|
|
|
isset($_GET['description']) ? $_GET['description'] : 'Выберите необходимое действие', |
|
165
|
|
|
$content); |
|
166
|
|
|
|
|
167
|
|
|
return str_replace('{cells}', $this->listingHeaderCells(), $content); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Method compiles listing items |
|
172
|
|
|
* |
|
173
|
|
|
* @param array $records |
|
174
|
|
|
* List of records |
|
175
|
|
|
* @return string Compiled simple list |
|
176
|
|
|
*/ |
|
177
|
|
|
private function simpleListingItems(array $records): string |
|
178
|
|
|
{ |
|
179
|
|
|
$content = ''; |
|
180
|
|
|
|
|
181
|
|
|
foreach ($records as $record) { |
|
182
|
|
|
$content .= str_replace('{items}', $this->listingItemsCells(), BootstrapWidgets::get('listing-row')); |
|
183
|
|
|
|
|
184
|
|
|
$record = $this->transformRecord($record); |
|
185
|
|
|
|
|
186
|
|
|
$record = $this->listBuilderAdapter->preprocessListItem($record); |
|
187
|
|
|
|
|
188
|
|
|
$content = TemplateEngine::printRecord($content, $record); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
return $content; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Method compiles simple_listing form |
|
196
|
|
|
* |
|
197
|
|
|
* @return string Compiled simple listing form |
|
198
|
|
|
*/ |
|
199
|
|
|
public function listingForm(): string |
|
200
|
|
|
{ |
|
201
|
|
|
$records = $this->listBuilderAdapter->all(); |
|
202
|
|
|
|
|
203
|
|
|
if (! empty($records)) { |
|
204
|
|
|
$header = $this->simpleListingHeader(); |
|
205
|
|
|
|
|
206
|
|
|
$items = $this->simpleListingItems($records); |
|
207
|
|
|
|
|
208
|
|
|
// they are the same with full feature listing |
|
209
|
|
|
$footer = BootstrapWidgets::get('listing-footer'); |
|
210
|
|
|
|
|
211
|
|
|
return $header . $items . $footer; |
|
212
|
|
|
} else { |
|
213
|
|
|
return BootstrapWidgets::get('listing-no-items'); |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Method returns fields of the list |
|
219
|
|
|
* |
|
220
|
|
|
* @return array fields list |
|
221
|
|
|
*/ |
|
222
|
|
|
public function getFields(): array |
|
223
|
|
|
{ |
|
224
|
|
|
return $this->fields; |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
|