ShowMapper.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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\Show;
  12. use Sonata\AdminBundle\Admin\AdminInterface;
  13. use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
  14. use Sonata\AdminBundle\Model\ModelManagerInterface;
  15. use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
  16. use Sonata\AdminBundle\Builder\ShowBuilderInterface;
  17. /**
  18. * This class is used to simulate the Form API
  19. *
  20. */
  21. class ShowMapper
  22. {
  23. protected $showBuilder;
  24. protected $list;
  25. protected $admin;
  26. protected $currentGroup;
  27. /**
  28. * @param \Sonata\AdminBundle\Builder\ShowBuilderInterface $showBuilder
  29. * @param \Sonata\AdminBundle\Admin\FieldDescriptionCollection $list
  30. * @param \Sonata\AdminBundle\Admin\AdminInterface $admin
  31. */
  32. public function __construct(ShowBuilderInterface $showBuilder, FieldDescriptionCollection $list, AdminInterface $admin)
  33. {
  34. $this->showBuilder = $showBuilder;
  35. $this->list = $list;
  36. $this->admin = $admin;
  37. }
  38. /**
  39. * @throws \RuntimeException
  40. * @param mixed $name
  41. * @param mixed $type
  42. * @param array $fieldDescriptionOptions
  43. * @return \Sonata\AdminBundle\Show\ShowMapper
  44. */
  45. public function add($name, $type = null, array $fieldDescriptionOptions = array())
  46. {
  47. if (!$this->currentGroup) {
  48. $this->with($this->admin->getLabel());
  49. }
  50. $formGroups = $this->admin->getShowGroups();
  51. $formGroups[$this->currentGroup]['fields'][$name] = $name;
  52. $this->admin->setShowGroups($formGroups);
  53. if ($name instanceof FieldDescriptionInterface) {
  54. $fieldDescription = $name;
  55. $fieldDescription->mergeOptions($fieldDescriptionOptions);
  56. } else if (is_string($name) && !$this->admin->hasShowFieldDescription($name)) {
  57. $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
  58. $this->admin->getClass(),
  59. $name,
  60. $fieldDescriptionOptions
  61. );
  62. } else {
  63. throw new \RuntimeException('invalid state');
  64. }
  65. // add the field with the FormBuilder
  66. $this->showBuilder->addField($this->list, $type, $fieldDescription, $this->admin);
  67. return $this;
  68. }
  69. /**
  70. * @param string $name
  71. * @return array
  72. */
  73. public function get($name)
  74. {
  75. return $this->list->get($name);
  76. }
  77. /**
  78. * @param string $key
  79. * @return bool
  80. */
  81. public function has($key)
  82. {
  83. return $this->list->has($key);
  84. }
  85. /**
  86. * @param string $key
  87. * @return void
  88. */
  89. public function remove($key)
  90. {
  91. $this->admin->removeShowFieldDescription($key);
  92. $this->list->remove($key);
  93. }
  94. /**
  95. * @param string $name
  96. * @param array $options
  97. * @return \Sonata\AdminBundle\Show\ShowMapper
  98. */
  99. public function with($name, array $options = array())
  100. {
  101. $showGroups = $this->admin->getShowGroups();
  102. if (!isset($showGroups[$name])) {
  103. $showGroups[$name] = array_merge(array('collapsed' => false, 'fields' => array()), $options);
  104. }
  105. $this->admin->setFormGroups($showGroups);
  106. $this->currentGroup = $name;
  107. return $this;
  108. }
  109. /**
  110. * @return \Sonata\AdminBundle\Show\ShowMapper
  111. */
  112. public function end()
  113. {
  114. $this->currentGroup = null;
  115. return $this;
  116. }
  117. }