BaseGroupedMapper.php 7.7 KB

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