DatagridMapper.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project 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. namespace Sonata\AdminBundle\Datagrid;
  11. use Sonata\AdminBundle\Admin\AdminInterface;
  12. use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
  13. use Sonata\AdminBundle\Builder\DatagridBuilderInterface;
  14. use Sonata\AdminBundle\Mapper\BaseMapper;
  15. /**
  16. * Class DatagridMapper
  17. * This class is use to simulate the Form API.
  18. *
  19. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  20. */
  21. class DatagridMapper extends BaseMapper
  22. {
  23. protected $datagrid;
  24. /**
  25. * @param \Sonata\AdminBundle\Builder\DatagridBuilderInterface $datagridBuilder
  26. * @param DatagridInterface $datagrid
  27. * @param \Sonata\AdminBundle\Admin\AdminInterface $admin
  28. */
  29. public function __construct(DatagridBuilderInterface $datagridBuilder, DatagridInterface $datagrid, AdminInterface $admin)
  30. {
  31. parent::__construct($datagridBuilder, $admin);
  32. $this->datagrid = $datagrid;
  33. }
  34. /**
  35. * @throws \RuntimeException
  36. *
  37. * @param string $name
  38. * @param string $type
  39. * @param array $filterOptions
  40. * @param string $fieldType
  41. * @param array $fieldOptions
  42. * @param array $fieldDescriptionOptions
  43. *
  44. * @return DatagridMapper
  45. */
  46. public function add($name, $type = null, array $filterOptions = array(), $fieldType = null, $fieldOptions = null, array $fieldDescriptionOptions = array())
  47. {
  48. if (is_array($fieldOptions)) {
  49. $filterOptions['field_options'] = $fieldOptions;
  50. }
  51. if ($fieldType) {
  52. $filterOptions['field_type'] = $fieldType;
  53. }
  54. if ($name instanceof FieldDescriptionInterface) {
  55. $fieldDescription = $name;
  56. $fieldDescription->mergeOptions($filterOptions);
  57. } elseif (is_string($name)) {
  58. if ($this->admin->hasFilterFieldDescription($name)) {
  59. throw new \RuntimeException(sprintf('Duplicate field name "%s" in datagrid mapper. Names should be unique.', $name));
  60. }
  61. if (!isset($filterOptions['field_name'])) {
  62. $filterOptions['field_name'] = substr(strrchr('.'.$name, '.'), 1);
  63. }
  64. $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
  65. $this->admin->getClass(),
  66. $name,
  67. array_merge($filterOptions, $fieldDescriptionOptions)
  68. );
  69. } else {
  70. throw new \RuntimeException('Unknown field name in datagrid mapper. Field name should be either of FieldDescriptionInterface interface or string.');
  71. }
  72. // add the field with the DatagridBuilder
  73. $this->builder->addFilter($this->datagrid, $type, $fieldDescription, $this->admin);
  74. return $this;
  75. }
  76. /**
  77. * @param string $name
  78. *
  79. * @return \Sonata\AdminBundle\Filter\FilterInterface
  80. */
  81. public function get($name)
  82. {
  83. return $this->datagrid->getFilter($name);
  84. }
  85. /**
  86. * @param string $key
  87. *
  88. * @return bool
  89. */
  90. public function has($key)
  91. {
  92. return $this->datagrid->hasFilter($key);
  93. }
  94. /**
  95. * @param string $key
  96. *
  97. * @return \Sonata\AdminBundle\Datagrid\DatagridMapper
  98. */
  99. public function remove($key)
  100. {
  101. $this->admin->removeFilterFieldDescription($key);
  102. $this->datagrid->removeFilter($key);
  103. return $this;
  104. }
  105. /**
  106. * @param array $keys field names
  107. *
  108. * @return \Sonata\AdminBundle\Datagrid\ListMapper
  109. */
  110. public function reorder(array $keys)
  111. {
  112. $this->datagrid->reorderFilters($keys);
  113. return $this;
  114. }
  115. }