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\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. $fieldKey = ($name instanceof FieldDescriptionInterface) ? $name->getName() : $name;
  46. $this->addFieldToCurrentGroup($fieldKey);
  47. if ($name instanceof FieldDescriptionInterface) {
  48. $fieldDescription = $name;
  49. $fieldDescription->mergeOptions($fieldDescriptionOptions);
  50. } elseif (is_string($name) && !$this->admin->hasShowFieldDescription($name)) {
  51. $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
  52. $this->admin->getClass(),
  53. $name,
  54. $fieldDescriptionOptions
  55. );
  56. } else {
  57. throw new \RuntimeException('invalid state');
  58. }
  59. if (!$fieldDescription->getLabel()) {
  60. $fieldDescription->setOption('label', $this->admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName(), 'show', 'label'));
  61. }
  62. $fieldDescription->setOption('safe', $fieldDescription->getOption('safe', false));
  63. // add the field with the FormBuilder
  64. $this->builder->addField($this->list, $type, $fieldDescription, $this->admin);
  65. return $this;
  66. }
  67. /**
  68. * @param string $name
  69. *
  70. * @return array
  71. */
  72. public function get($name)
  73. {
  74. return $this->list->get($name);
  75. }
  76. /**
  77. * @param string $key
  78. *
  79. * @return bool
  80. */
  81. public function has($key)
  82. {
  83. return $this->list->has($key);
  84. }
  85. /**
  86. * @param string $key
  87. *
  88. * @return \Sonata\AdminBundle\Show\ShowMapper
  89. */
  90. public function remove($key)
  91. {
  92. $this->admin->removeShowFieldDescription($key);
  93. $this->list->remove($key);
  94. return $this;
  95. }
  96. /**
  97. * @param array $keys field names
  98. *
  99. * @return \Sonata\AdminBundle\Show\ShowMapper
  100. */
  101. public function reorder(array $keys)
  102. {
  103. $this->admin->reorderShowGroup($this->getCurrentGroupName(), $keys);
  104. return $this;
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. protected function getGroups()
  110. {
  111. return $this->admin->getShowGroups();
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. protected function setGroups(array $groups)
  117. {
  118. $this->admin->setShowGroups($groups);
  119. }
  120. }