Filter.php 2.7 KB

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