ListMapper.php 4.8 KB

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