BaseGroupedMapper.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. * @param string $name
  26. * @param array $options
  27. *
  28. * @return \Sonata\AdminBundle\Mapper\BaseGroupedMapper
  29. */
  30. public function with($name, array $options = array())
  31. {
  32. if(array_key_exists("tab",$options) && $options["tab"]) {
  33. $tabs = $this->getTabs();
  34. if($this->currentTab) {
  35. if($tabs[$name]["auto_created"]) {
  36. throw new \Exception("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");
  37. } else {
  38. throw new \Exception("You should close previous tab with end() before adding new tab");
  39. }
  40. }elseif($this->currentGroup) {
  41. throw new \Exception("You should open tab before adding new groups");
  42. }
  43. if (!isset($tabs[$name])) {
  44. $groups[$name] = array();
  45. }
  46. $tabs["name"] = array_merge(array(
  47. 'auto_created' => false,
  48. 'collapsed' => false,
  49. 'class' => false,
  50. 'groups' => array(),
  51. 'description' => false,
  52. 'translation_domain' => null,
  53. ), $tabs[$name], $options);
  54. $this->currentTab = $name;
  55. } else {
  56. if($this->currentGroup) {
  57. throw new \Exception("You should close previous group with end() before adding new tab");
  58. }
  59. if(!$this->currentTab) {
  60. $this->with($this->admin->getLabel(),array("tab"=>true,"auto_created"=>true)); // add new tab automatically
  61. }
  62. $groups = $this->getGroups();
  63. if (!isset($groups[$name])) {
  64. $groups[$name] = array();
  65. }
  66. $groups[$name] = array_merge(array('collapsed' => false,
  67. 'class' => false,
  68. 'fields' => array(),
  69. 'description' => false,
  70. 'translation_domain' => null,
  71. ),$groups[$name],$options);
  72. $this->currentGroup = $name;
  73. $this->setGroups($groups);
  74. }
  75. $tabs = $this->getTabs();
  76. if($this->currentGroup) {
  77. $tabs[$this->currentTab]["groups"][] = $this->currentGroup;
  78. }
  79. $this->setTabs($tabs);
  80. return $this;
  81. }
  82. /**
  83. * @return \Sonata\AdminBundle\Mapper\BaseGroupedMapper
  84. */
  85. public function end()
  86. {
  87. if($this->currentGroup !== null) {
  88. $this->currentGroup = null;
  89. } elseif($this->currentTab !== null) {
  90. $this->currentTab = null;
  91. } else {
  92. throw new \Exception("No open tabs or groups, you can use end()");
  93. }
  94. return $this;
  95. }
  96. /**
  97. * Add the fieldname to the current group
  98. *
  99. * @param string $fieldName
  100. */
  101. protected function addFieldToCurrentGroup($fieldName)
  102. {
  103. // Note this line must happen before the next line.
  104. // See https://github.com/sonata-project/SonataAdminBundle/pull/1351
  105. $currentGroup = $this->getCurrentGroupName();
  106. $groups = $this->getGroups();
  107. $groups[$currentGroup]['fields'][$fieldName] = $fieldName;
  108. $this->setGroups($groups);
  109. return $groups[$currentGroup];
  110. }
  111. /**
  112. * Return the name of the currently selected group. The method also makes
  113. * sure a valid group name is currently selected
  114. *
  115. * Note that this can have the side effect to change the "group" value
  116. * returned by the getGroup function
  117. *
  118. * @return string
  119. */
  120. protected function getCurrentGroupName()
  121. {
  122. if (!$this->currentGroup) {
  123. $this->with($this->admin->getLabel(),array('auto_created'=>true));
  124. }
  125. return $this->currentGroup;
  126. }
  127. }