ShowBuilder.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /*
  3. * This file is part of the Sonata package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\DoctrineORMAdminBundle\Builder;
  11. use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
  12. use Sonata\AdminBundle\Admin\AdminInterface;
  13. use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
  14. use Sonata\AdminBundle\Builder\ShowBuilderInterface;
  15. use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
  16. use Doctrine\ORM\Mapping\ClassMetadataInfo;
  17. class ShowBuilder implements ShowBuilderInterface
  18. {
  19. protected $guesser;
  20. protected $templates;
  21. /**
  22. * @param \Sonata\AdminBundle\Guesser\TypeGuesserInterface $guesser
  23. * @param array $templates
  24. */
  25. public function __construct(TypeGuesserInterface $guesser, array $templates)
  26. {
  27. $this->guesser = $guesser;
  28. $this->templates = $templates;
  29. }
  30. /**
  31. * @param array $options
  32. *
  33. * @return \Sonata\AdminBundle\Admin\FieldDescriptionCollection
  34. */
  35. public function getBaseList(array $options = array())
  36. {
  37. return new FieldDescriptionCollection;
  38. }
  39. /**
  40. * @param \Sonata\AdminBundle\Admin\FieldDescriptionCollection $list
  41. * @param null $type
  42. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  43. * @param \Sonata\AdminBundle\Admin\AdminInterface $admin
  44. *
  45. * @return mixed
  46. */
  47. public function addField(FieldDescriptionCollection $list, $type = null, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
  48. {
  49. if ($type == null) {
  50. $guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName(), $admin->getModelManager());
  51. $fieldDescription->setType($guessType->getType());
  52. } else {
  53. $fieldDescription->setType($type);
  54. }
  55. $this->fixFieldDescription($admin, $fieldDescription);
  56. $admin->addShowFieldDescription($fieldDescription->getName(), $fieldDescription);
  57. $list->add($fieldDescription);
  58. }
  59. /**
  60. * @param string $type
  61. *
  62. * @return string
  63. */
  64. private function getTemplate($type)
  65. {
  66. if (!isset($this->templates[$type])) {
  67. return null;
  68. }
  69. return $this->templates[$type];
  70. }
  71. /**
  72. * The method defines the correct default settings for the provided FieldDescription
  73. *
  74. * @param \Sonata\AdminBundle\Admin\AdminInterface $admin
  75. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  76. *
  77. * @return void
  78. */
  79. public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
  80. {
  81. $fieldDescription->setAdmin($admin);
  82. if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
  83. list($metadata, $lastPropertyName, $parentAssociationMappings) = $admin->getModelManager()->getParentMetadataForProperty($admin->getClass(), $fieldDescription->getName());
  84. $fieldDescription->setParentAssociationMappings($parentAssociationMappings);
  85. // set the default field mapping
  86. if (isset($metadata->fieldMappings[$lastPropertyName])) {
  87. $fieldDescription->setFieldMapping($metadata->fieldMappings[$lastPropertyName]);
  88. }
  89. // set the default association mapping
  90. if (isset($metadata->associationMappings[$lastPropertyName])) {
  91. $fieldDescription->setAssociationMapping($metadata->associationMappings[$lastPropertyName]);
  92. }
  93. }
  94. if (!$fieldDescription->getType()) {
  95. throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), get_class($admin)));
  96. }
  97. $fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName()));
  98. $fieldDescription->setOption('label', $fieldDescription->getOption('label', $fieldDescription->getName()));
  99. if (!$fieldDescription->getTemplate()) {
  100. $fieldDescription->setTemplate($this->getTemplate($fieldDescription->getType()));
  101. if ($fieldDescription->getMappingType() == ClassMetadataInfo::MANY_TO_ONE) {
  102. $fieldDescription->setTemplate('SonataDoctrineORMAdminBundle:CRUD:show_orm_many_to_one.html.twig');
  103. }
  104. if ($fieldDescription->getMappingType() == ClassMetadataInfo::ONE_TO_ONE) {
  105. $fieldDescription->setTemplate('SonataDoctrineORMAdminBundle:CRUD:show_orm_one_to_one.html.twig');
  106. }
  107. if ($fieldDescription->getMappingType() == ClassMetadataInfo::ONE_TO_MANY) {
  108. $fieldDescription->setTemplate('SonataDoctrineORMAdminBundle:CRUD:show_orm_one_to_many.html.twig');
  109. }
  110. if ($fieldDescription->getMappingType() == ClassMetadataInfo::MANY_TO_MANY) {
  111. $fieldDescription->setTemplate('SonataDoctrineORMAdminBundle:CRUD:show_orm_many_to_many.html.twig');
  112. }
  113. }
  114. if ($fieldDescription->getMappingType() == ClassMetadataInfo::MANY_TO_ONE) {
  115. $admin->attachAdminClass($fieldDescription);
  116. }
  117. if ($fieldDescription->getMappingType() == ClassMetadataInfo::ONE_TO_ONE) {
  118. $admin->attachAdminClass($fieldDescription);
  119. }
  120. if ($fieldDescription->getMappingType() == ClassMetadataInfo::ONE_TO_MANY) {
  121. $admin->attachAdminClass($fieldDescription);
  122. }
  123. if ($fieldDescription->getMappingType() == ClassMetadataInfo::MANY_TO_MANY) {
  124. $admin->attachAdminClass($fieldDescription);
  125. }
  126. }
  127. }