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\autocomplete as transformer; |
13
|
|
|
use midcom\datamanager\extension\transformer\json as jsontransformer; |
14
|
|
|
use midcom\datamanager\extension\helper; |
15
|
|
|
use midcom; |
16
|
|
|
use midcom_error; |
17
|
|
|
use midcom_connection; |
18
|
|
|
use Symfony\Component\Form\FormInterface; |
19
|
|
|
use Symfony\Component\Form\FormView; |
20
|
|
|
use midcom\datamanager\extension\compat; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Experimental autocomplete type |
24
|
|
|
*/ |
25
|
|
|
class autocomplete extends AbstractType |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
public function configureOptions(OptionsResolver $resolver) |
31
|
|
|
{ |
32
|
|
|
$resolver->setNormalizer('widget_config', function (Options $options, $value) { |
33
|
|
|
$widget_defaults = array( |
34
|
|
|
'creation_mode_enabled' => false, |
35
|
|
|
'class' => null, |
36
|
|
|
'component' => null, |
37
|
|
|
'id_field' => 'guid', |
38
|
|
|
'constraints' => array(), |
39
|
|
|
'result_headers' => array(), |
40
|
|
|
'orders' => array(), |
41
|
|
|
'auto_wildcards' => 'both', |
42
|
|
|
'creation_handler' => null, |
43
|
|
|
'creation_default_key' => null, |
44
|
|
|
'get_label_for' => null, |
45
|
|
|
'categorize_by_parent_label' => false, |
46
|
|
|
'searchfields' => array(), |
47
|
|
|
'min_chars' => 2, |
48
|
|
|
'sortable' => false |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
if (!empty($value['clever_class'])) { |
52
|
|
|
$config = \midcom_baseclasses_components_configuration::get('midcom.helper.datamanager2', 'config'); |
53
|
|
|
|
54
|
|
|
/** @var \midcom_helper_configuration $config */ |
55
|
|
|
$config = $config->get('clever_classes'); |
56
|
|
|
$value = $config[$value['clever_class']]; |
57
|
|
|
if (!$value) { |
58
|
|
|
throw new midcom_error('Invalid clever class specified'); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return helper::resolve_options($widget_defaults, $value); |
63
|
|
|
}); |
64
|
|
|
$resolver->setNormalizer('type_config', function (Options $options, $value) { |
65
|
|
|
$type_defaults = array( |
66
|
|
|
'options' => array(), |
67
|
|
|
'allow_other' => false, |
68
|
|
|
'allow_multiple' => ($options['dm2_type'] == 'mnrelation'), |
69
|
|
|
'require_corresponding_option' => true, |
70
|
|
|
'multiple_storagemode' => 'serialized', |
71
|
|
|
'multiple_separator' => '|' |
72
|
|
|
); |
73
|
|
|
return helper::resolve_options($type_defaults, $value); |
74
|
|
|
}); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
|
|
public function buildForm(FormBuilderInterface $builder, array $options) |
81
|
|
|
{ |
82
|
|
|
$builder->addModelTransformer(new transformer($options)); |
83
|
|
|
$builder->add('selection', compat::get_type_name('hidden')); |
84
|
|
|
$builder->get('selection')->addViewTransformer(new jsontransformer); |
85
|
|
|
$builder->add('search_input', compat::get_type_name('search'), array('mapped' => false)); |
86
|
|
|
|
87
|
|
|
$head = midcom::get()->head; |
88
|
|
|
|
89
|
|
|
$components = array('menu', 'autocomplete'); |
90
|
|
|
|
91
|
|
|
if ($options['widget_config']['sortable']) { |
92
|
|
|
$components[] = 'mouse'; |
93
|
|
|
$components[] = 'sortable'; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if ($options['widget_config']['creation_mode_enabled']) { |
97
|
|
|
$components = array_merge($components, array('mouse', 'draggable', 'resizable', 'button', 'dialog')); |
98
|
|
|
} |
99
|
|
|
$head->enable_jquery_ui($components); |
100
|
|
|
$head->add_jsfile(MIDCOM_STATIC_URL . '/midcom.helper.datamanager2/autocomplete.js'); |
101
|
|
|
|
102
|
|
|
$head->add_stylesheet(MIDCOM_STATIC_URL . '/midcom.helper.datamanager2/autocomplete.css'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritdoc} |
107
|
|
|
*/ |
108
|
|
|
public function finishView(FormView $view, FormInterface $form, array $options) |
109
|
|
|
{ |
110
|
|
|
$handler_url = midcom_connection::get_url('self') . 'midcom-exec-midcom.helper.datamanager2/autocomplete_handler.php'; |
111
|
|
|
|
112
|
|
|
$preset = array(); |
113
|
|
|
if (!empty($view->vars['data']['selection'])) { |
114
|
|
|
foreach ($view->vars['data']['selection'] as $identifier) { |
115
|
|
|
if ($options['widget_config']['id_field'] == 'id') { |
116
|
|
|
$identifier = (int) $identifier; |
117
|
|
|
} |
118
|
|
|
try { |
119
|
|
|
$object = new $options['widget_config']['class']($identifier); |
120
|
|
|
$preset[$identifier] = \midcom_helper_datamanager2_widget_autocomplete::create_item_label($object, $options['widget_config']['result_headers'], $options['widget_config']['get_label_for']); |
121
|
|
|
} catch (midcom_error $e) { |
122
|
|
|
$e->log(); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$handler_options = $options['widget_config']; |
128
|
|
|
$handler_options['handler_url'] = $handler_url; |
129
|
|
|
$handler_options['allow_multiple'] = $options['type_config']['allow_multiple']; |
130
|
1 |
|
$handler_options['preset'] = $preset; |
131
|
|
|
$handler_options['preset_order'] = array_reverse(array_keys($preset)); |
132
|
1 |
|
|
133
|
|
|
$view->vars['min_chars'] = $options['widget_config']['min_chars']; |
134
|
|
|
$view->vars['handler_options'] = json_encode($handler_options); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
1 |
|
* {@inheritdoc} |
139
|
|
|
* |
140
|
1 |
|
* Symfony < 2.8 compat |
141
|
|
|
*/ |
142
|
|
|
public function getName() |
143
|
|
|
{ |
144
|
|
|
return $this->getBlockPrefix(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* {@inheritdoc} |
149
|
|
|
*/ |
150
|
|
|
public function getBlockPrefix() |
151
|
|
|
{ |
152
|
|
|
return 'autocomplete'; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* {@inheritdoc} |
157
|
|
|
*/ |
158
|
|
|
public function getParent() |
159
|
|
|
{ |
160
|
|
|
return compat::get_type_name('form'); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|