forked from sonata-project/SonataDoctrineORMAdminBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatagridBuilder.php
182 lines (147 loc) · 7.23 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
<?php
/*
* This file is part of the Sonata 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\ClassMetadataInfo;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
use Sonata\AdminBundle\Builder\DatagridBuilderInterface;
use Sonata\AdminBundle\Datagrid\Datagrid;
use Sonata\AdminBundle\Datagrid\DatagridInterface;
use Sonata\AdminBundle\Datagrid\SimplePager;
use Sonata\AdminBundle\Filter\FilterFactoryInterface;
use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
use Sonata\DoctrineORMAdminBundle\Datagrid\Pager;
use Symfony\Component\Form\FormFactory;
class DatagridBuilder implements DatagridBuilderInterface
{
protected $filterFactory;
protected $formFactory;
protected $guesser;
protected $csrfTokenEnabled;
/**
* @param FormFactory $formFactory
* @param FilterFactoryInterface $filterFactory
* @param TypeGuesserInterface $guesser
* @param bool $csrfTokenEnabled
*/
public function __construct(FormFactory $formFactory, FilterFactoryInterface $filterFactory, TypeGuesserInterface $guesser, $csrfTokenEnabled = true)
{
$this->formFactory = $formFactory;
$this->filterFactory = $filterFactory;
$this->guesser = $guesser;
$this->csrfTokenEnabled = $csrfTokenEnabled;
}
/**
* @param \Sonata\AdminBundle\Admin\AdminInterface $admin
* @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
*/
public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
{
// set default values
$fieldDescription->setAdmin($admin);
if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
list($metadata, $lastPropertyName, $parentAssociationMappings) = $admin->getModelManager()->getParentMetadataForProperty($admin->getClass(), $fieldDescription->getName());
// set the default field mapping
if (isset($metadata->fieldMappings[$lastPropertyName])) {
$fieldDescription->setOption('field_mapping', $fieldDescription->getOption('field_mapping', $metadata->fieldMappings[$lastPropertyName]));
if ($metadata->fieldMappings[$lastPropertyName]['type'] == 'string') {
$fieldDescription->setOption('global_search', $fieldDescription->getOption('global_search', true)); // always search on string field only
}
}
// 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));
}
$fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName()));
$fieldDescription->setOption('name', $fieldDescription->getOption('name', $fieldDescription->getName()));
if (in_array($fieldDescription->getMappingType(), array(ClassMetadataInfo::ONE_TO_MANY, ClassMetadataInfo::MANY_TO_MANY, ClassMetadataInfo::MANY_TO_ONE, ClassMetadataInfo::ONE_TO_ONE))) {
$admin->attachAdminClass($fieldDescription);
}
}
/**
* @param \Sonata\AdminBundle\Datagrid\DatagridInterface $datagrid
* @param null $type
* @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
* @param \Sonata\AdminBundle\Admin\AdminInterface $admin
*/
public function addFilter(DatagridInterface $datagrid, $type, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
{
if ($type == null) {
$guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName(), $admin->getModelManager());
$type = $guessType->getType();
$fieldDescription->setType($type);
$options = $guessType->getOptions();
foreach ($options as $name => $value) {
if (is_array($value)) {
$fieldDescription->setOption($name, array_merge($value, $fieldDescription->getOption($name, array())));
} else {
$fieldDescription->setOption($name, $fieldDescription->getOption($name, $value));
}
}
} else {
$fieldDescription->setType($type);
}
$this->fixFieldDescription($admin, $fieldDescription);
$admin->addFilterFieldDescription($fieldDescription->getName(), $fieldDescription);
$fieldDescription->mergeOption('field_options', array('required' => false));
if ($type === 'doctrine_orm_model_autocomplete') {
$fieldDescription->mergeOption('field_options', array(
'class' => $fieldDescription->getTargetEntity(),
'model_manager' => $fieldDescription->getAdmin()->getModelManager(),
'admin_code' => $admin->getCode(),
'context' => 'filter',
));
}
$filter = $this->filterFactory->create($fieldDescription->getName(), $type, $fieldDescription->getOptions());
if (false !== $filter->getLabel() && !$filter->getLabel()) {
$filter->setLabel($admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName(), 'filter', 'label'));
}
$datagrid->addFilter($filter);
}
/**
* @param \Sonata\AdminBundle\Admin\AdminInterface $admin
* @param array $values
*
* @return \Sonata\AdminBundle\Datagrid\DatagridInterface
*/
public function getBaseDatagrid(AdminInterface $admin, array $values = array())
{
$pager = $this->getPager($admin->getPagerType());
$pager->setCountColumn($admin->getModelManager()->getIdentifierFieldNames($admin->getClass()));
$defaultOptions = array();
if ($this->csrfTokenEnabled) {
$defaultOptions['csrf_protection'] = false;
}
$formBuilder = $this->formFactory->createNamedBuilder('filter', 'form', array(), $defaultOptions);
return new Datagrid($admin->createQuery(), $admin->getList(), $pager, $formBuilder, $values);
}
/**
* Get pager by pagerType.
*
* @param string $pagerType
*
* @return \Sonata\AdminBundle\Datagrid\PagerInterface
*
* @throws \RuntimeException If invalid pager type is set.
*/
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));
}
}
}