|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jidaikobo\Kontiki\Models\BaseModelTraits; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Trait FieldDefinitionTrait |
|
7
|
|
|
* |
|
8
|
|
|
* Provides methods for defining and managing field definitions in models. |
|
9
|
|
|
*/ |
|
10
|
|
|
trait FieldDefinitionTrait |
|
11
|
|
|
{ |
|
12
|
|
|
protected ?array $fieldDefinitions = null; |
|
13
|
|
|
protected ?array $metaDataFieldDefinitions = null; |
|
14
|
|
|
|
|
15
|
|
|
public function getFields( |
|
16
|
|
|
string $context = '', |
|
17
|
|
|
array $data = [], |
|
18
|
|
|
int $id = null |
|
19
|
|
|
): array { |
|
20
|
|
|
if (!empty($context)) { |
|
21
|
|
|
$this->processFields($context, $data, $id); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
return array_merge($this->fieldDefinitions, $this->metaDataFieldDefinitions); |
|
|
|
|
|
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function getFieldDefinitions(): array |
|
28
|
|
|
{ |
|
29
|
|
|
return $this->fieldDefinitions; |
|
|
|
|
|
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function getMetaDataFieldDefinitions(): array |
|
33
|
|
|
{ |
|
34
|
|
|
return $this->metaDataFieldDefinitions; |
|
|
|
|
|
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
protected function defineFieldDefinitions(): void |
|
38
|
|
|
{ |
|
39
|
|
|
$this->fieldDefinitions = []; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
protected function defineMetaDataFieldDefinitions(): void |
|
43
|
|
|
{ |
|
44
|
|
|
$this->metaDataFieldDefinitions = []; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
protected function processFieldDefinitions( |
|
48
|
|
|
string $context = '', |
|
|
|
|
|
|
49
|
|
|
array $data = [], |
|
|
|
|
|
|
50
|
|
|
int $id = null |
|
|
|
|
|
|
51
|
|
|
): void { |
|
52
|
|
|
return; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
protected function disableFormFieldsForContext(): void |
|
56
|
|
|
{ |
|
57
|
|
|
foreach (['metaDataFieldDefinitions', 'fieldDefinitions'] as $fieldType) { |
|
58
|
|
|
foreach ($this->$fieldType as &$field) { |
|
59
|
|
|
// Set input fields to readonly |
|
60
|
|
|
$field['attributes']['readonly'] = 'readonly'; |
|
61
|
|
|
|
|
62
|
|
|
// Remove file upload class and add "form-control-plaintext" |
|
63
|
|
|
$existingClass = $field['attributes']['class'] ?? ''; |
|
64
|
|
|
$existingClass = str_replace('kontiki-file-upload', '', $existingClass); |
|
65
|
|
|
$field['attributes']['class'] = trim( |
|
66
|
|
|
$existingClass . ' form-control-plaintext p-2' |
|
67
|
|
|
); |
|
68
|
|
|
|
|
69
|
|
|
// Remove descriptions |
|
70
|
|
|
$field['description'] = ''; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
private function fillValueFieldDefinitions( |
|
76
|
|
|
string $context = '', |
|
|
|
|
|
|
77
|
|
|
array $data = [], |
|
78
|
|
|
int $id = null |
|
|
|
|
|
|
79
|
|
|
): void { |
|
80
|
|
|
foreach (['metaDataFieldDefinitions', 'fieldDefinitions'] as $fieldType) { |
|
81
|
|
|
foreach (array_keys($this->$fieldType) as $fieldName) { |
|
82
|
|
|
if (isset($data[$fieldName])) { |
|
83
|
|
|
$this->$fieldType[$fieldName]['default'] = $data[$fieldName]; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
private function initializeFields(): void |
|
90
|
|
|
{ |
|
91
|
|
|
if ($this->fieldDefinitions === null) { |
|
92
|
|
|
$this->defineFieldDefinitions(); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
private function initializeMetaDataFields(): void |
|
97
|
|
|
{ |
|
98
|
|
|
if ($this->metaDataFieldDefinitions === null) { |
|
99
|
|
|
$this->defineMetaDataFieldDefinitions(); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
private function processFields( |
|
104
|
|
|
string $context = '', |
|
105
|
|
|
array $data = [], |
|
106
|
|
|
int $id = null |
|
107
|
|
|
): void { |
|
108
|
|
|
// set values for save / form |
|
109
|
|
|
$this->fillValueFieldDefinitions($context, $data, $id); |
|
110
|
|
|
|
|
111
|
|
|
// process definitions for various purpose |
|
112
|
|
|
$this->processFieldDefinitions($context, $data, $id); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Get the definition for Id field. |
|
117
|
|
|
* |
|
118
|
|
|
* wrapper of read-only field. |
|
119
|
|
|
*/ |
|
120
|
|
|
protected function getIdField(): array |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->getReadOnlyField( |
|
123
|
|
|
__('ID'), |
|
124
|
|
|
[ |
|
125
|
|
|
'display_in_list' => true |
|
126
|
|
|
] |
|
127
|
|
|
); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Get the definition for a read-only field. |
|
132
|
|
|
* |
|
133
|
|
|
* This method defines the structure of a field that is displayed in lists |
|
134
|
|
|
* but not included in form inputs. |
|
135
|
|
|
* |
|
136
|
|
|
* @param string $label The label to be used for the field. |
|
137
|
|
|
* @param array $options Optional settings for the field. |
|
138
|
|
|
* @return array The read-only field definition. |
|
139
|
|
|
*/ |
|
140
|
|
|
protected function getReadOnlyField(string $label, array $options = []): array |
|
141
|
|
|
{ |
|
142
|
|
|
return [ |
|
143
|
|
|
'label' => $label, |
|
144
|
|
|
'display_in_list' => $options['display_in_list'] ?? false, |
|
145
|
|
|
'save_as_utc' => $options['save_as_utc'] ?? false, |
|
146
|
|
|
]; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Generate a text field definition. |
|
151
|
|
|
* |
|
152
|
|
|
* @param string $label The field name. |
|
153
|
|
|
* @param array $options Optional settings for the field. |
|
154
|
|
|
* @return array Field definition. |
|
155
|
|
|
*/ |
|
156
|
|
|
protected function getField(string $label, array $options = []): array |
|
157
|
|
|
{ |
|
158
|
|
|
return [ |
|
159
|
|
|
'label' => $options['label'] ?? __($label), |
|
160
|
|
|
'type' => $options['type'] ?? 'text', |
|
161
|
|
|
'description' => $options['description'] ?? '', |
|
162
|
|
|
'attributes' => $options['attributes'] ?? ['class' => 'form-control'], |
|
163
|
|
|
'label_attributes' => $options['label_attributes'] ?? ['class' => 'form-label'], |
|
164
|
|
|
'options' => $options['options'] ?? [], |
|
165
|
|
|
'default' => $options['default'] ?? '', |
|
166
|
|
|
'searchable' => $options['searchable'] ?? true, |
|
167
|
|
|
'rules' => $options['rules'] ?? [], |
|
168
|
|
|
'filter' => $options['filter'] ?? (defined('FILTER_SANITIZE_FULL_SPECIAL_CHARS') |
|
169
|
|
|
? FILTER_SANITIZE_FULL_SPECIAL_CHARS |
|
|
|
|
|
|
170
|
|
|
: FILTER_SANITIZE_SPECIAL_CHARS), |
|
171
|
|
|
'template' => $options['template'] ?? 'default', |
|
172
|
|
|
'group' => $options['group'] ?? 'main', |
|
173
|
|
|
'fieldset_template' => $options['fieldset_template'] ?? 'forms/fieldset/flat.php', |
|
174
|
|
|
'display_in_list' => $options['display_in_list'] ?? false, |
|
175
|
|
|
'save_as_utc' => $options['save_as_utc'] ?? false |
|
176
|
|
|
]; |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|