1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Translatable\Controller; |
4
|
|
|
|
5
|
|
|
use SilverStripe\CMS\Controllers\CMSPagesController; |
6
|
|
|
use SilverStripe\CMS\Model\SiteTree; |
7
|
|
|
use SilverStripe\Control\Controller; |
8
|
|
|
use SilverStripe\Core\Convert; |
9
|
|
|
use SilverStripe\Core\Extension; |
10
|
|
|
use SilverStripe\Forms\FieldList; |
11
|
|
|
use SilverStripe\Forms\Form; |
12
|
|
|
use SilverStripe\Forms\FormAction; |
13
|
|
|
use SilverStripe\Forms\HiddenField; |
14
|
|
|
use SilverStripe\Forms\LiteralField; |
15
|
|
|
use SilverStripe\Forms\HTMLEditor\HTMLEditorConfig; |
16
|
|
|
use SilverStripe\i18n\i18n; |
17
|
|
|
use SilverStripe\ORM\DataObject; |
18
|
|
|
use SilverStripe\Security\Member; |
19
|
|
|
use SilverStripe\Security\Permission; |
20
|
|
|
use SilverStripe\Security\SecurityToken; |
21
|
|
|
use SilverStripe\SiteConfig\SiteConfig; |
22
|
|
|
use SilverStripe\Translatable\Forms\LanguageDropdownField; |
23
|
|
|
use SilverStripe\Translatable\Model\Translatable; |
24
|
|
|
use SilverStripe\View\Requirements; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @package translatable |
28
|
|
|
*/ |
29
|
|
|
class TranslatableCMSMainExtension extends Extension |
30
|
|
|
{ |
31
|
|
|
private static $allowed_actions = array( |
|
|
|
|
32
|
|
|
'createtranslation', |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
public function init() |
36
|
|
|
{ |
37
|
|
|
$req = $this->owner->getRequest(); |
38
|
|
|
|
39
|
|
|
// Ignore being called on LeftAndMain base class, |
40
|
|
|
// which is the case when requests are first routed through AdminRootController |
41
|
|
|
// as an intermediary rather than the endpoint controller |
42
|
|
|
if (!$this->owner->stat('tree_class')) { |
43
|
|
|
return; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// Locale" attribute is either explicitly added by LeftAndMain Javascript logic, |
47
|
|
|
// or implied on a translated record (see {@link Translatable->updateCMSFields()}). |
48
|
|
|
// $Lang serves as a "context" which can be inspected by Translatable - hence it |
49
|
|
|
// has the same name as the database property on Translatable. |
50
|
|
|
$id = $req->param('ID'); |
51
|
|
|
if ($req->requestVar("Locale")) { |
52
|
|
|
$this->owner->Locale = $req->requestVar("Locale"); |
53
|
|
|
} elseif ($id && is_numeric($id)) { |
54
|
|
|
$record = DataObject::get_by_id($this->owner->stat('tree_class'), $id); |
55
|
|
|
if ($record && $record->Locale) { |
56
|
|
|
$this->owner->Locale = $record->Locale; |
57
|
|
|
} |
58
|
|
|
} else { |
59
|
|
|
$this->owner->Locale = Translatable::default_locale(); |
60
|
|
|
if ($this->owner instanceof CMSPagesController) { |
61
|
|
|
// the CMSPagesController always needs to have the locale set, |
62
|
|
|
// otherwise page editing will cause an extra |
63
|
|
|
// ajax request which looks weird due to multiple "loading"-flashes |
64
|
|
|
$getVars = $req->getVars(); |
65
|
|
|
if (isset($getVars['url'])) { |
66
|
|
|
unset($getVars['url']); |
67
|
|
|
} |
68
|
|
|
return $this->owner->redirect(Controller::join_links( |
69
|
|
|
$this->owner->Link(), |
70
|
|
|
$req->param('Action'), |
71
|
|
|
$req->param('ID'), |
72
|
|
|
$req->param('OtherID'), |
73
|
|
|
($query = http_build_query($getVars)) ? "?$query" : null |
74
|
|
|
)); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
Translatable::set_current_locale($this->owner->Locale); |
78
|
|
|
|
79
|
|
|
// If a locale is set, it needs to match to the current record |
80
|
|
|
$requestLocale = $req->requestVar("Locale"); |
81
|
|
|
$page = $this->owner->currentPage(); |
82
|
|
|
if ($req->httpMethod() == 'GET' // leave form submissions alone |
83
|
|
|
&& $requestLocale |
84
|
|
|
&& $page |
85
|
|
|
&& $page->hasExtension(Translatable::class) |
86
|
|
|
&& $page->Locale != $requestLocale |
87
|
|
|
&& $req->latestParam('Action') != 'EditorToolbar' |
88
|
|
|
) { |
89
|
|
|
$transPage = $page->getTranslation($requestLocale); |
90
|
|
|
if ($transPage) { |
91
|
|
|
Translatable::set_current_locale($transPage->Locale); |
92
|
|
|
return $this->owner->redirect(Controller::join_links( |
93
|
|
|
$this->owner->Link('show'), |
94
|
|
|
$transPage->ID |
95
|
|
|
// ?locale will automatically be added |
96
|
|
|
)); |
97
|
|
|
} elseif (!($this->owner instanceof CMSPagesController)) { |
98
|
|
|
// If the record is not translated, redirect to pages overview |
99
|
|
|
return $this->owner->redirect(Controller::join_links( |
100
|
|
|
singleton(CMSPagesController::class)->Link(), |
101
|
|
|
'?Locale=' . $requestLocale |
102
|
|
|
)); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// collect languages for TinyMCE spellchecker plugin. |
107
|
|
|
// see http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/spellchecker |
108
|
|
|
$langName = i18n::getData()->localeName($this->owner->Locale); |
109
|
|
|
HtmlEditorConfig::get('cms')->setOption( |
110
|
|
|
'spellchecker_languages', |
111
|
|
|
"+{$langName}={$this->owner->Locale}" |
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
Requirements::javascript('translatable/javascript/CMSMain.Translatable.js'); |
115
|
|
|
Requirements::css('translatable/css/CMSMain.Translatable.css'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function updateEditForm(&$form) |
119
|
|
|
{ |
120
|
|
|
if ($form->getName() == 'RootForm' && SiteConfig::has_extension(Translatable::class)) { |
121
|
|
|
$siteConfig = SiteConfig::current_site_config(); |
122
|
|
|
$form->Fields()->push(HiddenField::create('Locale', '', $siteConfig->Locale)); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function updatePageOptions(&$fields) |
127
|
|
|
{ |
128
|
|
|
$fields->push(HiddenField::create("Locale", 'Locale', Translatable::get_current_locale())); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Create a new translation from an existing item, switch to this language and reload the tree. |
133
|
|
|
*/ |
134
|
|
|
public function createtranslation($data, $form) |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
$request = $this->owner->getRequest(); |
137
|
|
|
|
138
|
|
|
// Protect against CSRF on destructive action |
139
|
|
|
if (!SecurityToken::inst()->checkRequest($request)) { |
140
|
|
|
return $this->owner->httpError(400); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$langCode = Convert::raw2sql($request->postVar('NewTransLang')); |
144
|
|
|
$record = $this->owner->getRecord($request->postVar('ID')); |
145
|
|
|
if (!$record) { |
146
|
|
|
return $this->owner->httpError(404); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$this->owner->Locale = $langCode; |
150
|
|
|
Translatable::set_current_locale($langCode); |
151
|
|
|
|
152
|
|
|
// Create a new record in the database - this is different |
153
|
|
|
// to the usual "create page" pattern of storing the record |
154
|
|
|
// in-memory until a "save" is performed by the user, mainly |
155
|
|
|
// to simplify things a bit. |
156
|
|
|
// @todo Allow in-memory creation of translations that don't |
157
|
|
|
// persist in the database before the user requests it |
158
|
|
|
$translatedRecord = $record->createTranslation($langCode); |
159
|
|
|
|
160
|
|
|
$url = Controller::join_links( |
161
|
|
|
$this->owner->Link('show'), |
162
|
|
|
$translatedRecord->ID |
163
|
|
|
); |
164
|
|
|
|
165
|
|
|
// set the X-Pjax header to Content, so that the whole admin panel will be refreshed |
166
|
|
|
$this->owner->getResponse()->addHeader('X-Pjax', 'Content'); |
167
|
|
|
|
168
|
|
|
return $this->owner->redirect($url); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
View Code Duplication |
public function updateLink(&$link) |
|
|
|
|
172
|
|
|
{ |
173
|
|
|
$locale = $this->owner->Locale ? $this->owner->Locale : Translatable::get_current_locale(); |
174
|
|
|
if ($locale) { |
175
|
|
|
$link = Controller::join_links($link, '?Locale=' . $locale); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
View Code Duplication |
public function updateLinkWithSearch(&$link) |
|
|
|
|
180
|
|
|
{ |
181
|
|
|
$locale = $this->owner->Locale ? $this->owner->Locale : Translatable::get_current_locale(); |
182
|
|
|
if ($locale) { |
183
|
|
|
$link = Controller::join_links($link, '?Locale=' . $locale); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function updateExtraTreeTools(&$html) |
188
|
|
|
{ |
189
|
|
|
$locale = $this->owner->Locale ? $this->owner->Locale : Translatable::get_current_locale(); |
|
|
|
|
190
|
|
|
$html = $this->LangForm()->forTemplate() . $html; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
View Code Duplication |
public function updateLinkPageAdd(&$link) |
|
|
|
|
194
|
|
|
{ |
195
|
|
|
$locale = $this->owner->Locale ? $this->owner->Locale : Translatable::get_current_locale(); |
196
|
|
|
if ($locale) { |
197
|
|
|
$link = Controller::join_links($link, '?Locale=' . $locale); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Returns a form with all languages with languages already used appearing first. |
203
|
|
|
* |
204
|
|
|
* @return Form |
205
|
|
|
*/ |
206
|
|
|
public function LangForm() |
207
|
|
|
{ |
208
|
|
|
$member = Member::currentUser(); //check to see if the current user can switch langs or not |
|
|
|
|
209
|
|
|
if (Permission::checkMember($member, 'VIEW_LANGS')) { |
210
|
|
|
$field = new LanguageDropdownField( |
211
|
|
|
'Locale', |
212
|
|
|
_t('CMSMain.LANGUAGEDROPDOWNLABEL', 'Language'), |
213
|
|
|
array(), |
214
|
|
|
SiteTree::class, |
215
|
|
|
'Locale-English', |
216
|
|
|
singleton(SiteTree::class) |
217
|
|
|
); |
218
|
|
|
$field->setValue(Translatable::get_current_locale()); |
219
|
|
|
} else { |
220
|
|
|
// user doesn't have permission to switch langs |
221
|
|
|
// so just show a string displaying current language |
222
|
|
|
$field = new LiteralField( |
223
|
|
|
'Locale', |
224
|
|
|
i18n::getData()->localeName(Translatable::get_current_locale()) |
225
|
|
|
); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
$form = new Form( |
229
|
|
|
$this->owner, |
230
|
|
|
'LangForm', |
231
|
|
|
new FieldList( |
232
|
|
|
$field |
233
|
|
|
), |
234
|
|
|
new FieldList( |
235
|
|
|
new FormAction('selectlang', _t('CMSMain_left.GO', 'Go')) |
236
|
|
|
) |
237
|
|
|
); |
238
|
|
|
$form->unsetValidator(); |
239
|
|
|
$form->addExtraClass('nostyle'); |
240
|
|
|
|
241
|
|
|
return $form; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
public function selectlang($data, $form) |
|
|
|
|
245
|
|
|
{ |
246
|
|
|
return $this->owner; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Determine if there are more than one languages in our site tree. |
251
|
|
|
* |
252
|
|
|
* @return boolean |
253
|
|
|
*/ |
254
|
|
|
public function MultipleLanguages() |
255
|
|
|
{ |
256
|
|
|
$langs = Translatable::get_existing_content_languages(SiteTree::class); |
257
|
|
|
|
258
|
|
|
return (count($langs) > 1); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @return boolean |
263
|
|
|
*/ |
264
|
|
|
public function IsTranslatableEnabled() |
265
|
|
|
{ |
266
|
|
|
return SiteTree::has_extension(Translatable::class); |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|