functions.php ➔ language_selector_get_allowed_translations()   C
last analyzed

Complexity

Conditions 7
Paths 4

Size

Total Lines 36
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 19
nc 4
nop 0
dl 0
loc 36
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Functions for the language selector
5
 */
6
7
/**
8
 * Returns the translations that are allowed to be used
9
 * 
10
 * @return array
11
 */
12
function language_selector_get_allowed_translations() {
13
	
14
	$configured_allowed = elgg_get_plugin_setting("allowed_languages", "language_selector");
15
	
16
	if (empty($configured_allowed)) {
17
		$allowed = array("en");
18
		
19
		$installed_languages = get_installed_translations();
20
	
21
		$min_completeness = (int) elgg_get_plugin_setting("min_completeness", "language_selector");
22
		
23
		if ($min_completeness > 0) {
24
			if (elgg_is_active_plugin("translation_editor")) {
25
				$completeness_function = "translation_editor_get_language_completeness";
26
			} else {
27
				$completeness_function = "get_language_completeness";
28
			}
29
			
30
			foreach ($installed_languages as $lang_id => $lang_description) {
31
	
32
				if ($lang_id != "en") {
33
					if (($completeness = $completeness_function($lang_id)) >= $min_completeness) {
34
						$allowed[] = $lang_id;
35
					}
36
				}
37
			}
38
		}
39
		
40
		elgg_set_plugin_setting("allowed_languages", implode(",", $allowed), "language_selector");
41
		
42
	} else {
43
		$allowed = string_to_tag_array($configured_allowed);
44
	}
45
46
	return $allowed;
47
}
48
49
/**
50
 * Sets the language for the logged out user
51
 *
52
 * @return void
53
 */
54
function language_selector_set_logged_out_user_language() {
55
	global $CONFIG;
56
	
57
	if (elgg_is_logged_in()) {
58
		return;
59
	}
60
61
	if (!empty($_COOKIE['client_language'])) {
62
		// switched with language selector
63
		$new_lang = $_COOKIE['client_language'];
64
	} else {
65
66
		$browserlang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
67
68
		if (!empty($browserlang)) {
69
			// autodetect language
70
71
			if (elgg_get_plugin_setting("autodetect", "language_selector") == "yes") {
72
				$new_lang = $browserlang;
73
			}
74
		}
75
	}
76
77
	if (!empty($new_lang) && ($new_lang !== $CONFIG->language)) {
78
		$allowed = language_selector_get_allowed_translations();
79
		if (in_array($new_lang, $allowed)) {
80
			// set new language
81
			$CONFIG->language = $new_lang;
82
83
			// language has been change; reload language keys
84
			if (elgg_is_active_plugin("translation_editor")) {
85
				translation_editor_load_translations();
86
			} else {
87
				reload_all_translations();
88
			}
89
		}
90
	}
91
}
92
93
/**
94
 * Unset the plugin setting so it will be reset when used the next time
95
 *
96
 * @return void
97
 */
98
function language_selector_invalidate_setting() {
99
	elgg_unset_plugin_setting("allowed_languages", "language_selector");
100
}
101