FormMapper.php 5.0 KB

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