Filter.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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\BaseApplicationBundle\Filter;
  11. use Symfony\Component\Form\Configurable;
  12. use Sonata\BaseApplicationBundle\Admin\FieldDescription;
  13. use Doctrine\ORM\QueryBuilder;
  14. abstract class Filter extends Configurable
  15. {
  16. protected $description = array();
  17. protected $name = null;
  18. protected $field = null;
  19. protected $value = null;
  20. /**
  21. * apply the filter to the QueryBuilder instance
  22. *
  23. * @abstract
  24. * @param $query
  25. * @param $value
  26. * @param $alias the root alias
  27. * @return void
  28. */
  29. abstract public function filter(QueryBuilder $queryBuilder, $alias, $field, $value);
  30. /**
  31. * get the related form field filter
  32. *
  33. * @abstract
  34. * @return Field
  35. */
  36. abstract public function getFormField();
  37. public function __construct(FieldDescription $fieldDescription)
  38. {
  39. $this->name = $fieldDescription->getName();
  40. $this->description = $fieldDescription;
  41. parent::__construct($fieldDescription->getOption('filter_options', array()));
  42. $this->field = $this->getFormField();
  43. }
  44. /**
  45. *
  46. * set the object description
  47. *
  48. */
  49. public function setDescription(FieldDescription $description)
  50. {
  51. $this->description = $description;
  52. }
  53. /**
  54. * get the object description
  55. *
  56. * @return array
  57. */
  58. public function getDescription()
  59. {
  60. return $this->description;
  61. }
  62. public function apply(QueryBuilder $queryBuilder, $value)
  63. {
  64. $this->value = $value;
  65. $this->field->submit($value);
  66. list($alias, $field) = $this->association($queryBuilder, $this->field->getData());
  67. $this->filter($queryBuilder, $alias, $field, $this->field->getData());
  68. }
  69. protected function association(QueryBuilder $queryBuilder, $value)
  70. {
  71. if ($value) {
  72. if ($this->description->getType() == \Doctrine\ORM\Mapping\ClassMetadataInfo::MANY_TO_MANY) {
  73. $queryBuilder->leftJoin(
  74. sprintf('%s.%s', $queryBuilder->getRootAlias(), $this->description->getFieldName()),
  75. $this->getName()
  76. );
  77. // todo : use the metadata information to find the correct column name
  78. return array($this->getName(), 'id');
  79. }
  80. }
  81. return array($queryBuilder->getRootAlias(), $this->description->getFieldName());
  82. }
  83. public function setName($name)
  84. {
  85. $this->name = $name;
  86. }
  87. public function getName()
  88. {
  89. return $this->name;
  90. }
  91. public function setField($field)
  92. {
  93. $this->field = $field;
  94. }
  95. public function getField()
  96. {
  97. return $this->field;
  98. }
  99. }