FormMapper.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 string $name
  58. * @param string $type
  59. * @param array $options
  60. * @param array $fieldDescriptionOptions
  61. * @return \Sonata\AdminBundle\Form\FormMapper
  62. */
  63. public function add($name, $type = null, array $options = array(), array $fieldDescriptionOptions = array())
  64. {
  65. if (!$this->currentGroup) {
  66. $this->with($this->admin->getLabel());
  67. }
  68. $formGroups = $this->admin->getFormGroups();
  69. $formGroups[$this->currentGroup]['fields'][$name] = $name;
  70. $this->admin->setFormGroups($formGroups);
  71. if (!isset($fieldDescriptionOptions['type']) && is_string($type)) {
  72. $fieldDescriptionOptions['type'] = $type;
  73. }
  74. $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
  75. $this->admin->getClass(),
  76. $name instanceof FormBuilder ? $name->getName() : $name,
  77. $fieldDescriptionOptions
  78. );
  79. $this->formContractor->fixFieldDescription($this->admin, $fieldDescription, $fieldDescriptionOptions);
  80. $this->admin->addFormFieldDescription($name instanceof FormBuilder ? $name->getName() : $name, $fieldDescription);
  81. if ($name instanceof FormBuilder) {
  82. $this->formBuilder->add($name);
  83. } else {
  84. $options = array_replace_recursive($this->formContractor->getDefaultOptions($type, $fieldDescription), $options);
  85. if (!isset($options['label'])) {
  86. $options['label'] = $this->admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName(), 'form', 'label');
  87. }
  88. $this->formBuilder->add($name, $type, $options);
  89. }
  90. return $this;
  91. }
  92. /**
  93. * @param string $name
  94. * @return \Symfony\Component\Form\FieldInterface
  95. */
  96. public function get($name)
  97. {
  98. return $this->formBuilder->get($name);
  99. }
  100. /**
  101. * @param string $key
  102. * @return boolean
  103. */
  104. public function has($key)
  105. {
  106. return $this->formBuilder->has($key);
  107. }
  108. /**
  109. * @param string $key
  110. * @return \Sonata\AdminBundle\Form\FormMapper
  111. */
  112. public function remove($key)
  113. {
  114. $this->admin->removeFormFieldDescription($key);
  115. $this->formBuilder->remove($key);
  116. return $this;
  117. }
  118. /**
  119. * @return \Symfony\Component\Form\FormBuilder
  120. */
  121. public function getFormBuilder()
  122. {
  123. return $this->formBuilder;
  124. }
  125. /**
  126. * @return \Sonata\AdminBundle\Admin\AdminInterface
  127. */
  128. public function getAdmin()
  129. {
  130. return $this->admin;
  131. }
  132. /**
  133. * @param string $name
  134. * @param mixed $type
  135. * @param array $options
  136. * @return \Symfony\Component\Form\FormBuilder
  137. */
  138. public function create($name, $type = null, array $options = array())
  139. {
  140. return $this->formBuilder->create($name, $type, $options);
  141. }
  142. /**
  143. * @param array $helps
  144. * @return FormMapper
  145. */
  146. public function setHelps(array $helps = array())
  147. {
  148. foreach($helps as $name => $help) {
  149. if ($this->admin->hasFormFieldDescription($name)) {
  150. $this->admin->getFormFieldDescription($name)->setHelp($help);
  151. }
  152. }
  153. return $this;
  154. }
  155. }