These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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\DoctrinePHPCRAdminBundle\Builder; |
||
13 | |||
14 | use Doctrine\ODM\PHPCR\Mapping\ClassMetadata; |
||
15 | use Sonata\AdminBundle\Admin\AdminInterface; |
||
16 | use Sonata\AdminBundle\Admin\FieldDescriptionCollection; |
||
17 | use Sonata\AdminBundle\Admin\FieldDescriptionInterface; |
||
18 | use Sonata\AdminBundle\Builder\ShowBuilderInterface; |
||
19 | use Sonata\AdminBundle\Guesser\TypeGuesserInterface; |
||
20 | |||
21 | class ShowBuilder implements ShowBuilderInterface |
||
22 | { |
||
23 | /** |
||
24 | * @var TypeGuesserInterface |
||
25 | */ |
||
26 | protected $guesser; |
||
27 | |||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $templates; |
||
32 | |||
33 | /** |
||
34 | * @param TypeGuesserInterface $guesser |
||
35 | * @param array $templates Indexed by field type |
||
36 | */ |
||
37 | public function __construct(TypeGuesserInterface $guesser, array $templates) |
||
38 | { |
||
39 | $this->guesser = $guesser; |
||
40 | $this->templates = $templates; |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * {@inheritdoc} |
||
45 | */ |
||
46 | public function getBaseList(array $options = array()) |
||
47 | { |
||
48 | return new FieldDescriptionCollection(); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * {@inheritdoc} |
||
53 | */ |
||
54 | public function addField(FieldDescriptionCollection $list, $type, FieldDescriptionInterface $fieldDescription, AdminInterface $admin) |
||
55 | { |
||
56 | if ($type == null) { |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
57 | $guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName(), $admin->getModelManager()); |
||
58 | $fieldDescription->setType($guessType->getType()); |
||
59 | } else { |
||
60 | $fieldDescription->setType($type); |
||
61 | } |
||
62 | |||
63 | $this->fixFieldDescription($admin, $fieldDescription); |
||
64 | $admin->addShowFieldDescription($fieldDescription->getName(), $fieldDescription); |
||
65 | |||
66 | $list->add($fieldDescription); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * The method defines the correct default settings for the provided FieldDescription. |
||
71 | * |
||
72 | * {@inheritdoc} |
||
73 | * |
||
74 | * @throws \RuntimeException if the $fieldDescription does not have a type |
||
75 | */ |
||
76 | public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription) |
||
77 | { |
||
78 | $fieldDescription->setAdmin($admin); |
||
79 | |||
80 | $metadata = null; |
||
81 | if ($admin->getModelManager()->hasMetadata($admin->getClass())) { |
||
82 | /** @var ClassMetadata $metadata */ |
||
83 | $metadata = $admin->getModelManager()->getMetadata($admin->getClass()); |
||
84 | |||
85 | // set the default field mapping |
||
86 | if (isset($metadata->mappings[$fieldDescription->getName()])) { |
||
87 | $fieldDescription->setFieldMapping($metadata->mappings[$fieldDescription->getName()]); |
||
88 | } |
||
89 | |||
90 | // set the default association mapping |
||
91 | if ($metadata->hasAssociation($fieldDescription->getName())) { |
||
92 | $fieldDescription->setAssociationMapping($metadata->getAssociation($fieldDescription->getName())); |
||
93 | } |
||
94 | } |
||
95 | |||
96 | if (!$fieldDescription->getType()) { |
||
97 | throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), get_class($admin))); |
||
98 | } |
||
99 | |||
100 | $fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName())); |
||
101 | $fieldDescription->setOption('label', $fieldDescription->getOption('label', $fieldDescription->getName())); |
||
102 | |||
103 | if (!$fieldDescription->getTemplate()) { |
||
104 | $fieldDescription->setTemplate($this->getTemplate($fieldDescription->getType())); |
||
105 | |||
106 | if ($fieldDescription->getMappingType() == ClassMetadata::MANY_TO_ONE) { |
||
107 | $fieldDescription->setTemplate('SonataAdminBundle:CRUD/Association:show_many_to_one.html.twig'); |
||
108 | } |
||
109 | |||
110 | if ($fieldDescription->getMappingType() == ClassMetadata::MANY_TO_MANY) { |
||
111 | $fieldDescription->setTemplate('SonataAdminBundle:CRUD/Association:show_many_to_many.html.twig'); |
||
112 | } |
||
113 | } |
||
114 | |||
115 | $mappingTypes = array( |
||
116 | ClassMetadata::MANY_TO_ONE, |
||
117 | ClassMetadata::MANY_TO_MANY, |
||
118 | 'children', |
||
119 | 'child', |
||
120 | 'parent', |
||
121 | 'referrers', |
||
122 | ); |
||
123 | |||
124 | if ($metadata && $metadata->hasAssociation($fieldDescription->getName()) && in_array($fieldDescription->getMappingType(), $mappingTypes)) { |
||
125 | $admin->attachAdminClass($fieldDescription); |
||
126 | } |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @param string $type |
||
131 | * |
||
132 | * @return string|null The template if found |
||
133 | */ |
||
134 | private function getTemplate($type) |
||
135 | { |
||
136 | if (!isset($this->templates[$type])) { |
||
137 | return; |
||
138 | } |
||
139 | |||
140 | return $this->templates[$type]; |
||
141 | } |
||
142 | } |
||
143 |