NumberFilter.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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\Filter\NumberType;
  12. class NumberFilter extends Filter
  13. {
  14. /**
  15. * @param QueryBuilder $queryBuilder
  16. * @param string $alias
  17. * @param string $field
  18. * @param string $data
  19. * @return
  20. */
  21. public function filter($queryBuilder, $alias, $field, $data)
  22. {
  23. if (!$data || !is_array($data) || !array_key_exists('value', $data) || !is_numeric($data['value'])) {
  24. return;
  25. }
  26. $type = isset($data['type']) ? $data['type'] : false;
  27. $operator = $this->getOperator($type);
  28. if (!$operator) {
  29. $operator = '=';
  30. }
  31. // c.name > '1' => c.name OPERATOR :FIELDNAME
  32. $parameterName = $this->getNewParameterName();
  33. $this->applyWhere($queryBuilder, sprintf('%s.%s %s :%s', $alias, $field, $operator, $parameterName));
  34. $queryBuilder->setParameter($parameterName, $data['value']);
  35. }
  36. /**
  37. * @param $type
  38. * @return bool
  39. */
  40. private function getOperator($type)
  41. {
  42. $choices = array(
  43. NumberType::TYPE_EQUAL => '=',
  44. NumberType::TYPE_GREATER_EQUAL => '>=',
  45. NumberType::TYPE_GREATER_THAN => '>',
  46. NumberType::TYPE_LESS_EQUAL => '<=',
  47. NumberType::TYPE_LESS_THAN => '<',
  48. );
  49. return isset($choices[$type]) ? $choices[$type] : false;
  50. }
  51. /**
  52. * @return array
  53. */
  54. public function getDefaultOptions()
  55. {
  56. return array();
  57. }
  58. public function getRenderSettings()
  59. {
  60. return array('sonata_type_filter_number', array(
  61. 'field_type' => $this->getFieldType(),
  62. 'field_options' => $this->getFieldOptions(),
  63. 'label' => $this->getLabel()
  64. ));
  65. }
  66. }