ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 public function __construct($a_id = 0, $a_call_by_reference = false)
21 {
22 parent::__construct($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 if ($a_local == true) {
44 $this->setDescription("installed_local");
45 } else {
46 $this->setDescription("installed");
47 }
48 $this->update();
49 }
50 }
51
52
58 public function getLongDescription()
59 {
60 return $this->lng->txt($this->desc);
61 }
62
63
68 public function getDataPath()
69 {
70 if (!is_dir(CLIENT_DATA_DIR . '/lang_data')) {
71 ilUtil::makeDir(CLIENT_DATA_DIR . '/lang_data');
72 }
73 return CLIENT_DATA_DIR . '/lang_data';
74 }
75
81 public function getLangPath()
82 {
83 return $this->lang_path;
84 }
85
91 public function getCustLangPath()
92 {
93 return $this->cust_lang_path;
94 }
95
101 public function getAllRemarks()
102 {
103 return self::_getRemarks($this->key);
104 }
105
114 public function getAllValues($a_modules = array(), $a_pattern = '', $a_topics = array())
115 {
116 return self::_getValues($this->key, $a_modules, $a_topics, $a_pattern);
117 }
118
119
129 public function getChangedValues($a_modules = array(), $a_pattern = '', $a_topics = array())
130 {
131 return self::_getValues($this->key, $a_modules, $a_topics, $a_pattern, 'changed');
132 }
133
134
144 public function getUnchangedValues($a_modules = array(), $a_pattern = '', $a_topics = array())
145 {
146 return self::_getValues($this->key, $a_modules, $a_topics, $a_pattern, 'unchanged');
147 }
148
157 public function getAddedValues($a_modules = array(), $a_pattern = '', $a_topics = array())
158 {
159 $global_file_obj = $this->getGlobalLanguageFile();
160 $global_values = $global_file_obj->getAllValues();
161 $local_values = self::_getValues($this->key, $a_modules, $a_topics, $a_pattern);
162
163 return array_diff_key($local_values, $global_values);
164 }
165
166
178 public function getCommentedValues($a_modules = array(), $a_pattern = '', $a_topics = array())
179 {
180 $global_file_obj = $this->getGlobalLanguageFile();
181 $global_comments = $global_file_obj->getAllComments();
182 $local_values = self::_getValues($this->key, $a_modules, $a_topics, $a_pattern);
183
184 return array_intersect_key($local_values, $global_comments);
185 }
186
187
199 public function getMergedValues()
200 {
201 $global_file_obj = $this->getGlobalLanguageFile();
202 $global_values = $global_file_obj->getAllValues();
203 $local_values = self::_getValues($this->key);
204
205 return array_merge($global_values, $local_values);
206 }
207
219 public function getMergedRemarks()
220 {
221 $global_file_obj = $this->getGlobalLanguageFile();
222 $global_comments = $global_file_obj->getAllComments();
223
224 // get remarks including empty remarks for local changes
225 $local_remarks = self::_getRemarks($this->key, true);
226
227 return array_merge($global_comments, $local_remarks);
228 }
229
230
231
238 public function importLanguageFile($a_file, $a_mode_existing = 'keepnew')
239 {
240 global $DIC;
241 $ilDB = $DIC->database();
243 $ilErr = $DIC['ilErr'];
244
245 // read the new language file
246 require_once "./Services/Language/classes/class.ilLanguageFile.php";
247 $import_file_obj = new ilLanguageFile($a_file);
248 if (!$import_file_obj->read()) {
249 $ilErr->raiseError($import_file_obj->getErrorMessage(), $ilErr->MESSAGE);
250 }
251
252 switch ($a_mode_existing) {
253 // keep all existing entries
254 case 'keepall':
255 $to_keep = $this->getAllValues();
256 break;
257
258 // keep existing online changes
259 case 'keepnew':
260 $to_keep = $this->getChangedValues();
261 break;
262
263 // replace all existing definitions
264 case 'replace':
265 $to_keep = array();
266 break;
267
268 // delete all existing entries
269 case 'delete':
270 ilObjLanguage::_deleteLangData($this->key, false);
271 $ilDB->manipulate("DELETE FROM lng_modules WHERE lang_key = " .
272 $ilDB->quote($this->key, "text"));
273 $to_keep = array();
274 break;
275
276 default:
277 return;
278 }
279
280 // process the values of the import file
281 $to_save = array();
282 foreach ($import_file_obj->getAllValues() as $key => $value) {
283 if (!isset($to_keep[$key])) {
284 $to_save[$key] = $value;
285 }
286 }
287 self::_saveValues($this->key, $to_save, $import_file_obj->getAllComments());
288 }
289
297 public static function _getModules($a_lang_key)
298 {
299 global $DIC;
300 $ilDB = $DIC->database();
301
302 $q = "SELECT DISTINCT module FROM lng_data WHERE " .
303 " lang_key = " . $ilDB->quote($a_lang_key, "text") . " order by module";
304 $set = $ilDB->query($q);
305
306 $modules = array();
307 while ($rec = $set->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) {
308 $modules[] = $rec["module"];
309 }
310 return $modules;
311 }
312
313
322 public static function _getRemarks($a_lang_key, $a_all_changed = false)
323 {
324 global $DIC;
325 $ilDB = $DIC->database();
326 $lng = $DIC->language();
327
328 $q = "SELECT module, identifier, remarks"
329 . " FROM lng_data"
330 . " WHERE lang_key = " . $ilDB->quote($a_lang_key, "text");
331
332 if ($a_all_changed) {
333 $q .= " AND (remarks IS NOT NULL OR local_change IS NOT NULL)";
334 } else {
335 $q .= " AND remarks IS NOT NULL";
336 }
337
338 $result = $ilDB->query($q);
339
340 $remarks = array();
341 while ($row = $ilDB->fetchAssoc($result)) {
342 $remarks[$row["module"] . $lng->separator . $row["identifier"]] = $row["remarks"];
343 }
344 return $remarks;
345 }
346
347
359 public static function _getValues(
360 $a_lang_key,
361 $a_modules = array(),
362 $a_topics = array(),
363 $a_pattern = '',
364 $a_state = ''
365 ) {
366 global $DIC;
367 $ilDB = $DIC->database();
368 $lng = $DIC->language();
369
370 $q = "SELECT * FROM lng_data WHERE" .
371 " lang_key = " . $ilDB->quote($a_lang_key, "text") . " ";
372
373 if (is_array($a_modules) && count($a_modules) > 0) {
374 $q .= " AND " . $ilDB->in("module", $a_modules, false, "text");
375 }
376 if (is_array($a_topics) && count($a_topics) > 0) {
377 $q .= " AND " . $ilDB->in("identifier", $a_topics, false, "text");
378 }
379 if ($a_pattern) {
380 $q .= " AND " . $ilDB->like("value", "text", "%" . $a_pattern . "%");
381 }
382 if ($a_state == "changed") {
383 $q .= " AND NOT local_change IS NULL ";
384 }
385 if ($a_state == "unchanged") {
386 $q .= " AND local_change IS NULL ";
387 }
388 $q .= " ORDER BY module, identifier";
389
390 $set = $ilDB->query($q);
391
392 $values = array();
393 while ($rec = $set->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) {
394 $values[$rec["module"] . $lng->separator . $rec["identifier"]] = $rec["value"];
395 }
396 return $values;
397 }
398
399
400
401
410 public static function _saveValues($a_lang_key, $a_values = array(), $a_remarks = array())
411 {
412 global $DIC;
413 $ilDB = $DIC->database();
414 $lng = $DIC->language();
415
416 if (!is_array($a_values)) {
417 return;
418 }
419 $save_array = array();
420 $save_date = date("Y-m-d H:i:s", time());
421
422 // read and get the global values
423 require_once "./Services/Language/classes/class.ilLanguageFile.php";
424 $global_file_obj = ilLanguageFile::_getGlobalLanguageFile($a_lang_key);
425 $global_values = $global_file_obj->getAllValues();
426 $global_comments = $global_file_obj->getAllComments();
427
428 // save the single translations in lng_data
429 foreach ($a_values as $key => $value) {
430 $keys = explode($lng->separator, $key);
431 if (count($keys) == 2) {
432 $module = $keys[0];
433 $topic = $keys[1];
434 $save_array[$module][$topic] = $value;
435
436 if ($global_values[$key] != $value
437 or $global_comments[$key] != $a_remarks[$key]) {
438 $local_change = $save_date;
439 } else {
440 $local_change = null;
441 }
442
444 $module,
445 $topic,
446 $a_lang_key,
447 $value,
448 $local_change,
449 $a_remarks[$key]
450 );
451 }
452 }
453
454 // save the serialized module entries in lng_modules
455 foreach ($save_array as $module => $entries) {
456 $set = $ilDB->query(sprintf(
457 "SELECT * FROM lng_modules " .
458 "WHERE lang_key = %s AND module = %s",
459 $ilDB->quote($a_lang_key, "text"),
460 $ilDB->quote($module, "text")
461 ));
462 $row = $ilDB->fetchAssoc($set);
463
464 $arr = unserialize($row["lang_array"]);
465 if (is_array($arr)) {
466 $entries = array_merge($arr, $entries);
467 }
468 ilObjLanguage::replaceLangModule($a_lang_key, $module, $entries);
469 }
470
471
472 require_once('class.ilCachedLanguage.php');
473 ilCachedLanguage::getInstance($a_lang_key)->flush();
474 }
475
476
484 public static function _deleteValues($a_lang_key, $a_values = array())
485 {
486 global $DIC;
487 $ilDB = $DIC->database();
488 $lng = $DIC->language();
489
490 if (!is_array($a_values)) {
491 return;
492 }
493 $delete_array = array();
494
495 // save the single translations in lng_data
496 foreach ($a_values as $key => $value) {
497 $keys = explode($lng->separator, $key);
498 if (count($keys) == 2) {
499 $module = $keys[0];
500 $topic = $keys[1];
501 $delete_array[$module][$topic] = $value;
502
503 ilObjLanguage::deleteLangEntry($module, $topic, $a_lang_key);
504 }
505 }
506
507 // save the serialized module entries in lng_modules
508 foreach ($delete_array as $module => $entries) {
509 $set = $ilDB->query(sprintf(
510 "SELECT * FROM lng_modules " .
511 "WHERE lang_key = %s AND module = %s",
512 $ilDB->quote($a_lang_key, "text"),
513 $ilDB->quote($module, "text")
514 ));
515 $row = $ilDB->fetchAssoc($set);
516
517 $arr = unserialize($row["lang_array"]);
518 if (is_array($arr)) {
519 $entries = array_diff_key($arr, $entries);
520 }
521 ilObjLanguage::replaceLangModule($a_lang_key, $module, $entries);
522 }
523 }
524} // END class.ilObjLanguageExt
$result
An exception for terminatinating execution or to throw for unit testing.
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.
getAddedValues($a_modules=array(), $a_pattern='', $a_topics=array())
Get only the entries which don't exist in the global language file.
getAllValues($a_modules=array(), $a_pattern='', $a_topics=array())
Get all values from the database.
getChangedValues($a_modules=array(), $a_pattern='', $a_topics=array())
Get only the changed values from the database which differ from 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.
__construct($a_id=0, $a_call_by_reference=false)
Constructor.
getMergedValues()
Get the local values merged into the values of the global language file.
getCustLangPath()
Get the customized language files path.
getDataPath()
Get the path for language data written by ILIAS.
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.
getCommentedValues($a_modules=array(), $a_pattern='', $a_topics=array())
Get all values from the database for wich the global language file has a comment.
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.
getUnchangedValues($a_modules=array(), $a_pattern='', $a_topics=array())
Get only the unchanged values from the database which are equal to the original language file.
getMergedRemarks()
Get the local remarks merged into the remarks of the global language file.
static _getValues( $a_lang_key, $a_modules=array(), $a_topics=array(), $a_pattern='', $a_state='')
Get the translations of specified topics.
getAllRemarks()
Get all remarks from the database.
Class ilObjLanguage.
static _deleteLangData($a_lang_key, $a_keep_local_change=false)
Delete languge data.
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
static makeDir($a_dir)
creates a new directory and inherits all filesystem permissions of the parent directory You may pass ...
$keys
$row
if($modEnd===false) $module
Definition: module.php:59
$ilErr
Definition: raiseError.php:18
global $DIC
Definition: saml.php:7
global $ilDB
$values