123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- <?php
- /*
- * This file is part of the Sonata package.
- *
- * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Bundle\Sonata\BaseApplicationBundle\Filter;
- use Symfony\Component\Form\Configurable;
- use Bundle\Sonata\BaseApplicationBundle\Admin\FieldDescription;
- use Doctrine\ORM\QueryBuilder;
- abstract class Filter extends Configurable
- {
- protected $description = array();
- protected $name = null;
- protected $field = null;
- protected $value = null;
- public function __construct($name, FieldDescription $description)
- {
- $this->name = $name;
- $this->description = $description;
- parent::__construct($description->getOption('filter_options', array()));
- $this->field = $this->getFormField();
- }
- /**
- *
- * set the object description
- *
- */
- public function setDescription(array $description)
- {
- $this->description = $description;
- }
- /**
- * get the object description
- *
- * @return array
- */
- public function getDescription()
- {
- return $this->description;
- }
- public function apply(QueryBuilder $queryBuilder, $value)
- {
- $this->value = $value;
- $this->field->bind($value);
- list($alias, $field) = $this->association($queryBuilder, $this->field->getData());
- $this->filter($queryBuilder, $alias, $field, $this->field->getData());
- }
- protected function association(QueryBuilder $queryBuilder, $value)
- {
- if($value) {
- if($this->description->getType() == \Doctrine\ORM\Mapping\ClassMetadataInfo::MANY_TO_MANY) {
- $queryBuilder->leftJoin(
- sprintf('%s.%s', $queryBuilder->getRootAlias(), $this->description->getFieldName()),
- $this->getName()
- );
- // todo : use the metadata information to find the correct column name
- return array($this->getName(), 'id');
- }
- }
-
- return array($queryBuilder->getRootAlias(), $this->description->getFieldName());
- }
- /**
- * apply the filter to the QueryBuilder instance
- *
- * @abstract
- * @param $query
- * @param $value
- * @param $alias the root alias
- * @return void
- */
- abstract public function filter(QueryBuilder $queryBuilder, $alias, $field, $value);
- /**
- * get the related form field filter
- *
- * @abstract
- * @return Field
- */
- abstract public function getFormField();
- public function setName($name)
- {
- $this->name = $name;
- }
- public function getName()
- {
- return $this->name;
- }
- public function setField($field)
- {
- $this->field = $field;
- }
- public function getField()
- {
- return $this->field;
- }
- }
|