BooleanFilter.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 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. $parameterName = $this->getNewParameterName();
  43. $this->applyWhere($queryBuilder, sprintf('%s.%s = :%s', $alias, $field, $parameterName));
  44. $queryBuilder->setParameter($parameterName, ($data['value'] == BooleanType::TYPE_YES) ? 1 : 0);
  45. }
  46. }
  47. /**
  48. * @return array
  49. */
  50. public function getDefaultOptions()
  51. {
  52. return array();
  53. }
  54. public function getRenderSettings()
  55. {
  56. return array('sonata_type_filter_default', array(
  57. 'field_type' => $this->getFieldType(),
  58. 'field_options' => $this->getFieldOptions(),
  59. 'operator_type' => 'hidden',
  60. 'operator_options' => array(),
  61. 'label' => $this->getLabel()
  62. ));
  63. }
  64. }