FormContractor.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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\AdminInterface;
  12. use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
  13. use Sonata\AdminBundle\Builder\FormContractorInterface;
  14. use Symfony\Component\Form\FormFactoryInterface;
  15. use Doctrine\ORM\Mapping\ClassMetadataInfo;
  16. use Sonata\DoctrineORMAdminBundle\Admin\FieldDescription;
  17. class FormContractor implements FormContractorInterface
  18. {
  19. protected $fieldFactory;
  20. /**
  21. * @param \Symfony\Component\Form\FormFactoryInterface $formFactory
  22. */
  23. public function __construct(FormFactoryInterface $formFactory)
  24. {
  25. $this->formFactory = $formFactory;
  26. }
  27. /**
  28. * The method defines the correct default settings for the provided FieldDescription
  29. *
  30. * @param \Sonata\AdminBundle\Admin\AdminInterface $admin
  31. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  32. *
  33. * @return void
  34. */
  35. public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
  36. {
  37. if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
  38. $metadata = $admin->getModelManager()->getMetadata($admin->getClass());
  39. // set the default field mapping
  40. if (isset($metadata->fieldMappings[$fieldDescription->getName()])) {
  41. $fieldDescription->setFieldMapping($metadata->fieldMappings[$fieldDescription->getName()]);
  42. }
  43. // set the default association mapping
  44. if (isset($metadata->associationMappings[$fieldDescription->getName()])) {
  45. $fieldDescription->setAssociationMapping($metadata->associationMappings[$fieldDescription->getName()]);
  46. }
  47. }
  48. if (!$fieldDescription->getType()) {
  49. throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), get_class($admin)));
  50. }
  51. $fieldDescription->setAdmin($admin);
  52. $fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'standard'));
  53. if (in_array($fieldDescription->getMappingType(), array(ClassMetadataInfo::ONE_TO_MANY, ClassMetadataInfo::MANY_TO_MANY, ClassMetadataInfo::MANY_TO_ONE, ClassMetadataInfo::ONE_TO_ONE ))) {
  54. $admin->attachAdminClass($fieldDescription);
  55. }
  56. }
  57. /**
  58. * @return \Symfony\Component\Form\FormFactoryInterface
  59. */
  60. public function getFormFactory()
  61. {
  62. return $this->formFactory;
  63. }
  64. /**
  65. * @param string $name
  66. * @param array $options
  67. *
  68. * @return \Symfony\Component\Form\FormBuilder
  69. */
  70. public function getFormBuilder($name, array $options = array())
  71. {
  72. return $this->getFormFactory()->createNamedBuilder('form', $name, null, $options);
  73. }
  74. /**
  75. * @param string $type
  76. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  77. *
  78. * @return array
  79. */
  80. public function getDefaultOptions($type, FieldDescriptionInterface $fieldDescription)
  81. {
  82. $options = array();
  83. $options['sonata_field_description'] = $fieldDescription;
  84. if ($type == 'sonata_type_model') {
  85. $options['class'] = $fieldDescription->getTargetEntity();
  86. $options['model_manager'] = $fieldDescription->getAdmin()->getModelManager();
  87. switch ($fieldDescription->getMappingType()) {
  88. case ClassMetadataInfo::ONE_TO_MANY:
  89. case ClassMetadataInfo::MANY_TO_MANY:
  90. $options['multiple'] = true;
  91. $options['parent'] = 'choice';
  92. break;
  93. case ClassMetadataInfo::MANY_TO_ONE:
  94. case ClassMetadataInfo::ONE_TO_ONE:
  95. break;
  96. }
  97. if ($fieldDescription->getOption('edit') == 'list') {
  98. $options['parent'] = 'text';
  99. if (!array_key_exists('required', $options)) {
  100. $options['required'] = false;
  101. }
  102. }
  103. } else if ($type == 'sonata_type_admin') {
  104. if (!$fieldDescription->getAssociationAdmin()) {
  105. throw new \RuntimeException(sprintf('The current field `%s` is not linked to an admin. Please create one for the target entity : `%s`', $fieldDescription->getName(), $fieldDescription->getTargetEntity()));
  106. }
  107. $options['data_class']=$fieldDescription->getAssociationAdmin()->getClass();
  108. $fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'admin'));
  109. } else if ($type == 'sonata_type_collection') {
  110. if (!$fieldDescription->getAssociationAdmin()) {
  111. throw new \RuntimeException(sprintf('The current field `%s` is not linked to an admin. Please create one for the target entity : `%s`', $fieldDescription->getName(), $fieldDescription->getTargetEntity()));
  112. }
  113. $options['type'] = 'sonata_type_admin';
  114. $options['modifiable'] = true;
  115. $options['type_options'] = array(
  116. 'sonata_field_description' => $fieldDescription,
  117. 'data_class' => $fieldDescription->getAssociationAdmin()->getClass()
  118. );
  119. }
  120. return $options;
  121. }
  122. }