FormMapper.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. $options = array_merge($options, $this->formContractor->getDefaultOptions($type, $fieldDescription));
  81. $this->admin->addFormFieldDescription($name instanceof FormBuilder ? $name->getName() : $name, $fieldDescription);
  82. if ($name instanceof FormBuilder) {
  83. $this->formBuilder->add($name);
  84. } else {
  85. $this->formBuilder->add($name, $type, $options);
  86. }
  87. return $this;
  88. }
  89. /**
  90. * @param string $name
  91. * @return \Symfony\Component\Form\FieldInterface
  92. */
  93. public function get($name)
  94. {
  95. return $this->formBuilder->get($name);
  96. }
  97. /**
  98. * @param string $key
  99. * @return boolean
  100. */
  101. public function has($key)
  102. {
  103. return $this->formBuilder->has($key);
  104. }
  105. /**
  106. * @param string $key
  107. * @return void
  108. */
  109. public function remove($key)
  110. {
  111. $this->admin->removeFormFieldDescription($key);
  112. $this->formBuilder->remove($key);
  113. }
  114. /**
  115. * @return \Symfony\Component\Form\FormBuilder
  116. */
  117. public function getFormBuilder()
  118. {
  119. return $this->formBuilder;
  120. }
  121. /**
  122. * @return \Sonata\AdminBundle\Admin\AdminInterface
  123. */
  124. public function getAdmin()
  125. {
  126. return $this->admin;
  127. }
  128. /**
  129. * @param string $name
  130. * @param mixed $type
  131. * @param array $options
  132. * @return \Symfony\Component\Form\FormBuilder
  133. */
  134. public function create($name, $type = null, array $options = array())
  135. {
  136. return $this->formBuilder->create($name, $type, $options);
  137. }
  138. /**
  139. * @param array $helps
  140. * @return FormMapper
  141. */
  142. public function setHelps(array $helps = array())
  143. {
  144. foreach($helps as $name => $help) {
  145. if ($this->admin->hasFormFieldDescription($name)) {
  146. $this->admin->getFormFieldDescription($name)->setHelp($help);
  147. }
  148. }
  149. return $this;
  150. }
  151. }