ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilObjLanguageExt.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_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";
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 
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 
468  public static function _deleteValues($a_lang_key, $a_values = array())
469  {
470  global $ilDB, $lng;
471 
472  if (!is_array($a_values))
473  {
474  return;
475  }
476  $delete_array = array();
477 
478  // save the single translations in lng_data
479  foreach ($a_values as $key => $value)
480  {
481  $keys = explode($lng->separator, $key);
482  if (count($keys) == 2)
483  {
484  $module = $keys[0];
485  $topic = $keys[1];
486  $delete_array[$module][$topic] = $value;
487 
488  ilObjLanguage::deleteLangEntry($module, $topic, $a_lang_key);
489  }
490  }
491 
492  // save the serialized module entries in lng_modules
493  foreach ($delete_array as $module => $entries)
494  {
495  $set = $ilDB->query(sprintf("SELECT * FROM lng_modules " .
496  "WHERE lang_key = %s AND module = %s",
497  $ilDB->quote($a_lang_key, "text"), $ilDB->quote($module, "text")));
498  $row = $ilDB->fetchAssoc($set);
499 
500  $arr = unserialize($row["lang_array"]);
501  if (is_array($arr))
502  {
503  $entries = array_diff_key($arr, $entries);
504  }
505  ilObjLanguage::replaceLangModule($a_lang_key, $module, $entries);
506  }
507  }
508 
509 } // END class.ilObjLanguageExt
510 ?>