ListMapper.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. * This class is used to simulate the Form API.
  18. *
  19. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  20. */
  21. class ListMapper extends BaseMapper
  22. {
  23. /**
  24. * @var FieldDescriptionCollection
  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->hasAccess('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(
  71. 'Inline action "view" is deprecated since version 2.2.4 and will be removed in 4.0. '
  72. .'Use inline action "show" instead.',
  73. E_USER_DEPRECATED
  74. );
  75. $fieldDescriptionOptions['actions']['show'] = $fieldDescriptionOptions['actions']['view'];
  76. unset($fieldDescriptionOptions['actions']['view']);
  77. }
  78. }
  79. // Ensure batch and action pseudo-fields are tagged as virtual
  80. if (in_array($type, array('actions', 'batch', 'select'))) {
  81. $fieldDescriptionOptions['virtual_field'] = true;
  82. }
  83. if ($name instanceof FieldDescriptionInterface) {
  84. $fieldDescription = $name;
  85. $fieldDescription->mergeOptions($fieldDescriptionOptions);
  86. } elseif (is_string($name)) {
  87. if ($this->admin->hasListFieldDescription($name)) {
  88. throw new \RuntimeException(sprintf(
  89. 'Duplicate field name "%s" in list mapper. Names should be unique.',
  90. $name
  91. ));
  92. }
  93. $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
  94. $this->admin->getClass(),
  95. $name,
  96. $fieldDescriptionOptions
  97. );
  98. } else {
  99. throw new \RuntimeException(
  100. 'Unknown field name in list mapper. '
  101. .'Field name should be either of FieldDescriptionInterface interface or string.'
  102. );
  103. }
  104. if ($fieldDescription->getLabel() === null) {
  105. $fieldDescription->setOption(
  106. 'label',
  107. $this->admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName(), 'list', 'label')
  108. );
  109. }
  110. // add the field with the FormBuilder
  111. $this->builder->addField($this->list, $type, $fieldDescription, $this->admin);
  112. return $this;
  113. }
  114. /**
  115. * {@inheritdoc}
  116. */
  117. public function get($name)
  118. {
  119. return $this->list->get($name);
  120. }
  121. /**
  122. * {@inheritdoc}
  123. */
  124. public function has($key)
  125. {
  126. return $this->list->has($key);
  127. }
  128. /**
  129. * {@inheritdoc}
  130. */
  131. public function remove($key)
  132. {
  133. $this->admin->removeListFieldDescription($key);
  134. $this->list->remove($key);
  135. return $this;
  136. }
  137. /**
  138. * {@inheritdoc}
  139. */
  140. final public function keys()
  141. {
  142. return array_keys($this->list->getElements());
  143. }
  144. /**
  145. * {@inheritdoc}
  146. */
  147. public function reorder(array $keys)
  148. {
  149. $this->list->reorder($keys);
  150. return $this;
  151. }
  152. }