ShowMapper.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  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)) {
  54. if (!$this->admin->hasShowFieldDescription($name)) {
  55. $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
  56. $this->admin->getClass(),
  57. $name,
  58. $fieldDescriptionOptions
  59. );
  60. } else {
  61. throw new \RuntimeException(sprintf('Duplicate field name "%s" in show mapper. Names should be unique.', $name));
  62. }
  63. } else {
  64. throw new \RuntimeException('invalid state');
  65. }
  66. if (!$fieldDescription->getLabel() && false !== $fieldDescription->getOption('label')) {
  67. $fieldDescription->setOption('label', $this->admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName(), 'show', 'label'));
  68. }
  69. $fieldDescription->setOption('safe', $fieldDescription->getOption('safe', false));
  70. // add the field with the FormBuilder
  71. $this->builder->addField($this->list, $type, $fieldDescription, $this->admin);
  72. return $this;
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function get($name)
  78. {
  79. return $this->list->get($name);
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function has($key)
  85. {
  86. return $this->list->has($key);
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function remove($key)
  92. {
  93. $this->admin->removeShowFieldDescription($key);
  94. $this->list->remove($key);
  95. return $this;
  96. }
  97. /**
  98. * Removes a group.
  99. *
  100. * @param string $group The group to delete
  101. * @param string $tab The tab the group belongs to, defaults to 'default'
  102. * @param bool $deleteEmptyTab Whether or not the parent Tab should be deleted too,
  103. * when the deleted group leaves the tab empty after deletion
  104. *
  105. * @return $this
  106. */
  107. public function removeGroup($group, $tab = 'default', $deleteEmptyTab = false)
  108. {
  109. $groups = $this->getGroups();
  110. // When the default tab is used, the tabname is not prepended to the index in the group array
  111. if ($tab !== 'default') {
  112. $group = $tab.'.'.$group;
  113. }
  114. if (isset($groups[$group])) {
  115. foreach ($groups[$group]['fields'] as $field) {
  116. $this->remove($field);
  117. }
  118. }
  119. unset($groups[$group]);
  120. $tabs = $this->getTabs();
  121. $key = array_search($group, $tabs[$tab]['groups']);
  122. if (false !== $key) {
  123. unset($tabs[$tab]['groups'][$key]);
  124. }
  125. if ($deleteEmptyTab && count($tabs[$tab]['groups']) == 0) {
  126. unset($tabs[$tab]);
  127. }
  128. $this->setTabs($tabs);
  129. $this->setGroups($groups);
  130. return $this;
  131. }
  132. /**
  133. * {@inheritdoc}
  134. */
  135. final public function keys()
  136. {
  137. return array_keys($this->list->getElements());
  138. }
  139. /**
  140. * {@inheritdoc}
  141. */
  142. public function reorder(array $keys)
  143. {
  144. $this->admin->reorderShowGroup($this->getCurrentGroupName(), $keys);
  145. return $this;
  146. }
  147. /**
  148. * {@inheritdoc}
  149. */
  150. protected function getGroups()
  151. {
  152. return $this->admin->getShowGroups();
  153. }
  154. /**
  155. * {@inheritdoc}
  156. */
  157. protected function setGroups(array $groups)
  158. {
  159. $this->admin->setShowGroups($groups);
  160. }
  161. /**
  162. * {@inheritdoc}
  163. */
  164. protected function getTabs()
  165. {
  166. return $this->admin->getShowTabs();
  167. }
  168. /**
  169. * {@inheritdoc}
  170. */
  171. protected function setTabs(array $tabs)
  172. {
  173. $this->admin->setShowTabs($tabs);
  174. }
  175. /**
  176. * {@inheritdoc}
  177. */
  178. protected function getName()
  179. {
  180. return 'show';
  181. }
  182. }