Filter.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. abstract class Filter extends BaseFilter
  13. {
  14. protected $active = false;
  15. protected $parameterUniqueId = 0;
  16. public function apply($queryBuilder, $value)
  17. {
  18. $this->value = $value;
  19. list($alias, $field) = $this->association($queryBuilder, $value);
  20. $this->filter($queryBuilder, $alias, $field, $value);
  21. }
  22. protected function association($queryBuilder, $value)
  23. {
  24. $parentAssociationMappings = $this->getParentAssociationMappings();
  25. $alias = $this->getOption('alias', $queryBuilder->getRootAlias());
  26. $newAlias = 's_'.$alias;
  27. foreach($parentAssociationMappings as $parentAssociationMapping){
  28. $newAlias .= '_'.$parentAssociationMapping['fieldName'];
  29. $queryBuilder->leftJoin(sprintf('%s.%s', $alias, $parentAssociationMapping['fieldName']), $newAlias);
  30. $alias = $newAlias;
  31. }
  32. return array($alias, $this->getFieldName());
  33. }
  34. protected function applyWhere($queryBuilder, $parameter)
  35. {
  36. if ($this->getCondition() == self::CONDITION_OR) {
  37. $queryBuilder->orWhere($parameter);
  38. } else {
  39. $queryBuilder->andWhere($parameter);
  40. }
  41. // filter is active since it's added to the queryBuilder
  42. $this->active = true;
  43. }
  44. protected function getNewParameterName()
  45. {
  46. // dots are not accepted in a DQL identifier so replace them
  47. // by underscores. To avoid any name conflict with two filters
  48. // named myEntity.myProperty and myEntity_myProperty used at the
  49. // same time, we also use the object hash, that are different
  50. // for two objects alive at the same time.
  51. return str_replace('.', '_', $this->getName()).'_'.spl_object_hash($this).'_'.($this->parameterUniqueId++);
  52. }
  53. public function isActive()
  54. {
  55. return $this->active;
  56. }
  57. }