FormMapper.php 6.0 KB

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