[6547] | 1 | <?php |
---|
| 2 | if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!'); |
---|
| 3 | |
---|
| 4 | class CF_Config_Lang { |
---|
| 5 | static $default_keys = array(); |
---|
| 6 | static protected $available_languages = null; |
---|
| 7 | protected $defined_languages = array(); |
---|
| 8 | protected $added_languages = array(); |
---|
| 9 | protected $keys = array(); |
---|
| 10 | |
---|
| 11 | static private function update_available_languages() { |
---|
| 12 | CF_Config_Lang::$available_languages = array(); |
---|
| 13 | foreach(get_dirs(CF_LANGUAGE) as $language) { |
---|
| 14 | array_push(CF_Config_Lang::$available_languages, $language); |
---|
| 15 | } |
---|
| 16 | return CF_Config_Lang::$available_languages; |
---|
| 17 | } |
---|
| 18 | static function get_available_languages() { |
---|
| 19 | if (empty(CF_Config_Lang::$available_languages)) { |
---|
| 20 | CF_Config_Lang::update_available_languages(); |
---|
| 21 | } |
---|
| 22 | return CF_Config_Lang::$available_languages; |
---|
| 23 | } |
---|
| 24 | /* ************************ */ |
---|
| 25 | /* ** Constructor ** */ |
---|
| 26 | /* ************************ */ |
---|
| 27 | |
---|
| 28 | function __construct() { |
---|
| 29 | $this->init(); |
---|
| 30 | } |
---|
| 31 | |
---|
| 32 | /* ************************ */ |
---|
| 33 | /* ** Accessors ** */ |
---|
| 34 | /* ************************ */ |
---|
| 35 | |
---|
| 36 | function update_keys() { |
---|
| 37 | $this->keys = array(); |
---|
| 38 | foreach($this->get_merged_values() as $language => $keys) { |
---|
| 39 | foreach($keys as $key => $value) { |
---|
| 40 | $this->add_key($key); |
---|
| 41 | } |
---|
| 42 | } |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | function set_default_values() { |
---|
| 46 | foreach(CF_Config_Lang::$default_keys as $default_key => $default_values) { |
---|
| 47 | foreach($default_values as $language => $value) { |
---|
| 48 | if (empty($this->defined_languages[$language][$default_key])) { |
---|
| 49 | $this->defined_languages[$language][$default_key] = $value; |
---|
| 50 | } |
---|
| 51 | } |
---|
| 52 | $this->add_key($default_key); |
---|
| 53 | } |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | function get_keys() { |
---|
| 57 | return $this->keys; |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | function add_key($key) { |
---|
| 61 | if (in_array($key, $this->keys)) { |
---|
| 62 | // already exists |
---|
| 63 | return; |
---|
| 64 | } |
---|
| 65 | $this->keys[]=$key; |
---|
| 66 | // Add key to all languages |
---|
| 67 | foreach($this->automatic_values as $language => $keys) { |
---|
| 68 | if (!isset($this->defined_languages[$language][$key])) { |
---|
| 69 | $this->defined_languages[$language][$key] = ''; |
---|
| 70 | } |
---|
| 71 | } |
---|
| 72 | foreach($this->added_languages as $language => $keys) { |
---|
| 73 | if (!isset($this->added_languages[$language][$key])) { |
---|
| 74 | $this->added_languages[$language][$key] = ''; |
---|
| 75 | } |
---|
| 76 | } |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | function get_value($language=null, $key=null, $return_default=true) { |
---|
| 80 | $values = $this->get_merged_values(); |
---|
| 81 | if (null == $language) { |
---|
| 82 | return $values; |
---|
| 83 | } |
---|
| 84 | if (!isset($values[$language])) { |
---|
| 85 | return null; |
---|
| 86 | } |
---|
| 87 | if (null == $key) { |
---|
| 88 | return $values[$language]; |
---|
| 89 | } |
---|
| 90 | if (!isset($values[$language][$key]) or empty($values[$language][$key])) { |
---|
| 91 | if ($return_default) { |
---|
| 92 | return $values[CF_LANG_DEFAULT][$key]; |
---|
| 93 | } |
---|
| 94 | return null; |
---|
| 95 | } |
---|
| 96 | return $values[$language][$key]; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | function set_value($language, $key, $value) { |
---|
| 100 | if (isset($this->defined_languages[$language])) { |
---|
| 101 | $this->defined_languages[$language][$key] = $value; |
---|
| 102 | } else { |
---|
| 103 | if (!isset($this->added_languages[$language])) { |
---|
| 104 | $this->added_languages[$language] = array(); |
---|
| 105 | } |
---|
| 106 | $this->added_languages[$language][$key] = $value; |
---|
| 107 | } |
---|
| 108 | $this->add_key($key); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | function get_extended_values($language=null) { |
---|
| 112 | $values = $this->get_merged_values(); |
---|
| 113 | $return_values = array(); |
---|
| 114 | foreach($values as $language => $keys) { |
---|
| 115 | foreach($keys as $key => $value) { |
---|
| 116 | if (!isset($return_values[$key])) { |
---|
| 117 | $return_values[$key] = ''; |
---|
| 118 | } |
---|
| 119 | if (!empty($value)) { |
---|
| 120 | if (strcmp($language, CF_LANG_DEFAULT) != 0) { |
---|
| 121 | $language = substr($language, 0, 2); |
---|
| 122 | } |
---|
| 123 | $return_values[$key] .= '[lang=' . $language . ']'; |
---|
| 124 | $return_values[$key] .= $value; |
---|
| 125 | $return_values[$key] .= '[/lang]'; |
---|
| 126 | } |
---|
| 127 | } |
---|
| 128 | } |
---|
| 129 | return $return_values; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | function mass_update($new_values = array()) { |
---|
| 133 | foreach($new_values as $key => $values) { |
---|
| 134 | if (is_array($values)) { |
---|
| 135 | foreach($values as $language => $content) { |
---|
| 136 | $this->set_value($language, $key, $content); |
---|
| 137 | } |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | /* ************************ */ |
---|
| 143 | /* ** Private functions ** */ |
---|
| 144 | /* ************************ */ |
---|
| 145 | |
---|
| 146 | protected function get_merged_values() { |
---|
| 147 | return array_merge_recursive($this->defined_languages, $this->added_languages); |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | protected function init() { |
---|
| 151 | $this->automatic_values = array(); |
---|
| 152 | foreach(CF_Config_Lang::get_available_languages() as $language) { |
---|
| 153 | $this->defined_languages[$language] = array(); |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | } |
---|
| 157 | ?> |
---|