/[drupal]/contributions/modules/localizer/localizer.module
ViewVC logotype

Contents of /contributions/modules/localizer/localizer.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.16 - (show annotations) (download) (as text)
Sat Feb 2 14:47:10 2008 UTC (21 months, 3 weeks ago) by robertogerola
Branch: MAIN
CVS Tags: DRUPAL-5--3-0, HEAD
Changes since 1.15: +2 -3 lines
File MIME type: text/x-php
Fixed bug in user form redirect
1 <?php
2 /**
3 * Localizer module.
4 * @author Roberto Gerola, 2006, http://www.speedtech.it
5 */
6
7 if(function_exists('drupal_get_path')) {
8 include_once(drupal_get_path('module', 'localizer') . '/models/localizertranslation.php');
9 include_once(drupal_get_path('module', 'localizer') . '/includes/localizer.inc');
10 }
11
12 function localizer_perm() {
13 $perm[] = 'Localizer strings: administer translations';
14 $perm[] = 'Localizer strings: delete translations';
15 $perm[] = 'Localizer content: administer translations';
16 $perm[] = 'Localizer content: access translations';
17 $perm[] = 'Localizer content: create translations';
18 $perm[] = 'Localizer content: disconnect translations';
19 $perm[] = 'Localizer content: view all available translations';
20 $perm[] = 'Localizer content: view own available translations';
21 $perm[] = 'Localizer translation management: all';
22
23 foreach(localizer_supported_languages_all() as $l=>$n) {
24 $perm[] = 'Localizer translation management: ' . $n;
25 }
26 return $perm;
27 }
28
29 function _localizer_protect_directoryandfile($directoryname, $filename) {
30 $r = TRUE;
31 if (!drupal_verify_install_file($directoryname, FILE_EXIST|FILE_READABLE|FILE_NOT_WRITABLE|FILE_EXECUTABLE)) {
32 drupal_set_message(st('All necessary changes to %directory have been made, so you should now remove write permissions to this directory. Failure to remove write permissions to this directory is a security risk.', array('%directory' => $directoryname)), 'error');
33 $r = FALSE;
34 }
35 if (!drupal_verify_install_file($filename, FILE_EXIST|FILE_READABLE|FILE_NOT_WRITABLE)) {
36 drupal_set_message(st('All necessary changes to %file have been made, so you should now remove write permissions to this file. Failure to remove write permissions to this file is a security risk.', array('%file' => $filename)), 'error');
37 $r = FALSE;
38 }
39 return $r;
40 }
41
42 function localizer_enable() {
43 $dirname = 'sites/all/modules/localizer/taxonomy';
44 $fileorig = $dirname . '/taxonomy.module.off';
45 $filedest = $dirname . '/taxonomy.module';
46 if(file_exists($fileorig) && !file_exists($filedest)) {
47 if(!is_writeable($fileorig)) {
48 $msg = t('Localizer : to complete the module activation you should rename the file %fileorig to %filedest or
49 grant writing privileges to the directory %dirname and to the file %fileorig and disabling and enabling the module again.', array('%dirname'=>$dirname, '%fileorig'=>$fileorig, '%filedest'=>$filedest));
50 drupal_set_message($msg, 'error');
51 } else {
52 rename($fileorig, $filedest);
53 if(_localizer_protect_directoryandfile($dirname, $filedest))
54 {
55 drupal_set_message(st('Localizer: all necessary changes to %file have been made. It has been set to read-only for security.', array('%file' => $filedest)));
56 }
57 }
58 }
59 $dirname = 'sites/all/modules/localizer/menu';
60 $fileorig = $dirname . '/menu.module.off';
61 $filedest = $dirname . '/menu.module';
62 if(file_exists($fileorig) && !file_exists($filedest)) {
63 if(!is_writeable($fileorig)) {
64 $msg = t('Localizer : to complete the module activation you should rename the file %fileorig to %filedest or
65 grant writing privileges to the directory %dirname and to the file %fileorig and disabling and enabling the module again.', array('%dirname'=>$dirname, '%fileorig'=>$fileorig, '%filedest'=>$filedest));
66 drupal_set_message($msg, 'error');
67 } else {
68 rename($fileorig, $filedest);
69 if(_localizer_protect_directoryandfile($dirname, $filedest))
70 {
71 drupal_set_message(t('Localizer: all necessary changes to %file have been made. It has been set to read-only for security.', array('%file' => $filedest)));
72 }
73 }
74 }
75 $msg = t('Localizer: to complete the module activation you need now to append this line %line at the end of your settings.php file, before the closure tag.', array('%line'=>"include_once('sites/all/modules/localizer/localizer_settings.php');"));
76 drupal_set_message($msg, 'status');
77 }
78
79 function localizer_disable() {
80 $dirname = 'sites/all/modules/localizer/taxonomy';
81 $fileorig = $dirname . '/taxonomy.module';
82 $filedest = $dirname . '/taxonomy.module.off';
83 if(file_exists($fileorig) && !file_exists($filedest)) {
84 if(!is_writeable($fileorig)) {
85 $msg = t('Localizer : to complete the module deactivation you should rename the file %fileorig to %filedest or
86 grant writing privileges to the directory %dirname and to the file %fileorig and disabling and enabling the module again.', array('%dirname'=>$dirname, '%fileorig'=>$fileorig, '%filedest'=>$filedest));
87 drupal_set_message($msg, 'error');
88 } else {
89 rename($fileorig, $filedest);
90 if(_localizer_protect_directoryandfile($dirname, $filedest))
91 {
92 drupal_set_message(t('Localizer: all necessary changes to %file have been made. It has been set to read-only for security.', array('%file' => $filedest)));
93 }
94 }
95 }
96 $dirname = 'sites/all/modules/localizer/menu';
97 $fileorig = $dirname . '/menu.module';
98 $filedest = $dirname . '/menu.module.off';
99 if(file_exists($fileorig) && !file_exists($filedest)) {
100 if(!is_writeable($fileorig)) {
101 $msg = t('Localizer : to complete the module deactivation you should rename the file %fileorig to %filedest or
102 grant writing privileges to the directory %dirname and to the file %fileorig and disabling and enabling the module again.', array('%dirname'=>$dirname, '%fileorig'=>$fileorig, '%filedest'=>$filedest));
103 drupal_set_message($msg, 'error');
104 } else {
105 rename($fileorig, $filedest);
106 if(_localizer_protect_directoryandfile($dirname, $filedest))
107 {
108 drupal_set_message(t('Localizer: all necessary changes to %file have been made. It has been set to read-only for security.', array('%file' => $filedest)));
109 }
110 }
111 }
112 }
113
114 function _localizer_access_translation($language) {
115 if(user_access('Localizer translation management: all')) return TRUE;
116 if(user_access('Localizer translation management: ' . $language)) return TRUE;
117 return FALSE;
118 }
119
120 function localizer_init() {
121 if(!function_exists('localizer_supported_languages')) {
122 function localizer_supported_languages() {
123 static $languages;
124 if(!isset($languages)) {
125 if (function_exists('locale')) {
126 $languages = locale_supported_languages();
127 $languages = $languages['name'];
128 }
129 else {
130 $languages = array('en' => 'English');
131 }
132 }
133 return $languages;
134 }
135 }
136
137 if(!function_exists('localizer_get_default_language')) {
138 function localizer_get_default_language() {
139 return key(localizer_supported_languages());
140 }
141 }
142
143 global $__localizer_default_language;
144
145 $__localizer_default_language = localizer_get_default_language();
146 }
147
148 /**
149 * Set the initial language in the order : session, user preferences or browser detect
150 */
151 if (module_exists('localizer')) {
152 function i18n_get_lang() {
153 global $user;
154
155 if (isset($_SESSION['current_locale'])) {
156 localizer_set_language($_SESSION['current_locale']);
157 }
158 else if ($user->uid) {
159 localizer_set_language($user->language);
160 }
161 else if (variable_get('localizer_detect_browser', TRUE)) {
162 localizer_set_language(localizer_get_browser_lang());
163 }
164 else {
165 localizer_set_language(localizer_get_default_language());
166 }
167 return localizer_get_language();
168 }
169 }
170
171 function localizer_form_alter($form_id, &$form) {
172 if($form_id=='user_login' || $form_id=='user_login_block') {
173 $path = localizer_path_without_language($_GET['q']);
174
175 $destination = drupal_get_destination();
176 if($destination) $destination = localizer_path_without_language($destination);
177 if(array_key_exists('#action', $form)) {
178 $form['#action'] = url($path, $destination);
179 }
180 }
181 }
182
183 function localizer_menu($may_cache) {
184 $items = array();
185 $supported_languages = localizer_supported_languages();
186
187 if(!$may_cache) {
188 $items[] = array(
189 'path' => 'admin/settings/localizer',
190 'title' => t('Localizer'),
191 'description' => t('Configure multilingual support'),
192 'callback' => 'drupal_get_form',
193 'callback arguments' => 'localizer_admin_settings',
194 'access' => user_access('administer site configuration'),
195 'type' => MENU_NORMAL_ITEM);
196
197 $items[] = array(
198 'path' => 'admin/content/localizer/strings',
199 'title' => t('Localizer strings translation'),
200 'description' => t('Manage Localizer strings translations'),
201 'callback' => 'localizer_strings',
202 'access' => user_access('Localizer strings: administer translations'),
203 'weight' => 18,
204 'type' => MENU_NORMAL_ITEM);
205
206 $items[] = array(
207 'path' => 'admin/content/localizer/strings/delete',
208 'title' => t('Delete the translation string'),
209 'callback' => 'localizer_strings_delete',
210 'access' => user_access('Localizer strings: delete translations'),
211 'type' => MENU_CALLBACK,
212 );
213
214 }
215 localizer_change_language();
216 return $items;
217 }
218
219 function localizer_strings() {
220 $o = drupal_get_form('localizer_strings_find');
221 $o .= drupal_get_form('localizer_strings_list');
222
223 print theme('page', $o);
224 }
225
226 function localizer_strings_list() {
227 $languages = localizer_supported_languages();
228 $language_translations = $_SESSION['localizer_strings_find']['language_translations'];
229
230 $header = array(
231 array (
232 'data' => t('Language'),
233 'field' => 'l.language'),
234 array (
235 'data' => t('Text'),
236 'field' => 'l.translation'),
237 array (
238 'data' => t('Operations')),
239 );
240
241 $cond = '';
242 $language = $_SESSION['localizer_strings_find']['language']? $_SESSION['localizer_strings_find']['language'] : localizer_get_language();
243 if($language) {
244 if($cond) $cond .= ' AND ';
245 $cond .= "language='" . $language ."'";
246 }
247
248 $translations_exist = $_SESSION['localizer_strings_find']['translations_exist'];
249 if($translations_exist && is_array($translations_exist)) {
250 if($cond) $cond .= ' AND ';
251 $l_string = implode($translations_exist, "','");
252 $count = sizeof($translations_exist);
253 $cond .= "(SELECT COUNT(l1.tid) FROM {localizertranslation} l1 WHERE l1.object_name=l.object_name AND l1.object_key=l.object_key AND l1.object_field=l.object_field AND l1.language IN('" . $l_string . "') AND l1.translation <> '') = " . $count;
254 }
255
256 $translations_not_exist = $_SESSION['localizer_strings_find']['translations_not_exist'];
257 if($translations_not_exist && is_array($translations_not_exist)) {
258 if($cond) $cond .= ' AND ';
259 $l_string = implode($translations_not_exist, "','");
260 $cond .= "(SELECT COUNT(l1.tid) FROM {localizertranslation} l1 WHERE l1.object_name=l.object_name AND l1.object_key=l.object_key AND l1.object_field=l.object_field AND l1.language IN('" . $l_string . "') AND l1.translation <> '') = 0";
261 }
262
263 $translation = $_SESSION['localizer_strings_find']['translation'];
264 if($translation) {
265 if($cond) $cond .= ' AND ';
266 $cond .= "LOWER(translation) LIKE LOWER('" . $translation . "')";
267 }
268
269 $s = "SELECT * FROM {localizertranslation} l";
270 if($cond) $s .= ' WHERE ' . $cond;
271 $tablesort = tablesort_sql($header);
272 $r = pager_query($s, $_SESSION['localizer_strings_find']['results_per_page'], 0, NULL);
273 $rows = array();
274 while($item = db_fetch_array($r)) {
275 $form['items'][$item['tid']]['language'] = array (
276 '#type' => 'checkbox',
277 '#title' => $languages[$item['language']],
278 );
279
280 $form['items'][$item['tid']]['translation'] = array (
281 '#value' => $item['translation'],
282 );
283
284 if(_localizer_access_translation($languages[$item['language']]) && user_access('Localizer strings: delete translations')) {
285 $form['items'][$item['tid']]['operations'] = array (
286 '#value' => l(t('Delete'), 'admin/content/localizer/strings/delete/' . $item['tid'], array(), 'destination=admin/content/localizer/strings'),
287 );
288 }
289
290 $translations = array();
291 $ts = "SELECT tid, translation, language FROM {localizertranslation} WHERE object_key='%s' AND object_name='%s' AND object_field='%s'";
292 $tr = db_query($ts, $item['object_key'], $item['object_name'], $item['object_field']);
293 while($t = db_fetch_array($tr)) {
294 $translations[$t['language']]['translation'] = $t['translation'];
295 $translations[$t['language']]['tid'] = $t['tid'];
296 }
297
298 foreach($languages as $l=>$n) {
299 if($l == $item['language']) continue;
300 if(sizeof($language_translations)>0 && !array_key_exists($l, $language_translations)) continue;
301 if(array_key_exists($l, $translations)) {
302 $translation = $translations[$l]['translation'];
303 }
304 else {
305 $translation = $item['translation'];
306 }
307 $key = 'translationfor|' . $item['object_key'] . '|' . $item['object_name'] . '|' . $item['object_field'] . '|' . $l;
308 $form['items'][$item['tid']]['translations']['language'][$key] = array (
309 '#value' => $n,
310 );
311
312 if(_localizer_access_translation($n)) {
313 $form['items'][$item['tid']]['translations']['translation'][$key] = array (
314 '#type' => 'textarea',
315 '#default_value' => $translation,
316 );
317 if(user_access('Localizer strings: delete translations')) {
318 $form['items'][$item['tid']]['translations']['operations'][$key] = array (
319 '#value' => l(t('Delete'), 'admin/content/localizer/strings/delete/' . $translations[$l]['tid'] , array(), 'destination=admin/content/localizer/strings'),
320 );
321 }
322 }
323 else {
324 $form['items'][$item['tid']]['translations']['translation'][$key] = array (
325 '#value' => $translation,
326 );
327 }
328 }
329 }
330
331 $form['submit'] = array(
332 '#type' => 'submit',
333 '#value' => t('Save')
334 );
335
336 $form['pager'] = array('#value' => theme('pager', NULL, $_SESSION['localizer_strings_find']['results_per_page'], 0));
337 $form['header'] = array('#value' => $header);
338 return $form;
339 }
340
341 function theme_localizer_strings_list($form) {
342 drupal_add_js('misc/collapse.js', 'core', 'header');
343 $pre = '<fieldset class="collapsible collapsed"><legend>' . t('Translations') . '</legend>';
344 $suf = '</fieldset>';
345
346 $header = $form['header']['#value'];
347 unset($form['header']);
348
349 $rows = array();
350 foreach(element_children($form['items']) as $tid) {
351 $rows[] = array (
352 drupal_render($form['items'][$tid]['language']),
353 drupal_render($form['items'][$tid]['translation']),
354 drupal_render($form['items'][$tid]['operations']),
355 );
356 $trows = array();
357 foreach(element_children($form['items'][$tid]['translations']['language']) as $key) {
358 $trows[] = array (
359 drupal_render($form['items'][$tid]['translations']['language'][$key]),
360 drupal_render($form['items'][$tid]['translations']['translation'][$key]),
361 drupal_render($form['items'][$tid]['translations']['operations'][$key]),
362 );
363 }
364
365 $rows[] = array (
366 array (
367 'data' => $pre . theme('table', array(), $trows) . $suf,
368 'colspan' => 3,
369 ),
370 );
371 }
372
373 $o = theme('table', $header, $rows);
374 $o .= drupal_render($form['pager']);
375 $o .= drupal_render($form);
376 return $o;
377 }
378
379 function localizer_strings_list_submit($form_id, $form_values) {
380 if ($form_values['op'] == t('Save')) {
381 $supported_languages = localizer_supported_languages();
382 foreach($form_values as $key=>$value) {
383 if(substr($key, 0, 14)=='translationfor') {
384 $translationfor = explode('|', $key);
385 $t = array();
386 $t['object_key'] = $translationfor[1];
387 $t['object_name'] = $translationfor[2];
388 $t['object_field'] = $translationfor[3];
389 $t['language'] = $translationfor[4];
390 $t['translation'] = $value;
391 if(localizertranslation_save($t)) {
392 drupal_set_message(t($supported_languages[$t['language']] . ' translation successfully saved.'));
393 }
394 }
395 }
396 }
397 }
398
399 function localizer_strings_find() {
400 $form = array();
401
402 $form['language'] = array(
403 '#type' => 'select',
404 '#title' => t('Language'),
405 '#default_value' => $_SESSION['localizer_strings_find']['language']? $_SESSION['localizer_strings_find']['language'] : localizer_get_language(),
406 '#options' => localizer_supported_languages(),
407 );
408
409 $form['language_translations'] = array(
410 '#type' => 'select',
411 '#size' => 5,
412 '#multiple' => true,
413 '#title' => t('Language translations'),
414 '#description' => t('No selection means all.<br />Select/deselect criteria by clicking on the item while holding ctrl-key.'),
415 '#default_value' => $_SESSION['localizer_strings_find']['language_translations'],
416 '#options' => localizer_supported_languages(),
417 );
418
419 $form['translations_exist'] = array(
420 '#type' => 'select',
421 '#size' => 5,
422 '#multiple' => true,
423 '#title' => t('With language translations'),
424 '#description' => t('No selection means not apply.<br />Select/deselect criteria by clicking on the item while holding ctrl-key.'),
425 '#default_value' => $_SESSION['localizer_strings_find']['translations_exist'],
426 '#options' => localizer_supported_languages(),
427 );
428
429 $form['translations_not_exist'] = array(
430 '#type' => 'select',
431 '#size' => 5,
432 '#multiple' => true,
433 '#title' => t('Without language translations'),
434 '#description' => t('No selection means not apply.<br />Select/deselect criteria by clicking on the item while holding ctrl-key.'),
435 '#default_value' => $_SESSION['localizer_strings_find']['translations_not_exist'],
436 '#options' => localizer_supported_languages(),
437 );
438
439 $form['translation'] = array(
440 '#type' => 'textfield',
441 '#title' => t('Translation'),
442 '#default_value' => $_SESSION['localizer_strings_find']['translation'],
443 );
444
445 if(!$_SESSION['localizer_strings_find']['results_per_page']) {
446 $_SESSION['localizer_strings_find']['results_per_page'] = 5;
447 }
448 $form['results_per_page'] = array(
449 '#type' => 'textfield',
450 '#size' => 5,
451 '#title' => t('Results per page'),
452 '#default_value' => $_SESSION['localizer_strings_find']['results_per_page'],
453 );
454
455 $form['submit'] = array(
456 '#type' => 'submit',
457 '#value' => t('Search')
458 );
459
460 return $form;
461 }
462
463 function localizer_strings_find_submit($form_id, $form_values) {
464 if ($form_values['op'] == t('Search')) {
465 $_SESSION['localizer_strings_find']['language'] = $form_values['language'];
466 $_SESSION['localizer_strings_find']['language_translations'] = $form_values['language_translations'];
467 $_SESSION['localizer_strings_find']['translation'] = $form_values['translation'];
468 $_SESSION['localizer_strings_find']['translations_exist'] = $form_values['translations_exist'];
469 $_SESSION['localizer_strings_find']['translations_not_exist'] = $form_values['translations_not_exist'];
470 $_SESSION['localizer_strings_find']['results_per_page'] = $form_values['results_per_page'];
471 return 'admin/content/localizer/strings';
472 }
473 }
474
475 function theme_localizer_strings_find($form) {
476 drupal_add_js('misc/collapse.js', 'core', 'header');
477 $o = '';
478 $o .= '<fieldset class="collapsible collapsed"><legend>' . t('Search') . '</legend>';
479 $o .= '<table><tbody style="border: none;">';
480 $o .= '<tr>';
481 $o .= '<td>' . drupal_render($form['language']) . '</td>';
482 $o .= '<td>' . drupal_render($form['language_translations']) . '</td>';
483 $o .= '<tr>';
484 $o .= '<tr>';
485 $o .= '<td>' . drupal_render($form['translations_exist']) . '</td>';
486 $o .= '<td>' . drupal_render($form['translations_not_exist']) . '</td>';
487 $o .= '</tr>';
488 $o .= '<tr>';
489 $o .= '<td colspan="2">' . drupal_render($form['translation']) . '</td>';
490 $o .= '</tr>';
491 $o .= '<tr>';
492 $o .= '<td>' . drupal_render($form['submit']) . '</td>';
493 $o .= '<td>' . drupal_render($form['results_per_page']) . '</td>';
494 $o .= '</tr>';
495 $o .= '</tbody></table>';
496 $o .= drupal_render($form);
497 $o .= '</fieldset';
498 return $o;
499 }
500
501 function localizer_strings_delete($tid) {
502 if($tid) {
503 localizertranslation_delete($tid);
504 drupal_set_message('Translation successfully deleted');
505 drupal_goto();
506 }
507 }
508
509 function localizer_admin_settings() {
510 $languages = localizer_supported_languages();
511
512 $form['languageswitchblock'] = array(
513 '#type' => 'fieldset',
514 '#title' => t('Language switching block'),
515 '#collapsible' => TRUE,
516 '#collapsed' => TRUE,
517 '#weight' => -18,
518 );
519
520 $form['languageswitchblock']['localizer_switchblock_hidecurrentlanguage'] = array (
521 '#type' => 'checkbox',
522 '#title' => t('Hide the current language'),
523 '#default_value' => variable_get('localizer_switchblock_hidecurrentlanguage', TRUE),
524 );
525
526 $form['languageswitchblock']['localizer_switchblock_showlangname'] = array (
527 '#type' => 'checkbox',
528 '#title' => t('Show the language name'),
529 '#default_value' => variable_get('localizer_switchblock_showlangname', TRUE),
530 );
531
532 $form['languageswitchblock']['localizer_switchblock_showflags'] = array (
533 '#type' => 'checkbox',
534 '#title' => t('Show flag'),
535 '#default_value' => variable_get('localizer_switchblock_showflags', TRUE),
536 );
537
538 $form['languageswitchblock']['localizer_switchblock_flagseparator'] = array (
539 '#type' => 'textfield',
540 '#title' => t('Flag and language name separator'),
541 '#default_value' => variable_get('localizer_switchblock_flagseparator', ' '),
542 '#size' => 5,
543 '#maxlength' => 10,
544 '#description' => t('Token separating the flag and the language name.'),
545 );
546
547 $form['languageswitchblock']['localizer_switchblock_flagspath'] = array (
548 '#type' => 'textfield',
549 '#title' => t('Flag icons path'),
550 '#default_value' => variable_get('localizer_switchblock_flagspath', drupal_get_path('module', 'localizer') . '/flags/*.png'),
551 '#size' => 50,
552 '#maxlength' => 180,
553 '#description' => t('Path for flag icons, relative to Drupal installation. \'*\' is a placeholder for language code.'),
554 );
555
556 $form['languageswitchblock']['localizer_switchblock_flagsize'] = array (
557 '#type' => 'textfield',
558 '#title' => t('Flag icons size'),
559 '#default_value' => variable_get('localizer_switchblock_flagsize', '16x11'),
560 '#size' => 10,
561 '#maxlength' => 10,
562 '#description' => t('Image size for flags, in the form "width x height".'),
563 );
564
565 $form['languagedetect'] = array(
566 '#type' => 'fieldset',
567 '#title' => t('Language initial detection options'),
568 '#collapsible' => TRUE,
569 '#collapsed' => TRUE,
570 '#weight' => -17,
571 );
572
573 $form['languagedetect']['localizer_detect_browser'] = array (
574 '#type' => 'checkbox',
575 '#title' => t("Detect through browser's language"),
576 '#default_value' => variable_get('localizer_detect_browser', TRUE),
577 '#weight' => -18,
578 );
579
580 $form['languageswitch'] = array(
581 '#type' => 'fieldset',
582 '#title' => t('Language switching options'),
583 '#collapsible' => TRUE,
584 '#collapsed' => TRUE,
585 '#weight' => -16,
586 );
587
588 $form['languageswitch']['localizer_frontpage_redirect'] = array (
589 '#type' => 'checkbox',
590 '#title' => t("Redirect front page to the localized version"),
591 '#default_value' => variable_get('localizer_frontpage_redirect', TRUE),
592 '#weight' => -19,
593 );
594
595 $form['languageswitch']['byhostname'] = array(
596 '#type' => 'fieldset',
597 '#title' => t('By hostname'),
598 '#collapsible' => FALSE,
599 '#collapsed' => FALSE,
600 '#weight' => -15,
601 );
602 $form['languageswitch']['byhostname']['localizer_switch_byhostname'] = array (
603 '#type' => 'checkbox',
604 '#title' => t('Switch by hostname'),
605 '#default_value' => variable_get('localizer_switch_byhostname', FALSE),
606 '#description' => t('If this option is active all the other switching options will be ignored.'),
607 );
608 foreach ($languages as $l => $n) {
609 $form['languageswitch']['byhostname']['localizer_switch_hostname_' . $l] = array(
610 '#type' => 'textfield',
611 '#default_value' => variable_get('localizer_switch_hostname_' . $l , $l . '.' . localizer_get_domain()),
612 '#title' => t('@hostname hostname', array('@hostname' => $n)),
613 );
614 }
615
616 $form['content'] = array(
617 '#type' => 'fieldset',
618 '#title' => t('Content settings'),
619 '#collapsible' => TRUE,
620 '#collapsed' => TRUE,
621 '#weight' => -12,
622 );
623
624 $form['content']['localizer_contents_types'] = array(
625 '#type' => 'checkboxes',
626 '#title' => t('Content types'),
627 '#default_value' => variable_get('localizer_contents_types', array('page'=>'page', 'story'=>'story')),
628 '#options' => node_get_types('names'),
629 '#weight' => -18,
630 '#description' => t('Localizer support is enabled for each content type checked.')
631 );
632
633 $form['content']['localizer_fallback_support'] = array (
634 '#type' => 'checkbox',
635 '#title' => t("Activate content language fallback support"),
636 '#default_value' => variable_get('localizer_fallback_support', TRUE),
637 '#weight' => -17,
638 );
639
640 $form['content']['fallback'] = array(
641 '#type' => 'fieldset',
642 '#title' => t('Fallback languages order'),
643 '#collapsible' => TRUE,
644 '#collapsed' => TRUE,
645 '#weight' => -12,
646 );
647
648 foreach($languages as $l=>$n) {
649 $form['content']['fallback']['languages']["localizer_language_fallback_weight_" . $l] = array (
650 '#prefix' => '<div class="container-inline">',
651 '#title' => $n,
652 '#default_value' => variable_get("localizer_language_fallback_weight_" . $l,0),
653 '#type' => 'weight',
654 '#suffix' => '</div>',
655 );
656 }
657
658 $form['externalmodulessupport'] = array(
659 '#type' => 'fieldset',
660 '#title' => t('Support for external modules'),
661 '#collapsible' => TRUE,
662 '#collapsed' => TRUE,
663 '#weight' => -11,
664 );
665
666 $form['externalmodulessupport']['localizer_global_viewsupport'] = array (
667 '#type' => 'checkbox',
668 '#title' => t('Views global support'),
669 '#default_value' => variable_get('localizer_global_viewsupport', TRUE),
670 '#weight' => -18,
671 '#description' => t('Filter all views contents by the current language')
672 );
673
674 $form['externalmodulessupport']['localizer_search_all_languages'] = array (
675 '#type' => 'checkbox',
676 '#title' => t('Searches return results in all languages'),
677 '#default_value' => variable_get('localizer_search_all_languages', FALSE),
678 '#weight' => -19,
679 '#description' => 'If enabled, searches will include results from all languages. If disabled (default) only results in the current language will be returned.'
680 );
681
682 return system_settings_form($form);
683 }
684
685 /**
686 * Generate HTML for the localizer block
687 * @param op the operation from the URL
688 * @param delta offset
689 * @returns block HTML
690 */
691 function localizer_block($op = 'list', $delta = 0, $edit = array()) {
692 switch ($op) {
693 case 'list':
694 $block[0]['info'] = t('Language');
695 break;
696 case 'view':
697 if($delta == 0) {
698 $block['subject'] = t('Language');
699 $block['content'] = localizer_block_switch_language();
700 }
701 break;
702 }
703 return $block;
704 }
705
706 /**
707 * Generate HTML for language change
708 * @out prints HTML code on standard output
709 * @returns HTML code
710 */
711 function localizer_block_switch_language($out=FALSE) {
712 $output = theme('localizer_block_switch_language', localizer_block_switch_language_links(variable_get('localizer_switchblock_showflags', TRUE), variable_get('localizer_switchblock_showlangname',TRUE), variable_get('localizer_switchblock_flagseparator', ' ')));
713 if($out) {
714 echo $output;
715 }
716 else {
717 return $output;
718 }
719 }
720
721 /**
722 * Create the links for the switching language block
723 * @param showflags shows the languages flags
724 * @param shownames shows the languages names
725 * @param separator text to use as separator between the flag and the name
726 * @returns links in HTML format
727 */
728 function localizer_block_switch_language_links($showflags=TRUE, $shownames=TRUE, $separator = ' ', $setcurrentactive=FALSE) {
729 global $base_path;
730 $languages = localizer_supported_languages();
731
732 foreach ($languages as $l => $n) {
733 if(variable_get('localizer_switchblock_hidecurrentlanguage', TRUE) && $l == localizer_get_language()) continue;
734
735 $langname_title = $shownames ? $n: '';
736 $flag = $showflags ? localizer_flag($l, 'localizer_switchblock',array('alt' => $n)) : '' ;
737 $separator = ($langname_title != '' && $flag != '') ? $separator : '';
738
739 if(variable_get('localizer_switch_byhostname', FALSE)) {
740 global $base_path;
741 $host = variable_get('localizer_switch_hostname_' . $l, $l . '.' . localizer_get_domain());
742 $path = localizer_get_localized_path($_GET['q'], $l);
743 if(variable_get('clean_url', 0)) {
744 $url = 'http://' . $host . $base_path . $path;
745 $query = NULL;
746 }
747 else {
748 $url = 'http://' . $host . $base_path;
749 $query = 'q=' . $path;
750 }
751 }
752 else
753 {
754 $path = localizer_path_without_language($_GET['q']);
755 if(localizer_exists_localized_content($path, $l)) {
756 $path = $l . '/' . localizer_get_localized_path($path, $l);
757 }
758 else {
759 $path = $l . '/' . localizer_path_without_language($_REQUEST['q']);
760 }
761 if(variable_get('clean_url', 0)) {
762 $url = $base_path . $path;
763 $query = NULL;
764 }
765 else {
766 $url = $base_path;
767 $query = $path;
768 }
769 }
770 $lpath = $url;
771 if($query) {
772 $lpath .= '?q=' . $query;
773 }
774 if($l==localizer_get_language() && $setcurrentactive) {
775 $links[$l]= '<a href="' . $lpath . '" class="currentuilanguage active" >' . $flag . $separator . $langname_title . '</a>';
776 }
777 else {
778 $links[$l]= '<a href="' . $lpath . '" class="uilanguage" >' . $flag . $separator . $langname_title . '</a>';
779 }
780 }
781 return $links;
782 }
783
784 /**
785 * Create the HTML a language's flag
786 * @param lang the language of the flag
787 * @param attribs additional attributes
788 * @returns HTML code
789 */
790 function localizer_flag($lang, $block='', $attribs = array()) {
791 if ($path = variable_get($block . '_flagspath', drupal_get_path('module', 'localizer') . '/flags/*.png')) {
792 $src = base_path() . str_replace('*', $lang, $path);
793 list($width, $height) = explode('x', variable_get($block . '_flagsize', '16x11'));
794 $attribs = array_merge(array('class' => 'localizer-flag', 'width' => $width, 'height' => $height, 'alt' => $lang), $attribs);
795 $html = "<img src=\"$src\" ".drupal_attributes($attribs)." />";
796 return $html;
797 }
798 }
799
800 function localizer_change_language() {
801 $valid_languages = localizer_supported_languages();
802 $session_support = array_key_exists('current_locale', $_SESSION);
803
804 //first page redirect
805 //If the page is accessed by a SE, do not apply the frontpage redirect
806 if (variable_get('localizer_frontpage_redirect', TRUE) && drupal_is_front_page()) {
807 if (variable_get('localizer_switch_byhostname', FALSE)) {
808 $currenthost = $_SERVER['HTTP_HOST'];
809 $languagebyhostname = localizer_language_by_hostname($currenthost);
810 if ((localizer_get_language() != $languagebyhostname) && ($languagebyhostname != '')) {
811 localizer_set_language($languagebyhostname);
812 global $base_path;
813 $url = 'http://' . $currenthost . $base_path;
814 $destination = localizer_get_localized_path($_GET['q'], $languagebyhostname);
815 $url .= drupal_lookup_path('alias', $destination);
816 if($session_support) {
817 drupal_goto($url);
818 }
819 }
820 }
821 else {
822 $language_page = localizer_language_in_path($_REQUEST['q']);
823 $path = localizer_path_without_language($_GET['q']);
824 if(!$language_page) {
825 $destination = localizer_get_localized_path($path, localizer_get_language());
826 if($session_support) {
827 drupal_goto($destination);
828 }
829 }
830 else if($language_page != localizer_get_language()){
831 localizer_set_language($language_page);
832 $destination = localizer_get_localized_path($path, localizer_get_language());
833 if($session_support) {
834 drupal_goto($destination);
835 }
836 }
837 else if(localizer_isvalid_language($_REQUEST['q'])) {
838 localizer_set_language($_REQUEST['q']);
839 $destination = localizer_get_localized_path(variable_get('site_frontpage', 'node'), localizer_get_language());
840 if($session_support) {
841 drupal_goto($destination);
842 }
843 }
844 }
845 return;
846 }
847
848 //language in hostname
849 if(variable_get('localizer_switch_byhostname', FALSE)) {
850 $currenthost = $_SERVER['HTTP_HOST'];
851 $languagebyhostname = localizer_language_by_hostname($currenthost);
852 if((localizer_get_language() != $languagebyhostname) && ($languagebyhostname != '')) {
853 localizer_set_language($languagebyhostname);
854 if($session_support) {
855 drupal_goto(localizer_get_localized_path(localizer_path_without_language($_GET['q']), $languagebyhostname));
856 }
857 }
858 return;
859 }
860
861 $language_in_path = localizer_language_in_path($_REQUEST['q']);
862 if((localizer_get_language() != $language_in_path) && localizer_isvalid_language($language_in_path)) {
863 localizer_set_language($language_in_path);
864 $query = NULL;
865 if(isset($_REQUEST['destination'])) {
866 $query = 'destination=' . $_REQUEST['destination'];
867 unset($_REQUEST['destination']);
868 }
869 if($session_support) {
870 drupal_goto(localizer_path_without_language($_REQUEST['q']), $query);
871 }
872 }
873 }
874
875 function theme_localizer_block_switch_language($links)
876 {
877 return theme('item_list', $links);
878 }
879
880 /**
881 * Return the language contained in the path
882 */
883 function localizer_language_in_path($path) {
884 $exploded_path = explode('/', $path);
885 $languageinpath = $exploded_path[0];
886 if(localizer_isvalid_language($languageinpath)){
887 return $languageinpath;
888 }
889 else
890 {
891 return '';
892 }
893 }
894
895 function localizer_language_by_hostname($hostname) {
896 $languagebyhostname = '';
897 foreach (localizer_supported_languages() as $lang => $language) {
898 $localizer_hostname = variable_get('localizer_switch_hostname_' . $lang, $lang . '.' . localizer_get_domain());
899 if($hostname == $localizer_hostname) {
900 $languagebyhostname = $lang;
901 break;
902 }
903 }
904 return $languagebyhostname;
905 }
906
907 function localizer_hostname_without_language($host) {
908 $exploded_host = explode('.', $host);
909 $languageinhost = $exploded_host[0];
910 if(localizer_isvalid_language($languageinhost) || ($languageinhost == 'www')){
911 array_shift($exploded_host);
912 return implode(".", $exploded_host);
913 }
914 else
915 {
916 return $host;
917 }
918 }
919
920 function localizer_get_host() {
921 static $host;
922
923 if(!isset($host)) {
924 global $base_url;
925 $parts = parse_url($base_url);
926 $host = ($parts['host']);
927 }
928 return $host;
929 }
930
931 function localizer_get_domain() {
932 static $domain;
933
934 if(!isset($domain)) {
935 $host_parts = explode('.', localizer_get_host());
936 $host_parts = array_reverse($host_parts);
937 if($host_parts[1]) {
938 $domain = $host_parts[1] . '.' . $host_parts[0];
939 }
940 else {
941 return $host_parts[0];
942 }
943 }
944
945 return $domain;
946 }
947
948 function localizer_is_front_page() {
949 if (variable_get('localizer_frontpage_redirect', TRUE)) {
950 $language = localizer_get_language();
951 $front = localizer_get_localized_path(variable_get('site_frontpage', 'node'), $language);
952 return (localizer_get_localize_dpath($_GET['q'], $language) == $front);
953 }
954 else {
955 return drupal_is_front_page();
956 }
957 }
958
959 function localizer_get_browser_lang() {
960 $languages = localizer_supported_languages();
961 $lang = split(",", $_SERVER['HTTP_ACCEPT_LANGUAGE']);
962 if (!empty($lang[0]) && array_key_exists($lang[0],$languages)) {
963 return $lang[0];
964 }
965 return localizer_get_default_language();
966 }
967
968 ?>

  ViewVC Help
Powered by ViewVC 1.1.2