ListMapper.php 4.8 KB

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