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