ShowMapper.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 \Sonata\AdminBundle\Builder\ShowBuilderInterface $showBuilder
  26. * @param \Sonata\AdminBundle\Admin\FieldDescriptionCollection $list
  27. * @param \Sonata\AdminBundle\Admin\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 \Sonata\AdminBundle\Show\ShowMapper
  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. * @param string $name
  72. *
  73. * @return array
  74. */
  75. public function get($name)
  76. {
  77. return $this->list->get($name);
  78. }
  79. /**
  80. * @param string $key
  81. *
  82. * @return bool
  83. */
  84. public function has($key)
  85. {
  86. return $this->list->has($key);
  87. }
  88. /**
  89. * @param string $key
  90. *
  91. * @return \Sonata\AdminBundle\Show\ShowMapper
  92. */
  93. public function remove($key)
  94. {
  95. $this->admin->removeShowFieldDescription($key);
  96. $this->list->remove($key);
  97. return $this;
  98. }
  99. /**
  100. * @param array $keys field names
  101. *
  102. * @return \Sonata\AdminBundle\Show\ShowMapper
  103. */
  104. public function reorder(array $keys)
  105. {
  106. $this->admin->reorderShowGroup($this->getCurrentGroupName(), $keys);
  107. return $this;
  108. }
  109. /**
  110. * {@inheritdoc}
  111. */
  112. protected function getGroups()
  113. {
  114. return $this->admin->getShowGroups();
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. protected function setGroups(array $groups)
  120. {
  121. $this->admin->setShowGroups($groups);
  122. }
  123. /**
  124. * {@inheritdoc}
  125. */
  126. protected function getTabs()
  127. {
  128. return $this->admin->getShowTabs();
  129. }
  130. /**
  131. * {@inheritdoc}
  132. */
  133. protected function setTabs(array $tabs)
  134. {
  135. $this->admin->setShowTabs($tabs);
  136. }
  137. }