BooleanFilter.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 Sonata\AdminBundle\Form\Type\BooleanType;
  12. class BooleanFilter extends Filter
  13. {
  14. /**
  15. * @param QueryBuilder $queryBuilder
  16. * @param string $alias
  17. * @param string $field
  18. * @param mixed $data
  19. * @return
  20. */
  21. public function filter($queryBuilder, $alias, $field, $data)
  22. {
  23. if (!$data || !is_array($data) || !array_key_exists('type', $data) || !array_key_exists('value', $data)) {
  24. return;
  25. }
  26. if (is_array($data['value'])) {
  27. $values = array();
  28. foreach ($data['value'] as $v) {
  29. if (!in_array($v, array(BooleanType::TYPE_NO, BooleanType::TYPE_YES))) {
  30. continue;
  31. }
  32. $values[] = ($v == BooleanType::TYPE_YES) ? 1 : 0;
  33. }
  34. if (count($values) == 0) {
  35. return;
  36. }
  37. $this->applyWhere($queryBuilder, $queryBuilder->expr()->in(sprintf('%s.%s', $alias, $field), $values));
  38. } else {
  39. if (!in_array($data['value'], array(BooleanType::TYPE_NO, BooleanType::TYPE_YES))) {
  40. return;
  41. }
  42. $this->applyWhere($queryBuilder, sprintf('%s.%s = :%s', $alias, $field, $this->getName()));
  43. $queryBuilder->setParameter($this->getName(), ($data['value'] == BooleanType::TYPE_YES) ? 1 : 0);
  44. }
  45. }
  46. /**
  47. * @return array
  48. */
  49. function getDefaultOptions()
  50. {
  51. return array();
  52. }
  53. public function getRenderSettings()
  54. {
  55. return array('sonata_type_filter_default', array(
  56. 'field_type' => $this->getFieldType(),
  57. 'field_options' => $this->getFieldOptions(),
  58. 'operator_type' => 'hidden',
  59. 'operator_options' => array(),
  60. 'label' => $this->getLabel()
  61. ));
  62. }
  63. }