1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sonata Project package. |
5
|
|
|
* |
6
|
|
|
* (c) Thomas Rabaix <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sonata\DoctrineORMAdminBundle\Builder; |
13
|
|
|
|
14
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
15
|
|
|
use Sonata\AdminBundle\Admin\AdminInterface; |
16
|
|
|
use Sonata\AdminBundle\Admin\FieldDescriptionInterface; |
17
|
|
|
use Sonata\AdminBundle\Builder\FormContractorInterface; |
18
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
19
|
|
|
|
20
|
|
|
class FormContractor implements FormContractorInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* NEXT_MAJOR: remove this property. |
24
|
|
|
* |
25
|
|
|
* @deprecated since version 3.0.4, to be removed in 4.0 |
26
|
|
|
* |
27
|
|
|
* @var FormFactoryInterface |
28
|
|
|
*/ |
29
|
|
|
protected $fieldFactory; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var FormFactoryInterface |
33
|
|
|
*/ |
34
|
|
|
protected $formFactory; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param FormFactoryInterface $formFactory |
38
|
|
|
*/ |
39
|
|
|
public function __construct(FormFactoryInterface $formFactory) |
40
|
|
|
{ |
41
|
|
|
$this->formFactory = $formFactory; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription) |
48
|
|
|
{ |
49
|
|
|
if ($admin->getModelManager()->hasMetadata($admin->getClass())) { |
50
|
|
|
$metadata = $admin->getModelManager()->getMetadata($admin->getClass()); |
51
|
|
|
|
52
|
|
|
// set the default field mapping |
53
|
|
|
if (isset($metadata->fieldMappings[$fieldDescription->getName()])) { |
54
|
|
|
$fieldDescription->setFieldMapping($metadata->fieldMappings[$fieldDescription->getName()]); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// set the default association mapping |
58
|
|
|
if (isset($metadata->associationMappings[$fieldDescription->getName()])) { |
59
|
|
|
$fieldDescription->setAssociationMapping($metadata->associationMappings[$fieldDescription->getName()]); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if (!$fieldDescription->getType()) { |
64
|
|
|
throw new \RuntimeException(sprintf( |
65
|
|
|
'Please define a type for field `%s` in `%s`', |
66
|
|
|
$fieldDescription->getName(), |
67
|
|
|
get_class($admin) |
68
|
|
|
)); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$fieldDescription->setAdmin($admin); |
72
|
|
|
$fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'standard')); |
73
|
|
|
|
74
|
|
|
if ($this->hasAssociation($fieldDescription) || $fieldDescription->getOption('admin_code')) { |
75
|
|
|
$admin->attachAdminClass($fieldDescription); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return FormFactoryInterface |
81
|
|
|
*/ |
82
|
|
|
public function getFormFactory() |
83
|
|
|
{ |
84
|
|
|
return $this->formFactory; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
|
|
public function getFormBuilder($name, array $options = array()) |
91
|
|
|
{ |
92
|
|
|
return $this->getFormFactory()->createNamedBuilder( |
93
|
|
|
$name, |
94
|
|
|
'Symfony\Component\Form\Extension\Core\Type\FormType', |
95
|
|
|
null, |
96
|
|
|
$options |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
|
|
public function getDefaultOptions($type, FieldDescriptionInterface $fieldDescription) |
104
|
|
|
{ |
105
|
|
|
$options = array(); |
106
|
|
|
$options['sonata_field_description'] = $fieldDescription; |
107
|
|
|
|
108
|
|
|
if ($this->checkFormClass($type, array( |
109
|
|
|
'Sonata\AdminBundle\Form\Type\ModelType', |
110
|
|
|
'Sonata\AdminBundle\Form\Type\ModelTypeList', |
111
|
|
|
'Sonata\AdminBundle\Form\Type\ModelListType', |
112
|
|
|
'Sonata\AdminBundle\Form\Type\ModelHiddenType', |
113
|
|
|
'Sonata\AdminBundle\Form\Type\ModelAutocompleteType', |
114
|
|
|
))) { |
115
|
|
|
if ($fieldDescription->getOption('edit') === 'list') { |
116
|
|
|
throw new \LogicException( |
117
|
|
|
'The `sonata_type_model` type does not accept an `edit` option anymore,' |
118
|
|
|
.' please review the UPGRADE-2.1.md file from the SonataAdminBundle' |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$options['class'] = $fieldDescription->getTargetEntity(); |
123
|
|
|
$options['model_manager'] = $fieldDescription->getAdmin()->getModelManager(); |
124
|
|
|
|
125
|
|
|
if ($this->checkFormClass($type, array('Sonata\AdminBundle\Form\Type\ModelAutocompleteType'))) { |
126
|
|
|
if (!$fieldDescription->getAssociationAdmin()) { |
127
|
|
|
throw new \RuntimeException(sprintf( |
128
|
|
|
'The current field `%s` is not linked to an admin.' |
129
|
|
|
.' Please create one for the target entity: `%s`', |
130
|
|
|
$fieldDescription->getName(), |
131
|
|
|
$fieldDescription->getTargetEntity() |
132
|
|
|
)); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} elseif ($this->checkFormClass($type, array('Sonata\AdminBundle\Form\Type\AdminType'))) { |
136
|
|
|
if (!$fieldDescription->getAssociationAdmin()) { |
137
|
|
|
throw new \RuntimeException(sprintf( |
138
|
|
|
'The current field `%s` is not linked to an admin.' |
139
|
|
|
.' Please create one for the target entity : `%s`', |
140
|
|
|
$fieldDescription->getName(), |
141
|
|
|
$fieldDescription->getTargetEntity() |
142
|
|
|
)); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
if (!in_array($fieldDescription->getMappingType(), array(ClassMetadataInfo::ONE_TO_ONE, ClassMetadataInfo::MANY_TO_ONE))) { |
146
|
|
|
throw new \RuntimeException(sprintf( |
147
|
|
|
'You are trying to add `sonata_type_admin` field `%s` which is not One-To-One or Many-To-One.' |
148
|
|
|
.' Maybe you want `sonata_type_collection` instead?', |
149
|
|
|
$fieldDescription->getName() |
150
|
|
|
)); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
// set sensitive default value to have a component working fine out of the box |
154
|
|
|
$options['btn_add'] = false; |
155
|
|
|
$options['delete'] = false; |
156
|
|
|
|
157
|
|
|
$options['data_class'] = $fieldDescription->getAssociationAdmin()->getClass(); |
158
|
|
|
$fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'admin')); |
159
|
|
|
} elseif ($this->checkFormClass($type, array('Sonata\CoreBundle\Form\Type\CollectionType'))) { |
160
|
|
|
if (!$fieldDescription->getAssociationAdmin()) { |
161
|
|
|
throw new \RuntimeException(sprintf( |
162
|
|
|
'The current field `%s` is not linked to an admin.' |
163
|
|
|
.' Please create one for the target entity : `%s`', |
164
|
|
|
$fieldDescription->getName(), |
165
|
|
|
$fieldDescription->getTargetEntity() |
166
|
|
|
)); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
$options['type'] = 'sonata_type_admin'; |
170
|
|
|
$options['modifiable'] = true; |
171
|
|
|
$options['type_options'] = array( |
172
|
|
|
'sonata_field_description' => $fieldDescription, |
173
|
|
|
'data_class' => $fieldDescription->getAssociationAdmin()->getClass(), |
174
|
|
|
); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return $options; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param FieldDescriptionInterface $fieldDescription |
182
|
|
|
* |
183
|
|
|
* @return bool |
184
|
|
|
*/ |
185
|
|
|
private function hasAssociation(FieldDescriptionInterface $fieldDescription) |
186
|
|
|
{ |
187
|
|
|
return in_array($fieldDescription->getMappingType(), array( |
188
|
|
|
ClassMetadataInfo::ONE_TO_MANY, |
189
|
|
|
ClassMetadataInfo::MANY_TO_MANY, |
190
|
|
|
ClassMetadataInfo::MANY_TO_ONE, |
191
|
|
|
ClassMetadataInfo::ONE_TO_ONE, |
192
|
|
|
)); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param string $type |
197
|
|
|
* @param array $classes |
198
|
|
|
* |
199
|
|
|
* @return array |
200
|
|
|
*/ |
201
|
|
|
private function checkFormClass($type, $classes) |
202
|
|
|
{ |
203
|
|
|
return array_filter($classes, function ($subclass) use ($type) { |
204
|
|
|
return is_a($type, $subclass, true); |
205
|
|
|
}); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|