FormMapper.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project 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. namespace Sonata\AdminBundle\Form;
  11. use Sonata\AdminBundle\Admin\AdminInterface;
  12. use Sonata\AdminBundle\Builder\FormContractorInterface;
  13. use Sonata\AdminBundle\Mapper\BaseGroupedMapper;
  14. use Symfony\Component\Form\FormBuilderInterface;
  15. /**
  16. * Class FormMapper
  17. * This class is use to simulate the Form API.
  18. *
  19. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  20. */
  21. class FormMapper extends BaseGroupedMapper
  22. {
  23. /**
  24. * @var FormBuilderInterface
  25. */
  26. protected $formBuilder;
  27. /**
  28. * @param FormContractorInterface $formContractor
  29. * @param FormBuilderInterface $formBuilder
  30. * @param AdminInterface $admin
  31. */
  32. public function __construct(FormContractorInterface $formContractor, FormBuilderInterface $formBuilder, AdminInterface $admin)
  33. {
  34. parent::__construct($formContractor, $admin);
  35. $this->formBuilder = $formBuilder;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function reorder(array $keys)
  41. {
  42. $this->admin->reorderFormGroup($this->getCurrentGroupName(), $keys);
  43. return $this;
  44. }
  45. /**
  46. * @param string $name
  47. * @param string $type
  48. * @param array $options
  49. * @param array $fieldDescriptionOptions
  50. *
  51. * @return $this
  52. */
  53. public function add($name, $type = null, array $options = array(), array $fieldDescriptionOptions = array())
  54. {
  55. if ($this->apply !== null && !$this->apply) {
  56. return $this;
  57. }
  58. if ($name instanceof FormBuilderInterface) {
  59. $fieldName = $name->getName();
  60. } else {
  61. $fieldName = $name;
  62. }
  63. // "Dot" notation is not allowed as form name, but can be used as property path to access nested data.
  64. if (!$name instanceof FormBuilderInterface && strpos($fieldName, '.') !== false && !isset($options['property_path'])) {
  65. $options['property_path'] = $fieldName;
  66. // fix the form name
  67. $fieldName = str_replace('.', '__', $fieldName);
  68. }
  69. // change `collection` to `sonata_type_native_collection` form type to
  70. // avoid BC break problems
  71. if ($type === 'collection' || $type === 'Symfony\Component\Form\Extension\Core\Type\CollectionType') {
  72. // the field name is used to preserve Symfony <2.8 compatibility, the FQCN should be used instead
  73. $type = 'sonata_type_native_collection';
  74. }
  75. $label = $fieldName;
  76. $group = $this->addFieldToCurrentGroup($label);
  77. // Try to autodetect type
  78. if ($name instanceof FormBuilderInterface && null === $type) {
  79. $fieldDescriptionOptions['type'] = get_class($name->getType()->getInnerType());
  80. }
  81. if (!isset($fieldDescriptionOptions['type']) && is_string($type)) {
  82. $fieldDescriptionOptions['type'] = $type;
  83. }
  84. if ($group['translation_domain'] && !isset($fieldDescriptionOptions['translation_domain'])) {
  85. $fieldDescriptionOptions['translation_domain'] = $group['translation_domain'];
  86. }
  87. $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
  88. $this->admin->getClass(),
  89. $name instanceof FormBuilderInterface ? $name->getName() : $name,
  90. $fieldDescriptionOptions
  91. );
  92. // Note that the builder var is actually the formContractor:
  93. $this->builder->fixFieldDescription($this->admin, $fieldDescription, $fieldDescriptionOptions);
  94. if ($fieldName != $name) {
  95. $fieldDescription->setName($fieldName);
  96. }
  97. $this->admin->addFormFieldDescription($fieldName, $fieldDescription);
  98. if ($name instanceof FormBuilderInterface) {
  99. $this->formBuilder->add($name);
  100. } else {
  101. // Note that the builder var is actually the formContractor:
  102. $options = array_replace_recursive($this->builder->getDefaultOptions($type, $fieldDescription), $options);
  103. // be compatible with mopa if not installed, avoid generating an exception for invalid option
  104. // force the default to false ...
  105. if (!isset($options['label_render'])) {
  106. $options['label_render'] = false;
  107. }
  108. if (!isset($options['label'])) {
  109. $options['label'] = $this->admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName(), 'form', 'label');
  110. }
  111. $help = null;
  112. if (isset($options['help'])) {
  113. $help = $options['help'];
  114. unset($options['help']);
  115. }
  116. $this->formBuilder->add($fieldDescription->getName(), $type, $options);
  117. if (null !== $help) {
  118. $this->admin->getFormFieldDescription($fieldDescription->getName())->setHelp($help);
  119. }
  120. }
  121. return $this;
  122. }
  123. /**
  124. * {@inheritdoc}
  125. */
  126. public function get($name)
  127. {
  128. return $this->formBuilder->get($name);
  129. }
  130. /**
  131. * {@inheritdoc}
  132. */
  133. public function has($key)
  134. {
  135. return $this->formBuilder->has($key);
  136. }
  137. /**
  138. * {@inheritdoc}
  139. */
  140. final public function keys()
  141. {
  142. return array_keys($this->formBuilder->all());
  143. }
  144. /**
  145. * {@inheritdoc}
  146. */
  147. public function remove($key)
  148. {
  149. $this->admin->removeFormFieldDescription($key);
  150. $this->admin->removeFieldFromFormGroup($key);
  151. $this->formBuilder->remove($key);
  152. return $this;
  153. }
  154. /**
  155. * Removes a group.
  156. *
  157. * @param string $group The group to delete
  158. * @param string $tab The tab the group belongs to, defaults to 'default'
  159. * @param bool $deleteEmptyTab Whether or not the Tab should be deleted, when the deleted group leaves the tab empty after deletion
  160. *
  161. * @return $this
  162. */
  163. public function removeGroup($group, $tab = 'default', $deleteEmptyTab = false)
  164. {
  165. $groups = $this->getGroups();
  166. // When the default tab is used, the tabname is not prepended to the index in the group array
  167. if ($tab !== 'default') {
  168. $group = $tab.'.'.$group;
  169. }
  170. if (isset($groups[$group])) {
  171. foreach ($groups[$group]['fields'] as $field) {
  172. $this->remove($field);
  173. }
  174. }
  175. unset($groups[$group]);
  176. $tabs = $this->getTabs();
  177. $key = array_search($group, $tabs[$tab]['groups']);
  178. if (false !== $key) {
  179. unset($tabs[$tab]['groups'][$key]);
  180. }
  181. if ($deleteEmptyTab && count($tabs[$tab]['groups']) == 0) {
  182. unset($tabs[$tab]);
  183. }
  184. $this->setTabs($tabs);
  185. $this->setGroups($groups);
  186. return $this;
  187. }
  188. /**
  189. * @return FormBuilderInterface
  190. */
  191. public function getFormBuilder()
  192. {
  193. return $this->formBuilder;
  194. }
  195. /**
  196. * @param string $name
  197. * @param mixed $type
  198. * @param array $options
  199. *
  200. * @return FormBuilderInterface
  201. */
  202. public function create($name, $type = null, array $options = array())
  203. {
  204. return $this->formBuilder->create($name, $type, $options);
  205. }
  206. /**
  207. * @param array $helps
  208. *
  209. * @return FormMapper
  210. */
  211. public function setHelps(array $helps = array())
  212. {
  213. foreach ($helps as $name => $help) {
  214. $this->addHelp($name, $help);
  215. }
  216. return $this;
  217. }
  218. /**
  219. * @param $name
  220. * @param $help
  221. *
  222. * @return FormMapper
  223. */
  224. public function addHelp($name, $help)
  225. {
  226. if ($this->admin->hasFormFieldDescription($name)) {
  227. $this->admin->getFormFieldDescription($name)->setHelp($help);
  228. }
  229. return $this;
  230. }
  231. /**
  232. * {@inheritdoc}
  233. */
  234. protected function getGroups()
  235. {
  236. return $this->admin->getFormGroups();
  237. }
  238. /**
  239. * {@inheritdoc}
  240. */
  241. protected function setGroups(array $groups)
  242. {
  243. $this->admin->setFormGroups($groups);
  244. }
  245. /**
  246. * {@inheritdoc}
  247. */
  248. protected function getTabs()
  249. {
  250. return $this->admin->getFormTabs();
  251. }
  252. /**
  253. * {@inheritdoc}
  254. */
  255. protected function setTabs(array $tabs)
  256. {
  257. $this->admin->setFormTabs($tabs);
  258. }
  259. }