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; |
13
|
|
|
|
14
|
|
|
use Sonata\AdminBundle\Admin\AdminExtension; |
15
|
|
|
use Sonata\AdminBundle\Admin\AdminInterface; |
16
|
|
|
use Sonata\TranslationBundle\Checker\TranslatableChecker; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Nicolas Bastien <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
abstract class AbstractTranslatableAdminExtension extends AdminExtension |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Request parameter. |
25
|
|
|
*/ |
26
|
|
|
const TRANSLATABLE_LOCALE_PARAMETER = 'tl'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $translatableLocale; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var TranslatableChecker |
35
|
|
|
*/ |
36
|
|
|
protected $translatableChecker; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param TranslatableChecker $translatableChecker |
40
|
|
|
*/ |
41
|
|
|
public function __construct(TranslatableChecker $translatableChecker) |
42
|
|
|
{ |
43
|
|
|
$this->translatableChecker = $translatableChecker; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return ContainerInterface |
48
|
|
|
*/ |
49
|
|
|
protected function getContainer(AdminInterface $admin) |
50
|
|
|
{ |
51
|
|
|
return $admin->getConfigurationPool()->getContainer(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Return the list of possible locales for your models. |
56
|
|
|
* |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
protected function getTranslationLocales(AdminInterface $admin) |
60
|
|
|
{ |
61
|
|
|
return $this->getContainer($admin)->getParameter('sonata_translation.locales'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Return the default locale if url parameter is not present. |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
protected function getDefaultTranslationLocale(AdminInterface $admin) |
70
|
|
|
{ |
71
|
|
|
return $this->getContainer($admin)->getParameter('sonata_translation.default_locale'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param TranslatableChecker $translatableChecker |
76
|
|
|
*/ |
77
|
|
|
public function setTranslatableChecker($translatableChecker) |
78
|
|
|
{ |
79
|
|
|
$this->translatableChecker = $translatableChecker; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return TranslatableChecker |
84
|
|
|
*/ |
85
|
|
|
public function getTranslatableChecker() |
86
|
|
|
{ |
87
|
|
|
return $this->translatableChecker; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Return current translatable locale |
92
|
|
|
* ie: the locale used to load object translations != current request locale. |
93
|
|
|
* |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
public function getTranslatableLocale(AdminInterface $admin) |
97
|
|
|
{ |
98
|
|
|
if ($this->translatableLocale == null) { |
99
|
|
|
if ($admin->getRequest()) { |
100
|
|
|
$this->translatableLocale = $admin->getRequest()->get(self::TRANSLATABLE_LOCALE_PARAMETER); |
101
|
|
|
} |
102
|
|
|
if ($this->translatableLocale == null) { |
103
|
|
|
$this->translatableLocale = $this->getDefaultTranslationLocale($admin); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $this->translatableLocale; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritdoc} |
112
|
|
|
*/ |
113
|
|
|
public function getPersistentParameters(AdminInterface $admin) |
114
|
|
|
{ |
115
|
|
|
return array(self::TRANSLATABLE_LOCALE_PARAMETER => $this->getTranslatableLocale($admin)); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
|
|
public function alterNewInstance(AdminInterface $admin, $object) |
122
|
|
|
{ |
123
|
|
|
if ($object->getLocale() === null) { |
124
|
|
|
$object->setLocale($this->getTranslatableLocale($admin)); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.