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