ListMapper.php 4.5 KB

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