ListMapper.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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\Model\ModelManagerInterface;
  15. use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
  16. use Sonata\AdminBundle\Builder\ListBuilderInterface;
  17. /**
  18. * This class is used to simulate the Form API
  19. *
  20. */
  21. class ListMapper
  22. {
  23. protected $listBuilder;
  24. protected $list;
  25. protected $admin;
  26. /**
  27. * @param \Sonata\AdminBundle\Builder\ListBuilderInterface $listBuilder
  28. * @param \Sonata\AdminBundle\Admin\FieldDescriptionCollection $list
  29. * @param \Sonata\AdminBundle\Admin\AdminInterface $admin
  30. */
  31. public function __construct(ListBuilderInterface $listBuilder, FieldDescriptionCollection $list, AdminInterface $admin)
  32. {
  33. $this->listBuilder = $listBuilder;
  34. $this->list = $list;
  35. $this->admin = $admin;
  36. }
  37. /**
  38. * @param string $name
  39. * @param null $type
  40. * @param array $fieldDescriptionOptions
  41. *
  42. * @return ListMapper
  43. */
  44. public function addIdentifier($name, $type = null, array $fieldDescriptionOptions = array())
  45. {
  46. $fieldDescriptionOptions['identifier'] = true;
  47. if (!isset($fieldDescriptionOptions['route']['name'])) {
  48. $fieldDescriptionOptions['route']['name'] = 'edit';
  49. }
  50. if (!isset($fieldDescriptionOptions['route']['parameters'])) {
  51. $fieldDescriptionOptions['route']['parameters'] = array();
  52. }
  53. return $this->add($name, $type, $fieldDescriptionOptions);
  54. }
  55. /**
  56. * @throws \RuntimeException
  57. *
  58. * @param mixed $name
  59. * @param mixed $type
  60. * @param array $fieldDescriptionOptions
  61. *
  62. * @return \Sonata\AdminBundle\Datagrid\ListMapper
  63. */
  64. public function add($name, $type = null, array $fieldDescriptionOptions = array())
  65. {
  66. if ($name instanceof FieldDescriptionInterface) {
  67. $fieldDescription = $name;
  68. $fieldDescription->mergeOptions($fieldDescriptionOptions);
  69. } else if (is_string($name) && !$this->admin->hasListFieldDescription($name)) {
  70. $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
  71. $this->admin->getClass(),
  72. $name,
  73. $fieldDescriptionOptions
  74. );
  75. } else {
  76. 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.');
  77. }
  78. if (!$fieldDescription->getLabel()) {
  79. $fieldDescription->setOption('label', $this->admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName(), 'list', 'label'));
  80. }
  81. // add the field with the FormBuilder
  82. $this->listBuilder->addField($this->list, $type, $fieldDescription, $this->admin);
  83. return $this;
  84. }
  85. /**
  86. * @param string $name
  87. *
  88. * @return \Sonata\AdminBundle\Admin\FieldDescriptionInterface
  89. */
  90. public function get($name)
  91. {
  92. return $this->list->get($name);
  93. }
  94. /**
  95. * @param string $key
  96. *
  97. * @return bool
  98. */
  99. public function has($key)
  100. {
  101. return $this->list->has($key);
  102. }
  103. /**
  104. * @param string $key
  105. *
  106. * @return \Sonata\AdminBundle\Datagrid\ListMapper
  107. */
  108. public function remove($key)
  109. {
  110. $this->admin->removeListFieldDescription($key);
  111. $this->list->remove($key);
  112. return $this;
  113. }
  114. /**
  115. * @param array $keys field names
  116. *
  117. * @return \Sonata\AdminBundle\Datagrid\ListMapper
  118. */
  119. public function reorder(array $keys)
  120. {
  121. $this->list->reorder($keys);
  122. return $this;
  123. }
  124. }