Completed
Pull Request — master (#84)
by David
04:28 queued 02:13
created

TranslatableAdminExtension::configureQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 3
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\DoctrineORMAdminBundle\Datagrid\ProxyQuery;
18
use Sonata\TranslationBundle\Admin\Extension\AbstractTranslatableAdminExtension;
19
use Sonata\TranslationBundle\Checker\TranslatableChecker;
20
21
/**
22
 * @author Nicolas Bastien <[email protected]>
23
 */
24
class TranslatableAdminExtension extends AbstractTranslatableAdminExtension
25
{
26
    /**
27
     * @var TranslatableListener
28
     */
29
    protected $translatableListener;
30
31
    public function __construct(TranslatableChecker $translatableChecker, TranslatableListener $translatableListener = null)
32
    {
33
        parent::__construct($translatableChecker);
34
        $this->translatableListener = $translatableListener;
35
    }
36
37
    /**
38
     * @param AdminInterface $admin Deprecated, set TranslatableListener in the constructor instead.
39
     *
40
     * @return TranslatableListener
41
     */
42
    protected function getTranslatableListener(AdminInterface $admin)
43
    {
44
        if (null === $this->translatableListener) {
45
            $this->translatableListener = $this->getContainer($admin)->get(
46
                'stof_doctrine_extensions.listener.translatable'
47
            );
48
        }
49
50
        return $this->translatableListener;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function alterObject(AdminInterface $admin, $object)
57
    {
58
        if ($this->getTranslatableChecker()->isTranslatable($object)) {
59
            $translatableListener = $this->getTranslatableListener($admin);
60
            $translatableListener->setTranslatableLocale($this->getTranslatableLocale($admin));
61
            $translatableListener->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...
62
63
            $this->getContainer($admin)->get('doctrine')->getManager()->refresh($object);
64
            $object->setLocale($this->getTranslatableLocale($admin));
65
        }
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function configureQuery(AdminInterface $admin, ProxyQueryInterface $query, $context = 'list')
72
    {
73
        $this->getTranslatableListener($admin)->setTranslatableLocale($this->getTranslatableLocale($admin));
74
        $this->getTranslatableListener($admin)->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...
75
    }
76
77
    /**
78
     * Search on normal field and on translation field
79
     * To use with a doctrine_orm_callback filter type.
80
     *
81
     * @param ProxyQuery $queryBuilder
82
     * @param string     $alias
83
     * @param string     $field
84
     * @param string     $value
85
     *
86
     * @return bool|null
87
     */
88
    public static function translationFieldFilter(ProxyQuery $queryBuilder, $alias, $field, $value)
89
    {
90
        if (!$value['value']) {
91
            return;
92
        }
93
94
        // verify if the join is not already done
95
        $aliasAlreadyExists = false;
96
        $joinDqlParts = $queryBuilder->getDQLParts()['join'];
97
        foreach ($joinDqlParts as $joins) {
98
            foreach ($joins as $join) {
99
                if ($join->getAlias() === 't') {
100
                    $aliasAlreadyExists = true;
101
                    break 2;
102
                }
103
            }
104
        }
105
106
        if (!$aliasAlreadyExists) {
107
            $queryBuilder->leftJoin($alias.'.translations', 't');
108
        }
109
110
        // search on translation OR on normal field
111
        $queryBuilder->andWhere($queryBuilder->expr()->orX(
112
            $queryBuilder->expr()->andX(
113
                $queryBuilder->expr()->eq('t.field', $queryBuilder->expr()->literal($field)),
114
                $queryBuilder->expr()->like('t.content', $queryBuilder->expr()->literal('%'.$value['value'].'%'))
115
            ),
116
            $queryBuilder->expr()->like(
117
                sprintf('%s.%s', $alias, $field),
118
                $queryBuilder->expr()->literal('%'.$value['value'].'%')
119
            )
120
        ));
121
122
        return true;
123
    }
124
}
125