1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* An extension to dropdown field, pre-configured to list languages. |
4
|
|
|
* The languages already used in the site will be on top. |
5
|
|
|
* |
6
|
|
|
* @package translatable |
7
|
|
|
*/ |
8
|
|
|
class LanguageDropdownField extends GroupedDropdownField |
9
|
|
|
{ |
10
|
|
|
private static $allowed_actions = array( |
11
|
|
|
'getLocaleForObject' |
12
|
|
|
); |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Create a new LanguageDropdownField |
16
|
|
|
* @param string $name |
17
|
|
|
* @param string $title |
18
|
|
|
* @param array $excludeLocales List of locales that won't be included |
19
|
|
|
* @param string $translatingClass Name of the class with translated instances |
20
|
|
|
* where to look for used languages |
21
|
|
|
* @param string $list Indicates the source language list. |
22
|
|
|
* Can be either Common-English, Common-Native, Locale-English, Locale-Native |
23
|
|
|
*/ |
24
|
|
|
public function __construct($name, $title, $excludeLocales = array(), |
25
|
|
|
$translatingClass = 'SiteTree', $list = 'Common-English', $instance = null |
26
|
|
|
) { |
27
|
|
|
$usedLocalesWithTitle = Translatable::get_existing_content_languages($translatingClass); |
28
|
|
|
$usedLocalesWithTitle = array_diff_key($usedLocalesWithTitle, $excludeLocales); |
29
|
|
|
|
30
|
|
|
if ('Common-English' == $list) { |
31
|
|
|
$allLocalesWithTitle = i18n::get_common_languages(); |
32
|
|
|
} elseif ('Common-Native' == $list) { |
33
|
|
|
$allLocalesWithTitle = i18n::get_common_languages(true); |
34
|
|
|
} elseif ('Locale-English' == $list) { |
35
|
|
|
$allLocalesWithTitle = i18n::get_common_locales(); |
36
|
|
|
} elseif ('Locale-Native' == $list) { |
37
|
|
|
$allLocalesWithTitle = i18n::get_common_locales(true); |
38
|
|
|
} else { |
39
|
|
|
$allLocalesWithTitle = i18n::config()->all_locales; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if (isset($allLocales[Translatable::default_locale()])) { |
|
|
|
|
43
|
|
|
unset($allLocales[Translatable::default_locale()]); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// Limit to allowed locales if defined |
47
|
|
|
// Check for canTranslate() if an $instance is given |
48
|
|
|
$allowedLocales = Translatable::get_allowed_locales(); |
49
|
|
|
foreach ($allLocalesWithTitle as $locale => $localeTitle) { |
50
|
|
|
if ( |
51
|
|
|
($allowedLocales && !in_array($locale, $allowedLocales)) |
|
|
|
|
52
|
|
|
|| ($excludeLocales && in_array($locale, $excludeLocales)) |
|
|
|
|
53
|
|
|
|| ($usedLocalesWithTitle && array_key_exists($locale, $usedLocalesWithTitle)) |
|
|
|
|
54
|
|
|
) { |
55
|
|
|
unset($allLocalesWithTitle[$locale]); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
// instance specific permissions |
59
|
|
View Code Duplication |
foreach ($allLocalesWithTitle as $locale => $localeTitle) { |
|
|
|
|
60
|
|
|
if ($instance && !$instance->canTranslate(null, $locale)) { |
61
|
|
|
unset($allLocalesWithTitle[$locale]); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
View Code Duplication |
foreach ($usedLocalesWithTitle as $locale => $localeTitle) { |
|
|
|
|
65
|
|
|
if ($instance && !$instance->canTranslate(null, $locale)) { |
66
|
|
|
unset($usedLocalesWithTitle[$locale]); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// Sort by title (array value) |
71
|
|
|
asort($allLocalesWithTitle); |
72
|
|
|
|
73
|
|
|
if (count($usedLocalesWithTitle)) { |
74
|
|
|
asort($usedLocalesWithTitle); |
75
|
|
|
$source = array( |
76
|
|
|
_t('Form.LANGAVAIL', "Available languages") => $usedLocalesWithTitle, |
77
|
|
|
_t('Form.LANGAOTHER', "Other languages") => $allLocalesWithTitle |
78
|
|
|
); |
79
|
|
|
} else { |
80
|
|
|
$source = $allLocalesWithTitle; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
parent::__construct($name, $title, $source); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function Type() |
87
|
|
|
{ |
88
|
|
|
return 'languagedropdown dropdown'; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function getAttributes() |
92
|
|
|
{ |
93
|
|
|
return array_merge( |
94
|
|
|
parent::getAttributes(), |
95
|
|
|
array('data-locale-url' => $this->Link('getLocaleForObject')) |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get the locale for an object that has the Translatable extension. |
101
|
|
|
* |
102
|
|
|
* @return locale |
103
|
|
|
*/ |
104
|
|
|
public function getLocaleForObject() |
105
|
|
|
{ |
106
|
|
|
$id = (int)$this->getRequest()->requestVar('id'); |
107
|
|
|
$class = Convert::raw2sql($this->getRequest()->requestVar('class')); |
108
|
|
|
$locale = Translatable::get_current_locale(); |
109
|
|
|
if ($id && $class && class_exists($class) && $class::has_extension('Translatable')) { |
110
|
|
|
// temporarily disable locale filter so that we won't filter out the object |
111
|
|
|
Translatable::disable_locale_filter(); |
112
|
|
|
$object = DataObject::get_by_id($class, $id); |
113
|
|
|
Translatable::enable_locale_filter(); |
114
|
|
|
if ($object) { |
115
|
|
|
$locale = $object->Locale; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
return $locale; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.
The variable may have been renamed without also renaming all references.