BaseGroupedMapper.php 7.9 KB

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