ModelFilter.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. namespace Sonata\DoctrineORMAdminBundle\Filter;
  11. use Doctrine\Common\Collections\Collection;
  12. use Doctrine\ORM\Mapping\ClassMetadataInfo;
  13. use Sonata\AdminBundle\Form\Type\EqualType;
  14. use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
  15. class ModelFilter extends Filter
  16. {
  17. /**
  18. * {@inheritdoc}
  19. */
  20. public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
  21. {
  22. if (!$data || !is_array($data) || !array_key_exists('value', $data)) {
  23. return;
  24. }
  25. if ($data['value'] instanceof Collection) {
  26. $data['value'] = $data['value']->toArray();
  27. }
  28. if (is_array($data['value'])) {
  29. $this->handleMultiple($queryBuilder, $alias, $data);
  30. } else {
  31. $this->handleModel($queryBuilder, $alias, $data);
  32. }
  33. }
  34. /**
  35. * For the record, the $alias value is provided by the association method (and the entity join method)
  36. * so the field value is not used here
  37. *
  38. * @param \Sonata\AdminBundle\Datagrid\ProxyQueryInterface $queryBuilder
  39. * @param string $alias
  40. * @param mixed $data
  41. *
  42. * @return mixed
  43. */
  44. protected function handleMultiple(ProxyQueryInterface $queryBuilder, $alias, $data)
  45. {
  46. if (count($data['value']) == 0) {
  47. return;
  48. }
  49. $parameterName = $this->getNewParameterName($queryBuilder);
  50. if (isset($data['type']) && $data['type'] == EqualType::TYPE_IS_NOT_EQUAL) {
  51. $this->applyWhere($queryBuilder, $queryBuilder->expr()->notIn($alias, ':'.$parameterName));
  52. } else {
  53. $this->applyWhere($queryBuilder, $queryBuilder->expr()->in($alias, ':'.$parameterName));
  54. }
  55. $queryBuilder->setParameter($parameterName, $data['value']);
  56. }
  57. /**
  58. * @param \Sonata\AdminBundle\Datagrid\ProxyQueryInterface $queryBuilder
  59. * @param string $alias
  60. * @param mixed $data
  61. *
  62. * @return mixed
  63. */
  64. protected function handleModel(ProxyQueryInterface $queryBuilder, $alias, $data)
  65. {
  66. if (empty($data['value'])) {
  67. return;
  68. }
  69. $parameterName = $this->getNewParameterName($queryBuilder);
  70. if (isset($data['type']) && $data['type'] == EqualType::TYPE_IS_NOT_EQUAL) {
  71. $this->applyWhere($queryBuilder, sprintf('%s != :%s', $alias, $parameterName));
  72. } else {
  73. $this->applyWhere($queryBuilder, sprintf('%s = :%s', $alias, $parameterName));
  74. }
  75. $queryBuilder->setParameter($parameterName, $data['value']);
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. protected function association(ProxyQueryInterface $queryBuilder, $data)
  81. {
  82. $types = array(
  83. ClassMetadataInfo::ONE_TO_ONE,
  84. ClassMetadataInfo::ONE_TO_MANY,
  85. ClassMetadataInfo::MANY_TO_MANY,
  86. ClassMetadataInfo::MANY_TO_ONE,
  87. );
  88. if (!in_array($this->getOption('mapping_type'), $types)) {
  89. throw new \RunTimeException('Invalid mapping type');
  90. }
  91. $associationMappings = $this->getParentAssociationMappings();
  92. $associationMappings[] = $this->getAssociationMapping();
  93. $alias = $queryBuilder->entityJoin($associationMappings);
  94. return array($alias, false);
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function getDefaultOptions()
  100. {
  101. return array(
  102. 'mapping_type' => false,
  103. 'field_name' => false,
  104. 'field_type' => 'entity',
  105. 'field_options' => array(),
  106. 'operator_type' => 'sonata_type_equal',
  107. 'operator_options' => array(),
  108. );
  109. }
  110. /**
  111. * {@inheritdoc}
  112. */
  113. public function getRenderSettings()
  114. {
  115. return array('sonata_type_filter_default', array(
  116. 'field_type' => $this->getFieldType(),
  117. 'field_options' => $this->getFieldOptions(),
  118. 'operator_type' => $this->getOption('operator_type'),
  119. 'operator_options' => $this->getOption('operator_options'),
  120. 'label' => $this->getLabel()
  121. ));
  122. }
  123. }