Completed
Push — 3.x ( 7b8913...94af5b )
by
unknown
01:20
created

src/Builder/ListBuilder.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\FieldDescriptionCollection;
19
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
20
use Sonata\AdminBundle\Builder\ListBuilderInterface;
21
use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
22
23
class ListBuilder implements ListBuilderInterface
24
{
25
    protected $guesser;
26
27
    protected $templates = [];
28
29
    public function __construct(TypeGuesserInterface $guesser, array $templates = [])
30
    {
31
        $this->guesser = $guesser;
32
        $this->templates = $templates;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function getBaseList(array $options = [])
39
    {
40
        return new FieldDescriptionCollection();
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function buildField($type, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
47
    {
48
        if (null === $type) {
49
            $guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName(), $admin->getModelManager());
50
            $fieldDescription->setType($guessType->getType());
51
        } else {
52
            $fieldDescription->setType($type);
53
        }
54
55
        $this->fixFieldDescription($admin, $fieldDescription);
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function addField(FieldDescriptionCollection $list, $type, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
62
    {
63
        $this->buildField($type, $fieldDescription, $admin);
64
        $admin->addListFieldDescription($fieldDescription->getName(), $fieldDescription);
65
66
        $list->add($fieldDescription);
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
73
    {
74
        if ('_action' === $fieldDescription->getName() || 'actions' === $fieldDescription->getType()) {
75
            $this->buildActionFieldDescription($fieldDescription);
76
        }
77
78
        $fieldDescription->setAdmin($admin);
79
80
        if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
81
            list($metadata, $lastPropertyName, $parentAssociationMappings) = $admin->getModelManager()->getParentMetadataForProperty($admin->getClass(), $fieldDescription->getName());
82
            $fieldDescription->setParentAssociationMappings($parentAssociationMappings);
83
84
            // set the default field mapping
85
            if (isset($metadata->fieldMappings[$lastPropertyName])) {
86
                $fieldDescription->setFieldMapping($metadata->fieldMappings[$lastPropertyName]);
87
                if (false !== $fieldDescription->getOption('sortable')) {
88
                    $fieldDescription->setOption('sortable', $fieldDescription->getOption('sortable', true));
89
                    $fieldDescription->setOption('sort_parent_association_mappings', $fieldDescription->getOption('sort_parent_association_mappings', $fieldDescription->getParentAssociationMappings()));
90
                    $fieldDescription->setOption('sort_field_mapping', $fieldDescription->getOption('sort_field_mapping', $fieldDescription->getFieldMapping()));
91
                }
92
            }
93
94
            // set the default association mapping
95
            if (isset($metadata->associationMappings[$lastPropertyName])) {
96
                $fieldDescription->setAssociationMapping($metadata->associationMappings[$lastPropertyName]);
97
            }
98
99
            $fieldDescription->setOption('_sort_order', $fieldDescription->getOption('_sort_order', 'ASC'));
100
        }
101
102
        if (!$fieldDescription->getType()) {
103
            throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), \get_class($admin)));
104
        }
105
106
        $fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName()));
107
        $fieldDescription->setOption('label', $fieldDescription->getOption('label', $fieldDescription->getName()));
108
109
        if (!$fieldDescription->getTemplate()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $fieldDescription->getTemplate() of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
110
            if ('id' === $fieldDescription->getType()) {
111
                $fieldDescription->setType('string');
112
            }
113
114
            if ('int' === $fieldDescription->getType()) {
115
                $fieldDescription->setType('integer');
116
            }
117
118
            $template = $this->getTemplate($fieldDescription->getType());
119
120
            if (null === $template) {
121
                if (ClassMetadata::ONE === $fieldDescription->getMappingType()) {
122
                    $template = '@SonataAdmin/CRUD/Association/list_many_to_one.html.twig';
123
                } elseif (ClassMetadata::MANY === $fieldDescription->getMappingType()) {
124
                    $template = '@SonataAdmin/CRUD/Association/list_many_to_many.html.twig';
125
                }
126
            }
127
128
            $fieldDescription->setTemplate($template);
129
        }
130
131
        if (\in_array($fieldDescription->getMappingType(), [ClassMetadata::ONE, ClassMetadata::MANY], true)) {
132
            $admin->attachAdminClass($fieldDescription);
133
        }
134
    }
135
136
    /**
137
     * @return \Sonata\AdminBundle\Admin\FieldDescriptionInterface
138
     */
139
    public function buildActionFieldDescription(FieldDescriptionInterface $fieldDescription)
140
    {
141
        if (null === $fieldDescription->getTemplate()) {
142
            $fieldDescription->setTemplate('@SonataAdmin/CRUD/list__action.html.twig');
143
        }
144
145
        if (null === $fieldDescription->getType()) {
146
            $fieldDescription->setType('actions');
147
        }
148
149
        if (null === $fieldDescription->getOption('name')) {
150
            $fieldDescription->setOption('name', 'Action');
151
        }
152
153
        if (null === $fieldDescription->getOption('code')) {
154
            $fieldDescription->setOption('code', 'Action');
155
        }
156
157
        if (null !== $fieldDescription->getOption('actions')) {
158
            $actions = $fieldDescription->getOption('actions');
159
            foreach ($actions as $k => $action) {
160
                if (!isset($action['template'])) {
161
                    $actions[$k]['template'] = sprintf('@SonataAdmin/CRUD/list__action_%s.html.twig', $k);
162
                }
163
            }
164
165
            $fieldDescription->setOption('actions', $actions);
166
        }
167
168
        return $fieldDescription;
169
    }
170
171
    /**
172
     * @param string $type
173
     *
174
     * @return string
175
     */
176
    private function getTemplate($type)
177
    {
178
        if (!isset($this->templates[$type])) {
179
            return;
180
        }
181
182
        return $this->templates[$type];
183
    }
184
}
185