ILIAS  Release_4_0_x_branch Revision 61816
 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 
94  public function getAllValues($a_modules = array(), $a_pattern = '')
95  {
96  return self::_getValues($this->key, $a_modules, NULL, $a_pattern);
97  }
98 
99 
108  public function getChangedValues($a_modules = array(), $a_pattern = '')
109  {
110  return self::_getValues($this->key, $a_modules, NULL, $a_pattern, 'changed');
111  }
112 
113 
122  public function getUnchangedValues($a_modules = array(), $a_pattern = '')
123  {
124  return self::_getValues($this->key, $a_modules, NULL, $a_pattern, 'unchanged');
125  }
126 
127 
136  public function getCommentedValues($a_modules = array(), $a_pattern = '')
137  {
138  $global_file_obj = $this->getGlobalLanguageFile();
139  $global_values = $global_file_obj->getAllValues();
140  $local_values = self::_getValues($this->key, $a_modules, NULL, $a_pattern);
141 
142  $commented = array();
143  foreach ($local_values as $key => $value)
144  {
145  if ($global_comments[$key] != "")
146  {
147  $commented[$key] = $value;
148  }
149  }
150  return $commented;
151  }
152 
153 
160  public function importLanguageFile($a_file, $a_mode_existing = 'keepnew')
161  {
162  global $ilDB, $ilErr;
163 
164  // read the new language file
165  require_once "./Services/Language/classes/class.ilLanguageFile.php";
166  $import_file_obj = new ilLanguageFile($a_file);
167  if (!$import_file_obj->read())
168  {
169  $ilErr->raiseError($import_file_obj->getErrorMessage(),$ilErr->MESSAGE);
170  }
171 
172  switch($a_mode_existing)
173  {
174  // keep all existing entries
175  case 'keepall':
176  $to_keep = $this->getAllValues();
177  break;
178 
179  // keep existing online changes
180  case 'keepnew':
181  $to_keep = $this->getChangedValues();
182  break;
183 
184  // replace all existing definitions
185  case 'replace':
186  $to_keep = array();
187  break;
188 
189  // delete all existing entries
190  case 'delete':
191  ilObjLanguage::_deleteLangData($this->key, false);
192  $ilDB->manipulate("DELETE FROM lng_modules WHERE lang_key = ".
193  $ilDB->quote($this->key, "text"));
194  $to_keep = array();
195  break;
196 
197  default:
198  return;
199  }
200 
201  // process the values of the import file
202  $to_save = array();
203  foreach ($import_file_obj->getAllValues() as $key => $value)
204  {
205  if (!isset($to_keep[$key]))
206  {
207  $to_save[$key] = $value;
208  }
209  }
210  self::_saveValues($this->key, $to_save);
211  }
212 
220  public static function _getModules($a_lang_key)
221  {
222  global $ilDB;
223 
224  $q = "SELECT DISTINCT module FROM lng_data WHERE ".
225  " lang_key = ".$ilDB->quote($a_lang_key, "text")." order by module";
226  $set = $ilDB->query($q);
227 
228  while ($rec = $set->fetchRow(DB_FETCHMODE_ASSOC))
229  {
230  $modules[] = $rec["module"];
231  }
232  return $modules;
233  }
234 
246  public static function _getValues($a_lang_key,
247  $a_modules = array(), $a_topics = array(),
248  $a_pattern = '', $a_state = '')
249  {
250  global $ilDB, $lng;
251 
252  $q = "SELECT * FROM lng_data WHERE".
253  " lang_key = ".$ilDB->quote($a_lang_key, "text")." ";
254 
255  if (is_array($a_modules) && count($a_modules) > 0)
256  {
257  $q .= " AND ".$ilDB->in("module", $a_modules, false, "text");
258  }
259  if (is_array($a_topics) && count($a_topics) > 0)
260  {
261  $q .= " AND ".$ilDB->in("identifier", $a_topics, false, "text");
262  }
263  if ($a_pattern)
264  {
265  $q .= " AND ".$ilDB->like("value", "text", "%".$a_pattern."%");
266  }
267  if ($a_state == "changed")
268  {
269  $q .= " AND NOT local_change IS NULL ";
270  }
271  if ($a_state == "unchanged")
272  {
273  $q .= " AND local_change IS NULL ";
274  }
275  $q .= " ORDER BY module, identifier";
276 
277  $set = $ilDB->query($q);
278 
279  $values = array();
280  while ($rec = $set->fetchRow(DB_FETCHMODE_ASSOC))
281  {
282  $values[$rec["module"].$lng->separator.$rec["identifier"]] = $rec["value"];
283  }
284  return $values;
285  }
286 
294  public static function _saveValues($a_lang_key, $a_values = array())
295  {
296  global $ilDB, $lng;
297 
298  if (!is_array($a_values))
299  {
300  return;
301  }
302  $save_array = array();
303  $save_date = date("Y-m-d H:i:s", time());
304 
305  // read and get the global values
306  require_once "./Services/Language/classes/class.ilLanguageFile.php";
307  $global_file_obj = ilLanguageFile::_getGlobalLanguageFile($a_lang_key);
308  $global_values = $global_file_obj->getAllValues();
309 
310  // save the single translations in lng_data
311  foreach ($a_values as $key => $value)
312  {
313  $keys = explode($lng->separator, $key);
314  if (count($keys) == 2)
315  {
316  $module = $keys[0];
317  $topic = $keys[1];
318  $save_array[$module][$topic] = $value;
319  $local_change = $global_values[$key] == $value ?
320  null : $save_date;
321 
322  ilObjLanguage::replaceLangEntry($module, $topic,
323  $a_lang_key, $value, $local_change);
324  }
325  }
326 
327  // save the serialized module entries in lng_modules
328  foreach ($save_array as $module => $entries)
329  {
330  $set = $ilDB->query(sprintf("SELECT * FROM lng_modules " .
331  "WHERE lang_key = %s AND module = %s",
332  $ilDB->quote($a_lang_key, "text"), $ilDB->quote($module, "text")));
333  $row = $ilDB->fetchAssoc($set);
334 
335  $arr = unserialize($row["lang_array"]);
336  if (is_array($arr))
337  {
338  $entries = array_merge($arr, $entries);
339  }
340  ilObjLanguage::replaceLangModule($a_lang_key, $module, $entries);
341  }
342  }
343 } // END class.ilObjLanguageExt
344 ?>