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