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\ShowBuilderInterface; |
21
|
|
|
use Sonata\AdminBundle\Guesser\TypeGuesserInterface; |
22
|
|
|
|
23
|
|
|
class ShowBuilder implements ShowBuilderInterface |
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
|
|
|
public function getBaseList(array $options = []) |
36
|
|
|
{ |
37
|
|
|
return new FieldDescriptionCollection(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function addField(FieldDescriptionCollection $list, $type, FieldDescriptionInterface $fieldDescription, AdminInterface $admin) |
41
|
|
|
{ |
42
|
|
|
if (null === $type) { |
43
|
|
|
$guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName(), $admin->getModelManager()); |
44
|
|
|
$fieldDescription->setType($guessType->getType()); |
45
|
|
|
} else { |
46
|
|
|
$fieldDescription->setType($type); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$this->fixFieldDescription($admin, $fieldDescription); |
50
|
|
|
$admin->addShowFieldDescription($fieldDescription->getName(), $fieldDescription); |
51
|
|
|
|
52
|
|
|
$list->add($fieldDescription); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* The method defines the correct default settings for the provided FieldDescription. |
57
|
|
|
*/ |
58
|
|
|
public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription): void |
59
|
|
|
{ |
60
|
|
|
$fieldDescription->setAdmin($admin); |
61
|
|
|
|
62
|
|
|
if ($admin->getModelManager()->hasMetadata($admin->getClass())) { |
63
|
|
|
[$metadata, $lastPropertyName, $parentAssociationMappings] = $admin->getModelManager()->getParentMetadataForProperty($admin->getClass(), $fieldDescription->getName()); |
64
|
|
|
$fieldDescription->setParentAssociationMappings($parentAssociationMappings); |
65
|
|
|
|
66
|
|
|
// set the default field mapping |
67
|
|
|
if (isset($metadata->fieldMappings[$lastPropertyName])) { |
68
|
|
|
$fieldDescription->setFieldMapping($metadata->fieldMappings[$lastPropertyName]); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// set the default association mapping |
72
|
|
|
if (isset($metadata->associationMappings[$lastPropertyName])) { |
73
|
|
|
$fieldDescription->setAssociationMapping($metadata->associationMappings[$lastPropertyName]); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (!$fieldDescription->getType()) { |
78
|
|
|
throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), \get_class($admin))); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName())); |
82
|
|
|
$fieldDescription->setOption('label', $fieldDescription->getOption('label', $fieldDescription->getName())); |
83
|
|
|
|
84
|
|
|
if (!$fieldDescription->getTemplate()) { |
|
|
|
|
85
|
|
|
if ('id' === $fieldDescription->getType()) { |
86
|
|
|
$fieldDescription->setType('string'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if ('int' === $fieldDescription->getType()) { |
90
|
|
|
$fieldDescription->setType('integer'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$template = $this->getTemplate($fieldDescription->getType()); |
94
|
|
|
|
95
|
|
|
if (null === $template) { |
96
|
|
|
if (ClassMetadata::ONE === $fieldDescription->getMappingType()) { |
97
|
|
|
$template = '@SonataAdmin/CRUD/Association/show_many_to_one.html.twig'; |
98
|
|
|
} elseif (ClassMetadata::MANY === $fieldDescription->getMappingType()) { |
99
|
|
|
$template = '@SonataAdmin/CRUD/Association/show_many_to_many.html.twig'; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$fieldDescription->setTemplate($template); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (\in_array($fieldDescription->getMappingType(), [ClassMetadata::ONE, ClassMetadata::MANY], true)) { |
107
|
|
|
$admin->attachAdminClass($fieldDescription); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param int|string $type |
113
|
|
|
*/ |
114
|
|
|
private function getTemplate($type): ?string |
115
|
|
|
{ |
116
|
|
|
if (!isset($this->templates[$type])) { |
117
|
|
|
return null; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $this->templates[$type]; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
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: