FormMapper.php 6.0 KB

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