DatagridMapper.php 3.4 KB

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