Filter.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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\Filter\Filter as BaseFilter;
  12. use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
  13. abstract class Filter extends BaseFilter
  14. {
  15. protected $active = false;
  16. /**
  17. * {@inheritdoc}
  18. */
  19. public function apply($queryBuilder, $value)
  20. {
  21. $this->value = $value;
  22. list($alias, $field) = $this->association($queryBuilder, $value);
  23. $this->filter($queryBuilder, $alias, $field, $value);
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. protected function association(ProxyQueryInterface $queryBuilder, $value)
  29. {
  30. $alias = $queryBuilder->entityJoin($this->getParentAssociationMappings());
  31. return array($alias, $this->getFieldName());
  32. }
  33. /**
  34. * @param \Sonata\AdminBundle\Datagrid\ProxyQueryInterface $queryBuilder
  35. * @param mixed $parameter
  36. */
  37. protected function applyWhere(ProxyQueryInterface $queryBuilder, $parameter)
  38. {
  39. if ($this->getCondition() == self::CONDITION_OR) {
  40. $queryBuilder->orWhere($parameter);
  41. } else {
  42. $queryBuilder->andWhere($parameter);
  43. }
  44. // filter is active since it's added to the queryBuilder
  45. $this->active = true;
  46. }
  47. /**
  48. * @param \Sonata\AdminBundle\Datagrid\ProxyQueryInterface $queryBuilder
  49. *
  50. * @return string
  51. */
  52. protected function getNewParameterName(ProxyQueryInterface $queryBuilder)
  53. {
  54. // dots are not accepted in a DQL identifier so replace them
  55. // by underscores.
  56. return str_replace('.', '_', $this->getName()) . '_' . $queryBuilder->getUniqueParameterId();
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function isActive()
  62. {
  63. return $this->active;
  64. }
  65. }