DatagridMapper.php 3.5 KB

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