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