BaseGroupedMapper.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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\Mapper;
  12. /**
  13. * This class is used to simulate the Form API
  14. *
  15. */
  16. abstract class BaseGroupedMapper extends BaseMapper
  17. {
  18. protected $currentGroup;
  19. protected $currentTab;
  20. abstract protected function getGroups();
  21. abstract protected function getTabs();
  22. abstract protected function setGroups(array $groups);
  23. abstract protected function setTabs(array $tabs);
  24. /**
  25. * Add new group or tab (if parameter "tab=true" is available in options)
  26. *
  27. * @param string $name
  28. * @param array $options
  29. *
  30. * @return BaseGroupedMapper
  31. *
  32. * @throws \RuntimeException
  33. */
  34. public function with($name, array $options = array())
  35. {
  36. /**
  37. * The current implementation should work with the following workflow:
  38. *
  39. * $formMapper
  40. * ->with('group1')
  41. * ->add('username')
  42. * ->add('password')
  43. * ->end()
  44. * ->with('tab1', array('tab' => true))
  45. * ->with('group1')
  46. * ->add('username')
  47. * ->add('password')
  48. * ->end()
  49. * ->with('group2', array('collapsed' => true))
  50. * ->add('enabled')
  51. * ->add('createdAt')
  52. * ->end()
  53. * ->end();
  54. *
  55. */
  56. $defaultOptions = array(
  57. 'collapsed' => false,
  58. 'class' => false,
  59. 'description' => false,
  60. 'translation_domain' => null,
  61. 'name' => $name,
  62. );
  63. $code = $name;
  64. // Open
  65. if (array_key_exists('tab', $options) && $options['tab']) {
  66. $tabs = $this->getTabs();
  67. if ($this->currentTab) {
  68. if (isset($tabs[$this->currentTab]['auto_created']) && true === $tabs[$this->currentTab]['auto_created']) {
  69. throw new \RuntimeException('New tab was added automatically when you have added field or group. You should close current tab before adding new one OR add tabs before adding groups and fields.');
  70. } else {
  71. throw new \RuntimeException(sprintf('You should close previous tab "%s" with end() before adding new tab "%s".', $this->currentTab, $name));
  72. }
  73. } elseif ($this->currentGroup) {
  74. throw new \RuntimeException(sprintf('You should open tab before adding new group "%s".', $name));
  75. }
  76. if (!isset($tabs[$name])) {
  77. $tabs[$name] = array();
  78. }
  79. $tabs[$code] = array_merge($defaultOptions, array(
  80. 'auto_created' => false,
  81. 'groups' => array(),
  82. ), $tabs[$code], $options);
  83. $this->currentTab = $code;
  84. } else {
  85. if ($this->currentGroup) {
  86. throw new \RuntimeException(sprintf('You should close previous group "%s" with end() before adding new tab "%s".', $this->currentGroup, $name));
  87. }
  88. if (!$this->currentTab) {
  89. // no tab define
  90. $this->with('default', array(
  91. 'tab' => true,
  92. 'auto_created' => true,
  93. 'translation_domain' => isset($options['translation_domain']) ? $options['translation_domain'] : null
  94. )); // add new tab automatically
  95. }
  96. // if no tab is selected, we go the the main one named '_' ..
  97. if ($this->currentTab !== 'default') {
  98. $code = $this->currentTab.'.'.$name; // groups with the same name can be on different tabs, so we prefix them in order to make unique group name
  99. }
  100. $groups = $this->getGroups();
  101. if (!isset($groups[$code])) {
  102. $groups[$code] = array();
  103. }
  104. $groups[$code] = array_merge($defaultOptions, array(
  105. 'fields' => array(),
  106. ), $groups[$code], $options);
  107. $this->currentGroup = $code;
  108. $this->setGroups($groups);
  109. $tabs = $this->getTabs();
  110. }
  111. if ($this->currentGroup && isset($tabs[$this->currentTab]) && !in_array($this->currentGroup, $tabs[$this->currentTab]['groups'])) {
  112. $tabs[$this->currentTab]['groups'][] = $this->currentGroup;
  113. }
  114. $this->setTabs($tabs);
  115. return $this;
  116. }
  117. /**
  118. * Add new tab
  119. *
  120. * @param string $name
  121. * @param array $options
  122. *
  123. * @return BaseGroupedMapper
  124. */
  125. public function tab($name, array $options = array())
  126. {
  127. return $this->with($name, array_merge($options, array('tab' => true)));
  128. }
  129. /**
  130. * Close the current group or tab
  131. *
  132. * @return BaseGroupedMapper
  133. *
  134. * @throws \RuntimeException
  135. */
  136. public function end()
  137. {
  138. if ($this->currentGroup !== null) {
  139. $this->currentGroup = null;
  140. } elseif ($this->currentTab !== null) {
  141. $this->currentTab = null;
  142. } else {
  143. throw new \RuntimeException('No open tabs or groups, you cannot use end()');
  144. }
  145. return $this;
  146. }
  147. /**
  148. * Add the fieldname to the current group
  149. *
  150. * @param string $fieldName
  151. */
  152. protected function addFieldToCurrentGroup($fieldName)
  153. {
  154. // Note this line must happen before the next line.
  155. // See https://github.com/sonata-project/SonataAdminBundle/pull/1351
  156. $currentGroup = $this->getCurrentGroupName();
  157. $groups = $this->getGroups();
  158. $groups[$currentGroup]['fields'][$fieldName] = $fieldName;
  159. $this->setGroups($groups);
  160. return $groups[$currentGroup];
  161. }
  162. /**
  163. * Return the name of the currently selected group. The method also makes
  164. * sure a valid group name is currently selected
  165. *
  166. * Note that this can have the side effect to change the 'group' value
  167. * returned by the getGroup function
  168. *
  169. * @return string
  170. */
  171. protected function getCurrentGroupName()
  172. {
  173. if (!$this->currentGroup) {
  174. $this->with($this->admin->getLabel(), array('auto_created' => true));
  175. }
  176. return $this->currentGroup;
  177. }
  178. }