-
Notifications
You must be signed in to change notification settings - Fork 12
/
extension.driver.php
412 lines (327 loc) · 12.5 KB
/
extension.driver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<?php
if (!defined('__IN_SYMPHONY__')) {
die('<h2>Symphony Error</h2><p>You cannot directly access this file</p>');
}
require_once(EXTENSIONS . '/frontend_localisation/lib/class.FLang.php');
final class Extension_Frontend_Localisation extends Extension {
private static $assets_loaded = false;
/*------------------------------------------------------------------------------------------------*/
/* Installation */
/*------------------------------------------------------------------------------------------------*/
public function install() {
try {
$this->meetDependencies();
} catch (Exception $e) {
$this->showAlertIfPossible($e);
}
/* Configuration */
Symphony::Configuration()->set('langs', '', 'frontend_localisation');
Symphony::Configuration()->set('main_lang', '', 'frontend_localisation');
Symphony::Configuration()->set('main_reg', '', 'frontend_localisation');
return Symphony::Configuration()->write();
}
public function uninstall() {
/* Configuration */
Symphony::Configuration()->remove('frontend_localisation');
Symphony::Configuration()->write();
return true;
}
public function update($previousVersion = false) {
try {
$this->meetDependencies();
} catch (Exception $e) {
$this->showAlertIfPossible($e);
}
if (version_compare($previousVersion, '1.4', '<')) {
Symphony::Configuration()->remove('fl_driver', 'frontend_localisation');
Symphony::Configuration()->set('lang_codes', '', 'frontend_localisation');
Symphony::Configuration()->set('main_lang', '', 'frontend_localisation');
$ref_lang = Symphony::Configuration()->get('reference_language', 'frontend_localisation');
Symphony::Configuration()->set('ref_lang', $ref_lang, 'frontend_localisation');
$consolidate = Symphony::Configuration()->get('consolidate_translations', 'frontend_localisation');
Symphony::Configuration()->set('consolidate', $consolidate, 'frontend_localisation');
}
if (version_compare($previousVersion, '2.0', '<')) {
General::deleteDirectory(WORKSPACE . Symphony::Configuration()->get('translation_path', 'frontend_localisation'));
try {
Symphony::Database()->query("ALTER TABLE `tbl_pages` DROP `translations`");
} catch (Exception $e) {
// ignore
}
}
if (version_compare($previousVersion, '2.7.0', '<')) {
// This value is deprecated
Symphony::Configuration()->remove('ref_lang', 'frontend_localisation');
// Add this new value, to be able to set a default region
if (Symphony::Configuration()->get('main_reg', 'frontend_localisation') === null) {
Symphony::Configuration()->set('main_reg', '', 'frontend_localisation');
}
}
return Symphony::Configuration()->write();
}
/*------------------------------------------------------------------------------------------------*/
/* Delegates */
/*------------------------------------------------------------------------------------------------*/
public function getSubscribedDelegates() {
return array(
array(
'page' => '/frontend/',
'delegate' => 'FrontendInitialised',
'callback' => 'dFrontendInitialised'
),
array(
'page' => '/frontend/',
'delegate' => 'FrontendParamsPostResolve',
'callback' => 'dFrontendParamsPostResolve'
),
array(
'page' => '/backend/',
'delegate' => 'AdminPagePreBuild',
'callback' => 'dAdminPagePreBuild'
),
array(
'page' => '/system/preferences/',
'delegate' => 'AddCustomPreferenceFieldsets',
'callback' => 'dAddCustomPreferenceFieldsets'
),
array(
'page' => '/system/preferences/',
'delegate' => 'Save',
'callback' => 'dSave'
),
);
}
/*------------------------------------------------------------------------------------------------*/
/* Initialisation */
/*------------------------------------------------------------------------------------------------*/
/**
* Frontend
*/
public function dFrontendInitialised() {
if (!$this->meetDependencies(true)) {
return;
}
$this->_initFLang();
Lang::set(FLang::getLangCode(), false);
}
public function dFrontendParamsPostResolve($context) {
if (!$this->meetDependencies(true)) {
return;
}
if (isset($context['params']['url-fl-language'])) {
unset($context['params']['url-fl-language']);
}
if (isset($context['params']['url-fl-region'])) {
unset($context['params']['url-fl-region']);
}
}
/**
* Backend
*/
public function dAdminPagePreBuild() {
try {
$this->meetDependencies();
} catch (Exception $e) {
$this->showAlertIfPossible($e);
}
$this->_initFLang();
}
private function _initFLang() {
// initialize Language codes
$langs = Symphony::Configuration()->get('langs', 'frontend_localisation');
FLang::setLangs($langs);
// initialize Main language
$main_lang = Symphony::Configuration()->get('main_lang', 'frontend_localisation');
if (!FLang::setMainLang($main_lang)) {
// try the first one
if (!FLang::setMainLang($langs[0])) {
// Use backend as main
FLang::setMainLang(Lang::get());
}
}
// initialize Main region
$main_reg = Symphony::Configuration()->get('main_reg', 'frontend_localisation');
FLang::setMainReg($main_reg);
// read current language
$language = isset($_REQUEST['fl-language']) ? General::sanitize($_REQUEST['fl-language']) : null;
$region = isset($_REQUEST['fl-region']) ? General::sanitize($_REQUEST['fl-region']) : null;
// set language code
if (!FLang::setLangCode($language, $region)) {
// try to set language from Admin
if (class_exists('Administration', false) && Symphony::isLoggedIn()) {
$author = Symphony::Author();
if (!$author || !FLang::setLangCode($author->get('language'))) {
// author language is not supported, use main lang
if (!FLang::setLangCode(FLang::getMainLang())) {
FLang::setLangCode(Lang::get());
}
}
}
// use main lang
else {
FLang::setLangCode(FLang::getMainLang(), FLang::getMainReg());
}
}
}
/*------------------------------------------------------------------------------------------------*/
/* System preferences */
/*------------------------------------------------------------------------------------------------*/
/**
* Display options on Preferences page.
*
* @param array $context
*/
public function dAddCustomPreferenceFieldsets(array $context) {
if (!$this->meetDependencies(true)) {
return;
}
Administration::instance()->Page->addScriptToHead(URL . '/extensions/frontend_localisation/assets/frontend_localisation.preferences.js', null, false);
$group = new XMLElement('fieldset');
$group->setAttribute('class', 'settings');
$group->appendChild(new XMLElement('legend', __('Frontend Localisation')));
$div = new XMLElement('div', null, array('class' => 'two columns'));
$this->_appendLangs($div, $context);
$this->_appendMainLang($div, $context);
$group->appendChild($div);
$context['wrapper']->appendChild($group);
}
/**
* Convenience method; builds language codes input
*
* @param XMLElement &$wrapper
* @param array $context
*/
private function _appendLangs(&$wrapper, $context) {
$label = Widget::Label(__('Site languages'), null, 'column', 'frontend_localisation' . '_langs');
require_once EXTENSIONS . '/languages/extension.driver.php';
$options = Extension_Languages::findOptions(FLang::getLangs());
$label->appendChild(Widget::Select('settings[' . 'frontend_localisation' . '][langs]', $options, array('multiple' => 'multiple')));
$label->appendChild(new XMLElement('p', __('Select languages of the site.'), array('class' => 'help')));
if (isset($context['errors']['frontend_localisation']['langs'])) {
$wrapper->appendChild(Widget::Error($label, $context['errors']['frontend_localisation']['langs']));
}
else {
$wrapper->appendChild($label);
}
}
/**
* Convenience method; builds main language select
*
* @param XMLElement &$wrapper
* @param array $context
*/
private function _appendMainLang(&$wrapper, $context) {
$label = Widget::Label(__('Main language'), null, 'column', 'frontend_localisation' . '_main_lang');
$options = Extension_Languages::findOptions(FLang::getMainLang(), FLang::getLangs());
$label->appendChild(Widget::Select('settings[' . 'frontend_localisation' . '][main_lang]', $options));
$label->appendChild(new XMLElement('p', __('Select the main language of the site.'), array('class' => 'help')));
if (isset($context['errors']['frontend_localisation']['main_lang'])) {
$wrapper->appendChild(Widget::Error($label, $context['errors']['frontend_localisation']['main_lang']));
}
else {
$wrapper->appendChild($label);
}
}
/**
* Save options from Preferences page
*
* @param array $context
*
* @return boolean
*/
public function dSave(array $context) {
$valid = true;
$data = & $context['settings']['frontend_localisation'];
$errors = array();
if (!$this->meetDependencies(true)) {
return $valid;
}
/* Language codes */
$old_langs = FLang::getLangs();
if (!FLang::setLangs($data['langs'])) {
$valid = false;
$errors['langs'] = __('Please fill at least one valid language code.');
}
$new_langs = FLang::getLangs();
unset($data['langs']);
Symphony::Configuration()->set('langs', implode(',', $new_langs), 'frontend_localisation');
/**
* When saving Preferences, supplies the old_languages and new_languages arrays.
*
* @delegate FLSavePreferences
* @since 1.4
*
* @param string $context - '/extensions/frontend_localisation/'
* @param array $context - the original context from @delegate Save
* @param array $old_langs - old language codes
* @param array $new_langs - new language codes
*/
Symphony::ExtensionManager()->notifyMembers('FLSavePreferences', '/extensions/frontend_localisation/', array(
'context' => $context,
'old_lang' => $old_langs,
'new_langs' => $new_langs
));
/* Main language */
if (!FLang::setMainLang($data['main_lang'])) {
if (!empty($old_langs) || !(isset($new_langs[0]) && FLang::setMainLang($new_langs[0]))) {
$valid = false;
$errors['main_lang'] = __('Invalid language code.');
}
}
$main_lang = FLang::getMainLang();
unset($data['main_lang']);
Symphony::Configuration()->set('main_lang', $main_lang, 'frontend_localisation');
Symphony::Configuration()->write();
if (!empty($errors)) {
$context['errors']['frontend_localisation'] = $errors;
}
return $valid;
}
/*------------------------------------------------------------------------------------------------*/
/* Public utilities */
/*------------------------------------------------------------------------------------------------*/
public static function appendAssets() {
if (self::$assets_loaded === false
&& class_exists('Administration')
&& Administration::instance() instanceof Administration
&& Administration::instance()->Page instanceof HTMLPage
) {
self::$assets_loaded = true;
$page = Administration::instance()->Page;
$page->addStylesheetToHead(URL . '/extensions/frontend_localisation/assets/frontend_localisation.multilingual_tabs.css', 'screen', null, false);
$page->addScriptToHead(URL . '/extensions/frontend_localisation/assets/frontend_localisation.multilingual_tabs.js', null, false);
$page->addScriptToHead(URL . '/extensions/frontend_localisation/assets/frontend_localisation.multilingual_tabs_init.js', null, false);
}
}
/**
* Returns true or false if dependencies are met.
*
* @param bool $return_status - if this is set, it will return true or false if dependencies are met. if it is not set, error is thrown
*
* @throws Exception
*
* @return bool
*/
public function meetDependencies($return_status = false) {
// depends on "Languages"
$languages_status = ExtensionManager::fetchStatus(array('handle' => 'languages'));
$languages_status = current($languages_status);
if ($languages_status != EXTENSION_ENABLED) {
if ($return_status) {
return false;
}
else {
throw new Exception('Frontend Localisation depends on Languages extension.');
}
}
return true;
}
private function showAlertIfPossible($e) {
if (class_exists('Administration') && Administration::instance()->Page) {
Administration::instance()->Page->pageAlert($e->getMessage(), Alert::ERROR);
}
else {
Symphony::Log()->pushExceptionToLog($e, true);
}
}
}