PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/sonata-project/doctrine-orm-admin-bundle/Builder/ListBuilder.php

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