ListMapper.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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\Datagrid;
  11. use Sonata\AdminBundle\Admin\AdminInterface;
  12. use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
  13. use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
  14. use Sonata\AdminBundle\Builder\ListBuilderInterface;
  15. use Sonata\AdminBundle\Mapper\BaseMapper;
  16. /**
  17. * Class ListMapper
  18. * This class is used to simulate the Form API.
  19. *
  20. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  21. */
  22. class ListMapper extends BaseMapper
  23. {
  24. /**
  25. * @var FieldDescriptionCollection
  26. */
  27. protected $list;
  28. /**
  29. * @param ListBuilderInterface $listBuilder
  30. * @param FieldDescriptionCollection $list
  31. * @param AdminInterface $admin
  32. */
  33. public function __construct(ListBuilderInterface $listBuilder, FieldDescriptionCollection $list, AdminInterface $admin)
  34. {
  35. parent::__construct($listBuilder, $admin);
  36. $this->list = $list;
  37. }
  38. /**
  39. * @param string $name
  40. * @param null $type
  41. * @param array $fieldDescriptionOptions
  42. *
  43. * @return ListMapper
  44. */
  45. public function addIdentifier($name, $type = null, array $fieldDescriptionOptions = array())
  46. {
  47. $fieldDescriptionOptions['identifier'] = true;
  48. if (!isset($fieldDescriptionOptions['route']['name'])) {
  49. $routeName = ($this->admin->isGranted('EDIT') && $this->admin->hasRoute('edit')) ? 'edit' : 'show';
  50. $fieldDescriptionOptions['route']['name'] = $routeName;
  51. }
  52. if (!isset($fieldDescriptionOptions['route']['parameters'])) {
  53. $fieldDescriptionOptions['route']['parameters'] = array();
  54. }
  55. return $this->add($name, $type, $fieldDescriptionOptions);
  56. }
  57. /**
  58. * @throws \RuntimeException
  59. *
  60. * @param mixed $name
  61. * @param mixed $type
  62. * @param array $fieldDescriptionOptions
  63. *
  64. * @return ListMapper
  65. */
  66. public function add($name, $type = null, array $fieldDescriptionOptions = array())
  67. {
  68. // Change deprecated inline action "view" to "show"
  69. if ($name == '_action' && $type == 'actions') {
  70. if (isset($fieldDescriptionOptions['actions']['view'])) {
  71. @trigger_error('Inline action "view" is deprecated since version 2.2.4 and will be removed in 4.0. Use inline action "show" instead.', E_USER_DEPRECATED);
  72. $fieldDescriptionOptions['actions']['show'] = $fieldDescriptionOptions['actions']['view'];
  73. unset($fieldDescriptionOptions['actions']['view']);
  74. }
  75. }
  76. // Ensure batch and action pseudo-fields are tagged as virtual
  77. if (in_array($type, array('actions', 'batch', 'select'))) {
  78. $fieldDescriptionOptions['virtual_field'] = true;
  79. }
  80. if ($name instanceof FieldDescriptionInterface) {
  81. $fieldDescription = $name;
  82. $fieldDescription->mergeOptions($fieldDescriptionOptions);
  83. } elseif (is_string($name)) {
  84. if ($this->admin->hasListFieldDescription($name)) {
  85. throw new \RuntimeException(sprintf('Duplicate field name "%s" in list mapper. Names should be unique.', $name));
  86. }
  87. $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
  88. $this->admin->getClass(),
  89. $name,
  90. $fieldDescriptionOptions
  91. );
  92. } else {
  93. throw new \RuntimeException('Unknown field name in list mapper. Field name should be either of FieldDescriptionInterface interface or string.');
  94. }
  95. if (!$fieldDescription->getLabel()) {
  96. $fieldDescription->setOption('label', $this->admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName(), 'list', 'label'));
  97. }
  98. // add the field with the FormBuilder
  99. $this->builder->addField($this->list, $type, $fieldDescription, $this->admin);
  100. return $this;
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function get($name)
  106. {
  107. return $this->list->get($name);
  108. }
  109. /**
  110. * {@inheritdoc}
  111. */
  112. public function has($key)
  113. {
  114. return $this->list->has($key);
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. public function remove($key)
  120. {
  121. $this->admin->removeListFieldDescription($key);
  122. $this->list->remove($key);
  123. return $this;
  124. }
  125. /**
  126. * {@inheritdoc}
  127. */
  128. final public function keys()
  129. {
  130. return array_keys($this->list->getElements());
  131. }
  132. /**
  133. * {@inheritdoc}
  134. */
  135. public function reorder(array $keys)
  136. {
  137. $this->list->reorder($keys);
  138. return $this;
  139. }
  140. }