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('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), get_class($admin))); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName())); |
115
|
|
|
$fieldDescription->setOption('label', $fieldDescription->getOption('label', $fieldDescription->getName())); |
116
|
|
|
|
117
|
|
|
if (!$fieldDescription->getTemplate()) { |
118
|
|
|
$fieldDescription->setTemplate($this->getTemplate($fieldDescription->getType())); |
119
|
|
|
|
120
|
|
|
if (!$fieldDescription->getTemplate()) { |
121
|
|
|
switch ($fieldDescription->getMappingType()) { |
122
|
|
|
case ClassMetadataInfo::MANY_TO_ONE: |
123
|
|
|
$fieldDescription->setTemplate('SonataDoctrineORMAdminBundle:CRUD:list_orm_many_to_one.html.twig'); |
124
|
|
|
break; |
125
|
|
|
case ClassMetadataInfo::ONE_TO_ONE: |
126
|
|
|
$fieldDescription->setTemplate('SonataDoctrineORMAdminBundle:CRUD:list_orm_one_to_one.html.twig'); |
127
|
|
|
break; |
128
|
|
|
case ClassMetadataInfo::ONE_TO_MANY: |
129
|
|
|
$fieldDescription->setTemplate('SonataDoctrineORMAdminBundle:CRUD:list_orm_one_to_many.html.twig'); |
130
|
|
|
break; |
131
|
|
|
case ClassMetadataInfo::MANY_TO_MANY: |
132
|
|
|
$fieldDescription->setTemplate('SonataDoctrineORMAdminBundle:CRUD:list_orm_many_to_many.html.twig'); |
133
|
|
|
break; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if (in_array($fieldDescription->getMappingType(), array(ClassMetadataInfo::MANY_TO_ONE, ClassMetadataInfo::ONE_TO_ONE, ClassMetadataInfo::ONE_TO_MANY, ClassMetadataInfo::MANY_TO_MANY))) { |
139
|
|
|
$admin->attachAdminClass($fieldDescription); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param FieldDescriptionInterface $fieldDescription |
145
|
|
|
* |
146
|
|
|
* @return FieldDescriptionInterface |
147
|
|
|
*/ |
148
|
|
|
public function buildActionFieldDescription(FieldDescriptionInterface $fieldDescription) |
149
|
|
|
{ |
150
|
|
|
if (null === $fieldDescription->getTemplate()) { |
151
|
|
|
$fieldDescription->setTemplate('SonataAdminBundle:CRUD:list__action.html.twig'); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
if (null === $fieldDescription->getType()) { |
155
|
|
|
$fieldDescription->setType('action'); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if (null === $fieldDescription->getOption('name')) { |
159
|
|
|
$fieldDescription->setOption('name', 'Action'); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
if (null === $fieldDescription->getOption('code')) { |
163
|
|
|
$fieldDescription->setOption('code', 'Action'); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
if (null !== $fieldDescription->getOption('actions')) { |
167
|
|
|
$actions = $fieldDescription->getOption('actions'); |
168
|
|
|
foreach ($actions as $k => $action) { |
169
|
|
|
if (!isset($action['template'])) { |
170
|
|
|
$actions[$k]['template'] = sprintf('SonataAdminBundle:CRUD:list__action_%s.html.twig', $k); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$fieldDescription->setOption('actions', $actions); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return $fieldDescription; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param string $type |
182
|
|
|
* |
183
|
|
|
* @return string |
184
|
|
|
*/ |
185
|
|
|
private function getTemplate($type) |
186
|
|
|
{ |
187
|
|
|
if (!isset($this->templates[$type])) { |
188
|
|
|
return; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return $this->templates[$type]; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|