DatagridBuilder::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
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\FormFactoryInterface;
27
28
class DatagridBuilder implements DatagridBuilderInterface
29
{
30
    /**
31
     * @var FilterFactoryInterface
32
     */
33
    protected $filterFactory;
34
35
    /**
36
     * @var FormFactoryInterface
37
     */
38
    protected $formFactory;
39
40
    /**
41
     * @var TypeGuesserInterface
42
     */
43
    protected $guesser;
44
45
    /**
46
     * Indicates that csrf protection enabled.
47
     *
48
     * @var bool
49
     */
50
    protected $csrfTokenEnabled;
51
52
    /**
53
     * @param bool $csrfTokenEnabled
54
     */
55
    public function __construct(FormFactoryInterface $formFactory, FilterFactoryInterface $filterFactory, TypeGuesserInterface $guesser, $csrfTokenEnabled = true)
56
    {
57
        $this->formFactory = $formFactory;
58
        $this->filterFactory = $filterFactory;
59
        $this->guesser = $guesser;
60
        $this->csrfTokenEnabled = $csrfTokenEnabled;
61
    }
62
63
    public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription): void
64
    {
65
        // set default values
66
        $fieldDescription->setAdmin($admin);
67
68
        if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
69
            [$metadata, $lastPropertyName, $parentAssociationMappings] = $admin->getModelManager()->getParentMetadataForProperty($admin->getClass(), $fieldDescription->getName());
0 ignored issues
show
Bug introduced by
The variable $metadata does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $lastPropertyName does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $parentAssociationMappings does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
70
71
            // set the default field mapping
72
            if (isset($metadata->fieldMappings[$lastPropertyName])) {
73
                $fieldDescription->setOption('field_mapping', $fieldDescription->getOption('field_mapping', $metadata->fieldMappings[$lastPropertyName]));
74
75
                if ('string' === $metadata->fieldMappings[$lastPropertyName]['type']) {
76
                    $fieldDescription->setOption('global_search', $fieldDescription->getOption('global_search', true)); // always search on string field only
77
                }
78
            }
79
80
            // set the default association mapping
81
            if (isset($metadata->associationMappings[$lastPropertyName])) {
82
                $fieldDescription->setOption('association_mapping', $fieldDescription->getOption('association_mapping', $metadata->associationMappings[$lastPropertyName]));
83
            }
84
85
            $fieldDescription->setOption('parent_association_mappings', $fieldDescription->getOption('parent_association_mappings', $parentAssociationMappings));
86
        }
87
88
        $fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName()));
89
        $fieldDescription->setOption('name', $fieldDescription->getOption('name', $fieldDescription->getName()));
90
91
        if (\in_array($fieldDescription->getMappingType(), [ClassMetadata::ONE, ClassMetadata::MANY], true)) {
92
            $admin->attachAdminClass($fieldDescription);
93
        }
94
    }
95
96
    public function addFilter(DatagridInterface $datagrid, $type, FieldDescriptionInterface $fieldDescription, AdminInterface $admin): void
97
    {
98
        if (null === $type) {
99
            $guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName(), $admin->getModelManager());
100
101
            $type = $guessType->getType();
102
103
            $fieldDescription->setType($type);
104
105
            $options = $guessType->getOptions();
106
107
            foreach ($options as $name => $value) {
108
                if (\is_array($value)) {
109
                    $fieldDescription->setOption($name, array_merge($value, $fieldDescription->getOption($name, [])));
110
                } else {
111
                    $fieldDescription->setOption($name, $fieldDescription->getOption($name, $value));
112
                }
113
            }
114
        } else {
115
            $fieldDescription->setType($type);
116
        }
117
118
        $this->fixFieldDescription($admin, $fieldDescription);
119
        $admin->addFilterFieldDescription($fieldDescription->getName(), $fieldDescription);
120
121
        $fieldDescription->mergeOption('field_options', ['required' => false]);
122
        $filter = $this->filterFactory->create($fieldDescription->getName(), $type, $fieldDescription->getOptions());
123
124
        if (false !== $filter->getLabel() && !$filter->getLabel()) {
125
            $filter->setLabel($admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName(), 'filter', 'label'));
126
        }
127
128
        $datagrid->addFilter($filter);
129
    }
130
131
    /**
132
     * @return \Sonata\AdminBundle\Datagrid\DatagridInterface
133
     */
134
    public function getBaseDatagrid(AdminInterface $admin, array $values = [])
135
    {
136
        $pager = new Pager();
137
        $pager->setCountColumn($admin->getModelManager()->getIdentifierFieldNames($admin->getClass()));
138
139
        $defaultOptions = [];
140
        if ($this->csrfTokenEnabled) {
141
            $defaultOptions['csrf_protection'] = false;
142
        }
143
144
        $formBuilder = $this->formFactory->createNamedBuilder('filter', FormType::class, [], $defaultOptions);
145
146
        return new Datagrid($admin->createQuery(), $admin->getList(), $pager, $formBuilder, $values);
147
    }
148
}
149