NumberFilter.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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\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('type', $data) || !array_key_exists('value', $data)) {
  24. return;
  25. }
  26. $operator = $this->getOperator((int) $data['type']);
  27. if (!$operator) {
  28. return;
  29. }
  30. // c.name > '1' => c.name OPERATOR :FIELDNAME
  31. $this->applyWhere($queryBuilder, sprintf('%s.%s %s :%s', $alias, $field, $operator, $this->getName()));
  32. $queryBuilder->setParameter($this->getName(), $data['value']);
  33. }
  34. /**
  35. * @param $type
  36. * @return bool
  37. */
  38. private function getOperator($type)
  39. {
  40. $choices = array(
  41. NumberType::TYPE_EQUAL => '=',
  42. NumberType::TYPE_GREATER_EQUAL => '>=',
  43. NumberType::TYPE_GREATER_THAN => '>',
  44. NumberType::TYPE_LESS_EQUAL => '<=',
  45. NumberType::TYPE_LESS_THAN => '<',
  46. );
  47. return isset($choices[$type]) ? $choices[$type] : false;
  48. }
  49. /**
  50. * @return array
  51. */
  52. function getDefaultOptions()
  53. {
  54. return array();
  55. }
  56. public function getRenderSettings()
  57. {
  58. return array('sonata_type_filter_number', array(
  59. 'field_type' => $this->getFieldType(),
  60. 'field_options' => $this->getFieldOptions(),
  61. ));
  62. }
  63. }