ShowMapper.php 3.7 KB

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