ShowMapper.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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\Show;
  11. use Sonata\AdminBundle\Admin\AdminInterface;
  12. use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
  13. use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
  14. use Sonata\AdminBundle\Builder\ShowBuilderInterface;
  15. use Sonata\AdminBundle\Mapper\BaseGroupedMapper;
  16. /**
  17. * Class ShowMapper
  18. * This class is used to simulate the Form API.
  19. *
  20. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  21. */
  22. class ShowMapper extends BaseGroupedMapper
  23. {
  24. protected $list;
  25. /**
  26. * @param ShowBuilderInterface $showBuilder
  27. * @param FieldDescriptionCollection $list
  28. * @param AdminInterface $admin
  29. */
  30. public function __construct(ShowBuilderInterface $showBuilder, FieldDescriptionCollection $list, AdminInterface $admin)
  31. {
  32. parent::__construct($showBuilder, $admin);
  33. $this->list = $list;
  34. }
  35. /**
  36. * @throws \RuntimeException
  37. *
  38. * @param mixed $name
  39. * @param mixed $type
  40. * @param array $fieldDescriptionOptions
  41. *
  42. * @return $this
  43. */
  44. public function add($name, $type = null, array $fieldDescriptionOptions = array())
  45. {
  46. if ($this->apply !== null && !$this->apply) {
  47. return $this;
  48. }
  49. $fieldKey = ($name instanceof FieldDescriptionInterface) ? $name->getName() : $name;
  50. $this->addFieldToCurrentGroup($fieldKey);
  51. if ($name instanceof FieldDescriptionInterface) {
  52. $fieldDescription = $name;
  53. $fieldDescription->mergeOptions($fieldDescriptionOptions);
  54. } elseif (is_string($name)) {
  55. if (!$this->admin->hasShowFieldDescription($name)) {
  56. $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
  57. $this->admin->getClass(),
  58. $name,
  59. $fieldDescriptionOptions
  60. );
  61. } else {
  62. throw new \RuntimeException(sprintf('Duplicate field name "%s" in show mapper. Names should be unique.', $name));
  63. }
  64. } else {
  65. throw new \RuntimeException('invalid state');
  66. }
  67. if (!$fieldDescription->getLabel()) {
  68. $fieldDescription->setOption('label', $this->admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName(), 'show', 'label'));
  69. }
  70. $fieldDescription->setOption('safe', $fieldDescription->getOption('safe', false));
  71. // add the field with the FormBuilder
  72. $this->builder->addField($this->list, $type, $fieldDescription, $this->admin);
  73. return $this;
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function get($name)
  79. {
  80. return $this->list->get($name);
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function has($key)
  86. {
  87. return $this->list->has($key);
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function remove($key)
  93. {
  94. $this->admin->removeShowFieldDescription($key);
  95. $this->list->remove($key);
  96. return $this;
  97. }
  98. /**
  99. * {@inheritdoc}
  100. */
  101. final public function keys()
  102. {
  103. return array_keys($this->list->getElements());
  104. }
  105. /**
  106. * {@inheritdoc}
  107. */
  108. public function reorder(array $keys)
  109. {
  110. $this->admin->reorderShowGroup($this->getCurrentGroupName(), $keys);
  111. return $this;
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. protected function getGroups()
  117. {
  118. return $this->admin->getShowGroups();
  119. }
  120. /**
  121. * {@inheritdoc}
  122. */
  123. protected function setGroups(array $groups)
  124. {
  125. $this->admin->setShowGroups($groups);
  126. }
  127. /**
  128. * {@inheritdoc}
  129. */
  130. protected function getTabs()
  131. {
  132. return $this->admin->getShowTabs();
  133. }
  134. /**
  135. * {@inheritdoc}
  136. */
  137. protected function setTabs(array $tabs)
  138. {
  139. $this->admin->setShowTabs($tabs);
  140. }
  141. }