ListMapper.php 4.5 KB

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