ListMapper.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. public function __construct(ListBuilderInterface $listBuilder, FieldDescriptionCollection $list, AdminInterface $admin)
  27. {
  28. $this->listBuilder = $listBuilder;
  29. $this->list = $list;
  30. $this->admin = $admin;
  31. }
  32. public function addIdentifier($name, $type = null, array $fieldDescriptionOptions = array())
  33. {
  34. $fieldDescriptionOptions['identifier'] = true;
  35. if (!isset($fieldDescriptionOptions['route']['name'])) {
  36. $fieldDescriptionOptions['route']['name'] = 'edit';
  37. }
  38. if (!isset($fieldDescriptionOptions['route']['parameters'])) {
  39. $fieldDescriptionOptions['route']['parameters'] = array();
  40. }
  41. return $this->add($name, $type, $fieldDescriptionOptions);
  42. }
  43. /**
  44. * @throws \RuntimeException
  45. *
  46. * @param mixed $name
  47. * @param mixed $type
  48. * @param array $fieldDescriptionOptions
  49. * @return \Sonata\AdminBundle\Datagrid\ListMapper
  50. */
  51. public function add($name, $type = null, array $fieldDescriptionOptions = array())
  52. {
  53. if ($name instanceof FieldDescriptionInterface) {
  54. $fieldDescription = $name;
  55. $fieldDescription->mergeOptions($fieldDescriptionOptions);
  56. } else if (is_string($name) && !$this->admin->hasListFieldDescription($name)) {
  57. $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
  58. $this->admin->getClass(),
  59. $name,
  60. $fieldDescriptionOptions
  61. );
  62. } else {
  63. 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.');
  64. }
  65. if (!$fieldDescription->getLabel()) {
  66. $fieldDescription->setOption('label', $this->admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName(), 'list', 'label'));
  67. }
  68. // add the field with the FormBuilder
  69. $this->listBuilder->addField($this->list, $type, $fieldDescription, $this->admin);
  70. return $this;
  71. }
  72. /**
  73. * @param string $name
  74. * @return array
  75. */
  76. public function get($name)
  77. {
  78. return $this->list->get($name);
  79. }
  80. /**
  81. * @param string $key
  82. * @return bool
  83. */
  84. public function has($key)
  85. {
  86. return $this->list->has($key);
  87. }
  88. /**
  89. * @param string $key
  90. * @return \Sonata\AdminBundle\Datagrid\ListMapper
  91. */
  92. public function remove($key)
  93. {
  94. $this->admin->removeListFieldDescription($key);
  95. $this->list->remove($key);
  96. return $this;
  97. }
  98. /**
  99. * @param array $keys field names
  100. * @return \Sonata\AdminBundle\Datagrid\ListMapper
  101. */
  102. public function reorder(array $keys)
  103. {
  104. $this->list->reorder($keys);
  105. return $this;
  106. }
  107. }