ShowMapper.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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\Model\ModelManagerInterface;
  15. use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
  16. use Sonata\AdminBundle\Builder\ShowBuilderInterface;
  17. /**
  18. * This class is used to simulate the Form API
  19. *
  20. */
  21. class ShowMapper
  22. {
  23. protected $showBuilder;
  24. protected $list;
  25. protected $admin;
  26. protected $currentGroup;
  27. /**
  28. * @param \Sonata\AdminBundle\Builder\ShowBuilderInterface $showBuilder
  29. * @param \Sonata\AdminBundle\Admin\FieldDescriptionCollection $list
  30. * @param \Sonata\AdminBundle\Admin\AdminInterface $admin
  31. */
  32. public function __construct(ShowBuilderInterface $showBuilder, FieldDescriptionCollection $list, AdminInterface $admin)
  33. {
  34. $this->showBuilder = $showBuilder;
  35. $this->list = $list;
  36. $this->admin = $admin;
  37. }
  38. /**
  39. * @throws \RuntimeException
  40. * @param mixed $name
  41. * @param mixed $type
  42. * @param array $fieldDescriptionOptions
  43. * @return \Sonata\AdminBundle\Show\ShowMapper
  44. */
  45. public function add($name, $type = null, array $fieldDescriptionOptions = array())
  46. {
  47. if (!$this->currentGroup) {
  48. $this->with($this->admin->getLabel());
  49. }
  50. $fieldKey = ($name instanceof FieldDescriptionInterface) ? $name->getName() : $name;
  51. $formGroups = $this->admin->getShowGroups();
  52. $formGroups[$this->currentGroup]['fields'][$fieldKey] = $fieldKey;
  53. $this->admin->setShowGroups($formGroups);
  54. if ($name instanceof FieldDescriptionInterface) {
  55. $fieldDescription = $name;
  56. $fieldDescription->mergeOptions($fieldDescriptionOptions);
  57. } else if (is_string($name) && !$this->admin->hasShowFieldDescription($name)) {
  58. $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
  59. $this->admin->getClass(),
  60. $name,
  61. $fieldDescriptionOptions
  62. );
  63. } else {
  64. throw new \RuntimeException('invalid state');
  65. }
  66. if (!$fieldDescription->getLabel()) {
  67. $fieldDescription->setOption('label', $this->admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName(), 'show', 'label'));
  68. }
  69. // add the field with the FormBuilder
  70. $this->showBuilder->addField($this->list, $type, $fieldDescription, $this->admin);
  71. return $this;
  72. }
  73. /**
  74. * @param string $name
  75. * @return array
  76. */
  77. public function get($name)
  78. {
  79. return $this->list->get($name);
  80. }
  81. /**
  82. * @param string $key
  83. * @return bool
  84. */
  85. public function has($key)
  86. {
  87. return $this->list->has($key);
  88. }
  89. /**
  90. * @param string $key
  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. * @return \Sonata\AdminBundle\Show\ShowMapper
  102. */
  103. public function reorder(array $keys)
  104. {
  105. if (!$this->currentGroup) {
  106. $this->with($this->admin->getLabel());
  107. }
  108. $this->admin->reorderShowGroup($this->currentGroup, $keys);
  109. return $this;
  110. }
  111. /**
  112. * @param string $name
  113. * @param array $options
  114. * @return \Sonata\AdminBundle\Show\ShowMapper
  115. */
  116. public function with($name, array $options = array())
  117. {
  118. $showGroups = $this->admin->getShowGroups();
  119. if (!isset($showGroups[$name])) {
  120. $showGroups[$name] = array_merge(array('collapsed' => false, 'fields' => array()), $options);
  121. }
  122. $this->admin->setShowGroups($showGroups);
  123. $this->currentGroup = $name;
  124. return $this;
  125. }
  126. /**
  127. * @return \Sonata\AdminBundle\Show\ShowMapper
  128. */
  129. public function end()
  130. {
  131. $this->currentGroup = null;
  132. return $this;
  133. }
  134. }