Completed
Pull Request — master (#83)
by Matthieu
02:23
created

translationFieldFilter()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 36
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 36
rs 8.439
cc 6
eloc 20
nc 9
nop 4
1
<?php
2
3
/*
4
 * This file is part of the Sonata package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\TranslationBundle\Admin\Extension\Gedmo;
13
14
use Gedmo\Translatable\TranslatableListener;
15
use Sonata\AdminBundle\Admin\AdminInterface;
16
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
17
use Sonata\AdminBundle\Datagrid\DatagridMapper;
18
use Sonata\TranslationBundle\Admin\Extension\AbstractTranslatableAdminExtension;
19
use Sonata\TranslationBundle\Checker\TranslatableChecker;
20
use Doctrine\DBAL\Query\QueryBuilder;
21
22
/**
23
 * @author Nicolas Bastien <[email protected]>
24
 */
25
class TranslatableAdminExtension extends AbstractTranslatableAdminExtension
26
{
27
    /**
28
     * @var TranslatableListener
29
     */
30
    protected $translatableListener;
31
32
    public function __construct(TranslatableChecker $translatableChecker, TranslatableListener $translatableListener)
33
    {
34
        parent::__construct($translatableChecker);
35
        $this->translatableListener = $translatableListener;
36
    }
37
38
    /**
39
     * @param AdminInterface $admin
0 ignored issues
show
Bug introduced by
There is no parameter named $admin. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
40
     *
41
     * @return TranslatableListener
42
     */
43
    protected function getTranslatableListener()
44
    {
45
        return $this->translatableListener;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function alterObject(AdminInterface $admin, $object)
52
    {
53
        if ($this->getTranslatableChecker()->isTranslatable($object)) {
54
            $this->getTranslatableListener()->setTranslatableLocale($this->getTranslatableLocale($admin));
55
            $this->getTranslatableListener()->setTranslationFallback('');
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
56
57
            $this->getContainer($admin)->get('doctrine')->getManager()->refresh($object);
58
            $object->setLocale($this->getTranslatableLocale($admin));
59
        }
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function configureQuery(AdminInterface $admin, ProxyQueryInterface $query, $context = 'list')
66
    {
67
        $this->getTranslatableListener()->setTranslatableLocale($this->getTranslatableLocale($admin));
68
        $this->getTranslatableListener()->setTranslationFallback('');
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
69
    }
70
71
    /**
72
     * Search on normal field and on translation field
73
     * To use with a doctrine_orm_callback filter type
74
     *
75
     * @param QueryBuilder $queryBuilder
76
     * @param string $alias
77
     * @param string $field
78
     * @param string $value
79
     *
80
     * @return bool
81
     */
82
    public static function translationFieldFilter($queryBuilder, $alias, $field, $value)
83
    {
84
        if (!$value['value']) {
85
            return;
86
        }
87
88
        // verify if the join is not already done
89
        $aliasAlreadyExists = false;
90
        $joinDqlParts = $queryBuilder->getDQLParts()['join'];
0 ignored issues
show
Bug introduced by
The method getDQLParts() does not seem to exist on object<Doctrine\DBAL\Query\QueryBuilder>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
91
        foreach ($joinDqlParts as $joins) {
92
            foreach ($joins as $join) {
93
                if ($join->getAlias() === 't') {
94
                    $aliasAlreadyExists = true;
95
                    break 2;
96
                }
97
            }
98
        }
99
100
        if ($aliasAlreadyExists === false) {
101
            $queryBuilder->leftJoin($alias . '.translations', 't');
0 ignored issues
show
Bug introduced by
The call to leftJoin() misses a required argument $alias.

This check looks for function calls that miss required arguments.

Loading history...
102
        }
103
104
        // search on translation OR on normal field
105
        $queryBuilder->andWhere($queryBuilder->expr()->orX(
106
            $queryBuilder->expr()->andX(
107
                $queryBuilder->expr()->eq('t.field', $queryBuilder->expr()->literal($field)),
108
                $queryBuilder->expr()->like('t.content', $queryBuilder->expr()->literal('%' . $value['value'] . '%'))
109
            ),
110
            $queryBuilder->expr()->like(
111
                sprintf('%s.%s', $alias, $field),
112
                $queryBuilder->expr()->literal('%' . $value['value'] . '%')
113
            )
114
        ));
115
116
        return true;
117
    }
118
}
119