BaseGroupedMapper.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. use Sonata\AdminBundle\Admin\AbstractAdmin;
  12. /**
  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|null
  21. */
  22. protected $currentGroup;
  23. /**
  24. * @var string|null
  25. */
  26. protected $currentTab;
  27. /**
  28. * @var bool|null
  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. 'label' => $name, // NEXT_MAJOR: Remove this line and uncomment the next one
  68. // 'label' => $this->admin->getLabelTranslatorStrategy()->getLabel($name, $this->getName(), 'group'),
  69. 'translation_domain' => null,
  70. 'name' => $name,
  71. 'box_class' => 'box box-primary',
  72. );
  73. // NEXT_MAJOR: remove this code
  74. if ($this->admin instanceof AbstractAdmin && $pool = $this->admin->getConfigurationPool()) {
  75. if ($pool->getContainer()->getParameter('sonata.admin.configuration.translate_group_label')) {
  76. $defaultOptions['label'] = $this->admin->getLabelTranslatorStrategy()->getLabel($name, $this->getName(), 'group');
  77. }
  78. }
  79. $code = $name;
  80. // Open
  81. if (array_key_exists('tab', $options) && $options['tab']) {
  82. $tabs = $this->getTabs();
  83. if ($this->currentTab) {
  84. if (isset($tabs[$this->currentTab]['auto_created']) && true === $tabs[$this->currentTab]['auto_created']) {
  85. 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.');
  86. }
  87. throw new \RuntimeException(sprintf('You should close previous tab "%s" with end() before adding new tab "%s".', $this->currentTab, $name));
  88. } elseif ($this->currentGroup) {
  89. throw new \RuntimeException(sprintf('You should open tab before adding new group "%s".', $name));
  90. }
  91. if (!isset($tabs[$name])) {
  92. $tabs[$name] = array();
  93. }
  94. $tabs[$code] = array_merge($defaultOptions, array(
  95. 'auto_created' => false,
  96. 'groups' => array(),
  97. ), $tabs[$code], $options);
  98. $this->currentTab = $code;
  99. } else {
  100. if ($this->currentGroup) {
  101. throw new \RuntimeException(sprintf('You should close previous group "%s" with end() before adding new tab "%s".', $this->currentGroup, $name));
  102. }
  103. if (!$this->currentTab) {
  104. // no tab define
  105. $this->with('default', array(
  106. 'tab' => true,
  107. 'auto_created' => true,
  108. 'translation_domain' => isset($options['translation_domain']) ? $options['translation_domain'] : null,
  109. )); // add new tab automatically
  110. }
  111. // if no tab is selected, we go the the main one named '_' ..
  112. if ($this->currentTab !== 'default') {
  113. $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
  114. }
  115. $groups = $this->getGroups();
  116. if (!isset($groups[$code])) {
  117. $groups[$code] = array();
  118. }
  119. $groups[$code] = array_merge($defaultOptions, array(
  120. 'fields' => array(),
  121. ), $groups[$code], $options);
  122. $this->currentGroup = $code;
  123. $this->setGroups($groups);
  124. $tabs = $this->getTabs();
  125. }
  126. if ($this->currentGroup && isset($tabs[$this->currentTab]) && !in_array($this->currentGroup, $tabs[$this->currentTab]['groups'])) {
  127. $tabs[$this->currentTab]['groups'][] = $this->currentGroup;
  128. }
  129. $this->setTabs($tabs);
  130. return $this;
  131. }
  132. /**
  133. * Only nested add if the condition match true.
  134. *
  135. * @param bool $bool
  136. *
  137. * @return $this
  138. *
  139. * @throws \RuntimeException
  140. */
  141. public function ifTrue($bool)
  142. {
  143. if ($this->apply !== null) {
  144. throw new \RuntimeException('Cannot nest ifTrue or ifFalse call');
  145. }
  146. $this->apply = ($bool === true);
  147. return $this;
  148. }
  149. /**
  150. * Only nested add if the condition match false.
  151. *
  152. * @param bool $bool
  153. *
  154. * @return $this
  155. *
  156. * @throws \RuntimeException
  157. */
  158. public function ifFalse($bool)
  159. {
  160. if ($this->apply !== null) {
  161. throw new \RuntimeException('Cannot nest ifTrue or ifFalse call');
  162. }
  163. $this->apply = ($bool === false);
  164. return $this;
  165. }
  166. /**
  167. * @return $this
  168. */
  169. public function ifEnd()
  170. {
  171. $this->apply = null;
  172. return $this;
  173. }
  174. /**
  175. * Add new tab.
  176. *
  177. * @param string $name
  178. * @param array $options
  179. *
  180. * @return $this
  181. */
  182. public function tab($name, array $options = array())
  183. {
  184. return $this->with($name, array_merge($options, array('tab' => true)));
  185. }
  186. /**
  187. * Close the current group or tab.
  188. *
  189. * @return $this
  190. *
  191. * @throws \RuntimeException
  192. */
  193. public function end()
  194. {
  195. if ($this->currentGroup !== null) {
  196. $this->currentGroup = null;
  197. } elseif ($this->currentTab !== null) {
  198. $this->currentTab = null;
  199. } else {
  200. throw new \RuntimeException('No open tabs or groups, you cannot use end()');
  201. }
  202. return $this;
  203. }
  204. /**
  205. * Returns a boolean indicating if there is an open tab at the moment.
  206. *
  207. * @return bool
  208. */
  209. public function hasOpenTab()
  210. {
  211. return null !== $this->currentTab;
  212. }
  213. /**
  214. * @return array
  215. */
  216. abstract protected function getGroups();
  217. /**
  218. * @return array
  219. */
  220. abstract protected function getTabs();
  221. /**
  222. * @param array $groups
  223. */
  224. abstract protected function setGroups(array $groups);
  225. /**
  226. * @param array $tabs
  227. */
  228. abstract protected function setTabs(array $tabs);
  229. /**
  230. * NEXT_MAJOR: make this method abstract.
  231. *
  232. * @return string
  233. */
  234. protected function getName()
  235. {
  236. @trigger_error(__METHOD__.' should be implemented and will be abstract in 4.0.', E_USER_DEPRECATED);
  237. return 'default';
  238. }
  239. /**
  240. * Add the field name to the current group.
  241. *
  242. * @param string $fieldName
  243. */
  244. protected function addFieldToCurrentGroup($fieldName)
  245. {
  246. // Note this line must happen before the next line.
  247. // See https://github.com/sonata-project/SonataAdminBundle/pull/1351
  248. $currentGroup = $this->getCurrentGroupName();
  249. $groups = $this->getGroups();
  250. $groups[$currentGroup]['fields'][$fieldName] = $fieldName;
  251. $this->setGroups($groups);
  252. return $groups[$currentGroup];
  253. }
  254. /**
  255. * Return the name of the currently selected group. The method also makes
  256. * sure a valid group name is currently selected.
  257. *
  258. * Note that this can have the side effect to change the 'group' value
  259. * returned by the getGroup function
  260. *
  261. * @return string
  262. */
  263. protected function getCurrentGroupName()
  264. {
  265. if (!$this->currentGroup) {
  266. $this->with($this->admin->getLabel(), array('auto_created' => true));
  267. }
  268. return $this->currentGroup;
  269. }
  270. }