FormMapper.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. */
  11. namespace Sonata\AdminBundle\Form;
  12. use Sonata\AdminBundle\Builder\FormContractorInterface;
  13. use Sonata\AdminBundle\Admin\AdminInterface;
  14. use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
  15. use Symfony\Component\Form\FormBuilder;
  16. /**
  17. * This class is use to simulate the Form API
  18. *
  19. */
  20. class FormMapper
  21. {
  22. protected $formBuilder;
  23. protected $formContractor;
  24. protected $admin;
  25. protected $currentGroup;
  26. public function __construct(FormContractorInterface $formContractor, FormBuilder $formBuilder, AdminInterface $admin)
  27. {
  28. $this->formBuilder = $formBuilder;
  29. $this->formContractor = $formContractor;
  30. $this->admin = $admin;
  31. }
  32. /**
  33. * @param $name
  34. * @param array $options
  35. * @return void
  36. */
  37. public function with($name, array $options = array())
  38. {
  39. $formGroups = $this->admin->getFormGroups();
  40. if (!isset($formGroups[$name])) {
  41. $formGroups[$name] = array_merge(array('collapsed' => false, 'fields' => array()), $options);
  42. }
  43. $this->admin->setFormGroups($formGroups);
  44. $this->currentGroup = $name;
  45. return $this;
  46. }
  47. /**
  48. * @return void
  49. */
  50. public function end()
  51. {
  52. $this->currentGroup = null;
  53. return $this;
  54. }
  55. /**
  56. * @param string $name
  57. * @param string $type
  58. * @param array $options
  59. * @param array $fieldDescriptionOptions
  60. * @return \Symfony\Component\Form\FormInterface
  61. */
  62. public function add($name, $type = null, array $options = array(), array $fieldDescriptionOptions = array())
  63. {
  64. if (!$this->currentGroup) {
  65. $this->with($this->admin->getLabel());
  66. }
  67. $formGroups = $this->admin->getFormGroups();
  68. $formGroups[$this->currentGroup]['fields'][$name] = $name;
  69. $this->admin->setFormGroups($formGroups);
  70. if (!isset($fieldDescriptionOptions['type']) && is_string($type)) {
  71. $fieldDescriptionOptions['type'] = $type;
  72. }
  73. $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
  74. $this->admin->getClass(),
  75. $name instanceof FormBuilder ? $name->getName() : $name,
  76. $fieldDescriptionOptions
  77. );
  78. $this->formContractor->fixFieldDescription($this->admin, $fieldDescription, $fieldDescriptionOptions);
  79. $options = $this->formContractor->getDefaultOptions($type, $fieldDescription, $options);
  80. $this->admin->addFormFieldDescription($name instanceof FormBuilder ? $name->getName() : $name, $fieldDescription);
  81. if ($name instanceof FormBuilder) {
  82. $this->formBuilder->add($name);
  83. } else {
  84. $this->formBuilder->add($name, $type, $options);
  85. }
  86. return $this;
  87. }
  88. /**
  89. * @param string $name
  90. * @return \Symfony\Component\Form\FieldInterface
  91. */
  92. public function get($name)
  93. {
  94. return $this->formBuilder->get($name);
  95. }
  96. /**
  97. * @param string $key
  98. * @return boolean
  99. */
  100. public function has($key)
  101. {
  102. return $this->formBuilder->has($key);
  103. }
  104. /**
  105. * @param string $key
  106. * @return void
  107. */
  108. public function remove($key)
  109. {
  110. $this->admin->removeFormFieldDescription($key);
  111. $this->formBuilder->remove($key);
  112. }
  113. /**
  114. * @return \Symfony\Component\Form\FormBuilder
  115. */
  116. public function getFormBuilder()
  117. {
  118. return $this->formBuilder;
  119. }
  120. /**
  121. * @return \Sonata\AdminBundle\Admin\AdminInterface
  122. */
  123. public function getAdmin()
  124. {
  125. return $this->admin;
  126. }
  127. /**
  128. * @param string $name
  129. * @param mixed $type
  130. * @param array $options
  131. * @return void
  132. */
  133. public function create($name, $type = null, array $options = array())
  134. {
  135. return $this->formBuilder->create($name, $type, $options);
  136. }
  137. public function setHelps(array $helps = array())
  138. {
  139. foreach($helps as $name => $help) {
  140. if ($this->admin->hasFormFieldDescription($name)) {
  141. $this->admin->getFormFieldDescription($name)->setHelp($help);
  142. }
  143. }
  144. return $this;
  145. }
  146. }