FormContractor.php 5.0 KB

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