ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
class.ilObjLanguageExt.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-20014 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4require_once "./Services/Language/classes/class.ilObjLanguage.php";
5
15{
16
20 function ilObjLanguageExt($a_id = 0, $a_call_by_reference = false)
21 {
22 $this->ilObjLanguage($a_id, $a_call_by_reference);
23 }
24
29 public function getGlobalLanguageFile()
30 {
31 require_once "./Services/Language/classes/class.ilLanguageFile.php";
33 }
34
40 public function setLocal($a_local = true)
41 {
42 if ($this->isInstalled())
43 {
44 if ($a_local == true)
45 {
46 $this->setDescription("installed_local");
47 }
48 else
49 {
50 $this->setDescription("installed");
51 }
52 $this->update();
53 }
54 }
55
56
62 public function getLongDescription()
63 {
64 return $this->lng->txt($this->desc);
65 }
66
72 public function getLangPath()
73 {
74 return $this->lang_path;
75 }
76
82 public function getCustLangPath()
83 {
84 return $this->cust_lang_path;
85 }
86
92 public function getAllRemarks()
93 {
94 return self::_getRemarks($this->key);
95 }
96
104 public function getAllValues($a_modules = array(), $a_pattern = '')
105 {
106 return self::_getValues($this->key, $a_modules, NULL, $a_pattern);
107 }
108
109
118 public function getChangedValues($a_modules = array(), $a_pattern = '')
119 {
120 return self::_getValues($this->key, $a_modules, NULL, $a_pattern, 'changed');
121 }
122
123
132 public function getUnchangedValues($a_modules = array(), $a_pattern = '')
133 {
134 return self::_getValues($this->key, $a_modules, NULL, $a_pattern, 'unchanged');
135 }
136
144 public function getAddedValues($a_modules = array(), $a_pattern = '')
145 {
146 $global_file_obj = $this->getGlobalLanguageFile();
147 $global_values = $global_file_obj->getAllValues();
148 $local_values = self::_getValues($this->key, $a_modules, NULL, $a_pattern);
149
150 return array_diff_key($local_values, $global_values);
151 }
152
153
164 public function getCommentedValues($a_modules = array(), $a_pattern = '')
165 {
166 $global_file_obj = $this->getGlobalLanguageFile();
167 $global_comments = $global_file_obj->getAllComments();
168 $local_values = self::_getValues($this->key, $a_modules, NULL, $a_pattern);
169
170 return array_intersect_key($local_values, $global_comments);
171 }
172
173
185 public function getMergedValues()
186 {
187
188 $global_file_obj = $this->getGlobalLanguageFile();
189 $global_values = $global_file_obj->getAllValues();
190 $local_values = self::_getValues($this->key);
191
192 return array_merge($global_values, $local_values);
193 }
194
206 public function getMergedRemarks()
207 {
208
209 $global_file_obj = $this->getGlobalLanguageFile();
210 $global_comments = $global_file_obj->getAllComments();
211
212 // get remarks including empty remarks for local changes
213 $local_remarks = self::_getRemarks($this->key, true);
214
215 return array_merge($global_comments, $local_remarks);
216 }
217
218
219
226 public function importLanguageFile($a_file, $a_mode_existing = 'keepnew')
227 {
228 global $ilDB, $ilErr;
229
230 // read the new language file
231 require_once "./Services/Language/classes/class.ilLanguageFile.php";
232 $import_file_obj = new ilLanguageFile($a_file);
233 if (!$import_file_obj->read())
234 {
235 $ilErr->raiseError($import_file_obj->getErrorMessage(),$ilErr->MESSAGE);
236 }
237
238 switch($a_mode_existing)
239 {
240 // keep all existing entries
241 case 'keepall':
242 $to_keep = $this->getAllValues();
243 break;
244
245 // keep existing online changes
246 case 'keepnew':
247 $to_keep = $this->getChangedValues();
248 break;
249
250 // replace all existing definitions
251 case 'replace':
252 $to_keep = array();
253 break;
254
255 // delete all existing entries
256 case 'delete':
257 ilObjLanguage::_deleteLangData($this->key, false);
258 $ilDB->manipulate("DELETE FROM lng_modules WHERE lang_key = ".
259 $ilDB->quote($this->key, "text"));
260 $to_keep = array();
261 break;
262
263 default:
264 return;
265 }
266
267 // process the values of the import file
268 $to_save = array();
269 foreach ($import_file_obj->getAllValues() as $key => $value)
270 {
271 if (!isset($to_keep[$key]))
272 {
273 $to_save[$key] = $value;
274 }
275 }
276 self::_saveValues($this->key, $to_save, $import_file_obj->getAllComments());
277 }
278
286 public static function _getModules($a_lang_key)
287 {
288 global $ilDB;
289
290 $q = "SELECT DISTINCT module FROM lng_data WHERE ".
291 " lang_key = ".$ilDB->quote($a_lang_key, "text")." order by module";
292 $set = $ilDB->query($q);
293
294 while ($rec = $set->fetchRow(DB_FETCHMODE_ASSOC))
295 {
296 $modules[] = $rec["module"];
297 }
298 return $modules;
299 }
300
301
310 public static function _getRemarks($a_lang_key, $a_all_changed = false)
311 {
312 global $ilDB, $lng;
313
314 $q = "SELECT module, identifier, remarks"
315 . " FROM lng_data"
316 . " WHERE lang_key = ".$ilDB->quote($a_lang_key, "text");
317
318 if ($a_all_changed)
319 {
320 $q .= " AND (remarks IS NOT NULL OR local_change IS NOT NULL)";
321 }
322 else
323 {
324 $q .= " AND remarks IS NOT NULL";
325 }
326
327 $result = $ilDB->query($q);
328
329 $remarks = array();
330 while ($row = $ilDB->fetchAssoc($result))
331 {
332 $remarks[$row["module"].$lng->separator.$row["identifier"]] = $row["remarks"];
333 }
334 return $remarks;
335 }
336
337
349 public static function _getValues($a_lang_key,
350 $a_modules = array(), $a_topics = array(),
351 $a_pattern = '', $a_state = '')
352 {
353 global $ilDB, $lng;
354
355 $q = "SELECT * FROM lng_data WHERE".
356 " lang_key = ".$ilDB->quote($a_lang_key, "text")." ";
357
358 if (is_array($a_modules) && count($a_modules) > 0)
359 {
360 $q .= " AND ".$ilDB->in("module", $a_modules, false, "text");
361 }
362 if (is_array($a_topics) && count($a_topics) > 0)
363 {
364 $q .= " AND ".$ilDB->in("identifier", $a_topics, false, "text");
365 }
366 if ($a_pattern)
367 {
368 $q .= " AND ".$ilDB->like("value", "text", "%".$a_pattern."%");
369 }
370 if ($a_state == "changed")
371 {
372 $q .= " AND NOT local_change IS NULL ";
373 }
374 if ($a_state == "unchanged")
375 {
376 $q .= " AND local_change IS NULL ";
377 }
378 $q .= " ORDER BY module, identifier";
379
380 $set = $ilDB->query($q);
381
382 $values = array();
383 while ($rec = $set->fetchRow(DB_FETCHMODE_ASSOC))
384 {
385 $values[$rec["module"].$lng->separator.$rec["identifier"]] = $rec["value"];
386 }
387 return $values;
388 }
389
390
391
392
401 public static function _saveValues($a_lang_key, $a_values = array(), $a_remarks = array())
402 {
403 global $ilDB, $lng;
404
405 if (!is_array($a_values))
406 {
407 return;
408 }
409 $save_array = array();
410 $save_date = date("Y-m-d H:i:s", time());
411
412 // read and get the global values
413 require_once "./Services/Language/classes/class.ilLanguageFile.php";
414 $global_file_obj = ilLanguageFile::_getGlobalLanguageFile($a_lang_key);
415 $global_values = $global_file_obj->getAllValues();
416 $global_comments = $global_file_obj->getAllComments();
417
418 // save the single translations in lng_data
419 foreach ($a_values as $key => $value)
420 {
421 $keys = explode($lng->separator, $key);
422 if (count($keys) == 2)
423 {
424 $module = $keys[0];
425 $topic = $keys[1];
426 $save_array[$module][$topic] = $value;
427
428 if ($global_values[$key] != $value
429 or $global_comments[$key] != $a_remarks[$key])
430 {
431 $local_change = $save_date;
432 }
433 else
434 {
435 $local_change = null;
436 }
437
438 ilObjLanguage::replaceLangEntry($module, $topic,
439 $a_lang_key, $value, $local_change, $a_remarks[$key]);
440 }
441 }
442
443 // save the serialized module entries in lng_modules
444 foreach ($save_array as $module => $entries)
445 {
446 $set = $ilDB->query(sprintf("SELECT * FROM lng_modules " .
447 "WHERE lang_key = %s AND module = %s",
448 $ilDB->quote($a_lang_key, "text"), $ilDB->quote($module, "text")));
449 $row = $ilDB->fetchAssoc($set);
450
451 $arr = unserialize($row["lang_array"]);
452 if (is_array($arr))
453 {
454 $entries = array_merge($arr, $entries);
455 }
456 ilObjLanguage::replaceLangModule($a_lang_key, $module, $entries);
457 }
458
459
460 require_once('class.ilCachedLanguage.php');
461 ilCachedLanguage::getInstance($a_lang_key)->flush();
462 }
463
464
472 public static function _deleteValues($a_lang_key, $a_values = array())
473 {
474 global $ilDB, $lng;
475
476 if (!is_array($a_values))
477 {
478 return;
479 }
480 $delete_array = array();
481
482 // save the single translations in lng_data
483 foreach ($a_values as $key => $value)
484 {
485 $keys = explode($lng->separator, $key);
486 if (count($keys) == 2)
487 {
488 $module = $keys[0];
489 $topic = $keys[1];
490 $delete_array[$module][$topic] = $value;
491
492 ilObjLanguage::deleteLangEntry($module, $topic, $a_lang_key);
493 }
494 }
495
496 // save the serialized module entries in lng_modules
497 foreach ($delete_array as $module => $entries)
498 {
499 $set = $ilDB->query(sprintf("SELECT * FROM lng_modules " .
500 "WHERE lang_key = %s AND module = %s",
501 $ilDB->quote($a_lang_key, "text"), $ilDB->quote($module, "text")));
502 $row = $ilDB->fetchAssoc($set);
503
504 $arr = unserialize($row["lang_array"]);
505 if (is_array($arr))
506 {
507 $entries = array_diff_key($arr, $entries);
508 }
509 ilObjLanguage::replaceLangModule($a_lang_key, $module, $entries);
510 }
511 }
512
513} // END class.ilObjLanguageExt
514?>
$result
const DB_FETCHMODE_ASSOC
Definition: class.ilDB.php:10
Class ilLanguageFile.
static _getGlobalLanguageFile($a_lang_key)
Read and get a global language file as a singleton object.
Class ilObjLanguageExt.
getGlobalLanguageFile()
Read and get the global language file as an object.
getCommentedValues($a_modules=array(), $a_pattern='')
Get all values from the database for wich the global language file has a comment.
getAllValues($a_modules=array(), $a_pattern='')
Get all values from the database.
importLanguageFile($a_file, $a_mode_existing='keepnew')
Import a language file into the ilias database.
ilObjLanguageExt($a_id=0, $a_call_by_reference=false)
Constructor.
static _getValues($a_lang_key, $a_modules=array(), $a_topics=array(), $a_pattern='', $a_state='')
Get the translations of specified topics.
getUnchangedValues($a_modules=array(), $a_pattern='')
Get only the unchanged values from the database which are equal to the original language file.
static _deleteValues($a_lang_key, $a_values=array())
Delete a set of translation in the database.
getLongDescription()
Get the full language description.
static _getModules($a_lang_key)
Get all modules of a language.
getMergedValues()
Get the local values merged into the values of the global language file.
getCustLangPath()
Get the customized language files path.
static _getRemarks($a_lang_key, $a_all_changed=false)
Get all remarks of a language.
setLocal($a_local=true)
Set the local status of the language.
getAddedValues($a_modules=array(), $a_pattern='')
Get only the entries which don't exist in the global language file.
getLangPath()
Get the language files path.
static _saveValues($a_lang_key, $a_values=array(), $a_remarks=array())
Save a set of translation in the database.
getMergedRemarks()
Get the local remarks merged into the remarks of the global language file.
getChangedValues($a_modules=array(), $a_pattern='')
Get only the changed values from the database which differ from the original language file.
getAllRemarks()
Get all remarks from the database.
Class ilObjLanguage.
static _deleteLangData($a_lang_key, $a_keep_local_change=false)
Delete languge data.
ilObjLanguage($a_id=0, $a_call_by_reference=false)
Constructor.
static deleteLangEntry($a_module, $a_identifier, $a_lang_key)
Delete lang entry.
isInstalled()
Check language object status, and return true if language is installed.
static replaceLangEntry($a_module, $a_identifier, $a_lang_key, $a_value, $a_local_change=null, $a_remarks=null)
Replace lang entry.
update()
update object in db
setDescription($a_desc)
set object description
global $ilDB