IntegerFilter.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 Doctrine\ORM\QueryBuilder;
  12. use Symfony\Component\Form\FormFactory;
  13. class IntegerFilter extends Filter
  14. {
  15. /**
  16. * @param QueryBuilder $queryBuilder
  17. * @param string $alias
  18. * @param string $field
  19. * @param string $value
  20. * @return
  21. */
  22. public function filter($queryBuilder, $alias, $field, $value)
  23. {
  24. if ($value == null) {
  25. return;
  26. }
  27. // c.name > '1' => c.name OPERATOR :FIELDNAME
  28. $queryBuilder->andWhere(sprintf('%s.%s %s :%s',
  29. $alias,
  30. $field,
  31. $this->getOption('operator'),
  32. $this->getName()
  33. ));
  34. $queryBuilder->setParameter($this->getName(), (int)sprintf($this->getOption('format'), $value));
  35. }
  36. /**
  37. * @return array
  38. */
  39. public function getDefaultOptions()
  40. {
  41. return array(
  42. 'operator' => '=',
  43. 'format' => '%d'
  44. );
  45. }
  46. /**
  47. * @param \Symfony\Component\Form\FormFactory $formFactory
  48. * @return void
  49. */
  50. public function defineFieldBuilder(FormFactory $formFactory)
  51. {
  52. $options = $this->fieldDescription->getOption('filter_field_options', array('required' => false));
  53. $this->field = $formFactory->createNamedBuilder('text', $this->getName(), null, $options)->getForm();
  54. }
  55. }