|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright CONTENT CONTROL GmbH, http://www.contentcontrol-berlin.de |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace midcom\datamanager; |
|
7
|
|
|
|
|
8
|
|
|
use midcom_error; |
|
9
|
|
|
use Symfony\Component\OptionsResolver\Options; |
|
10
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
11
|
|
|
use midcom; |
|
12
|
|
|
use midcom_core_context; |
|
13
|
|
|
use midcom_services_i18n_l10n; |
|
14
|
|
|
use Symfony\Component\Form\Extension\Core\Type\PasswordType; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Experimental schema class |
|
18
|
|
|
*/ |
|
19
|
|
|
class schema |
|
20
|
|
|
{ |
|
21
|
|
|
private array $defaults = [ |
|
22
|
|
|
'operations' => ['save' => '', 'cancel' => ''], |
|
23
|
|
|
'fields' => [], |
|
24
|
|
|
'customdata' => [], |
|
25
|
|
|
'validation' => [], |
|
26
|
|
|
'action' => '' |
|
27
|
|
|
]; |
|
28
|
|
|
|
|
29
|
|
|
private array $config = []; |
|
30
|
|
|
|
|
31
|
|
|
private string $name = 'default'; |
|
32
|
|
|
|
|
33
|
198 |
|
public function __construct(array $config) |
|
34
|
|
|
{ |
|
35
|
198 |
|
$this->config = array_merge($this->defaults, [ |
|
36
|
198 |
|
'templates' => \midcom_baseclasses_components_configuration::get('midcom.datamanager', 'config')->get_array('templates') |
|
37
|
198 |
|
], $config); |
|
38
|
198 |
|
$this->complete_fields(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
194 |
|
public function set_name(string $name) |
|
42
|
|
|
{ |
|
43
|
194 |
|
$this->name = $name; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
54 |
|
public function get_name() : string |
|
47
|
|
|
{ |
|
48
|
54 |
|
return $this->name; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
187 |
|
public function get(string $key) |
|
52
|
|
|
{ |
|
53
|
187 |
|
return $this->config[$key]; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
12 |
|
public function set(string $key, $value) |
|
57
|
|
|
{ |
|
58
|
12 |
|
$this->config[$key] = $value; |
|
59
|
12 |
|
if ($key === 'fields') { |
|
60
|
8 |
|
$this->complete_fields(); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
50 |
|
public function has_field(string $name) : bool |
|
65
|
|
|
{ |
|
66
|
50 |
|
return array_key_exists($name, $this->config['fields']); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Returns reference to field config (for on the fly modification) |
|
71
|
|
|
*/ |
|
72
|
41 |
|
public function & get_field(string $name) : array |
|
73
|
|
|
{ |
|
74
|
41 |
|
if (!$this->has_field($name)) { |
|
75
|
|
|
throw new midcom_error('Field ' . $name . ' is not available in this schema'); |
|
76
|
|
|
} |
|
77
|
41 |
|
return $this->config['fields'][$name]; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
172 |
|
public function get_defaults() : array |
|
81
|
|
|
{ |
|
82
|
172 |
|
$defaults = []; |
|
83
|
172 |
|
foreach ($this->config['fields'] as $name => $config) { |
|
84
|
169 |
|
if (!empty($config['default'])) { |
|
85
|
40 |
|
$defaults[$name] = $config['default']; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
172 |
|
return $defaults; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
150 |
|
public function get_l10n() : midcom_services_i18n_l10n |
|
92
|
|
|
{ |
|
93
|
|
|
// Populate the l10n_schema member |
|
94
|
150 |
|
$l10n_name = $this->config['l10n_db'] ?? midcom_core_context::get()->get_key(MIDCOM_CONTEXT_COMPONENT); |
|
95
|
150 |
|
if (!$l10n_name || !midcom::get()->componentloader->is_installed($l10n_name)) { |
|
96
|
1 |
|
$l10n_name = 'midcom'; |
|
97
|
|
|
} |
|
98
|
150 |
|
return midcom::get()->i18n->get_l10n($l10n_name); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
198 |
|
private function complete_fields() |
|
102
|
|
|
{ |
|
103
|
198 |
|
foreach ($this->config['fields'] as $name => &$config) { |
|
104
|
195 |
|
$config = $this->resolve_field_options($config, $name); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
195 |
|
private function resolve_field_options(array $config, string $name) : array |
|
109
|
|
|
{ |
|
110
|
195 |
|
$resolver = new OptionsResolver(); |
|
111
|
|
|
|
|
112
|
195 |
|
$resolver->setDefaults(array_merge([ |
|
113
|
195 |
|
'title' => '', |
|
114
|
195 |
|
'description' => '', |
|
115
|
195 |
|
'type' => '', |
|
116
|
195 |
|
'type_config' => [], |
|
117
|
195 |
|
'widget' => null, |
|
118
|
195 |
|
'widget_config' => [], |
|
119
|
195 |
|
'required' => false, |
|
120
|
195 |
|
'readonly' => false, |
|
121
|
195 |
|
'hidden' => false, |
|
122
|
195 |
|
'default' => null, |
|
123
|
195 |
|
'storage' => '__UNSET__', |
|
124
|
195 |
|
'index_method' => 'auto', |
|
125
|
195 |
|
'index_merge_with_content' => true, |
|
126
|
195 |
|
'start_fieldset' => null, |
|
127
|
195 |
|
'end_fieldset' => null, |
|
128
|
195 |
|
'validation' => [], |
|
129
|
195 |
|
'helptext' => null, |
|
130
|
195 |
|
'write_privilege' => null, |
|
131
|
195 |
|
'customdata' => [] |
|
132
|
195 |
|
], $config)); |
|
133
|
|
|
|
|
134
|
195 |
|
$normalize_widget = function (Options $options, $value) { |
|
135
|
195 |
|
if ($value == 'text') { |
|
136
|
184 |
|
if (!empty($options['widget_config']['hideinput'])) { |
|
137
|
1 |
|
return PasswordType::class; |
|
138
|
|
|
} |
|
139
|
184 |
|
if ($options['type'] === 'number') { |
|
140
|
65 |
|
return 'number'; |
|
141
|
|
|
} |
|
142
|
174 |
|
if ($options['type'] === 'urlname') { |
|
143
|
38 |
|
return 'urlname'; |
|
144
|
|
|
} |
|
145
|
174 |
|
if (!empty($options['validation'])) { |
|
146
|
42 |
|
foreach ($options['validation'] as $rule) { |
|
147
|
42 |
|
if (is_array($rule) && $rule['type'] === 'email') { |
|
148
|
42 |
|
return 'email'; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
195 |
|
if ($value == 'select' && !empty($options['type_config']['allow_other'])) { |
|
154
|
16 |
|
return 'other'; |
|
155
|
|
|
} |
|
156
|
195 |
|
return $value; |
|
157
|
195 |
|
}; |
|
158
|
|
|
|
|
159
|
195 |
|
$normalize_storage = function (Options $options, $value) use ($name) { |
|
160
|
195 |
|
$default = [ |
|
161
|
195 |
|
'location' => 'parameter', |
|
162
|
195 |
|
'domain' => 'midcom.helper.datamanager2', |
|
163
|
195 |
|
'name' => $name |
|
164
|
195 |
|
]; |
|
165
|
195 |
|
if ($value === '__UNSET__') { |
|
166
|
54 |
|
return $default; |
|
167
|
|
|
} |
|
168
|
195 |
|
if ($options['type'] === 'privilege') { |
|
169
|
5 |
|
return [ |
|
170
|
5 |
|
'location' => 'privilege', |
|
171
|
5 |
|
'name' => $name |
|
172
|
5 |
|
]; |
|
173
|
|
|
} |
|
174
|
193 |
|
if ($options['type'] === 'tags' || $value === null) { |
|
175
|
116 |
|
return null; |
|
176
|
|
|
} |
|
177
|
186 |
|
if (is_string($value)) { |
|
178
|
177 |
|
if ($value === 'metadata') { |
|
179
|
2 |
|
return ['location' => $value, 'name' => $name]; |
|
180
|
|
|
} |
|
181
|
175 |
|
if ($value === 'parameter') { |
|
182
|
34 |
|
return $default; |
|
183
|
|
|
} |
|
184
|
174 |
|
return ['location' => $value]; |
|
185
|
|
|
} |
|
186
|
41 |
|
if (strtolower($value['location']) === 'parameter') { |
|
187
|
12 |
|
$value['location'] = strtolower($value['location']); |
|
188
|
12 |
|
if (!array_key_exists('domain', $value)) { |
|
189
|
|
|
$value['domain'] = 'midcom.helper.datamanager2'; |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
41 |
|
if (strtolower($value['location']) === 'configuration') { |
|
193
|
36 |
|
$value['location'] = 'parameter'; |
|
194
|
|
|
} |
|
195
|
41 |
|
return $value; |
|
196
|
195 |
|
}; |
|
197
|
|
|
|
|
198
|
195 |
|
$normalize_validation = function (Options $options, $value) { |
|
199
|
195 |
|
$validation = []; |
|
200
|
|
|
|
|
201
|
195 |
|
foreach ((array) $value as $key => $rule) { |
|
202
|
42 |
|
if (!is_array($rule)) { |
|
203
|
42 |
|
if (is_object($rule)) { |
|
204
|
|
|
$validation[] = $rule; |
|
205
|
|
|
continue; |
|
206
|
|
|
} |
|
207
|
42 |
|
$rule = ['type' => $rule]; |
|
208
|
2 |
|
} elseif (!array_key_exists('type', $rule)) { |
|
209
|
|
|
throw new midcom_error("Missing validation rule type for rule {$key} on field {$options['name']}, this is a required option."); |
|
210
|
2 |
|
} elseif ( $rule['type'] == 'compare' |
|
211
|
2 |
|
&& !array_key_exists('compare_with', $rule)) { |
|
212
|
|
|
throw new midcom_error("Missing compare_with option for compare type rule {$key} on field {$options['name']}, this is a required option."); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
42 |
|
$defaults = [ |
|
216
|
42 |
|
'message' => "validation failed: {$rule['type']}", |
|
217
|
42 |
|
'format' => '' |
|
218
|
42 |
|
]; |
|
219
|
|
|
|
|
220
|
42 |
|
$validation[] = array_merge($defaults, $rule); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
195 |
|
return $validation; |
|
224
|
195 |
|
}; |
|
225
|
|
|
|
|
226
|
195 |
|
$normalize_type = function(Options $options, $value) { |
|
227
|
195 |
|
if ($value === 'tags') { |
|
228
|
54 |
|
return 'net_nemein_tag_datamanager_storage'; |
|
229
|
|
|
} |
|
230
|
195 |
|
return $value; |
|
231
|
195 |
|
}; |
|
232
|
|
|
|
|
233
|
195 |
|
$resolver->setNormalizer('storage', $normalize_storage); |
|
234
|
195 |
|
$resolver->setNormalizer('widget', $normalize_widget); |
|
235
|
195 |
|
$resolver->setNormalizer('validation', $normalize_validation); |
|
236
|
195 |
|
$resolver->setNormalizer('type', $normalize_type); |
|
237
|
|
|
|
|
238
|
195 |
|
return $resolver->resolve($config); |
|
239
|
|
|
} |
|
240
|
|
|
} |
|
241
|
|
|
|