ListBuilder.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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\ListBuilderInterface;
  15. use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
  16. use Doctrine\ORM\Mapping\ClassMetadataInfo;
  17. class ListBuilder implements ListBuilderInterface
  18. {
  19. protected $guesser;
  20. protected $templates = array();
  21. /**
  22. * @param \Sonata\AdminBundle\Guesser\TypeGuesserInterface $guesser
  23. * @param array $templates
  24. */
  25. public function __construct(TypeGuesserInterface $guesser, $templates = array())
  26. {
  27. $this->guesser = $guesser;
  28. $this->templates = $templates;
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function getBaseList(array $options = array())
  34. {
  35. return new FieldDescriptionCollection;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function buildField($type = null, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
  41. {
  42. if ($type == null) {
  43. $guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName(), $admin->getModelManager());
  44. $fieldDescription->setType($guessType->getType());
  45. } else {
  46. $fieldDescription->setType($type);
  47. }
  48. $this->fixFieldDescription($admin, $fieldDescription);
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function addField(FieldDescriptionCollection $list, $type = null, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
  54. {
  55. $this->buildField($type, $fieldDescription, $admin);
  56. $admin->addListFieldDescription($fieldDescription->getName(), $fieldDescription);
  57. return $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. * {@inheritdoc}
  73. */
  74. public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
  75. {
  76. if ($fieldDescription->getName() == '_action') {
  77. $this->buildActionFieldDescription($fieldDescription);
  78. }
  79. $fieldDescription->setAdmin($admin);
  80. if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
  81. list($metadata, $lastPropertyName, $parentAssociationMappings) = $admin->getModelManager()->getParentMetadataForProperty($admin->getClass(), $fieldDescription->getName());
  82. $fieldDescription->setParentAssociationMappings($parentAssociationMappings);
  83. // set the default field mapping
  84. if (isset($metadata->fieldMappings[$lastPropertyName])) {
  85. $fieldDescription->setFieldMapping($metadata->fieldMappings[$lastPropertyName]);
  86. if ($fieldDescription->getOption('sortable') !== false) {
  87. $fieldDescription->setOption('sortable', $fieldDescription->getOption('sortable', true));
  88. $fieldDescription->setOption('sort_parent_association_mappings', $fieldDescription->getOption('sort_parent_association_mappings', $fieldDescription->getParentAssociationMappings()));
  89. $fieldDescription->setOption('sort_field_mapping', $fieldDescription->getOption('sort_field_mapping', $fieldDescription->getFieldMapping()));
  90. }
  91. }
  92. // set the default association mapping
  93. if (isset($metadata->associationMappings[$lastPropertyName])) {
  94. $fieldDescription->setAssociationMapping($metadata->associationMappings[$lastPropertyName]);
  95. }
  96. $fieldDescription->setOption('_sort_order', $fieldDescription->getOption('_sort_order', 'ASC'));
  97. }
  98. if (!$fieldDescription->getType()) {
  99. throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), get_class($admin)));
  100. }
  101. $fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName()));
  102. $fieldDescription->setOption('label', $fieldDescription->getOption('label', $fieldDescription->getName()));
  103. if (!$fieldDescription->getTemplate()) {
  104. $fieldDescription->setTemplate($this->getTemplate($fieldDescription->getType()));
  105. if ($fieldDescription->getMappingType() == ClassMetadataInfo::MANY_TO_ONE) {
  106. $fieldDescription->setTemplate('SonataDoctrineORMAdminBundle:CRUD:list_orm_many_to_one.html.twig');
  107. }
  108. if ($fieldDescription->getMappingType() == ClassMetadataInfo::ONE_TO_ONE) {
  109. $fieldDescription->setTemplate('SonataDoctrineORMAdminBundle:CRUD:list_orm_one_to_one.html.twig');
  110. }
  111. if ($fieldDescription->getMappingType() == ClassMetadataInfo::ONE_TO_MANY) {
  112. $fieldDescription->setTemplate('SonataDoctrineORMAdminBundle:CRUD:list_orm_one_to_many.html.twig');
  113. }
  114. if ($fieldDescription->getMappingType() == ClassMetadataInfo::MANY_TO_MANY) {
  115. $fieldDescription->setTemplate('SonataDoctrineORMAdminBundle:CRUD:list_orm_many_to_many.html.twig');
  116. }
  117. }
  118. if ($fieldDescription->getMappingType() == ClassMetadataInfo::MANY_TO_ONE) {
  119. $admin->attachAdminClass($fieldDescription);
  120. }
  121. if ($fieldDescription->getMappingType() == ClassMetadataInfo::ONE_TO_ONE) {
  122. $admin->attachAdminClass($fieldDescription);
  123. }
  124. if ($fieldDescription->getMappingType() == ClassMetadataInfo::ONE_TO_MANY) {
  125. $admin->attachAdminClass($fieldDescription);
  126. }
  127. if ($fieldDescription->getMappingType() == ClassMetadataInfo::MANY_TO_MANY) {
  128. $admin->attachAdminClass($fieldDescription);
  129. }
  130. }
  131. /**
  132. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  133. *
  134. * @return \Sonata\AdminBundle\Admin\FieldDescriptionInterface
  135. */
  136. public function buildActionFieldDescription(FieldDescriptionInterface $fieldDescription)
  137. {
  138. if (null === $fieldDescription->getTemplate()) {
  139. $fieldDescription->setTemplate('SonataAdminBundle:CRUD:list__action.html.twig');
  140. }
  141. if (null === $fieldDescription->getType()) {
  142. $fieldDescription->setType('action');
  143. }
  144. if (null === $fieldDescription->getOption('name')) {
  145. $fieldDescription->setOption('name', 'Action');
  146. }
  147. if (null === $fieldDescription->getOption('code')) {
  148. $fieldDescription->setOption('code', 'Action');
  149. }
  150. if (null !== $fieldDescription->getOption('actions')) {
  151. $actions = $fieldDescription->getOption('actions');
  152. foreach ($actions as $k => $action) {
  153. if (!isset($action['template'])) {
  154. $actions[$k]['template'] = sprintf('SonataAdminBundle:CRUD:list__action_%s.html.twig', $k);
  155. }
  156. }
  157. $fieldDescription->setOption('actions', $actions);
  158. }
  159. return $fieldDescription;
  160. }
  161. }