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\FieldDescriptionCollection; |
17
|
|
|
use Sonata\AdminBundle\Admin\FieldDescriptionInterface; |
18
|
|
|
use Sonata\AdminBundle\Builder\ListBuilderInterface; |
19
|
|
|
use Sonata\AdminBundle\Guesser\TypeGuesserInterface; |
20
|
|
|
|
21
|
|
|
class ListBuilder implements ListBuilderInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var TypeGuesserInterface |
25
|
|
|
*/ |
26
|
|
|
protected $guesser; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string[] |
30
|
|
|
*/ |
31
|
|
|
protected $templates = array(); |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param TypeGuesserInterface $guesser |
35
|
|
|
* @param string[] $templates |
36
|
|
|
*/ |
37
|
|
|
public function __construct(TypeGuesserInterface $guesser, array $templates = array()) |
38
|
|
|
{ |
39
|
|
|
$this->guesser = $guesser; |
40
|
|
|
$this->templates = $templates; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
public function getBaseList(array $options = array()) |
47
|
|
|
{ |
48
|
|
|
return new FieldDescriptionCollection(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
public function buildField($type, FieldDescriptionInterface $fieldDescription, AdminInterface $admin) |
55
|
|
|
{ |
56
|
|
|
if ($type == null) { |
57
|
|
|
$guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName(), $admin->getModelManager()); |
58
|
|
|
$fieldDescription->setType($guessType->getType()); |
59
|
|
|
} else { |
60
|
|
|
$fieldDescription->setType($type); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->fixFieldDescription($admin, $fieldDescription); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public function addField(FieldDescriptionCollection $list, $type, FieldDescriptionInterface $fieldDescription, AdminInterface $admin) |
70
|
|
|
{ |
71
|
|
|
$this->buildField($type, $fieldDescription, $admin); |
72
|
|
|
$admin->addListFieldDescription($fieldDescription->getName(), $fieldDescription); |
73
|
|
|
|
74
|
|
|
$list->add($fieldDescription); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
|
|
public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription) |
81
|
|
|
{ |
82
|
|
|
if ($fieldDescription->getName() == '_action') { |
83
|
|
|
$this->buildActionFieldDescription($fieldDescription); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$fieldDescription->setAdmin($admin); |
87
|
|
|
|
88
|
|
|
if ($admin->getModelManager()->hasMetadata($admin->getClass())) { |
89
|
|
|
list($metadata, $lastPropertyName, $parentAssociationMappings) = $admin->getModelManager()->getParentMetadataForProperty($admin->getClass(), $fieldDescription->getName()); |
90
|
|
|
$fieldDescription->setParentAssociationMappings($parentAssociationMappings); |
91
|
|
|
|
92
|
|
|
// set the default field mapping |
93
|
|
|
if (isset($metadata->fieldMappings[$lastPropertyName])) { |
94
|
|
|
$fieldDescription->setFieldMapping($metadata->fieldMappings[$lastPropertyName]); |
95
|
|
|
if ($fieldDescription->getOption('sortable') !== false) { |
96
|
|
|
$fieldDescription->setOption('sortable', $fieldDescription->getOption('sortable', true)); |
97
|
|
|
$fieldDescription->setOption('sort_parent_association_mappings', $fieldDescription->getOption('sort_parent_association_mappings', $fieldDescription->getParentAssociationMappings())); |
98
|
|
|
$fieldDescription->setOption('sort_field_mapping', $fieldDescription->getOption('sort_field_mapping', $fieldDescription->getFieldMapping())); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// set the default association mapping |
103
|
|
|
if (isset($metadata->associationMappings[$lastPropertyName])) { |
104
|
|
|
$fieldDescription->setAssociationMapping($metadata->associationMappings[$lastPropertyName]); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$fieldDescription->setOption('_sort_order', $fieldDescription->getOption('_sort_order', 'ASC')); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if (!$fieldDescription->getType()) { |
111
|
|
|
throw new \RuntimeException(sprintf( |
112
|
|
|
'Please define a type for field `%s` in `%s`', |
113
|
|
|
$fieldDescription->getName(), |
114
|
|
|
get_class($admin) |
115
|
|
|
)); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName())); |
119
|
|
|
$fieldDescription->setOption('label', $fieldDescription->getOption('label', $fieldDescription->getName())); |
120
|
|
|
|
121
|
|
|
if (!$fieldDescription->getTemplate()) { |
122
|
|
|
$fieldDescription->setTemplate($this->getTemplate($fieldDescription->getType())); |
123
|
|
|
|
124
|
|
|
if (!$fieldDescription->getTemplate()) { |
125
|
|
|
switch ($fieldDescription->getMappingType()) { |
126
|
|
|
case ClassMetadataInfo::MANY_TO_ONE: |
127
|
|
|
$fieldDescription->setTemplate( |
128
|
|
|
'SonataDoctrineORMAdminBundle:CRUD:list_orm_many_to_one.html.twig' |
129
|
|
|
); |
130
|
|
|
break; |
131
|
|
|
case ClassMetadataInfo::ONE_TO_ONE: |
132
|
|
|
$fieldDescription->setTemplate( |
133
|
|
|
'SonataDoctrineORMAdminBundle:CRUD:list_orm_one_to_one.html.twig' |
134
|
|
|
); |
135
|
|
|
break; |
136
|
|
|
case ClassMetadataInfo::ONE_TO_MANY: |
137
|
|
|
$fieldDescription->setTemplate( |
138
|
|
|
'SonataDoctrineORMAdminBundle:CRUD:list_orm_one_to_many.html.twig' |
139
|
|
|
); |
140
|
|
|
break; |
141
|
|
|
case ClassMetadataInfo::MANY_TO_MANY: |
142
|
|
|
$fieldDescription->setTemplate( |
143
|
|
|
'SonataDoctrineORMAdminBundle:CRUD:list_orm_many_to_many.html.twig' |
144
|
|
|
); |
145
|
|
|
break; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
if (in_array($fieldDescription->getMappingType(), array(ClassMetadataInfo::MANY_TO_ONE, ClassMetadataInfo::ONE_TO_ONE, ClassMetadataInfo::ONE_TO_MANY, ClassMetadataInfo::MANY_TO_MANY))) { |
151
|
|
|
$admin->attachAdminClass($fieldDescription); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param FieldDescriptionInterface $fieldDescription |
157
|
|
|
* |
158
|
|
|
* @return FieldDescriptionInterface |
159
|
|
|
*/ |
160
|
|
|
public function buildActionFieldDescription(FieldDescriptionInterface $fieldDescription) |
161
|
|
|
{ |
162
|
|
|
if (null === $fieldDescription->getTemplate()) { |
163
|
|
|
$fieldDescription->setTemplate('SonataAdminBundle:CRUD:list__action.html.twig'); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
if (null === $fieldDescription->getType()) { |
167
|
|
|
$fieldDescription->setType('action'); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
if (null === $fieldDescription->getOption('name')) { |
171
|
|
|
$fieldDescription->setOption('name', 'Action'); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
if (null === $fieldDescription->getOption('code')) { |
175
|
|
|
$fieldDescription->setOption('code', 'Action'); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
if (null !== $fieldDescription->getOption('actions')) { |
179
|
|
|
$actions = $fieldDescription->getOption('actions'); |
180
|
|
|
foreach ($actions as $k => $action) { |
181
|
|
|
if (!isset($action['template'])) { |
182
|
|
|
$actions[$k]['template'] = sprintf('SonataAdminBundle:CRUD:list__action_%s.html.twig', $k); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
$fieldDescription->setOption('actions', $actions); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return $fieldDescription; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param string $type |
194
|
|
|
* |
195
|
|
|
* @return string |
196
|
|
|
*/ |
197
|
|
|
private function getTemplate($type) |
198
|
|
|
{ |
199
|
|
|
if (!isset($this->templates[$type])) { |
200
|
|
|
return; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return $this->templates[$type]; |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|