|
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\Form\FormBuilderInterface; |
|
10
|
|
|
use Symfony\Component\OptionsResolver\Options; |
|
11
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
12
|
|
|
use Symfony\Component\Validator\Constraints\NotBlank; |
|
13
|
|
|
use midcom; |
|
14
|
|
|
use midcom_core_context; |
|
15
|
|
|
use midcom\datamanager\extension\compat; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Experimental schema class |
|
19
|
|
|
*/ |
|
20
|
|
|
class schema |
|
21
|
|
|
{ |
|
22
|
|
|
private $defaults = array( |
|
23
|
|
|
'operations' => array('save' => '', 'cancel' => '') |
|
24
|
|
|
); |
|
25
|
|
|
|
|
26
|
|
|
private $config = array(); |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
private $name = 'default'; |
|
33
|
5 |
|
|
|
34
|
|
|
public function __construct(array $config) |
|
35
|
5 |
|
{ |
|
36
|
5 |
|
$this->config = array_merge($this->defaults, $config); |
|
37
|
5 |
|
$this->complete_fields(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $name |
|
43
|
1 |
|
*/ |
|
44
|
|
|
public function set_name($name) |
|
45
|
1 |
|
{ |
|
46
|
1 |
|
$this->name = $name; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* |
|
51
|
|
|
* @return string |
|
52
|
1 |
|
*/ |
|
53
|
|
|
public function get_name() |
|
54
|
1 |
|
{ |
|
55
|
|
|
return $this->name; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* |
|
60
|
|
|
* @param FormBuilderInterface $builder |
|
61
|
|
|
* @return \Symfony\Component\Form\Form |
|
62
|
1 |
|
*/ |
|
63
|
|
|
public function build_form(FormBuilderInterface $builder) |
|
64
|
1 |
|
{ |
|
65
|
|
|
foreach ($this->config['fields'] as $name => $config) { |
|
66
|
|
|
$options = array( |
|
67
|
|
|
'label' => $config['title'], |
|
68
|
|
|
'widget_config' => $config['widget_config'], |
|
69
|
|
|
'type_config' => $config['type_config'], |
|
70
|
|
|
'required' => $config['required'], |
|
71
|
|
|
'constraints' => $config['required'] ? array(new NotBlank()) : null, |
|
72
|
|
|
'dm2_type' => $config['type'], |
|
73
|
|
|
'start_fieldset' => $config['start_fieldset'], |
|
74
|
|
|
'end_fieldset' => $config['end_fieldset'], |
|
75
|
|
|
'index_method' => $config['index_method'] |
|
76
|
|
|
); |
|
77
|
|
|
|
|
78
|
|
|
// Symfony < 2.8 compat |
|
79
|
|
|
if (compat::is_legacy()) { |
|
80
|
|
|
$options['read_only'] = $config['readonly']; |
|
81
|
|
|
} else { |
|
82
|
|
|
$options['attr']['readonly'] = $config['readonly']; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$builder->add($name, compat::get_type_name($config['widget']), $options); |
|
86
|
|
|
} |
|
87
|
1 |
|
|
|
88
|
1 |
|
$builder->add('form_toolbar', compat::get_type_name('toolbar'), array('operations' => $this->config['operations'])); |
|
89
|
|
|
return $builder->getForm(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* |
|
94
|
|
|
* @return array |
|
95
|
5 |
|
*/ |
|
96
|
|
|
public function get_fields() |
|
97
|
5 |
|
{ |
|
98
|
|
|
return $this->config['fields']; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* |
|
103
|
|
|
* @return \midcom_services_i18n_l10n |
|
104
|
1 |
|
*/ |
|
105
|
|
|
public function get_l10n() |
|
106
|
|
|
{ |
|
107
|
1 |
|
// Populate the l10n_schema member |
|
108
|
|
|
if (array_key_exists('l10n_db', $this->config)) { |
|
109
|
|
|
$l10n_name = $this->config['l10n_db']; |
|
110
|
1 |
|
} else { |
|
111
|
|
|
$l10n_name = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_COMPONENT); |
|
112
|
1 |
|
} |
|
113
|
1 |
|
if (!midcom::get()->componentloader->is_installed($l10n_name)) { |
|
114
|
|
|
$l10n_name = 'midcom'; |
|
115
|
1 |
|
} |
|
116
|
|
|
return midcom::get()->i18n->get_l10n($l10n_name); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
5 |
|
|
|
120
|
|
|
private function complete_fields() |
|
121
|
5 |
|
{ |
|
122
|
4 |
|
foreach ($this->config['fields'] as $name => &$config) { |
|
123
|
|
|
$config = $this->resolve_field_options($config, $name); |
|
124
|
5 |
|
} |
|
125
|
|
|
} |
|
126
|
4 |
|
|
|
127
|
|
|
private function resolve_field_options(array $config, $name) |
|
128
|
4 |
|
{ |
|
129
|
|
|
$resolver = new OptionsResolver(); |
|
130
|
4 |
|
|
|
131
|
4 |
|
$resolver->setDefaults(array( |
|
132
|
|
|
'title' => '', |
|
133
|
|
|
'type' => null, |
|
134
|
|
|
'type_config' => array(), |
|
135
|
|
|
'widget' => null, |
|
136
|
|
|
'widget_config' => array(), |
|
137
|
|
|
'required' => false, |
|
138
|
|
|
'readonly' => false, |
|
139
|
|
|
'default' => null, |
|
140
|
|
|
'storage' => '__UNSET__', |
|
141
|
|
|
'index_method' => 'auto', |
|
142
|
|
|
'start_fieldset' => null, |
|
143
|
|
|
'end_fieldset' => null, |
|
144
|
|
|
'validation' => array() |
|
145
|
|
|
)); |
|
146
|
4 |
|
|
|
147
|
4 |
|
$normalize_widget = function (Options $options, $value) { |
|
148
|
1 |
|
if ( $value == 'images' |
|
149
|
|
|
|| $value == 'downloads') { |
|
150
|
3 |
|
return 'subform'; |
|
151
|
4 |
|
} |
|
152
|
|
|
return $value; |
|
153
|
4 |
|
}; |
|
154
|
|
|
|
|
155
|
4 |
|
$normalize_storage = function (Options $options, $value) use ($name) { |
|
156
|
4 |
|
$default = array( |
|
157
|
4 |
|
'location' => 'parameter', |
|
158
|
|
|
'domain' => 'midcom.helper.datamanager2', |
|
159
|
4 |
|
'name' => $name |
|
160
|
|
|
); |
|
161
|
|
|
if ($value === '__UNSET__') { |
|
162
|
4 |
|
return $default; |
|
163
|
1 |
|
} |
|
164
|
|
|
if ($value === null) { |
|
165
|
3 |
|
|
|
166
|
1 |
|
if($options['type'] === 'privilege') |
|
167
|
|
|
{ |
|
168
|
|
|
return array( |
|
169
|
1 |
|
'location' => 'privilege', |
|
170
|
1 |
|
'name' => $name |
|
171
|
|
|
); |
|
172
|
|
|
} |
|
173
|
|
|
return null; |
|
174
|
2 |
|
} |
|
175
|
2 |
|
if (is_string($value)) { |
|
176
|
2 |
|
if ($value === 'metadata') { |
|
177
|
|
|
return array('location' => $value, 'name' => $name); |
|
178
|
|
|
} |
|
179
|
|
|
if ($value === 'parameter') { |
|
180
|
2 |
|
return $default; |
|
181
|
|
|
} |
|
182
|
|
|
return array('location' => $value); |
|
183
|
2 |
|
} |
|
184
|
4 |
|
if (strtolower($value['location']) === 'parameter') { |
|
185
|
|
|
$value['location'] = strtolower($value['location']); |
|
186
|
4 |
|
if (!array_key_exists('domain', $value)) { |
|
187
|
4 |
|
$value['domain'] = 'midcom.helper.datamanager2'; |
|
188
|
|
|
} |
|
189
|
4 |
|
} |
|
190
|
|
|
if (strtolower($value['location']) === 'configuration') { |
|
191
|
|
|
$value['location'] = 'parameter'; |
|
192
|
|
|
} |
|
193
|
|
|
return $value; |
|
194
|
|
|
}; |
|
195
|
|
|
|
|
196
|
|
|
$normalize_validation = function (Options $options, $config) { |
|
197
|
|
|
$validation = array(); |
|
198
|
|
|
if (array_key_exists('validation',(array) $config)) { |
|
199
|
|
|
$validation = (array) $config['validation']; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
foreach ($validation as $key => $rule) { |
|
203
|
|
|
if (!is_array($rule)) { |
|
204
|
|
|
$rule = array('type' => $rule); |
|
205
|
|
|
} elseif (!array_key_exists('type', $rule)) { |
|
206
|
|
|
throw new midcom_error("Missing validation rule type for rule {$key} on field {$config['name']}, this is a required option."); |
|
207
|
|
|
} elseif ( $rule['type'] == 'compare' |
|
208
|
|
|
&& !array_key_exists('compare_with', $rule)) { |
|
209
|
|
|
throw new midcom_error("Missing compare_with option for compare type rule {$key} on field {$config['name']}, this is a required option."); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
$defaults = array( |
|
213
|
|
|
'message' => "validation failed: {$rule['type']}", |
|
214
|
|
|
'format' => '' |
|
215
|
|
|
); |
|
216
|
|
|
|
|
217
|
|
|
$validation[$key] = array_merge($defaults, $rule); |
|
218
|
|
|
} |
|
219
|
|
|
return $validation; |
|
220
|
|
|
}; |
|
221
|
|
|
|
|
222
|
|
|
$resolver->setNormalizer('storage', $normalize_storage); |
|
223
|
|
|
$resolver->setNormalizer('widget', $normalize_widget); |
|
224
|
|
|
$resolver->setNormalizer('validation', $normalize_validation); |
|
225
|
|
|
|
|
226
|
|
|
return $resolver->resolve($config); |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
|