Completed
Push — 3.x ( d0ebbd...810696 )
by Grégoire
01:37
created

DatagridBuilder::fixFieldDescription()   B

Complexity

Conditions 6
Paths 14

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 14
nop 2
dl 0
loc 32
rs 8.7857
c 0
b 0
f 0
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\DoctrineMongoDBAdminBundle\Builder;
13
14
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
15
use Sonata\AdminBundle\Admin\AdminInterface;
16
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
17
use Sonata\AdminBundle\Builder\DatagridBuilderInterface;
18
use Sonata\AdminBundle\Datagrid\Datagrid;
19
use Sonata\AdminBundle\Datagrid\DatagridInterface;
20
use Sonata\AdminBundle\Filter\FilterFactoryInterface;
21
use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
22
use Sonata\DoctrineMongoDBAdminBundle\Datagrid\Pager;
23
use Symfony\Component\Form\Extension\Core\Type\FormType;
24
use Symfony\Component\Form\FormFactory;
25
26
class DatagridBuilder implements DatagridBuilderInterface
27
{
28
    protected $filterFactory;
29
30
    protected $formFactory;
31
32
    protected $guesser;
33
34
    /**
35
     * Indicates that csrf protection enabled.
36
     *
37
     * @var bool
38
     */
39
    protected $csrfTokenEnabled;
40
41
    /**
42
     * @param FormFactory            $formFactory
43
     * @param FilterFactoryInterface $filterFactory
44
     * @param TypeGuesserInterface   $guesser
45
     * @param bool                   $csrfTokenEnabled
46
     */
47
    public function __construct(FormFactory $formFactory, FilterFactoryInterface $filterFactory, TypeGuesserInterface $guesser, $csrfTokenEnabled = true)
48
    {
49
        $this->formFactory = $formFactory;
50
        $this->filterFactory = $filterFactory;
51
        $this->guesser = $guesser;
52
        $this->csrfTokenEnabled = $csrfTokenEnabled;
53
    }
54
55
    /**
56
     * @param \Sonata\AdminBundle\Admin\AdminInterface            $admin
57
     * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
58
     */
59
    public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
60
    {
61
        // set default values
62
        $fieldDescription->setAdmin($admin);
63
64
        if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
65
            list($metadata, $lastPropertyName, $parentAssociationMappings) = $admin->getModelManager()->getParentMetadataForProperty($admin->getClass(), $fieldDescription->getName());
66
67
            // set the default field mapping
68
            if (isset($metadata->fieldMappings[$lastPropertyName])) {
69
                $fieldDescription->setOption('field_mapping', $fieldDescription->getOption('field_mapping', $metadata->fieldMappings[$lastPropertyName]));
70
71
                if ('string' == $metadata->fieldMappings[$lastPropertyName]['type']) {
72
                    $fieldDescription->setOption('global_search', $fieldDescription->getOption('global_search', true)); // always search on string field only
73
                }
74
            }
75
76
            // set the default association mapping
77
            if (isset($metadata->associationMappings[$lastPropertyName])) {
78
                $fieldDescription->setOption('association_mapping', $fieldDescription->getOption('association_mapping', $metadata->associationMappings[$lastPropertyName]));
79
            }
80
81
            $fieldDescription->setOption('parent_association_mappings', $fieldDescription->getOption('parent_association_mappings', $parentAssociationMappings));
82
        }
83
84
        $fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName()));
85
        $fieldDescription->setOption('name', $fieldDescription->getOption('name', $fieldDescription->getName()));
86
87
        if (\in_array($fieldDescription->getMappingType(), [ClassMetadata::ONE, ClassMetadata::MANY])) {
88
            $admin->attachAdminClass($fieldDescription);
89
        }
90
    }
91
92
    /**
93
     * @param \Sonata\AdminBundle\Datagrid\DatagridInterface      $datagrid
94
     * @param null                                                $type
95
     * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
96
     * @param \Sonata\AdminBundle\Admin\AdminInterface            $admin
97
     */
98
    public function addFilter(DatagridInterface $datagrid, $type, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
99
    {
100
        if (null == $type) {
101
            $guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName(), $admin->getModelManager());
102
103
            $type = $guessType->getType();
104
105
            $fieldDescription->setType($type);
106
107
            $options = $guessType->getOptions();
108
109
            foreach ($options as $name => $value) {
110
                if (\is_array($value)) {
111
                    $fieldDescription->setOption($name, array_merge($value, $fieldDescription->getOption($name, [])));
112
                } else {
113
                    $fieldDescription->setOption($name, $fieldDescription->getOption($name, $value));
114
                }
115
            }
116
        } else {
117
            $fieldDescription->setType($type);
118
        }
119
120
        $this->fixFieldDescription($admin, $fieldDescription);
121
        $admin->addFilterFieldDescription($fieldDescription->getName(), $fieldDescription);
122
123
        $fieldDescription->mergeOption('field_options', ['required' => false]);
124
        $filter = $this->filterFactory->create($fieldDescription->getName(), $type, $fieldDescription->getOptions());
125
126
        if (false !== $filter->getLabel() && !$filter->getLabel()) {
127
            $filter->setLabel($admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName(), 'filter', 'label'));
128
        }
129
130
        $datagrid->addFilter($filter);
131
    }
132
133
    /**
134
     * @param \Sonata\AdminBundle\Admin\AdminInterface $admin
135
     * @param array                                    $values
136
     *
137
     * @return \Sonata\AdminBundle\Datagrid\DatagridInterface
138
     */
139
    public function getBaseDatagrid(AdminInterface $admin, array $values = [])
140
    {
141
        $pager = new Pager();
142
        $pager->setCountColumn($admin->getModelManager()->getIdentifierFieldNames($admin->getClass()));
143
144
        $defaultOptions = [];
145
        if ($this->csrfTokenEnabled) {
146
            $defaultOptions['csrf_protection'] = false;
147
        }
148
149
        $formBuilder = $this->formFactory->createNamedBuilder('filter', FormType::class, [], $defaultOptions);
150
151
        return new Datagrid($admin->createQuery(), $admin->getList(), $pager, $formBuilder, $values);
152
    }
153
}
154