|
1
|
|
|
<?php |
|
2
|
|
|
namespace Mezon\Gui\ListBuilder; |
|
3
|
|
|
|
|
4
|
|
|
use Mezon\Gui\WidgetsRegistry\BootstrapWidgets; |
|
5
|
|
|
|
|
6
|
|
|
class Base |
|
7
|
|
|
{ |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Fields |
|
11
|
|
|
* |
|
12
|
|
|
* @var array |
|
13
|
|
|
*/ |
|
14
|
|
|
private $fields = []; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Service logic adapter |
|
18
|
|
|
* |
|
19
|
|
|
* @var ListBuilderAdapter |
|
20
|
|
|
*/ |
|
21
|
|
|
private $listBuilderAdapter = false; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* List item transformation callback |
|
25
|
|
|
* |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
private $recordTransformer = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* List title |
|
32
|
|
|
* |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
public $listTitle = 'Список записей'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* List description |
|
39
|
|
|
* |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
public $listDescription = 'Выберите необходимое действие'; |
|
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
|
|
|
* Method returns content for the case when no items were found |
|
70
|
|
|
* |
|
71
|
|
|
* @return string content for the case when no items were found |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getNoItemsContent(): string |
|
74
|
|
|
{ |
|
75
|
|
|
return str_replace([ |
|
76
|
|
|
'{list-description}', |
|
77
|
|
|
'{list-title}' |
|
78
|
|
|
], [ |
|
79
|
|
|
'Ни одной записи не найдено', |
|
80
|
|
|
$this->listTitle |
|
81
|
|
|
], BootstrapWidgets::get('listing-no-items')); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Setting record transformer |
|
86
|
|
|
* |
|
87
|
|
|
* @param mixed $recordTransformer |
|
88
|
|
|
* callable record transformer |
|
89
|
|
|
* @codeCoverageIgnore |
|
90
|
|
|
*/ |
|
91
|
|
|
public function setRecordTransformer($recordTransformer): void |
|
92
|
|
|
{ |
|
93
|
|
|
$this->recordTransformer = $recordTransformer; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Method returns fields |
|
98
|
|
|
* |
|
99
|
|
|
* @return array fields |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getFields(): array |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->fields; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Method returns list builder adapter |
|
108
|
|
|
* |
|
109
|
|
|
* @return ListBuilderAdapter |
|
110
|
|
|
*/ |
|
111
|
|
|
protected function getListBuilderAdapter(): ListBuilderAdapter |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->listBuilderAdapter; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Method transforms database record |
|
118
|
|
|
* |
|
119
|
|
|
* @param array $record |
|
120
|
|
|
* Transforming record |
|
121
|
|
|
* @return object Transformed record |
|
122
|
|
|
*/ |
|
123
|
|
|
protected function transformRecord(object $record): object |
|
124
|
|
|
{ |
|
125
|
|
|
// here we assume that we get from service |
|
126
|
|
|
// already transformed |
|
127
|
|
|
// and here we provide only additional transformations |
|
128
|
|
|
if (is_callable($this->recordTransformer)) { |
|
129
|
|
|
$record = call_user_func($this->recordTransformer, $record); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
return $record; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|