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