ListMapper.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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) && !$this->admin->hasListFieldDescription($name)) {
  76. $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
  77. $this->admin->getClass(),
  78. $name,
  79. $fieldDescriptionOptions
  80. );
  81. } else {
  82. throw new \RuntimeException('Unknown or duplicate field name in list mapper. Field name should be either of FieldDescriptionInterface interface or string. Names should be unique.');
  83. }
  84. if (!$fieldDescription->getLabel()) {
  85. $fieldDescription->setOption('label', $this->admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName(), 'list', 'label'));
  86. }
  87. // add the field with the FormBuilder
  88. $this->builder->addField($this->list, $type, $fieldDescription, $this->admin);
  89. return $this;
  90. }
  91. /**
  92. * @param string $name
  93. *
  94. * @return FieldDescriptionInterface
  95. */
  96. public function get($name)
  97. {
  98. return $this->list->get($name);
  99. }
  100. /**
  101. * @param string $key
  102. *
  103. * @return bool
  104. */
  105. public function has($key)
  106. {
  107. return $this->list->has($key);
  108. }
  109. /**
  110. * @param string $key
  111. *
  112. * @return ListMapper
  113. */
  114. public function remove($key)
  115. {
  116. $this->admin->removeListFieldDescription($key);
  117. $this->list->remove($key);
  118. return $this;
  119. }
  120. /**
  121. * @param array $keys field names
  122. *
  123. * @return ListMapper
  124. */
  125. public function reorder(array $keys)
  126. {
  127. $this->list->reorder($keys);
  128. return $this;
  129. }
  130. }