ShowMapper.php 3.6 KB

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