-
-
Notifications
You must be signed in to change notification settings - Fork 344
/
DatagridBuilder.php
238 lines (205 loc) · 8.9 KB
/
DatagridBuilder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
declare(strict_types=1);
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\DoctrineORMAdminBundle\Builder;
use Doctrine\ORM\Mapping\ClassMetadata;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Builder\DatagridBuilderInterface;
use Sonata\AdminBundle\Datagrid\Datagrid;
use Sonata\AdminBundle\Datagrid\DatagridInterface;
use Sonata\AdminBundle\Datagrid\PagerInterface;
use Sonata\AdminBundle\Datagrid\SimplePager;
use Sonata\AdminBundle\FieldDescription\FieldDescriptionInterface;
use Sonata\AdminBundle\FieldDescription\TypeGuesserInterface;
use Sonata\AdminBundle\Filter\FilterFactoryInterface;
use Sonata\AdminBundle\Guesser\TypeGuesserInterface as DeprecatedTypeGuesserInterface;
use Sonata\DoctrineORMAdminBundle\Datagrid\Pager;
use Sonata\DoctrineORMAdminBundle\Filter\ModelAutocompleteFilter;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\FormFactoryInterface;
/**
* @final since sonata-project/doctrine-orm-admin-bundle 3.24
*/
class DatagridBuilder implements DatagridBuilderInterface
{
/**
* @var FilterFactoryInterface
*/
protected $filterFactory;
/**
* @var FormFactoryInterface
*/
protected $formFactory;
/**
* NEXT_MAJOR: Restrict guesser type to TypeGuesserInterface.
*
* @var DeprecatedTypeGuesserInterface|TypeGuesserInterface
*/
protected $guesser;
/**
* @var bool
*/
protected $csrfTokenEnabled;
/**
* NEXT_MAJOR: Restrict guesser type to TypeGuesserInterface.
*
* @param DeprecatedTypeGuesserInterface|TypeGuesserInterface $guesser
* @param bool $csrfTokenEnabled
*/
public function __construct(
FormFactoryInterface $formFactory,
FilterFactoryInterface $filterFactory,
$guesser,
$csrfTokenEnabled = true
) {
$this->formFactory = $formFactory;
$this->filterFactory = $filterFactory;
$this->guesser = $guesser;
$this->csrfTokenEnabled = $csrfTokenEnabled;
}
public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
{
// NEXT_MAJOR: Remove this line.
$fieldDescription->setAdmin($admin);
// NEXT_MAJOR: Remove this block.
if ($admin->getModelManager()->hasMetadata($admin->getClass(), 'sonata_deprecation_mute')) {
[$metadata, $lastPropertyName, $parentAssociationMappings] = $admin->getModelManager()
->getParentMetadataForProperty($admin->getClass(), $fieldDescription->getName(), 'sonata_deprecation_mute');
// set the default field mapping
if (isset($metadata->fieldMappings[$lastPropertyName])) {
$fieldDescription->setOption(
'field_mapping',
$fieldDescription->getOption(
'field_mapping',
$fieldMapping = $metadata->fieldMappings[$lastPropertyName]
)
);
// NEXT_MAJOR: Remove this, the fieldName should be correctly set at the creation.
if (!empty($embeddedClasses = $metadata->embeddedClasses)
&& isset($fieldMapping['declaredField'])
&& \array_key_exists($fieldMapping['declaredField'], $embeddedClasses)
) {
$fieldDescription->setOption(
'field_name',
$fieldMapping['fieldName']
);
}
}
// set the default association mapping
if (isset($metadata->associationMappings[$lastPropertyName])) {
$fieldDescription->setOption(
'association_mapping',
$fieldDescription->getOption(
'association_mapping',
$metadata->associationMappings[$lastPropertyName]
)
);
}
$fieldDescription->setOption(
'parent_association_mappings',
$fieldDescription->getOption('parent_association_mappings', $parentAssociationMappings)
);
}
// NEXT_MAJOR: Uncomment this code.
//if ([] !== $fieldDescription->getFieldMapping()) {
// $fieldDescription->setOption('field_mapping', $fieldDescription->getOption('field_mapping', $fieldDescription->getFieldMapping()));
//}
//
//if ([] !== $fieldDescription->getAssociationMapping()) {
// $fieldDescription->setOption('association_mapping', $fieldDescription->getOption('association_mapping', $fieldDescription->getAssociationMapping()));
//}
//
//if ([] !== $fieldDescription->getParentAssociationMappings()) {
// $fieldDescription->setOption('parent_association_mappings', $fieldDescription->getOption('parent_association_mappings', $fieldDescription->getParentAssociationMappings()));
//}
if (\in_array($fieldDescription->getMappingType(), [
ClassMetadata::ONE_TO_MANY,
ClassMetadata::MANY_TO_MANY,
ClassMetadata::MANY_TO_ONE,
ClassMetadata::ONE_TO_ONE,
], true)) {
$admin->attachAdminClass($fieldDescription);
}
}
public function addFilter(DatagridInterface $datagrid, $type, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
{
if (null === $type) {
// NEXT_MAJOR: Remove the condition and keep the if part.
if ($this->guesser instanceof TypeGuesserInterface) {
$guessType = $this->guesser->guess($fieldDescription);
} else {
$guessType = $this->guesser->guessType(
$admin->getClass(),
$fieldDescription->getName(),
$admin->getModelManager()
);
}
$type = $guessType->getType();
$fieldDescription->setType($type);
foreach ($guessType->getOptions() as $name => $value) {
if (\is_array($value)) {
$fieldDescription->setOption($name, array_merge($value, $fieldDescription->getOption($name, [])));
} else {
$fieldDescription->setOption($name, $fieldDescription->getOption($name, $value));
}
}
} else {
$fieldDescription->setType($type);
}
$this->fixFieldDescription($admin, $fieldDescription);
$admin->addFilterFieldDescription($fieldDescription->getName(), $fieldDescription);
// NEXT_MAJOR: Remove this line (see https://github.com/sonata-project/SonataAdminBundle/pull/6828)
$fieldDescription->mergeOption('field_options', ['required' => false]);
if (ModelAutocompleteFilter::class === $type) {
$fieldDescription->mergeOption('field_options', [
'class' => $fieldDescription->getTargetModel(),
'model_manager' => $fieldDescription->getAdmin()->getModelManager(),
'admin_code' => $admin->getCode(),
'context' => 'filter',
]);
}
$filter = $this->filterFactory->create($fieldDescription->getName(), $type, $fieldDescription->getOptions());
// NEXT_MAJOR: Remove this code since it was introduced in SonataAdmin (https://github.com/sonata-project/SonataAdminBundle/pull/6571)
if (false !== $filter->getLabel() && !$filter->getLabel()) {
$filter->setLabel($admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName(), 'filter', 'label'));
}
$datagrid->addFilter($filter);
}
public function getBaseDatagrid(AdminInterface $admin, array $values = [])
{
$pager = $this->getPager($admin->getPagerType());
$defaultOptions = ['validation_groups' => false];
if ($this->csrfTokenEnabled) {
$defaultOptions['csrf_protection'] = false;
}
$formBuilder = $this->formFactory->createNamedBuilder('filter', FormType::class, [], $defaultOptions);
return new Datagrid($admin->createQuery(), $admin->getList(), $pager, $formBuilder, $values);
}
/**
* Get pager by pagerType.
*
* @param string $pagerType
*
* @throws \RuntimeException If invalid pager type is set
*
* @return PagerInterface
*/
protected function getPager($pagerType)
{
switch ($pagerType) {
case Pager::TYPE_DEFAULT:
return new Pager();
case Pager::TYPE_SIMPLE:
return new SimplePager();
default:
throw new \RuntimeException(sprintf('Unknown pager type "%s".', $pagerType));
}
}
}