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