NumberFilter.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. $this->applyWhere($queryBuilder, sprintf('%s.%s %s :%s', $alias, $field, $operator, $this->getName()));
  33. $queryBuilder->setParameter($this->getName(), $data['value']);
  34. }
  35. /**
  36. * @param $type
  37. * @return bool
  38. */
  39. private function getOperator($type)
  40. {
  41. $choices = array(
  42. NumberType::TYPE_EQUAL => '=',
  43. NumberType::TYPE_GREATER_EQUAL => '>=',
  44. NumberType::TYPE_GREATER_THAN => '>',
  45. NumberType::TYPE_LESS_EQUAL => '<=',
  46. NumberType::TYPE_LESS_THAN => '<',
  47. );
  48. return isset($choices[$type]) ? $choices[$type] : false;
  49. }
  50. /**
  51. * @return array
  52. */
  53. public function getDefaultOptions()
  54. {
  55. return array();
  56. }
  57. public function getRenderSettings()
  58. {
  59. return array('sonata_type_filter_number', array(
  60. 'field_type' => $this->getFieldType(),
  61. 'field_options' => $this->getFieldOptions(),
  62. 'label' => $this->getLabel()
  63. ));
  64. }
  65. }