1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <?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 Sonata\DoctrineORMAdminBundle\Filter;
- use Doctrine\ORM\QueryBuilder;
- use Sonata\AdminBundle\Form\Type\Filter\ChoiceType;
- use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
- class ChoiceFilter extends Filter
- {
- /**
- * {@inheritdoc}
- */
- public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
- {
- if (!$data || !is_array($data) || !array_key_exists('type', $data) || !array_key_exists('value', $data)) {
- return;
- }
- if (is_array($data['value'])) {
- if (count($data['value']) == 0) {
- return;
- }
- if (in_array('all', $data['value'])) {
- return;
- }
- if ($data['type'] == ChoiceType::TYPE_NOT_CONTAINS) {
- $this->applyWhere($queryBuilder, $queryBuilder->expr()->notIn(sprintf('%s.%s', $alias, $field ), $data['value']));
- } else {
- $this->applyWhere($queryBuilder, $queryBuilder->expr()->in(sprintf('%s.%s', $alias, $field ), $data['value']));
- }
- } else {
- if ($data['value'] === '' || $data['value'] === null || $data['value'] === false || $data['value'] == 'all') {
- return;
- }
- $parameterName = $this->getNewParameterName($queryBuilder);
- if ($data['type'] == ChoiceType::TYPE_NOT_CONTAINS) {
- $this->applyWhere($queryBuilder, sprintf('%s.%s <> :%s', $alias, $field, $parameterName));
- } else {
- $this->applyWhere($queryBuilder, sprintf('%s.%s = :%s', $alias, $field, $parameterName));
- }
- $queryBuilder->setParameter($parameterName, $data['value']);
- }
- }
- /**
- * @param string $type
- *
- * @return bool
- */
- private function getOperator($type)
- {
- $choices = array(
- ChoiceType::TYPE_CONTAINS => 'IN',
- ChoiceType::TYPE_NOT_CONTAINS => 'NOT IN',
- ChoiceType::TYPE_EQUAL => '=',
- );
- return isset($choices[$type]) ? $choices[$type] : false;
- }
- /**
- * {@inheritdoc}
- */
- public function getDefaultOptions()
- {
- return array();
- }
- /**
- * {@inheritdoc}
- */
- public function getRenderSettings()
- {
- return array('sonata_type_filter_default', array(
- 'operator_type' => 'sonata_type_equal',
- 'field_type' => $this->getFieldType(),
- 'field_options' => $this->getFieldOptions(),
- 'label' => $this->getLabel()
- ));
- }
- }
|