BaseGroupedMapper.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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\Mapper;
  11. /**
  12. * Class BaseGroupedMapper
  13. * This class is used to simulate the Form API.
  14. *
  15. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  16. */
  17. abstract class BaseGroupedMapper extends BaseMapper
  18. {
  19. protected $currentGroup;
  20. protected $currentTab;
  21. protected $apply;
  22. abstract protected function getGroups();
  23. abstract protected function getTabs();
  24. abstract protected function setGroups(array $groups);
  25. abstract protected function setTabs(array $tabs);
  26. /**
  27. * Add new group or tab (if parameter "tab=true" is available in options).
  28. *
  29. * @param string $name
  30. * @param array $options
  31. *
  32. * @return $this
  33. *
  34. * @throws \RuntimeException
  35. */
  36. public function with($name, array $options = array())
  37. {
  38. /*
  39. * The current implementation should work with the following workflow:
  40. *
  41. * $formMapper
  42. * ->with('group1')
  43. * ->add('username')
  44. * ->add('password')
  45. * ->end()
  46. * ->with('tab1', array('tab' => true))
  47. * ->with('group1')
  48. * ->add('username')
  49. * ->add('password')
  50. * ->end()
  51. * ->with('group2', array('collapsed' => true))
  52. * ->add('enabled')
  53. * ->add('createdAt')
  54. * ->end()
  55. * ->end();
  56. *
  57. */
  58. $defaultOptions = array(
  59. 'collapsed' => false,
  60. 'class' => false,
  61. 'description' => false,
  62. 'translation_domain' => null,
  63. 'name' => $name,
  64. 'box_class' => 'box box-primary',
  65. );
  66. $code = $name;
  67. // Open
  68. if (array_key_exists('tab', $options) && $options['tab']) {
  69. $tabs = $this->getTabs();
  70. if ($this->currentTab) {
  71. if (isset($tabs[$this->currentTab]['auto_created']) && true === $tabs[$this->currentTab]['auto_created']) {
  72. 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.');
  73. } else {
  74. throw new \RuntimeException(sprintf('You should close previous tab "%s" with end() before adding new tab "%s".', $this->currentTab, $name));
  75. }
  76. } elseif ($this->currentGroup) {
  77. throw new \RuntimeException(sprintf('You should open tab before adding new group "%s".', $name));
  78. }
  79. if (!isset($tabs[$name])) {
  80. $tabs[$name] = array();
  81. }
  82. $tabs[$code] = array_merge($defaultOptions, array(
  83. 'auto_created' => false,
  84. 'groups' => array(),
  85. ), $tabs[$code], $options);
  86. $this->currentTab = $code;
  87. } else {
  88. if ($this->currentGroup) {
  89. throw new \RuntimeException(sprintf('You should close previous group "%s" with end() before adding new tab "%s".', $this->currentGroup, $name));
  90. }
  91. if (!$this->currentTab) {
  92. // no tab define
  93. $this->with('default', array(
  94. 'tab' => true,
  95. 'auto_created' => true,
  96. 'translation_domain' => isset($options['translation_domain']) ? $options['translation_domain'] : null,
  97. )); // add new tab automatically
  98. }
  99. // if no tab is selected, we go the the main one named '_' ..
  100. if ($this->currentTab !== 'default') {
  101. $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
  102. }
  103. $groups = $this->getGroups();
  104. if (!isset($groups[$code])) {
  105. $groups[$code] = array();
  106. }
  107. $groups[$code] = array_merge($defaultOptions, array(
  108. 'fields' => array(),
  109. ), $groups[$code], $options);
  110. $this->currentGroup = $code;
  111. $this->setGroups($groups);
  112. $tabs = $this->getTabs();
  113. }
  114. if ($this->currentGroup && isset($tabs[$this->currentTab]) && !in_array($this->currentGroup, $tabs[$this->currentTab]['groups'])) {
  115. $tabs[$this->currentTab]['groups'][] = $this->currentGroup;
  116. }
  117. $this->setTabs($tabs);
  118. return $this;
  119. }
  120. /**
  121. * Only nested add if the condition match true.
  122. *
  123. * @param bool $bool
  124. *
  125. * @return $this
  126. *
  127. * @throws \RuntimeException
  128. */
  129. public function ifTrue($bool)
  130. {
  131. if ($this->apply !== null) {
  132. throw new \RuntimeException('Cannot nest ifTrue or ifFalse call');
  133. }
  134. $this->apply = ($bool === true);
  135. return $this;
  136. }
  137. /**
  138. * Only nested add if the condition match false.
  139. *
  140. * @param bool $bool
  141. *
  142. * @return $this
  143. *
  144. * @throws \RuntimeException
  145. */
  146. public function ifFalse($bool)
  147. {
  148. if ($this->apply !== null) {
  149. throw new \RuntimeException('Cannot nest ifTrue or ifFalse call');
  150. }
  151. $this->apply = ($bool === false);
  152. return $this;
  153. }
  154. /**
  155. * @return $this
  156. */
  157. public function ifEnd()
  158. {
  159. $this->apply = null;
  160. return $this;
  161. }
  162. /**
  163. * Add new tab.
  164. *
  165. * @param string $name
  166. * @param array $options
  167. *
  168. * @return $this
  169. */
  170. public function tab($name, array $options = array())
  171. {
  172. return $this->with($name, array_merge($options, array('tab' => true)));
  173. }
  174. /**
  175. * Close the current group or tab.
  176. *
  177. * @return $this
  178. *
  179. * @throws \RuntimeException
  180. */
  181. public function end()
  182. {
  183. if ($this->currentGroup !== null) {
  184. $this->currentGroup = null;
  185. } elseif ($this->currentTab !== null) {
  186. $this->currentTab = null;
  187. } else {
  188. throw new \RuntimeException('No open tabs or groups, you cannot use end()');
  189. }
  190. return $this;
  191. }
  192. /**
  193. * Returns a boolean indicating if there is an open tab at the moment.
  194. *
  195. * @return bool
  196. */
  197. public function hasOpenTab()
  198. {
  199. return null !== $this->currentTab;
  200. }
  201. /**
  202. * Add the field name to the current group.
  203. *
  204. * @param string $fieldName
  205. */
  206. protected function addFieldToCurrentGroup($fieldName)
  207. {
  208. // Note this line must happen before the next line.
  209. // See https://github.com/sonata-project/SonataAdminBundle/pull/1351
  210. $currentGroup = $this->getCurrentGroupName();
  211. $groups = $this->getGroups();
  212. $groups[$currentGroup]['fields'][$fieldName] = $fieldName;
  213. $this->setGroups($groups);
  214. return $groups[$currentGroup];
  215. }
  216. /**
  217. * Return the name of the currently selected group. The method also makes
  218. * sure a valid group name is currently selected.
  219. *
  220. * Note that this can have the side effect to change the 'group' value
  221. * returned by the getGroup function
  222. *
  223. * @return string
  224. */
  225. protected function getCurrentGroupName()
  226. {
  227. if (!$this->currentGroup) {
  228. $this->with($this->admin->getLabel(), array('auto_created' => true));
  229. }
  230. return $this->currentGroup;
  231. }
  232. }