aboutsummaryrefslogtreecommitdiff
path: root/mod/translation_editor/actions/add_custom_key.php
blob: 47269dfd2b4a3a9ad96d294c762a21491fac624d (plain)
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
<?php 
	global $CONFIG;
	
	admin_gatekeeper();
	
	$key = get_input("key");
	$translation = get_input("translation");
	
	if(!empty($key) && !empty($translation)){
		if(!is_numeric($key)){
			if(preg_match("/^[a-zA-Z0-9_:]{1,}$/", $key)){
				$exists = false;
				if(array_key_exists($key, $CONFIG->translations["en"])){
					$exists = true;
				}
					
				if(!$exists){
					// save
					
					$custom_translations = array();
					
					if($custom_translations = translation_editor_get_plugin("en", "custom_keys")){
						$custom_translations = $custom_translations["en"];
					}
					
					$custom_translations[$key] = $translation;					
					
					$base_dir = elgg_get_data_path() . "translation_editor" . DIRECTORY_SEPARATOR;
					if(!file_exists($base_dir)){
						mkdir($base_dir);
					}
					
					$location = $base_dir . "custom_keys" . DIRECTORY_SEPARATOR;
					if(!file_exists($location)){
						mkdir($location);
					}
					
					$file_contents = "<?php" . PHP_EOL;
					$file_contents .= '$language = ';
					$file_contents .= var_export($custom_translations, true);
					$file_contents .= ';' . PHP_EOL;
					$file_contents .= 'add_translation("en", $language);'  . PHP_EOL;
					$file_contents .= "?>";
					
					if(file_put_contents($location . "en.php", $file_contents)){
						
						system_message(elgg_echo("translation_editor:action:add_custom_key:success"));
					} else {
						register_error(elgg_echo("translation_editor:action:add_custom_key:file_error"));
					}	
				} else {
					register_error(elgg_echo("translation_editor:action:add_custom_key:exists"));
				}
			} else {
				register_error(elgg_echo("translation_editor:action:add_custom_key:invalid_chars"));
			}
		} else {
			register_error(elgg_echo("translation_editor:action:add_custom_key:key_numeric"));
		}
	} else {
		register_error(elgg_echo("translation_editor:action:add_custom_key:missing_input"));
	}
	
	forward(REFERER);