FormMapper.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 string $name
  34. * @param array $options
  35. * @return \Sonata\AdminBundle\Form\FormMapper
  36. */
  37. public function with($name, array $options = array())
  38. {
  39. $formGroups = $this->admin->getFormGroups();
  40. if (!isset($formGroups[$name])) {
  41. $formGroups[$name] = array();
  42. }
  43. $formGroups[$name] = array_merge(array('collapsed' => false, 'fields' => array()), $formGroups[$name], $options);
  44. $this->admin->setFormGroups($formGroups);
  45. $this->currentGroup = $name;
  46. return $this;
  47. }
  48. /**
  49. * @return \Sonata\AdminBundle\Form\FormMapper
  50. */
  51. public function end()
  52. {
  53. $this->currentGroup = null;
  54. return $this;
  55. }
  56. /**
  57. * @param array $keys field names
  58. * @return \Sonata\AdminBundle\Form\FormMapper
  59. */
  60. public function reorder(array $keys)
  61. {
  62. if (!$this->currentGroup) {
  63. $this->with($this->admin->getLabel());
  64. }
  65. $this->admin->reorderFormGroup($this->currentGroup, $keys);
  66. return $this;
  67. }
  68. /**
  69. * @param string $name
  70. * @param string $type
  71. * @param array $options
  72. * @param array $fieldDescriptionOptions
  73. * @return \Sonata\AdminBundle\Form\FormMapper
  74. */
  75. public function add($name, $type = null, array $options = array(), array $fieldDescriptionOptions = array())
  76. {
  77. if (!$this->currentGroup) {
  78. $this->with($this->admin->getLabel());
  79. }
  80. $label = $name instanceof FormBuilder ? $name->getName() : $name;
  81. $formGroups = $this->admin->getFormGroups();
  82. $formGroups[$this->currentGroup]['fields'][$label] = $label;
  83. $this->admin->setFormGroups($formGroups);
  84. if (!isset($fieldDescriptionOptions['type']) && is_string($type)) {
  85. $fieldDescriptionOptions['type'] = $type;
  86. }
  87. $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
  88. $this->admin->getClass(),
  89. $name instanceof FormBuilder ? $name->getName() : $name,
  90. $fieldDescriptionOptions
  91. );
  92. $this->formContractor->fixFieldDescription($this->admin, $fieldDescription, $fieldDescriptionOptions);
  93. $this->admin->addFormFieldDescription($name instanceof FormBuilder ? $name->getName() : $name, $fieldDescription);
  94. if ($name instanceof FormBuilder) {
  95. $this->formBuilder->add($name);
  96. } else {
  97. $options = array_replace_recursive($this->formContractor->getDefaultOptions($type, $fieldDescription), $options);
  98. if (!isset($options['label'])) {
  99. $options['label'] = $this->admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName(), 'form', 'label');
  100. }
  101. $help = null;
  102. if (isset($options['help'])) {
  103. $help = $options['help'];
  104. unset($options['help']);
  105. }
  106. $this->formBuilder->add($name, $type, $options);
  107. if (null !== $help) {
  108. $this->admin->getFormFieldDescription($name)->setHelp($help);
  109. }
  110. }
  111. return $this;
  112. }
  113. /**
  114. * @param string $name
  115. * @return \Symfony\Component\Form\FieldInterface
  116. */
  117. public function get($name)
  118. {
  119. return $this->formBuilder->get($name);
  120. }
  121. /**
  122. * @param string $key
  123. * @return boolean
  124. */
  125. public function has($key)
  126. {
  127. return $this->formBuilder->has($key);
  128. }
  129. /**
  130. * @param string $key
  131. * @return \Sonata\AdminBundle\Form\FormMapper
  132. */
  133. public function remove($key)
  134. {
  135. $this->admin->removeFormFieldDescription($key);
  136. $this->formBuilder->remove($key);
  137. return $this;
  138. }
  139. /**
  140. * @return \Symfony\Component\Form\FormBuilder
  141. */
  142. public function getFormBuilder()
  143. {
  144. return $this->formBuilder;
  145. }
  146. /**
  147. * @return \Sonata\AdminBundle\Admin\AdminInterface
  148. */
  149. public function getAdmin()
  150. {
  151. return $this->admin;
  152. }
  153. /**
  154. * @param string $name
  155. * @param mixed $type
  156. * @param array $options
  157. * @return \Symfony\Component\Form\FormBuilder
  158. */
  159. public function create($name, $type = null, array $options = array())
  160. {
  161. return $this->formBuilder->create($name, $type, $options);
  162. }
  163. /**
  164. * @param array $helps
  165. * @return FormMapper
  166. */
  167. public function setHelps(array $helps = array())
  168. {
  169. foreach($helps as $name => $help) {
  170. if ($this->admin->hasFormFieldDescription($name)) {
  171. $this->admin->getFormFieldDescription($name)->setHelp($help);
  172. }
  173. }
  174. return $this;
  175. }
  176. }