FormContractor.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. * @return void
  33. */
  34. public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
  35. {
  36. if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
  37. $metadata = $admin->getModelManager()->getMetadata($admin->getClass());
  38. // set the default field mapping
  39. if (isset($metadata->fieldMappings[$fieldDescription->getName()])) {
  40. $fieldDescription->setFieldMapping($metadata->fieldMappings[$fieldDescription->getName()]);
  41. }
  42. // set the default association mapping
  43. if (isset($metadata->associationMappings[$fieldDescription->getName()])) {
  44. $fieldDescription->setAssociationMapping($metadata->associationMappings[$fieldDescription->getName()]);
  45. }
  46. }
  47. if (!$fieldDescription->getType()) {
  48. throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), get_class($admin)));
  49. }
  50. $fieldDescription->setAdmin($admin);
  51. $fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'standard'));
  52. if (in_array($fieldDescription->getMappingType(), array(ClassMetadataInfo::ONE_TO_MANY, ClassMetadataInfo::MANY_TO_MANY, ClassMetadataInfo::MANY_TO_ONE, ClassMetadataInfo::ONE_TO_ONE ))) {
  53. $admin->attachAdminClass($fieldDescription);
  54. }
  55. }
  56. /**
  57. * @return \Symfony\Component\Form\FormFactoryInterface
  58. */
  59. public function getFormFactory()
  60. {
  61. return $this->formFactory;
  62. }
  63. /**
  64. * @param string $name
  65. * @param array $options
  66. * @return \Symfony\Component\Form\FormBuilder
  67. */
  68. public function getFormBuilder($name, array $options = array())
  69. {
  70. return $this->getFormFactory()->createNamedBuilder('form', $name, null, $options);
  71. }
  72. /**
  73. * @param $type
  74. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  75. * @return array
  76. */
  77. public function getDefaultOptions($type, FieldDescriptionInterface $fieldDescription)
  78. {
  79. $options = array();
  80. $options['sonata_field_description'] = $fieldDescription;
  81. if ($type == 'sonata_type_model') {
  82. $options['class'] = $fieldDescription->getTargetEntity();
  83. $options['model_manager'] = $fieldDescription->getAdmin()->getModelManager();
  84. switch ($fieldDescription->getMappingType()) {
  85. case ClassMetadataInfo::ONE_TO_MANY:
  86. case ClassMetadataInfo::MANY_TO_MANY:
  87. $options['multiple'] = true;
  88. $options['parent'] = 'choice';
  89. break;
  90. case ClassMetadataInfo::MANY_TO_ONE:
  91. case ClassMetadataInfo::ONE_TO_ONE:
  92. break;
  93. }
  94. if ($fieldDescription->getOption('edit') == 'list') {
  95. $options['parent'] = 'text';
  96. if (!array_key_exists('required', $options)) {
  97. $options['required'] = false;
  98. }
  99. }
  100. } else if ($type == 'sonata_type_admin') {
  101. if (!$fieldDescription->getAssociationAdmin()) {
  102. 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()));
  103. }
  104. $options['data_class']=$fieldDescription->getAssociationAdmin()->getClass();
  105. $fieldDescription->setOption('edit','admin');
  106. } else if ($type == 'sonata_type_collection') {
  107. if (!$fieldDescription->getAssociationAdmin()) {
  108. 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()));
  109. }
  110. $options['type'] = 'sonata_type_admin';
  111. $options['modifiable'] = true;
  112. $options['type_options'] = array(
  113. 'sonata_field_description' => $fieldDescription,
  114. 'data_class' => $fieldDescription->getAssociationAdmin()->getClass()
  115. );
  116. }
  117. return $options;
  118. }
  119. }