DatagridMapper.php 3.7 KB

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