ModelFilter.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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\AdminBundle\Filter\ORM;
  11. use Doctrine\ORM\Mapping\ClassMetadataInfo;
  12. use Sonata\AdminBundle\Form\Type\BooleanType;
  13. class ModelFilter extends Filter
  14. {
  15. /**
  16. * @param QueryBuilder $queryBuilder
  17. * @param string $alias
  18. * @param string $field
  19. * @param mixed $data
  20. * @return
  21. */
  22. public function filter($queryBuilder, $alias, $field, $data)
  23. {
  24. if (!$data || !is_array($data) || !array_key_exists('type', $data) || !array_key_exists('value', $data)) {
  25. return;
  26. }
  27. if (is_array($data['value'])) {
  28. $this->handleMultiple($queryBuilder, $alias, $field, $data);
  29. } else {
  30. $this->handleScalar($queryBuilder, $alias, $field, $data);
  31. }
  32. }
  33. protected function handleMultiple($queryBuilder, $alias, $field, $data)
  34. {
  35. if (count($data['value']) == 0) {
  36. return;
  37. }
  38. if ($data['type'] == BooleanType::TYPE_NO) {
  39. $this->applyWhere($queryBuilder, $queryBuilder->expr()->notIn(sprintf('%s.%s', $alias, $field), $data['value']));
  40. } else {
  41. $this->applyWhere($queryBuilder, $queryBuilder->expr()->in(sprintf('%s.%s', $alias, $field), $data['value']));
  42. }
  43. }
  44. protected function handleScalar($queryBuilder, $alias, $field, $data)
  45. {
  46. if (empty($data['value'])) {
  47. return;
  48. }
  49. if ($data['type'] == BooleanType::TYPE_NO) {
  50. $this->applyWhere($queryBuilder, sprintf('%s.%s != :%s', $alias, $field, $this->getName()));
  51. } else {
  52. $this->applyWhere($queryBuilder, sprintf('%s.%s = :%s', $alias, $field, $this->getName()));
  53. }
  54. $queryBuilder->setParameter($this->getName(), $data['value']);
  55. }
  56. protected function association($queryBuilder, $data)
  57. {
  58. $types = array(
  59. ClassMetadataInfo::ONE_TO_ONE,
  60. ClassMetadataInfo::ONE_TO_MANY,
  61. ClassMetadataInfo::MANY_TO_MANY,
  62. ClassMetadataInfo::MANY_TO_ONE,
  63. );
  64. if (!in_array($this->getOption('mapping_type'), $types)) {
  65. throw new \RunTimeException('Invalid mapping type');
  66. }
  67. if (!$this->getOption('field_name')) {
  68. throw new \RunTimeException('please provide a field_name options');
  69. }
  70. $queryBuilder->leftJoin(sprintf('%s.%s', $queryBuilder->getRootAlias(), $this->getFieldName()), $this->getName());
  71. return array($this->getFieldName(), 'id');
  72. }
  73. public function getDefaultOptions()
  74. {
  75. return array(
  76. 'mapping_type' => false,
  77. 'field_name' => false,
  78. 'field_type' => 'entity',
  79. 'field_options' => array(),
  80. 'operator_type' => 'sonata_type_boolean',
  81. 'operator_options' => array(),
  82. );
  83. }
  84. public function getRenderSettings()
  85. {
  86. return array('sonata_type_filter_default', array(
  87. 'field_type' => $this->getFieldType(),
  88. 'field_options' => $this->getFieldOptions(),
  89. 'operator_type' => $this->getOption('operator_type'),
  90. 'operator_options' => $this->getOption('operator_options'),
  91. ));
  92. }
  93. }