ListMapper.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. $fieldDescriptionOptions['route']['name'] = 'edit';
  46. }
  47. if (!isset($fieldDescriptionOptions['route']['parameters'])) {
  48. $fieldDescriptionOptions['route']['parameters'] = array();
  49. }
  50. return $this->add($name, $type, $fieldDescriptionOptions);
  51. }
  52. /**
  53. * @throws \RuntimeException
  54. *
  55. * @param mixed $name
  56. * @param mixed $type
  57. * @param array $fieldDescriptionOptions
  58. *
  59. * @return ListMapper
  60. */
  61. public function add($name, $type = null, array $fieldDescriptionOptions = array())
  62. {
  63. // Change deprecated inline action "view" to "show"
  64. if ($name == '_action' && $type == 'actions') {
  65. if (isset($fieldDescriptionOptions['actions']['view'])) {
  66. trigger_error('Inline action "view" is deprecated since version 2.2.4. Use inline action "show" instead.', E_USER_DEPRECATED);
  67. $fieldDescriptionOptions['actions']['show'] = $fieldDescriptionOptions['actions']['view'];
  68. unset($fieldDescriptionOptions['actions']['view']);
  69. }
  70. }
  71. if ($name instanceof FieldDescriptionInterface) {
  72. $fieldDescription = $name;
  73. $fieldDescription->mergeOptions($fieldDescriptionOptions);
  74. } elseif (is_string($name) && !$this->admin->hasListFieldDescription($name)) {
  75. $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
  76. $this->admin->getClass(),
  77. $name,
  78. $fieldDescriptionOptions
  79. );
  80. } else {
  81. 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.');
  82. }
  83. if (!$fieldDescription->getLabel()) {
  84. $fieldDescription->setOption('label', $this->admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName(), 'list', 'label'));
  85. }
  86. // add the field with the FormBuilder
  87. $this->builder->addField($this->list, $type, $fieldDescription, $this->admin);
  88. return $this;
  89. }
  90. /**
  91. * @param string $name
  92. *
  93. * @return FieldDescriptionInterface
  94. */
  95. public function get($name)
  96. {
  97. return $this->list->get($name);
  98. }
  99. /**
  100. * @param string $key
  101. *
  102. * @return bool
  103. */
  104. public function has($key)
  105. {
  106. return $this->list->has($key);
  107. }
  108. /**
  109. * @param string $key
  110. *
  111. * @return ListMapper
  112. */
  113. public function remove($key)
  114. {
  115. $this->admin->removeListFieldDescription($key);
  116. $this->list->remove($key);
  117. return $this;
  118. }
  119. /**
  120. * @param array $keys field names
  121. *
  122. * @return ListMapper
  123. */
  124. public function reorder(array $keys)
  125. {
  126. $this->list->reorder($keys);
  127. return $this;
  128. }
  129. }