ListMapper.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. if ($name instanceof FieldDescriptionInterface) {
  64. $fieldDescription = $name;
  65. $fieldDescription->mergeOptions($fieldDescriptionOptions);
  66. } elseif (is_string($name) && !$this->admin->hasListFieldDescription($name)) {
  67. $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
  68. $this->admin->getClass(),
  69. $name,
  70. $fieldDescriptionOptions
  71. );
  72. } else {
  73. 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.');
  74. }
  75. if (!$fieldDescription->getLabel()) {
  76. $fieldDescription->setOption('label', $this->admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName(), 'list', 'label'));
  77. }
  78. // add the field with the FormBuilder
  79. $this->builder->addField($this->list, $type, $fieldDescription, $this->admin);
  80. return $this;
  81. }
  82. /**
  83. * @param string $name
  84. *
  85. * @return FieldDescriptionInterface
  86. */
  87. public function get($name)
  88. {
  89. return $this->list->get($name);
  90. }
  91. /**
  92. * @param string $key
  93. *
  94. * @return bool
  95. */
  96. public function has($key)
  97. {
  98. return $this->list->has($key);
  99. }
  100. /**
  101. * @param string $key
  102. *
  103. * @return ListMapper
  104. */
  105. public function remove($key)
  106. {
  107. $this->admin->removeListFieldDescription($key);
  108. $this->list->remove($key);
  109. return $this;
  110. }
  111. /**
  112. * @param array $keys field names
  113. *
  114. * @return ListMapper
  115. */
  116. public function reorder(array $keys)
  117. {
  118. $this->list->reorder($keys);
  119. return $this;
  120. }
  121. }