DatagridMapper.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. *
  42. * @return DatagridMapper
  43. */
  44. public function add($name, $type = null, array $filterOptions = array(), $fieldType = null, $fieldOptions = null)
  45. {
  46. if (is_array($fieldOptions)) {
  47. $filterOptions['field_options'] = $fieldOptions;
  48. }
  49. if ($fieldType) {
  50. $filterOptions['field_type'] = $fieldType;
  51. }
  52. $filterOptions['field_name'] = isset($filterOptions['field_name']) ? $filterOptions['field_name'] : substr(strrchr('.'.$name, '.'), 1);
  53. if ($name instanceof FieldDescriptionInterface) {
  54. $fieldDescription = $name;
  55. $fieldDescription->mergeOptions($filterOptions);
  56. } elseif (is_string($name) && !$this->admin->hasFilterFieldDescription($name)) {
  57. $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
  58. $this->admin->getClass(),
  59. $name,
  60. $filterOptions
  61. );
  62. } elseif (is_string($name) && $this->admin->hasFilterFieldDescription($name)) {
  63. throw new \RuntimeException(sprintf('The field "%s" is already defined', $name));
  64. } else {
  65. throw new \RuntimeException('invalid state');
  66. }
  67. // add the field with the DatagridBuilder
  68. $this->builder->addFilter($this->datagrid, $type, $fieldDescription, $this->admin);
  69. return $this;
  70. }
  71. /**
  72. * @param string $name
  73. *
  74. * @return \Sonata\AdminBundle\Filter\FilterInterface
  75. */
  76. public function get($name)
  77. {
  78. return $this->datagrid->getFilter($name);
  79. }
  80. /**
  81. * @param string $key
  82. *
  83. * @return boolean
  84. */
  85. public function has($key)
  86. {
  87. return $this->datagrid->hasFilter($key);
  88. }
  89. /**
  90. * @param string $key
  91. *
  92. * @return \Sonata\AdminBundle\Datagrid\DatagridMapper
  93. */
  94. public function remove($key)
  95. {
  96. $this->admin->removeFilterFieldDescription($key);
  97. $this->datagrid->removeFilter($key);
  98. return $this;
  99. }
  100. /**
  101. * @param array $keys field names
  102. *
  103. * @return \Sonata\AdminBundle\Datagrid\ListMapper
  104. */
  105. public function reorder(array $keys)
  106. {
  107. $this->datagrid->reorderFilters($keys);
  108. return $this;
  109. }
  110. }