1 | <?php |
---|
2 | /**************************************************************************\ |
---|
3 | * eGroupWare API - Translation class for SQL * |
---|
4 | * This file written by Joseph Engo <jengo@phpgroupware.org> * |
---|
5 | * and Dan Kuykendall <seek3r@phpgroupware.org> * |
---|
6 | * Handles multi-language support use SQL tables * |
---|
7 | * Copyright (C) 2000, 2001 Joseph Engo * |
---|
8 | * ------------------------------------------------------------------------ * |
---|
9 | * This library is part of the eGroupWare API * |
---|
10 | * http://www.egroupware.org/api * |
---|
11 | * ------------------------------------------------------------------------ * |
---|
12 | * This library is free software; you can redistribute it and/or modify it * |
---|
13 | * under the terms of the GNU Lesser General Public License as published by * |
---|
14 | * the Free Software Foundation; either version 2.1 of the License, * |
---|
15 | * or any later version. * |
---|
16 | * This library is distributed in the hope that it will be useful, but * |
---|
17 | * WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * |
---|
19 | * See the GNU Lesser General Public License for more details. * |
---|
20 | * You should have received a copy of the GNU Lesser General Public License * |
---|
21 | * along with this library; if not, write to the Free Software Foundation, * |
---|
22 | * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
---|
23 | \**************************************************************************/ |
---|
24 | |
---|
25 | |
---|
26 | // define the maximal length of a message_id, all message_ids have to be unique |
---|
27 | // in this length, our column is varchar 255, but addslashes might add some length |
---|
28 | if (!defined('MAX_MESSAGE_ID_LENGTH')) |
---|
29 | { |
---|
30 | define('MAX_MESSAGE_ID_LENGTH',230); |
---|
31 | } |
---|
32 | // some constanst for pre php4.3 |
---|
33 | if (!defined('PHP_SHLIB_SUFFIX')) |
---|
34 | { |
---|
35 | define('PHP_SHLIB_SUFFIX',strtoupper(substr(PHP_OS, 0,3)) == 'WIN' ? 'dll' : 'so'); |
---|
36 | } |
---|
37 | if (!defined('PHP_SHLIB_PREFIX')) |
---|
38 | { |
---|
39 | define('PHP_SHLIB_PREFIX',PHP_SHLIB_SUFFIX == 'dll' ? 'php_' : ''); |
---|
40 | } |
---|
41 | |
---|
42 | class translation |
---|
43 | { |
---|
44 | var $userlang = 'en'; |
---|
45 | var $loaded_apps = array(); |
---|
46 | var $line_rejected = array(); |
---|
47 | |
---|
48 | function translation($warnings = False) |
---|
49 | { |
---|
50 | for ($i = 1; $i <= 9; $i++) |
---|
51 | { |
---|
52 | $this->placeholders[] = '%'.$i; |
---|
53 | } |
---|
54 | $this->db = is_object($GLOBALS['phpgw']->db) ? $GLOBALS['phpgw']->db : $GLOBALS['phpgw_setup']->db; |
---|
55 | if (!isset($GLOBALS['phpgw_setup'])) |
---|
56 | { |
---|
57 | $this->system_charset = @$GLOBALS['phpgw_info']['server']['system_charset']; |
---|
58 | } |
---|
59 | else |
---|
60 | { |
---|
61 | $this->db->query("SELECT config_value FROM phpgw_config WHERE config_app='phpgwapi' AND config_name='system_charset'",__LINE__,__FILE__); |
---|
62 | if ($this->db->next_record()) |
---|
63 | { |
---|
64 | $this->system_charset = $this->db->f(0); |
---|
65 | } |
---|
66 | } |
---|
67 | // load multi-byte-string-extension if needed, and set its internal encodeing to your system_charset |
---|
68 | if ($this->system_charset && substr($this->system_charset,0,9) != 'iso-8859-1') |
---|
69 | { |
---|
70 | if ($this->mbstring = extension_loaded('mbstring') || @dl(PHP_SHLIB_PREFIX.'mbstring.'.PHP_SHLIB_SUFFIX)) |
---|
71 | { |
---|
72 | ini_set('mbstring.internal_encoding',$this->system_charset); |
---|
73 | if (ini_get('mbstring.func_overload') < 7) |
---|
74 | { |
---|
75 | if ($warnings) |
---|
76 | { |
---|
77 | echo "<p>Warning: Please set <b>mbstring.func_overload = 7</b> in your php.ini for useing <b>$this->system_charset</b> as your charset !!!</p>\n"; |
---|
78 | } |
---|
79 | } |
---|
80 | } |
---|
81 | else |
---|
82 | { |
---|
83 | if ($warnings) |
---|
84 | { |
---|
85 | echo "<p>Warning: Please get and/or enable the <b>mbstring extension</b> in your php.ini for useing <b>$this->system_charset</b> as your charset, we are defaulting to <b>iconv</b> for now !!!</p>\n"; |
---|
86 | } |
---|
87 | } |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | /* |
---|
92 | @function charset |
---|
93 | @abstract returns the charset to use (!$lang) or the charset of the lang-files or $lang |
---|
94 | */ |
---|
95 | function charset($lang=False) |
---|
96 | { |
---|
97 | if ($lang) |
---|
98 | { |
---|
99 | if (!isset($this->charsets[$lang])) |
---|
100 | { |
---|
101 | $this->db->query("SELECT content FROM phpgw_lang WHERE lang='$lang' AND message_id='charset' AND app_name='common'",__LINE__,__FILE__); |
---|
102 | $this->charsets[$lang] = $this->db->next_record() ? strtolower($this->db->f(0)) : 'iso-8859-1'; |
---|
103 | } |
---|
104 | return $this->charsets[$lang]; |
---|
105 | } |
---|
106 | if ($this->system_charset) // do we have a system-charset ==> return it |
---|
107 | { |
---|
108 | return $this->system_charset; |
---|
109 | } |
---|
110 | // if no translations are loaded (system-startup) use a default, else lang('charset') |
---|
111 | return !is_array(@$GLOBALS['lang']) ? 'iso-8859-1' : strtolower($this->translate('charset')); |
---|
112 | } |
---|
113 | |
---|
114 | function init() |
---|
115 | { |
---|
116 | // post-nuke and php-nuke are using $GLOBALS['lang'] too |
---|
117 | // but not as array! |
---|
118 | // this produces very strange results |
---|
119 | if (!is_array(@$GLOBALS['lang'])) |
---|
120 | { |
---|
121 | $GLOBALS['lang'] = array(); |
---|
122 | } |
---|
123 | |
---|
124 | if ($GLOBALS['phpgw_info']['user']['preferences']['common']['lang']) |
---|
125 | { |
---|
126 | $this->userlang = $GLOBALS['phpgw_info']['user']['preferences']['common']['lang']; |
---|
127 | } |
---|
128 | elseif($GLOBALS['_SERVER']['HTTP_ACCEPT_LANGUAGE']) |
---|
129 | { |
---|
130 | //$this->userlang = $GLOBALS['_SERVER']['HTTP_ACCEPT_LANGUAGE']; |
---|
131 | $aux = explode(';',$GLOBALS['_SERVER']['HTTP_ACCEPT_LANGUAGE']); |
---|
132 | $aux = explode(',',$aux[0]); |
---|
133 | $this->userlang = $aux[0]; |
---|
134 | } |
---|
135 | $this->add_app('common'); |
---|
136 | if (!count($GLOBALS['lang'])) |
---|
137 | { |
---|
138 | $this->userlang = 'en'; |
---|
139 | $this->add_app('common'); |
---|
140 | } |
---|
141 | $this->add_app($GLOBALS['phpgw_info']['flags']['currentapp']); |
---|
142 | } |
---|
143 | |
---|
144 | /*! |
---|
145 | @function translate |
---|
146 | @abstract translates a phrase and evtl. substitute some variables |
---|
147 | @returns the translation |
---|
148 | */ |
---|
149 | function translate($key, $vars=false, $not_found='*' ) |
---|
150 | { |
---|
151 | if (!is_array(@$GLOBALS['lang']) || !count($GLOBALS['lang'])) |
---|
152 | { |
---|
153 | $this->init(); |
---|
154 | } |
---|
155 | $ret = $key.$not_found; // save key if we dont find a translation |
---|
156 | |
---|
157 | if (isset($GLOBALS['lang'][$key])) |
---|
158 | { |
---|
159 | $ret = $GLOBALS['lang'][$key]; |
---|
160 | } |
---|
161 | else |
---|
162 | { |
---|
163 | $new_key = strtolower(trim(substr($key,0,MAX_MESSAGE_ID_LENGTH))); |
---|
164 | |
---|
165 | if (isset($GLOBALS['lang'][$new_key])) |
---|
166 | { |
---|
167 | // we save the original key for performance |
---|
168 | $ret = $GLOBALS['lang'][$key] = $GLOBALS['lang'][$new_key]; |
---|
169 | } |
---|
170 | } |
---|
171 | if (is_array($vars) && count($vars)) |
---|
172 | { |
---|
173 | if (count($vars) > 1) |
---|
174 | { |
---|
175 | $ret = str_replace($this->placeholders,$vars,$ret); |
---|
176 | } |
---|
177 | else |
---|
178 | { |
---|
179 | $ret = str_replace('%1',$vars[0],$ret); |
---|
180 | } |
---|
181 | } |
---|
182 | return $ret; |
---|
183 | } |
---|
184 | |
---|
185 | |
---|
186 | function translate_async($key,$vars=false, $not_found='*' ) |
---|
187 | { |
---|
188 | $lindex = $_SESSION['phpgw_info']['calendar']['langAlarm']; |
---|
189 | if (!is_array(@$lindex) || !count($lindex)) |
---|
190 | { |
---|
191 | $this->init(); |
---|
192 | } |
---|
193 | $ret = $key.$not_found; // save key if we dont find a translation |
---|
194 | |
---|
195 | if (isset($lindex[$key])) |
---|
196 | { |
---|
197 | $ret = $lindex[$key]; |
---|
198 | } |
---|
199 | else |
---|
200 | { |
---|
201 | $new_key = strtolower(trim(substr($key,0,MAX_MESSAGE_ID_LENGTH))); |
---|
202 | |
---|
203 | if (isset($lindex[$new_key])) |
---|
204 | { |
---|
205 | // we save the original key for performance |
---|
206 | $ret = $lindex[$key] = $lindex[$new_key]; |
---|
207 | } |
---|
208 | } |
---|
209 | if (is_array($vars) && count($vars)) |
---|
210 | { |
---|
211 | if (count($vars) > 1) |
---|
212 | { |
---|
213 | $ret = str_replace($this->placeholders,$vars,$ret); |
---|
214 | } |
---|
215 | else |
---|
216 | { |
---|
217 | $ret = str_replace('%1',$vars[0],$ret); |
---|
218 | } |
---|
219 | } |
---|
220 | return $ret; |
---|
221 | } |
---|
222 | |
---|
223 | /*! |
---|
224 | @function add_app |
---|
225 | @abstract adds translations for an application from the database to the lang-array |
---|
226 | @syntax add_app($app,$lang=False) |
---|
227 | @param $app name of the application to add (or 'common' for the general translations) |
---|
228 | @param $lang 2-char code of the language to use or False if the users language should be used |
---|
229 | */ |
---|
230 | function add_app($app,$lang=False) |
---|
231 | { |
---|
232 | $lang = $lang ? $lang : $this->userlang; |
---|
233 | |
---|
234 | if (!isset($this->loaded_apps[$app]) || $this->loaded_apps[$app] != $lang) |
---|
235 | { |
---|
236 | $sql = "select message_id,content from phpgw_lang where lang='".pg_escape_string($lang)."' and app_name='".pg_escape_string($app)."'"; |
---|
237 | $this->db->query($sql,__LINE__,__FILE__); |
---|
238 | while ($this->db->next_record()) |
---|
239 | { |
---|
240 | $GLOBALS['lang'][strtolower ($this->db->f('message_id'))] = $this->db->f('content'); |
---|
241 | } |
---|
242 | $this->loaded_apps[$app] = $lang; |
---|
243 | } |
---|
244 | } |
---|
245 | |
---|
246 | /*! |
---|
247 | @function get_installed_langs |
---|
248 | @abstract returns a list of installed langs |
---|
249 | @returns array with 2-character lang-code as key and descriptiv lang-name as data |
---|
250 | */ |
---|
251 | function get_installed_langs() |
---|
252 | { |
---|
253 | if (!is_array($this->langs)) |
---|
254 | { |
---|
255 | $this->db->query("SELECT DISTINCT l.lang,ln.lang_name FROM phpgw_lang l,phpgw_languages ln WHERE l.lang = ln.lang_id",__LINE__,__FILE__); |
---|
256 | if (!$this->db->num_rows()) |
---|
257 | { |
---|
258 | return False; |
---|
259 | } |
---|
260 | while ($this->db->next_record()) |
---|
261 | { |
---|
262 | $this->langs[$this->db->f('lang')] = $this->db->f('lang_name'); |
---|
263 | } |
---|
264 | foreach($this->langs as $lang => $name) |
---|
265 | { |
---|
266 | $this->langs[$lang] = $this->translate($name,False,''); |
---|
267 | } |
---|
268 | } |
---|
269 | return $this->langs; |
---|
270 | } |
---|
271 | |
---|
272 | /*! |
---|
273 | @function get_installed_charsets |
---|
274 | @abstract returns a list of installed charsets |
---|
275 | @returns array with charset as key and comma-separated list of langs useing the charset as data |
---|
276 | */ |
---|
277 | function get_installed_charsets() |
---|
278 | { |
---|
279 | if (!is_array($this->charsets)) |
---|
280 | { |
---|
281 | $distinct = 'DISTINCT'; |
---|
282 | switch($this->db->Type) |
---|
283 | { |
---|
284 | case 'mssql': |
---|
285 | $distinct = ''; // cant use distinct on text columns (l.content is text) |
---|
286 | } |
---|
287 | $this->db->query("SELECT $distinct l.lang,lx.lang_name,l.content AS charset FROM phpgw_lang l,phpgw_languages lx WHERE l.lang = lx.lang_id AND l.message_id='charset'",__LINE__,__FILE__); |
---|
288 | if (!$this->db->num_rows()) |
---|
289 | { |
---|
290 | return False; |
---|
291 | } |
---|
292 | while ($this->db->next_record()) |
---|
293 | { |
---|
294 | $data = &$this->charsets[$charset = strtolower($this->db->f('charset'))]; |
---|
295 | $lang = $this->db->f('lang_name').' ('.$this->db->f('lang').')'; |
---|
296 | if ($distinct || strstr($data,$lang) === false) |
---|
297 | { |
---|
298 | $data .= ($data ? ', ' : $charset.': ').$lang; |
---|
299 | } |
---|
300 | } |
---|
301 | } |
---|
302 | return $this->charsets; |
---|
303 | } |
---|
304 | |
---|
305 | /*! |
---|
306 | @function convert |
---|
307 | @abstract converts a string $data from charset $from to charset $to |
---|
308 | @syntax convert($data,$from=False,$to=False) |
---|
309 | @param $data string or array of strings to convert |
---|
310 | @param $from charset $data is in or False if it should be detected |
---|
311 | @param $to charset to convert to or False for the system-charset |
---|
312 | @returns the converted string |
---|
313 | */ |
---|
314 | function convert($data,$from=False,$to=False) |
---|
315 | { |
---|
316 | if (is_array($data)) |
---|
317 | { |
---|
318 | foreach($data as $key => $str) |
---|
319 | { |
---|
320 | $ret[$key] = $this->convert($str,$from,$to); |
---|
321 | } |
---|
322 | return $ret; |
---|
323 | } |
---|
324 | |
---|
325 | if ($from) |
---|
326 | { |
---|
327 | $from = strtolower($from); |
---|
328 | } |
---|
329 | if ($to) |
---|
330 | { |
---|
331 | $to = strtolower($to); |
---|
332 | } |
---|
333 | |
---|
334 | if (!$from) |
---|
335 | { |
---|
336 | $from = $this->mbstring ? strtolower(mb_detect_encoding($data)) : 'iso-8859-1'; |
---|
337 | if($from == 'ascii') |
---|
338 | { |
---|
339 | $from = 'iso-8859-1'; |
---|
340 | } |
---|
341 | //echo "<p>autodetected charset of '$data' = '$from'</p>\n"; |
---|
342 | } |
---|
343 | /* |
---|
344 | php does not seem to support gb2312 |
---|
345 | but seems to be able to decode it as EUC-CN |
---|
346 | */ |
---|
347 | switch($from) |
---|
348 | { |
---|
349 | case 'gb2312': |
---|
350 | case 'gb18030': |
---|
351 | $from = 'EUC-CN'; |
---|
352 | break; |
---|
353 | case 'us-ascii': |
---|
354 | case 'macroman': |
---|
355 | $from = 'iso-8859-1'; |
---|
356 | break; |
---|
357 | } |
---|
358 | if (!$to) |
---|
359 | { |
---|
360 | $to = $this->charset(); |
---|
361 | } |
---|
362 | if ($from == $to || !$from || !$to || !$data) |
---|
363 | { |
---|
364 | return $data; |
---|
365 | } |
---|
366 | if ($from == 'iso-8859-1' && $to == 'utf-8') |
---|
367 | { |
---|
368 | return utf8_encode($data); |
---|
369 | } |
---|
370 | if ($to == 'iso-8859-1' && $from == 'utf-8') |
---|
371 | { |
---|
372 | return utf8_decode($data); |
---|
373 | } |
---|
374 | if ($this->mbstring) |
---|
375 | { |
---|
376 | return @mb_convert_encoding($data,$to,$from); |
---|
377 | } |
---|
378 | if(function_exists('iconv')) |
---|
379 | { |
---|
380 | if($from=='EUC-CN') $from='gb18030'; |
---|
381 | // the above is to workaround patch #962307 |
---|
382 | // if using EUC-CN, for iconv it strickly follow GB2312 and fail |
---|
383 | // in an email on the first Traditional/Japanese/Korean character, |
---|
384 | // but in reality when people send mails in GB2312, UMA mostly use |
---|
385 | // extended GB13000/GB18030 which allow T/Jap/Korean characters. |
---|
386 | if (($data = iconv($from,$to,$data))) |
---|
387 | { |
---|
388 | return $data; |
---|
389 | } |
---|
390 | } |
---|
391 | #die("<p>Can't convert from charset '$from' to '$to' without the <b>mbstring extension</b> !!!</p>"); |
---|
392 | |
---|
393 | // this is not good, not convert did succed |
---|
394 | return $data; |
---|
395 | } |
---|
396 | |
---|
397 | /*! |
---|
398 | @function install_langs |
---|
399 | @abstract installs translations for the selected langs into the database |
---|
400 | @syntax install_langs($langs,$upgrademethod='dumpold') |
---|
401 | @param $langs array of langs to install (as data NOT keys (!)) |
---|
402 | @param $upgrademethod 'dumpold' (recommended & fastest), 'addonlynew' languages, 'addmissing' phrases |
---|
403 | */ |
---|
404 | function install_langs($langs,$upgrademethod='dumpold',$only_app=False) |
---|
405 | { |
---|
406 | @set_time_limit(0); // we might need some time |
---|
407 | |
---|
408 | if (!isset($GLOBALS['phpgw_info']['server']) && $upgrademethod != 'dumpold') |
---|
409 | { |
---|
410 | $this->db->query("SELECT * FROM phpgw_config WHERE config_app='phpgwapi' AND config_name='lang_ctimes'",__LINE__,__FILE__); |
---|
411 | if ($this->db->next_record()) |
---|
412 | { |
---|
413 | $GLOBALS['phpgw_info']['server']['lang_ctimes'] = unserialize(stripslashes($this->db->f('config_value'))); |
---|
414 | } |
---|
415 | } |
---|
416 | |
---|
417 | if (!is_array($langs) || !count($langs)) |
---|
418 | { |
---|
419 | return; |
---|
420 | } |
---|
421 | $this->db->transaction_begin(); |
---|
422 | |
---|
423 | if ($upgrademethod == 'dumpold') |
---|
424 | { |
---|
425 | // dont delete the custom main- & loginscreen messages every time |
---|
426 | $this->db->query("DELETE FROM phpgw_lang WHERE app_name != 'mainscreen' AND app_name != 'loginscreen'",__LINE__,__FILE__); |
---|
427 | //echo '<br>Test: dumpold'; |
---|
428 | $GLOBALS['phpgw_info']['server']['lang_ctimes'] = array(); |
---|
429 | } |
---|
430 | foreach($langs as $lang) |
---|
431 | { |
---|
432 | //echo '<br>Working on: ' . $lang; |
---|
433 | $addlang = False; |
---|
434 | if ($upgrademethod == 'addonlynew') |
---|
435 | { |
---|
436 | //echo "<br>Test: addonlynew - select count(*) from phpgw_lang where lang='".$lang."'"; |
---|
437 | $this->db->query("SELECT COUNT(*) FROM phpgw_lang WHERE lang='".$lang."'",__LINE__,__FILE__); |
---|
438 | $this->db->next_record(); |
---|
439 | |
---|
440 | if ($this->db->f(0) == 0) |
---|
441 | { |
---|
442 | //echo '<br>Test: addonlynew - True'; |
---|
443 | $addlang = True; |
---|
444 | } |
---|
445 | } |
---|
446 | if ($addlang && $upgrademethod == 'addonlynew' || $upgrademethod != 'addonlynew') |
---|
447 | { |
---|
448 | //echo '<br>Test: loop above file()'; |
---|
449 | if (!is_object($GLOBALS['phpgw_setup'])) |
---|
450 | { |
---|
451 | $GLOBALS['phpgw_setup'] = CreateObject('phpgwapi.setup'); |
---|
452 | $GLOBALS['phpgw_setup']->db = $this->db; |
---|
453 | } |
---|
454 | $setup_info = $GLOBALS['phpgw_setup']->detection->get_versions(); |
---|
455 | $setup_info = $GLOBALS['phpgw_setup']->detection->get_db_versions($setup_info); |
---|
456 | $raw = array(); |
---|
457 | $apps = $only_app ? array($only_app) : array_keys($setup_info); |
---|
458 | // Visit each app/setup dir, look for a phpgw_lang file |
---|
459 | foreach($apps as $app) |
---|
460 | { |
---|
461 | $appfile = PHPGW_SERVER_ROOT . SEP . $app . SEP . 'setup' . SEP . 'phpgw_' . strtolower($lang) . '.lang'; |
---|
462 | //echo '<br>Checking in: ' . $app['name']; |
---|
463 | if($GLOBALS['phpgw_setup']->app_registered($app) && file_exists($appfile)) |
---|
464 | { |
---|
465 | //echo '<br>Including: ' . $appfile; |
---|
466 | $lines = file($appfile); |
---|
467 | foreach($lines as $line) |
---|
468 | { |
---|
469 | // explode with "\t" and removing "\n" with str_replace, needed to work with mbstring.overload=7 |
---|
470 | list($message_id,$app_name,,$content) = $_f_buffer = explode("\t",$line); |
---|
471 | $content=str_replace(array("\n","\r"),'',$content); |
---|
472 | |
---|
473 | if( count($_f_buffer) != 4 ) |
---|
474 | { |
---|
475 | $line_display = str_replace(array("\t","\n"), |
---|
476 | array("<font color='red'><b>\\t</b></font>","<font color='red'><b>\\n</b></font>"), $line); |
---|
477 | $this->line_rejected[] = array( |
---|
478 | 'appfile' => $appfile, |
---|
479 | 'line' => $line_display, |
---|
480 | ); |
---|
481 | } |
---|
482 | $message_id = substr(strtolower(chop($message_id)),0,MAX_MESSAGE_ID_LENGTH); |
---|
483 | $app_name = chop($app_name); |
---|
484 | $raw[$app_name][$message_id] = $content; |
---|
485 | } |
---|
486 | $GLOBALS['phpgw_info']['server']['lang_ctimes'][$lang][$app] = filectime($appfile); |
---|
487 | } |
---|
488 | } |
---|
489 | $charset = strtolower(@$raw['common']['charset'] ? $raw['common']['charset'] : $this->charset($lang)); |
---|
490 | //echo "<p>lang='$lang', charset='$charset', system_charset='$this->system_charset')</p>\n"; |
---|
491 | //echo "<p>raw($lang)=<pre>".print_r($raw,True)."</pre>\n"; |
---|
492 | foreach($raw as $app_name => $ids) |
---|
493 | { |
---|
494 | $app_name = $this->db->db_addslashes($app_name); |
---|
495 | |
---|
496 | foreach($ids as $message_id => $content) |
---|
497 | { |
---|
498 | if ($this->system_charset) |
---|
499 | { |
---|
500 | $content = $this->convert($content,$charset,$this->system_charset); |
---|
501 | } |
---|
502 | $message_id = $this->db->db_addslashes($message_id); |
---|
503 | $content = $this->db->db_addslashes($content); |
---|
504 | |
---|
505 | $addit = False; |
---|
506 | //echo '<br>APPNAME:' . $app_name . ' PHRASE:' . $message_id; |
---|
507 | if ($upgrademethod == 'addmissing') |
---|
508 | { |
---|
509 | //echo '<br>Test: addmissing'; |
---|
510 | $this->db->query("SELECT content,CASE WHEN app_name IN ('common') then 1 else 0 END AS in_api FROM phpgw_lang WHERE message_id='$message_id' AND lang='$lang' AND (app_name='$app_name' OR app_name='common') ORDER BY in_api DESC",__LINE__,__FILE__); |
---|
511 | |
---|
512 | if (!($row = $this->db->row(True))) |
---|
513 | { |
---|
514 | $addit = True; |
---|
515 | } |
---|
516 | else |
---|
517 | { |
---|
518 | if ($row['in_api']) // same phrase is in the api |
---|
519 | { |
---|
520 | $addit = $row['content'] != $content; // only add if not identical |
---|
521 | } |
---|
522 | $row2 = $this->db->row(True); |
---|
523 | if (!$row['in_api'] || $app_name=='common' || $row2) // phrase is alread in the db |
---|
524 | { |
---|
525 | $addit = $content != ($row2 ? $row2['content'] : $row['content']); |
---|
526 | if ($addit) // if we want to add/update it ==> delete it |
---|
527 | { |
---|
528 | $this->db->query($q="DELETE FROM phpgw_lang WHERE message_id='$message_id' AND lang='$lang' AND app_name='$app_name'",__LINE__,__FILE__); |
---|
529 | } |
---|
530 | } |
---|
531 | } |
---|
532 | } |
---|
533 | |
---|
534 | if ($addit || $upgrademethod == 'addonlynew' || $upgrademethod == 'dumpold') |
---|
535 | { |
---|
536 | if($message_id && $content) |
---|
537 | { |
---|
538 | //echo "<br>adding - insert into phpgw_lang values ('$message_id','$app_name','$lang','$content')"; |
---|
539 | $result = $this->db->query("INSERT INTO phpgw_lang (message_id,app_name,lang,content) VALUES('$message_id','$app_name','$lang','$content')",__LINE__,__FILE__); |
---|
540 | if ((int)$result <= 0) |
---|
541 | { |
---|
542 | echo "<br>Error inserting record: phpgw_lang values ('$message_id','$app_name','$lang','$content')"; |
---|
543 | } |
---|
544 | } |
---|
545 | } |
---|
546 | } |
---|
547 | } |
---|
548 | } |
---|
549 | } |
---|
550 | $this->db->transaction_commit(); |
---|
551 | |
---|
552 | // update the ctimes of the installed langsfiles for the autoloading of the lang-files |
---|
553 | // |
---|
554 | $config = CreateObject('phpgwapi.config.save_value'); |
---|
555 | $config->save_value('lang_ctimes',$GLOBALS['phpgw_info']['server']['lang_ctimes'],'phpgwapi'); |
---|
556 | } |
---|
557 | |
---|
558 | /*! |
---|
559 | @function autolaod_changed_langfiles |
---|
560 | @abstract re-loads all (!) langfiles if one langfile for the an app and the language of the user has changed |
---|
561 | */ |
---|
562 | function autoload_changed_langfiles() |
---|
563 | { |
---|
564 | //echo "<h1>check_langs()</h1>\n"; |
---|
565 | if ($GLOBALS['phpgw_info']['server']['lang_ctimes'] && !is_array($GLOBALS['phpgw_info']['server']['lang_ctimes'])) |
---|
566 | { |
---|
567 | $GLOBALS['phpgw_info']['server']['lang_ctimes'] = unserialize($GLOBALS['phpgw_info']['server']['lang_ctimes']); |
---|
568 | } |
---|
569 | //_debug_array($GLOBALS['phpgw_info']['server']['lang_ctimes']); |
---|
570 | |
---|
571 | $lang = $GLOBALS['phpgw_info']['user']['preferences']['common']['lang']; |
---|
572 | $apps = $GLOBALS['phpgw_info']['user']['apps']; |
---|
573 | $apps['phpgwapi'] = True; // check the api too |
---|
574 | foreach($apps as $app => $data) |
---|
575 | { |
---|
576 | $fname = PHPGW_SERVER_ROOT . "/$app/setup/phpgw_$lang.lang"; |
---|
577 | |
---|
578 | if (file_exists($fname)) |
---|
579 | { |
---|
580 | $ctime = filectime($fname); |
---|
581 | /* This is done to avoid string offset error at least in php5 */ |
---|
582 | $tmp = $GLOBALS['phpgw_info']['server']['lang_ctimes'][$lang]; |
---|
583 | $ltime = (int)$tmp[$app]; |
---|
584 | unset($tmp); |
---|
585 | //echo "checking lang='$lang', app='$app', ctime='$ctime', ltime='$ltime'<br>\n"; |
---|
586 | |
---|
587 | if ($ctime != $ltime) |
---|
588 | { |
---|
589 | // update all langs |
---|
590 | $installed = $this->get_installed_langs(); |
---|
591 | //echo "<p>install_langs(".print_r($installed,True).")</p>\n"; |
---|
592 | $this->install_langs($installed ? array_keys($installed) : array()); |
---|
593 | break; |
---|
594 | } |
---|
595 | } |
---|
596 | } |
---|
597 | } |
---|
598 | |
---|
599 | /* Following functions are called for app (un)install */ |
---|
600 | |
---|
601 | /*! |
---|
602 | @function get_langs |
---|
603 | @abstract return array of installed languages, e.g. array('de','en') |
---|
604 | */ |
---|
605 | function get_langs($DEBUG=False) |
---|
606 | { |
---|
607 | if($DEBUG) |
---|
608 | { |
---|
609 | echo '<br>get_langs(): checking db...' . "\n"; |
---|
610 | } |
---|
611 | if (!$this->langs) |
---|
612 | { |
---|
613 | $this->get_installed_langs(); |
---|
614 | } |
---|
615 | return $this->langs ? array_keys($this->langs) : array(); |
---|
616 | } |
---|
617 | |
---|
618 | /*! |
---|
619 | @function drop_langs |
---|
620 | @abstract delete all lang entries for an application, return True if langs were found |
---|
621 | @param $appname app_name whose translations you want to delete |
---|
622 | */ |
---|
623 | function drop_langs($appname,$DEBUG=False) |
---|
624 | { |
---|
625 | if($DEBUG) |
---|
626 | { |
---|
627 | echo '<br>drop_langs(): Working on: ' . $appname; |
---|
628 | } |
---|
629 | $this->db->query("SELECT COUNT(message_id) FROM phpgw_lang WHERE app_name='$appname'",__LINE__,__FILE__); |
---|
630 | $this->db->next_record(); |
---|
631 | if($this->db->f(0)) |
---|
632 | { |
---|
633 | $this->db->query("DELETE FROM phpgw_lang WHERE app_name='$appname'",__LINE__,__FILE__); |
---|
634 | return True; |
---|
635 | } |
---|
636 | return False; |
---|
637 | } |
---|
638 | |
---|
639 | /*! |
---|
640 | @function add_langs |
---|
641 | @abstract process an application's lang files, calling get_langs() to see what langs the admin installed already |
---|
642 | @param $appname app_name of application to process |
---|
643 | */ |
---|
644 | function add_langs($appname,$DEBUG=False,$force_langs=False) |
---|
645 | { |
---|
646 | $langs = $this->get_langs($DEBUG); |
---|
647 | if(is_array($force_langs)) |
---|
648 | { |
---|
649 | foreach($force_langs as $lang) |
---|
650 | { |
---|
651 | if (!in_array($lang,$langs)) |
---|
652 | { |
---|
653 | $langs[] = $lang; |
---|
654 | } |
---|
655 | } |
---|
656 | } |
---|
657 | |
---|
658 | if($DEBUG) |
---|
659 | { |
---|
660 | echo '<br>add_langs(): chose these langs: '; |
---|
661 | _debug_array($langs); |
---|
662 | } |
---|
663 | |
---|
664 | $this->install_langs($langs,'addmissing',$appname); |
---|
665 | } |
---|
666 | } |
---|