1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sonata 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 Sonata\AdminBundle\Admin\FieldDescriptionInterface; |
15
|
|
|
use Sonata\AdminBundle\Admin\AdminInterface; |
16
|
|
|
use Sonata\AdminBundle\Admin\FieldDescriptionCollection; |
17
|
|
|
use Sonata\AdminBundle\Builder\ShowBuilderInterface; |
18
|
|
|
use Sonata\AdminBundle\Guesser\TypeGuesserInterface; |
19
|
|
|
|
20
|
|
|
use Doctrine\ODM\PHPCR\Mapping\ClassMetadata; |
21
|
|
|
|
22
|
|
|
class ShowBuilder implements ShowBuilderInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var TypeGuesserInterface |
26
|
|
|
*/ |
27
|
|
|
protected $guesser; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $templates; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param TypeGuesserInterface $guesser |
36
|
|
|
* @param array $templates Indexed by field type |
37
|
|
|
*/ |
38
|
|
|
public function __construct(TypeGuesserInterface $guesser, array $templates) |
39
|
|
|
{ |
40
|
|
|
$this->guesser = $guesser; |
41
|
|
|
$this->templates = $templates; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritDoc} |
46
|
|
|
*/ |
47
|
|
|
public function getBaseList(array $options = array()) |
48
|
|
|
{ |
49
|
|
|
return new FieldDescriptionCollection(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritDoc} |
54
|
|
|
*/ |
55
|
|
|
public function addField(FieldDescriptionCollection $list, $type = null, FieldDescriptionInterface $fieldDescription, AdminInterface $admin) |
56
|
|
|
{ |
57
|
|
|
if ($type == null) { |
58
|
|
|
$guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName(), $admin->getModelManager()); |
59
|
|
|
$fieldDescription->setType($guessType->getType()); |
60
|
|
|
} else { |
61
|
|
|
$fieldDescription->setType($type); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$this->fixFieldDescription($admin, $fieldDescription); |
65
|
|
|
$admin->addShowFieldDescription($fieldDescription->getName(), $fieldDescription); |
66
|
|
|
|
67
|
|
|
$list->add($fieldDescription); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $type |
72
|
|
|
* |
73
|
|
|
* @return string|null The template if found |
74
|
|
|
*/ |
75
|
|
|
private function getTemplate($type) |
76
|
|
|
{ |
77
|
|
|
if (!isset($this->templates[$type])) { |
78
|
|
|
return null; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $this->templates[$type]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* The method defines the correct default settings for the provided FieldDescription |
86
|
|
|
* |
87
|
|
|
* {@inheritDoc} |
88
|
|
|
* |
89
|
|
|
* @throws \RuntimeException if the $fieldDescription does not have a type. |
90
|
|
|
*/ |
91
|
|
|
public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription) |
92
|
|
|
{ |
93
|
|
|
$fieldDescription->setAdmin($admin); |
94
|
|
|
|
95
|
|
|
if ($admin->getModelManager()->hasMetadata($admin->getClass())) { |
96
|
|
|
$metadata = $admin->getModelManager()->getMetadata($admin->getClass()); |
97
|
|
|
|
98
|
|
|
// set the default field mapping |
99
|
|
|
if (isset($metadata->fieldMappings[$fieldDescription->getName()])) { |
100
|
|
|
$fieldDescription->setFieldMapping($metadata->fieldMappings[$fieldDescription->getName()]); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
// set the default association mapping |
104
|
|
|
if (isset($metadata->associationMappings[$fieldDescription->getName()])) { |
105
|
|
|
$fieldDescription->setAssociationMapping($metadata->associationMappings[$fieldDescription->getName()]); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if (!$fieldDescription->getType()) { |
110
|
|
|
throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), get_class($admin))); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName())); |
114
|
|
|
$fieldDescription->setOption('label', $fieldDescription->getOption('label', $fieldDescription->getName())); |
115
|
|
|
|
116
|
|
|
if (!$fieldDescription->getTemplate()) { |
117
|
|
|
|
118
|
|
|
$fieldDescription->setTemplate($this->getTemplate($fieldDescription->getType())); |
119
|
|
|
|
120
|
|
|
if ($fieldDescription->getMappingType() == ClassMetadata::MANY_TO_ONE) { |
121
|
|
|
$fieldDescription->setTemplate('SonataDoctrinePhpcrAdminBundle:CRUD:show_phpcr_many_to_one.html.twig'); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if ($fieldDescription->getMappingType() == ClassMetadata::MANY_TO_MANY) { |
125
|
|
|
$fieldDescription->setTemplate('SonataDoctrinePhpcrAdminBundle:CRUD:show_phpcr_many_to_many.html.twig'); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
switch($fieldDescription->getMappingType()) { |
130
|
|
|
case ClassMetadata::MANY_TO_ONE: |
131
|
|
|
case ClassMetadata::MANY_TO_MANY: |
132
|
|
|
$admin->attachAdminClass($fieldDescription); |
133
|
|
|
break; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|