FormMapper.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. if (!isset($fieldDescriptionOptions['type']) && is_string($type)) {
  78. $fieldDescriptionOptions['type'] = $type;
  79. }
  80. if ($group['translation_domain'] && !isset($fieldDescriptionOptions['translation_domain'])) {
  81. $fieldDescriptionOptions['translation_domain'] = $group['translation_domain'];
  82. }
  83. $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
  84. $this->admin->getClass(),
  85. $name instanceof FormBuilderInterface ? $name->getName() : $name,
  86. $fieldDescriptionOptions
  87. );
  88. // Note that the builder var is actually the formContractor:
  89. $this->builder->fixFieldDescription($this->admin, $fieldDescription, $fieldDescriptionOptions);
  90. if ($fieldName != $name) {
  91. $fieldDescription->setName($fieldName);
  92. }
  93. $this->admin->addFormFieldDescription($fieldName, $fieldDescription);
  94. if ($name instanceof FormBuilderInterface) {
  95. $this->formBuilder->add($name);
  96. } else {
  97. // Note that the builder var is actually the formContractor:
  98. $options = array_replace_recursive($this->builder->getDefaultOptions($type, $fieldDescription), $options);
  99. // be compatible with mopa if not installed, avoid generating an exception for invalid option
  100. // force the default to false ...
  101. if (!isset($options['label_render'])) {
  102. $options['label_render'] = false;
  103. }
  104. if (!isset($options['label'])) {
  105. $options['label'] = $this->admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName(), 'form', 'label');
  106. }
  107. $help = null;
  108. if (isset($options['help'])) {
  109. $help = $options['help'];
  110. unset($options['help']);
  111. }
  112. $this->formBuilder->add($fieldDescription->getName(), $type, $options);
  113. if (null !== $help) {
  114. $this->admin->getFormFieldDescription($fieldDescription->getName())->setHelp($help);
  115. }
  116. }
  117. return $this;
  118. }
  119. /**
  120. * {@inheritdoc}
  121. */
  122. public function get($name)
  123. {
  124. return $this->formBuilder->get($name);
  125. }
  126. /**
  127. * {@inheritdoc}
  128. */
  129. public function has($key)
  130. {
  131. return $this->formBuilder->has($key);
  132. }
  133. /**
  134. * {@inheritdoc}
  135. */
  136. final public function keys()
  137. {
  138. return array_keys($this->formBuilder->all());
  139. }
  140. /**
  141. * {@inheritdoc}
  142. */
  143. public function remove($key)
  144. {
  145. $this->admin->removeFormFieldDescription($key);
  146. $this->admin->removeFieldFromFormGroup($key);
  147. $this->formBuilder->remove($key);
  148. return $this;
  149. }
  150. /**
  151. * Removes a group.
  152. *
  153. * @param string $group The group to delete
  154. * @param string $tab The tab the group belongs to, defaults to 'default'
  155. * @param bool $deleteEmptyTab Whether or not the Tab should be deleted, when the deleted group leaves the tab empty after deletion
  156. *
  157. * @return $this
  158. */
  159. public function removeGroup($group, $tab = 'default', $deleteEmptyTab = false)
  160. {
  161. $groups = $this->getGroups();
  162. // When the default tab is used, the tabname is not prepended to the index in the group array
  163. if ($tab !== 'default') {
  164. $group = $tab.'.'.$group;
  165. }
  166. if (isset($groups[$group])) {
  167. foreach ($groups[$group]['fields'] as $field) {
  168. $this->remove($field);
  169. }
  170. }
  171. unset($groups[$group]);
  172. $tabs = $this->getTabs();
  173. $key = array_search($group, $tabs[$tab]['groups']);
  174. if (false !== $key) {
  175. unset($tabs[$tab]['groups'][$key]);
  176. }
  177. if ($deleteEmptyTab && count($tabs[$tab]['groups']) == 0) {
  178. unset($tabs[$tab]);
  179. }
  180. $this->setTabs($tabs);
  181. $this->setGroups($groups);
  182. return $this;
  183. }
  184. /**
  185. * @return FormBuilderInterface
  186. */
  187. public function getFormBuilder()
  188. {
  189. return $this->formBuilder;
  190. }
  191. /**
  192. * @param string $name
  193. * @param mixed $type
  194. * @param array $options
  195. *
  196. * @return FormBuilderInterface
  197. */
  198. public function create($name, $type = null, array $options = array())
  199. {
  200. return $this->formBuilder->create($name, $type, $options);
  201. }
  202. /**
  203. * @param array $helps
  204. *
  205. * @return FormMapper
  206. */
  207. public function setHelps(array $helps = array())
  208. {
  209. foreach ($helps as $name => $help) {
  210. $this->addHelp($name, $help);
  211. }
  212. return $this;
  213. }
  214. /**
  215. * @param $name
  216. * @param $help
  217. *
  218. * @return FormMapper
  219. */
  220. public function addHelp($name, $help)
  221. {
  222. if ($this->admin->hasFormFieldDescription($name)) {
  223. $this->admin->getFormFieldDescription($name)->setHelp($help);
  224. }
  225. return $this;
  226. }
  227. /**
  228. * {@inheritdoc}
  229. */
  230. protected function getGroups()
  231. {
  232. return $this->admin->getFormGroups();
  233. }
  234. /**
  235. * {@inheritdoc}
  236. */
  237. protected function setGroups(array $groups)
  238. {
  239. $this->admin->setFormGroups($groups);
  240. }
  241. /**
  242. * {@inheritdoc}
  243. */
  244. protected function getTabs()
  245. {
  246. return $this->admin->getFormTabs();
  247. }
  248. /**
  249. * {@inheritdoc}
  250. */
  251. protected function setTabs(array $tabs)
  252. {
  253. $this->admin->setFormTabs($tabs);
  254. }
  255. }