CallbackFilter.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. class CallbackFilter extends Filter
  12. {
  13. /**
  14. * @param QueryBuilder $queryBuilder
  15. * @param mixed $data
  16. * @return array
  17. */
  18. protected function association($queryBuilder, $data)
  19. {
  20. return array($this->getOption('alias', $queryBuilder->getRootAlias()), false);
  21. }
  22. /**
  23. * @throws \RuntimeException
  24. * @param QueryBuilder $queryBuilder
  25. * @param string $alias
  26. * @param string $field
  27. * @param string $data
  28. * @return void
  29. */
  30. public function filter($queryBuilder, $alias, $field, $data)
  31. {
  32. if (!is_callable($this->getOption('callback'))) {
  33. throw new \RuntimeException(sprintf('Please provide a valid callback option "filter" for field "%s"', $this->getName()));
  34. }
  35. $this->active = call_user_func($this->getOption('callback'), $queryBuilder, $alias, $field, $data);
  36. }
  37. /**
  38. * @return array
  39. */
  40. public function getDefaultOptions()
  41. {
  42. return array(
  43. 'callback' => null,
  44. 'field_type' => 'text',
  45. 'operator_type' => 'hidden',
  46. 'operator_options' => array()
  47. );
  48. }
  49. public function getRenderSettings()
  50. {
  51. return array('sonata_type_filter_default', array(
  52. 'field_type' => $this->getFieldType(),
  53. 'field_options' => $this->getFieldOptions(),
  54. 'operator_type' => $this->getOption('operator_type'),
  55. 'operator_options' => $this->getOption('operator_options'),
  56. 'label' => $this->getLabel()
  57. ));
  58. }
  59. }