Completed
Push — master ( 78a4ca...d31c6f )
by
unknown
04:58
created

TranslatableAdminExtension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project 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\TranslationBundle\Admin\Extension\AbstractTranslatableAdminExtension;
18
use Sonata\TranslationBundle\Checker\TranslatableChecker;
19
20
/**
21
 * @author Nicolas Bastien <[email protected]>
22
 */
23
class TranslatableAdminExtension extends AbstractTranslatableAdminExtension
24
{
25
    /**
26
     * @var TranslatableListener
27
     */
28
    protected $translatableListener;
29
30
    public function __construct(TranslatableChecker $translatableChecker, TranslatableListener $translatableListener = null)
31
    {
32
        parent::__construct($translatableChecker);
33
        $this->translatableListener = $translatableListener;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function alterObject(AdminInterface $admin, $object)
40
    {
41
        if ($this->getTranslatableChecker()->isTranslatable($object)) {
42
            $translatableListener = $this->getTranslatableListener($admin);
43
            $translatableListener->setTranslatableLocale($this->getTranslatableLocale($admin));
44
            $translatableListener->setTranslationFallback(false);
45
46
            $this->getContainer($admin)->get('doctrine')->getManager()->refresh($object);
47
            $object->setLocale($this->getTranslatableLocale($admin));
48
        }
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function configureQuery(AdminInterface $admin, ProxyQueryInterface $query, $context = 'list')
55
    {
56
        $this->getTranslatableListener($admin)->setTranslatableLocale($this->getTranslatableLocale($admin));
57
        $this->getTranslatableListener($admin)->setTranslationFallback(false);
58
    }
59
60
    /**
61
     * @param AdminInterface $admin Deprecated, set TranslatableListener in the constructor instead
62
     *
63
     * @return TranslatableListener
64
     */
65
    protected function getTranslatableListener(AdminInterface $admin)
66
    {
67
        if (null === $this->translatableListener) {
68
            $this->translatableListener = $this->getContainer($admin)->get(
69
                'stof_doctrine_extensions.listener.translatable'
70
            );
71
        }
72
73
        return $this->translatableListener;
74
    }
75
}
76