|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright CONTENT CONTROL GmbH, http://www.contentcontrol-berlin.de |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace midcom\datamanager\extension\type; |
|
7
|
|
|
|
|
8
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
9
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
|
10
|
|
|
use Symfony\Component\OptionsResolver\Options; |
|
11
|
|
|
use Symfony\Component\Form\AbstractType; |
|
12
|
|
|
use midcom\datamanager\extension\transformer\autocompleteTransformer; |
|
13
|
|
|
use midcom\datamanager\extension\transformer\jsonTransformer; |
|
14
|
|
|
use midcom\datamanager\extension\transformer\multipleTransformer; |
|
15
|
|
|
use midcom\datamanager\extension\helper; |
|
16
|
|
|
use midcom\datamanager\helper\autocomplete as autocomplete_helper; |
|
17
|
|
|
use midcom_error; |
|
18
|
|
|
use midcom_connection; |
|
19
|
|
|
use Symfony\Component\Form\FormInterface; |
|
20
|
|
|
use Symfony\Component\Form\FormView; |
|
21
|
|
|
use Symfony\Component\Form\Extension\Core\Type\HiddenType; |
|
22
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SearchType; |
|
23
|
|
|
use Symfony\Component\Form\Extension\Core\Type\FormType; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Experimental autocomplete type |
|
27
|
|
|
*/ |
|
28
|
|
|
class autocompleteType extends AbstractType |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* {@inheritdoc} |
|
32
|
|
|
*/ |
|
33
|
90 |
|
public function configureOptions(OptionsResolver $resolver) |
|
34
|
|
|
{ |
|
35
|
1 |
|
$resolver->setDefault('error_bubbling', false); |
|
36
|
1 |
|
$resolver->setNormalizer('widget_config', function (Options $options, $value) { |
|
37
|
90 |
|
$widget_defaults = [ |
|
38
|
90 |
|
'creation_mode_enabled' => false, |
|
39
|
90 |
|
'class' => null, |
|
40
|
90 |
|
'component' => null, // unused, for backward-compat only |
|
41
|
90 |
|
'id_field' => 'guid', |
|
42
|
90 |
|
'constraints' => [], |
|
43
|
90 |
|
'result_headers' => [], |
|
44
|
90 |
|
'orders' => [], |
|
45
|
90 |
|
'auto_wildcards' => 'both', |
|
46
|
90 |
|
'creation_handler' => null, |
|
47
|
90 |
|
'creation_default_key' => null, |
|
48
|
90 |
|
'titlefield' => null, |
|
49
|
90 |
|
'categorize_by_parent_label' => false, |
|
50
|
90 |
|
'searchfields' => [], |
|
51
|
90 |
|
'min_chars' => 2, |
|
52
|
90 |
|
'handler_url' => midcom_connection::get_url('self') . 'midcom-exec-midcom.datamanager/autocomplete.php', |
|
53
|
90 |
|
'sortable' => false, |
|
54
|
90 |
|
'clever_class' => null |
|
55
|
90 |
|
]; |
|
56
|
|
|
|
|
57
|
90 |
|
if (!empty($value['clever_class'])) { |
|
58
|
|
|
/** @var \midcom_helper_configuration $config */ |
|
59
|
57 |
|
$config = \midcom_baseclasses_components_configuration::get('midcom.datamanager', 'config'); |
|
60
|
57 |
|
$config = $config->get_array('clever_classes'); |
|
61
|
57 |
|
if (!array_key_exists($value['clever_class'], $config)) { |
|
62
|
|
|
throw new midcom_error('Invalid clever class specified'); |
|
63
|
|
|
} |
|
64
|
57 |
|
$value = array_merge($config[$value['clever_class']], $value); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
90 |
|
return helper::normalize($widget_defaults, $value); |
|
68
|
1 |
|
}); |
|
69
|
1 |
|
$resolver->setNormalizer('type_config', function (Options $options, $value) { |
|
70
|
90 |
|
$type_defaults = [ |
|
71
|
90 |
|
'options' => [], |
|
72
|
90 |
|
'method' => 'GET', |
|
73
|
90 |
|
'constraints' => [], |
|
74
|
90 |
|
'allow_other' => false, |
|
75
|
90 |
|
'allow_multiple' => ($options['dm2_type'] == 'mnrelation'), |
|
76
|
90 |
|
'require_corresponding_option' => true, |
|
77
|
90 |
|
'multiple_storagemode' => 'serialized', |
|
78
|
90 |
|
'multiple_separator' => '|' |
|
79
|
90 |
|
]; |
|
80
|
|
|
|
|
81
|
90 |
|
$resolved = helper::normalize($type_defaults, $value); |
|
82
|
90 |
|
if (empty($resolved['constraints']) && !empty($options['widget_config']['constraints'])) { |
|
83
|
26 |
|
$resolved['constraints'] = $options['widget_config']['constraints']; |
|
84
|
|
|
} |
|
85
|
90 |
|
return $resolved; |
|
86
|
1 |
|
}); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* {@inheritdoc} |
|
91
|
|
|
*/ |
|
92
|
90 |
|
public function buildForm(FormBuilderInterface $builder, array $options) |
|
93
|
|
|
{ |
|
94
|
90 |
|
$builder->addModelTransformer(new autocompleteTransformer($options)); |
|
95
|
90 |
|
$builder->add('selection', HiddenType::class); |
|
96
|
90 |
|
$builder->get('selection')->addViewTransformer(new jsonTransformer); |
|
97
|
|
|
|
|
98
|
90 |
|
if ($options['type_config']['allow_multiple'] && $options['dm2_type'] == 'select') { |
|
99
|
19 |
|
$builder->get('selection')->addModelTransformer(new multipleTransformer($options)); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
90 |
|
$builder->add('search_input', SearchType::class, ['mapped' => false]); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* {@inheritdoc} |
|
107
|
|
|
*/ |
|
108
|
89 |
|
public function finishView(FormView $view, FormInterface $form, array $options) |
|
109
|
|
|
{ |
|
110
|
89 |
|
$preset = []; |
|
111
|
89 |
|
if ( !empty($view->children['selection']->vars['data']) |
|
112
|
89 |
|
&& !empty($options['widget_config']['class'])) { |
|
113
|
38 |
|
foreach (array_filter((array) $view->children['selection']->vars['data']) as $identifier) { |
|
114
|
38 |
|
if ($options['widget_config']['id_field'] == 'id') { |
|
115
|
32 |
|
$identifier = (int) $identifier; |
|
116
|
|
|
} |
|
117
|
|
|
try { |
|
118
|
38 |
|
$object = $options['widget_config']['class']::get_cached($identifier); |
|
119
|
38 |
|
$preset[$identifier] = autocomplete_helper::create_item_label($object, $options['widget_config']['result_headers'], $options['widget_config']['titlefield']); |
|
120
|
|
|
} catch (midcom_error $e) { |
|
121
|
|
|
$e->log(); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
89 |
|
$handler_options = array_replace([ |
|
127
|
89 |
|
'method' => $options['type_config']['method'], |
|
128
|
89 |
|
'allow_multiple' => $options['type_config']['allow_multiple'], |
|
129
|
89 |
|
'preset' => $preset, |
|
130
|
89 |
|
'preset_order' => array_reverse(array_keys($preset)) |
|
131
|
89 |
|
], $options['widget_config']); |
|
132
|
|
|
|
|
133
|
|
|
// @todo widget_config constraints are ignored, but they are used in the wild... |
|
134
|
89 |
|
$handler_options['constraints'] = $options['type_config']['constraints']; |
|
135
|
|
|
|
|
136
|
89 |
|
$view->vars['min_chars'] = $options['widget_config']['min_chars']; |
|
137
|
89 |
|
$view->vars['handler_options'] = $handler_options; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* {@inheritdoc} |
|
142
|
|
|
*/ |
|
143
|
89 |
|
public function getBlockPrefix() |
|
144
|
|
|
{ |
|
145
|
89 |
|
return 'autocomplete'; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* {@inheritdoc} |
|
150
|
|
|
*/ |
|
151
|
1 |
|
public function getParent() |
|
152
|
|
|
{ |
|
153
|
1 |
|
return FormType::class; |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|