BaseGroupedMapper.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 abstract function getGroups();
  20. protected abstract function setGroups(array $groups);
  21. /**
  22. * @param string $name
  23. * @param array $options
  24. *
  25. * @return \Sonata\AdminBundle\Mapper\BaseGroupedMapper
  26. */
  27. public function with($name, array $options = array())
  28. {
  29. $groups = $this->getGroups();
  30. if (!isset($groups[$name])) {
  31. $groups[$name] = array();
  32. }
  33. $groups[$name] = array_merge(array(
  34. 'collapsed' => false,
  35. 'fields' => array(),
  36. 'description' => false
  37. ), $groups[$name], $options);
  38. $this->setGroups($groups);
  39. $this->currentGroup = $name;
  40. return $this;
  41. }
  42. /**
  43. * @return \Sonata\AdminBundle\Mapper\BaseGroupedMapper
  44. */
  45. public function end()
  46. {
  47. $this->currentGroup = null;
  48. return $this;
  49. }
  50. /**
  51. * Add the fieldname to the current group
  52. *
  53. * @param string $fieldName
  54. */
  55. protected function addFieldToCurrentGroup($fieldName)
  56. {
  57. $groups = $this->getGroups();
  58. $groups[$this->getCurrentGroupName()]['fields'][$fieldName] = $fieldName;
  59. $this->setGroups($groups);
  60. }
  61. /**
  62. * Return the name of the currently selected group. The method also makes
  63. * sure a valid group name is currently selected
  64. *
  65. * @return string
  66. */
  67. protected function getCurrentGroupName() {
  68. if (!$this->currentGroup) {
  69. $this->with($this->admin->getLabel());
  70. }
  71. return $this->currentGroup;
  72. }
  73. }