DatagridMapper.php 3.5 KB

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