ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 
4 require_once "./Services/Language/classes/class.ilObjLanguage.php";
5 
15 {
16 
20  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";
32  return ilLanguageFile::_getGlobalLanguageFile($this->key);
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 
67 
72  public function getDataPath()
73  {
74  if (!is_dir(CLIENT_DATA_DIR.'/lang_data'))
75  {
76  ilUtil::makeDir(CLIENT_DATA_DIR.'/lang_data');
77  }
78  return CLIENT_DATA_DIR.'/lang_data';
79  }
80 
86  public function getLangPath()
87  {
88  return $this->lang_path;
89  }
90 
96  public function getCustLangPath()
97  {
98  return $this->cust_lang_path;
99  }
100 
106  public function getAllRemarks()
107  {
108  return self::_getRemarks($this->key);
109  }
110 
119  public function getAllValues($a_modules = array(), $a_pattern = '', $a_topics = array())
120  {
121  return self::_getValues($this->key, $a_modules, $a_topics, $a_pattern);
122  }
123 
124 
134  public function getChangedValues($a_modules = array(), $a_pattern = '', $a_topics = array())
135  {
136  return self::_getValues($this->key, $a_modules, $a_topics, $a_pattern, 'changed');
137  }
138 
139 
149  public function getUnchangedValues($a_modules = array(), $a_pattern = '', $a_topics = array())
150  {
151  return self::_getValues($this->key, $a_modules, $a_topics, $a_pattern, 'unchanged');
152  }
153 
162  public function getAddedValues($a_modules = array(), $a_pattern = '', $a_topics = array())
163  {
164  $global_file_obj = $this->getGlobalLanguageFile();
165  $global_values = $global_file_obj->getAllValues();
166  $local_values = self::_getValues($this->key, $a_modules, $a_topics, $a_pattern);
167 
168  return array_diff_key($local_values, $global_values);
169  }
170 
171 
183  public function getCommentedValues($a_modules = array(), $a_pattern = '', $a_topics = array())
184  {
185  $global_file_obj = $this->getGlobalLanguageFile();
186  $global_comments = $global_file_obj->getAllComments();
187  $local_values = self::_getValues($this->key, $a_modules, $a_topics, $a_pattern);
188 
189  return array_intersect_key($local_values, $global_comments);
190  }
191 
192 
204  public function getMergedValues()
205  {
206 
207  $global_file_obj = $this->getGlobalLanguageFile();
208  $global_values = $global_file_obj->getAllValues();
209  $local_values = self::_getValues($this->key);
210 
211  return array_merge($global_values, $local_values);
212  }
213 
225  public function getMergedRemarks()
226  {
227 
228  $global_file_obj = $this->getGlobalLanguageFile();
229  $global_comments = $global_file_obj->getAllComments();
230 
231  // get remarks including empty remarks for local changes
232  $local_remarks = self::_getRemarks($this->key, true);
233 
234  return array_merge($global_comments, $local_remarks);
235  }
236 
237 
238 
245  public function importLanguageFile($a_file, $a_mode_existing = 'keepnew')
246  {
247  global $ilDB, $ilErr;
248 
249  // read the new language file
250  require_once "./Services/Language/classes/class.ilLanguageFile.php";
251  $import_file_obj = new ilLanguageFile($a_file);
252  if (!$import_file_obj->read())
253  {
254  $ilErr->raiseError($import_file_obj->getErrorMessage(),$ilErr->MESSAGE);
255  }
256 
257  switch($a_mode_existing)
258  {
259  // keep all existing entries
260  case 'keepall':
261  $to_keep = $this->getAllValues();
262  break;
263 
264  // keep existing online changes
265  case 'keepnew':
266  $to_keep = $this->getChangedValues();
267  break;
268 
269  // replace all existing definitions
270  case 'replace':
271  $to_keep = array();
272  break;
273 
274  // delete all existing entries
275  case 'delete':
276  ilObjLanguage::_deleteLangData($this->key, false);
277  $ilDB->manipulate("DELETE FROM lng_modules WHERE lang_key = ".
278  $ilDB->quote($this->key, "text"));
279  $to_keep = array();
280  break;
281 
282  default:
283  return;
284  }
285 
286  // process the values of the import file
287  $to_save = array();
288  foreach ($import_file_obj->getAllValues() as $key => $value)
289  {
290  if (!isset($to_keep[$key]))
291  {
292  $to_save[$key] = $value;
293  }
294  }
295  self::_saveValues($this->key, $to_save, $import_file_obj->getAllComments());
296  }
297 
305  public static function _getModules($a_lang_key)
306  {
307  global $ilDB;
308 
309  $q = "SELECT DISTINCT module FROM lng_data WHERE ".
310  " lang_key = ".$ilDB->quote($a_lang_key, "text")." order by module";
311  $set = $ilDB->query($q);
312 
313  $modules = array();
314  while ($rec = $set->fetchRow(ilDBConstants::FETCHMODE_ASSOC))
315  {
316  $modules[] = $rec["module"];
317  }
318  return $modules;
319  }
320 
321 
330  public static function _getRemarks($a_lang_key, $a_all_changed = false)
331  {
332  global $ilDB, $lng;
333 
334  $q = "SELECT module, identifier, remarks"
335  . " FROM lng_data"
336  . " WHERE lang_key = ".$ilDB->quote($a_lang_key, "text");
337 
338  if ($a_all_changed)
339  {
340  $q .= " AND (remarks IS NOT NULL OR local_change IS NOT NULL)";
341  }
342  else
343  {
344  $q .= " AND remarks IS NOT NULL";
345  }
346 
347  $result = $ilDB->query($q);
348 
349  $remarks = array();
350  while ($row = $ilDB->fetchAssoc($result))
351  {
352  $remarks[$row["module"].$lng->separator.$row["identifier"]] = $row["remarks"];
353  }
354  return $remarks;
355  }
356 
357 
369  public static function _getValues($a_lang_key,
370  $a_modules = array(), $a_topics = array(),
371  $a_pattern = '', $a_state = '')
372  {
373  global $ilDB, $lng;
374 
375  $q = "SELECT * FROM lng_data WHERE".
376  " lang_key = ".$ilDB->quote($a_lang_key, "text")." ";
377 
378  if (is_array($a_modules) && count($a_modules) > 0)
379  {
380  $q .= " AND ".$ilDB->in("module", $a_modules, false, "text");
381  }
382  if (is_array($a_topics) && count($a_topics) > 0)
383  {
384  $q .= " AND ".$ilDB->in("identifier", $a_topics, false, "text");
385  }
386  if ($a_pattern)
387  {
388  $q .= " AND ".$ilDB->like("value", "text", "%".$a_pattern."%");
389  }
390  if ($a_state == "changed")
391  {
392  $q .= " AND NOT local_change IS NULL ";
393  }
394  if ($a_state == "unchanged")
395  {
396  $q .= " AND local_change IS NULL ";
397  }
398  $q .= " ORDER BY module, identifier";
399 
400  $set = $ilDB->query($q);
401 
402  $values = array();
403  while ($rec = $set->fetchRow(ilDBConstants::FETCHMODE_ASSOC))
404  {
405  $values[$rec["module"].$lng->separator.$rec["identifier"]] = $rec["value"];
406  }
407  return $values;
408  }
409 
410 
411 
412 
421  public static function _saveValues($a_lang_key, $a_values = array(), $a_remarks = array())
422  {
423  global $ilDB, $lng;
424 
425  if (!is_array($a_values))
426  {
427  return;
428  }
429  $save_array = array();
430  $save_date = date("Y-m-d H:i:s", time());
431 
432  // read and get the global values
433  require_once "./Services/Language/classes/class.ilLanguageFile.php";
434  $global_file_obj = ilLanguageFile::_getGlobalLanguageFile($a_lang_key);
435  $global_values = $global_file_obj->getAllValues();
436  $global_comments = $global_file_obj->getAllComments();
437 
438  // save the single translations in lng_data
439  foreach ($a_values as $key => $value)
440  {
441  $keys = explode($lng->separator, $key);
442  if (count($keys) == 2)
443  {
444  $module = $keys[0];
445  $topic = $keys[1];
446  $save_array[$module][$topic] = $value;
447 
448  if ($global_values[$key] != $value
449  or $global_comments[$key] != $a_remarks[$key])
450  {
451  $local_change = $save_date;
452  }
453  else
454  {
455  $local_change = null;
456  }
457 
458  ilObjLanguage::replaceLangEntry($module, $topic,
459  $a_lang_key, $value, $local_change, $a_remarks[$key]);
460  }
461  }
462 
463  // save the serialized module entries in lng_modules
464  foreach ($save_array as $module => $entries)
465  {
466  $set = $ilDB->query(sprintf("SELECT * FROM lng_modules " .
467  "WHERE lang_key = %s AND module = %s",
468  $ilDB->quote($a_lang_key, "text"), $ilDB->quote($module, "text")));
469  $row = $ilDB->fetchAssoc($set);
470 
471  $arr = unserialize($row["lang_array"]);
472  if (is_array($arr))
473  {
474  $entries = array_merge($arr, $entries);
475  }
476  ilObjLanguage::replaceLangModule($a_lang_key, $module, $entries);
477  }
478 
479 
480  require_once('class.ilCachedLanguage.php');
481  ilCachedLanguage::getInstance($a_lang_key)->flush();
482  }
483 
484 
492  public static function _deleteValues($a_lang_key, $a_values = array())
493  {
494  global $ilDB, $lng;
495 
496  if (!is_array($a_values))
497  {
498  return;
499  }
500  $delete_array = array();
501 
502  // save the single translations in lng_data
503  foreach ($a_values as $key => $value)
504  {
505  $keys = explode($lng->separator, $key);
506  if (count($keys) == 2)
507  {
508  $module = $keys[0];
509  $topic = $keys[1];
510  $delete_array[$module][$topic] = $value;
511 
512  ilObjLanguage::deleteLangEntry($module, $topic, $a_lang_key);
513  }
514  }
515 
516  // save the serialized module entries in lng_modules
517  foreach ($delete_array as $module => $entries)
518  {
519  $set = $ilDB->query(sprintf("SELECT * FROM lng_modules " .
520  "WHERE lang_key = %s AND module = %s",
521  $ilDB->quote($a_lang_key, "text"), $ilDB->quote($module, "text")));
522  $row = $ilDB->fetchAssoc($set);
523 
524  $arr = unserialize($row["lang_array"]);
525  if (is_array($arr))
526  {
527  $entries = array_diff_key($arr, $entries);
528  }
529  ilObjLanguage::replaceLangModule($a_lang_key, $module, $entries);
530  }
531  }
532 
533 } // END class.ilObjLanguageExt
534 ?>
getMergedRemarks()
Get the local remarks merged into the remarks of the global language file.
global $ilErr
Definition: raiseError.php:16
static replaceLangEntry($a_module, $a_identifier, $a_lang_key, $a_value, $a_local_change=null, $a_remarks=null)
Replace lang entry.
getMergedValues()
Get the local values merged into the values of the global language file.
$result
getAllRemarks()
Get all remarks from the database.
static _getRemarks($a_lang_key, $a_all_changed=false)
Get all remarks of a language.
getCustLangPath()
Get the customized language files path.
getAllValues($a_modules=array(), $a_pattern='', $a_topics=array())
Get all values from the database.
Class ilObjLanguage.
static _deleteValues($a_lang_key, $a_values=array())
Delete 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...
getDataPath()
Get the path for language data written by ILIAS.
isInstalled()
Check language object status, and return true if language is installed.
getChangedValues($a_modules=array(), $a_pattern='', $a_topics=array())
Get only the changed values from the database which differ from the original language file...
getGlobalLanguageFile()
Read and get the global language file as an object.
Class ilObjLanguageExt.
__construct($a_id=0, $a_call_by_reference=false)
Constructor.
static _deleteLangData($a_lang_key, $a_keep_local_change=false)
Delete languge data.
date( 'd-M-Y', $objPHPExcel->getProperties() ->getCreated())
static deleteLangEntry($a_module, $a_identifier, $a_lang_key)
Delete lang entry.
static _getValues($a_lang_key, $a_modules=array(), $a_topics=array(), $a_pattern='', $a_state='')
Get the translations of specified topics.
Create styles array
The data for the language used.
static makeDir($a_dir)
creates a new directory and inherits all filesystem permissions of the parent directory You may pass ...
static _saveValues($a_lang_key, $a_values=array(), $a_remarks=array())
Save a set of translation in the database.
setLocal($a_local=true)
Set the local status of the language.
getLongDescription()
Get the full language description.
getAddedValues($a_modules=array(), $a_pattern='', $a_topics=array())
Get only the entries which don&#39;t exist in the global language file.
Class ilLanguageFile.
global $ilDB
setDescription($a_desc)
set object description
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
static _getModules($a_lang_key)
Get all modules of a language.
update()
update object in db
getLangPath()
Get the language files path.
getCommentedValues($a_modules=array(), $a_pattern='', $a_topics=array())
Get all values from the database for wich the global language file has a comment. ...
static _getGlobalLanguageFile($a_lang_key)
Read and get a global language file as a singleton object.
importLanguageFile($a_file, $a_mode_existing='keepnew')
Import a language file into the ilias database.