Completed
Push — master ( dd59f7...c87e78 )
by Grégoire
13s queued 10s
created

src/Filter/TranslationFieldFilter.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\TranslationBundle\Filter;
15
16
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
17
use Sonata\AdminBundle\Form\Type\Filter\DefaultType;
18
use Sonata\DoctrineORMAdminBundle\Filter\Filter;
19
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
20
use Symfony\Component\Form\Extension\Core\Type\TextType;
21
22
final class TranslationFieldFilter extends Filter
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data): void
28
    {
29
        if (!$data || !\is_array($data) || !\array_key_exists('value', $data) || null === $data['value']) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
30
            return;
31
        }
32
33
        $data['value'] = trim((string) $data['value']);
34
35
        if (0 === \strlen($data['value'])) {
36
            return;
37
        }
38
39
        $joinAlias = 'tff';
40
41
        // verify if the join is not already done
42
        $aliasAlreadyExists = false;
43
        foreach ($queryBuilder->getDQLParts()['join'] as $joins) {
44
            foreach ($joins as $join) {
45
                if ($join->getAlias() === $joinAlias) {
46
                    $aliasAlreadyExists = true;
47
48
                    break 2;
49
                }
50
            }
51
        }
52
53
        if (!$aliasAlreadyExists) {
54
            $queryBuilder->leftJoin($alias.'.translations', $joinAlias);
55
        }
56
57
        // search on translation OR on normal field
58
        $this->applyWhere($queryBuilder, $queryBuilder->expr()->orX(
59
            $queryBuilder->expr()->andX(
60
                $queryBuilder->expr()->eq($joinAlias.'.field', $queryBuilder->expr()->literal($field)),
61
                $queryBuilder->expr()->like(
62
                    $joinAlias.'.content',
63
                    $queryBuilder->expr()->literal('%'.$data['value'].'%')
64
                )
65
            ),
66
            $queryBuilder->expr()->like(
67
                sprintf('%s.%s', $alias, $field),
68
                $queryBuilder->expr()->literal('%'.$data['value'].'%')
69
            )
70
        ));
71
72
        $this->active = true;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function getDefaultOptions()
79
    {
80
        return [
81
            'field_type' => TextType::class,
82
            'operator_type' => HiddenType::class,
83
            'operator_options' => [],
84
        ];
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function getRenderSettings()
91
    {
92
        return [DefaultType::class, [
93
            'field_type' => $this->getFieldType(),
94
            'field_options' => $this->getFieldOptions(),
95
            'operator_type' => $this->getOption('operator_type'),
96
            'operator_options' => $this->getOption('operator_options'),
97
            'label' => $this->getLabel(),
98
        ]];
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    protected function association(ProxyQueryInterface $queryBuilder, $data)
105
    {
106
        $alias = $queryBuilder->entityJoin($this->getParentAssociationMappings());
107
108
        return [$this->getOption('alias', $alias), $this->getFieldName()];
109
    }
110
}
111