DatagridMapper.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. } elseif (is_string($name) && $this->admin->hasFilterFieldDescription($name)) {
  64. throw new \RuntimeException(sprintf('The field "%s" is already defined', $name));
  65. } else {
  66. throw new \RuntimeException('invalid state');
  67. }
  68. // add the field with the DatagridBuilder
  69. $this->builder->addFilter($this->datagrid, $type, $fieldDescription, $this->admin);
  70. return $this;
  71. }
  72. /**
  73. * @param string $name
  74. *
  75. * @return \Sonata\AdminBundle\Filter\FilterInterface
  76. */
  77. public function get($name)
  78. {
  79. return $this->datagrid->getFilter($name);
  80. }
  81. /**
  82. * @param string $key
  83. *
  84. * @return boolean
  85. */
  86. public function has($key)
  87. {
  88. return $this->datagrid->hasFilter($key);
  89. }
  90. /**
  91. * @param string $key
  92. *
  93. * @return \Sonata\AdminBundle\Datagrid\DatagridMapper
  94. */
  95. public function remove($key)
  96. {
  97. $this->admin->removeFilterFieldDescription($key);
  98. $this->datagrid->removeFilter($key);
  99. return $this;
  100. }
  101. /**
  102. * @param array $keys field names
  103. *
  104. * @return \Sonata\AdminBundle\Datagrid\ListMapper
  105. */
  106. public function reorder(array $keys)
  107. {
  108. $this->datagrid->reorderFilters($keys);
  109. return $this;
  110. }
  111. }