ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilObjLanguageExt.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 require_once "./Services/Language/classes/class.ilObjLanguage.php";
25 
35 {
36 
40  function ilObjLanguageExt($a_id = 0, $a_call_by_reference = false)
41  {
42  $this->ilObjLanguage($a_id, $a_call_by_reference);
43  }
44 
49  public function getGlobalLanguageFile()
50  {
51  require_once "./Services/Language/classes/class.ilLanguageFile.php";
52  return ilLanguageFile::_getGlobalLanguageFile($this->key);
53  }
54 
60  public function setLocal($a_local = true)
61  {
62  if ($this->isInstalled())
63  {
64  if ($a_local == true)
65  {
66  $this->setDescription("installed_local");
67  }
68  else
69  {
70  $this->setDescription("installed");
71  }
72  $this->update();
73  }
74  }
75 
76 
82  public function getLongDescription()
83  {
84  return $this->lng->txt($this->desc);
85  }
86 
92  public function getLangPath()
93  {
94  return $this->lang_path;
95  }
96 
102  public function getCustLangPath()
103  {
104  return $this->cust_lang_path;
105  }
106 
114  public function getAllValues($a_modules = array(), $a_pattern = '')
115  {
116  return self::_getValues($this->key, $a_modules, NULL, $a_pattern);
117  }
118 
119 
128  public function getChangedValues($a_modules = array(), $a_pattern = '')
129  {
130  return self::_getValues($this->key, $a_modules, NULL, $a_pattern, 'changed');
131  }
132 
133 
142  public function getUnchangedValues($a_modules = array(), $a_pattern = '')
143  {
144  return self::_getValues($this->key, $a_modules, NULL, $a_pattern, 'unchanged');
145  }
146 
147 
156  public function getCommentedValues($a_modules = array(), $a_pattern = '')
157  {
158  $global_file_obj = $this->getGlobalLanguageFile();
159  $global_values = $global_file_obj->getAllValues();
160  $local_values = self::_getValues($this->key, $a_modules, NULL, $a_pattern);
161 
162  $commented = array();
163  foreach ($local_values as $key => $value)
164  {
165  if ($global_comments[$key] != "")
166  {
167  $commented[$key] = $value;
168  }
169  }
170  return $commented;
171  }
172 
173 
180  public function importLanguageFile($a_file, $a_mode_existing = 'keepnew')
181  {
182  global $ilDB, $ilErr;
183 
184  // read the new language file
185  require_once "./Services/Language/classes/class.ilLanguageFile.php";
186  $import_file_obj = new ilLanguageFile($a_file);
187  if (!$import_file_obj->read())
188  {
189  $ilErr->raiseError($import_file_obj->getErrorMessage(),$ilErr->MESSAGE);
190  }
191 
192  switch($a_mode_existing)
193  {
194  // keep all existing entries
195  case 'keepall':
196  $to_keep = $this->getAllValues();
197  break;
198 
199  // keep existing online changes
200  case 'keepnew':
201  $to_keep = $this->getChangedValues();
202  break;
203 
204  // replace all existing definitions
205  case 'replace':
206  $to_keep = array();
207  break;
208 
209  // delete all existing entries
210  case 'delete':
211  $query = "DELETE FROM lng_data WHERE lang_key='".$this->key."'";
212  $ilDB->query($query);
213  $query = "DELETE FROM lng_modules WHERE lang_key='".$this->key."'";
214  $ilDB->query($query);
215  $to_keep = array();
216  break;
217 
218  default:
219  return;
220  }
221 
222  // process the values of the import file
223  $to_save = array();
224  foreach ($import_file_obj->getAllValues() as $key => $value)
225  {
226  if (!isset($to_keep[$key]))
227  {
228  $to_save[$key] = $value;
229  }
230  }
231  self::_saveValues($this->key, $to_save);
232  }
233 
241  public static function _getModules($a_lang_key)
242  {
243  global $ilDB;
244 
245  $q = "SELECT DISTINCT module FROM lng_data WHERE ".
246  " lang_key = ".$ilDB->quote($a_lang_key).
247  " order by module";
248  $set = $ilDB->query($q);
249 
250  while ($rec = $set->fetchRow(DB_FETCHMODE_ASSOC))
251  {
252  $modules[] = $rec["module"];
253  }
254  return $modules;
255  }
256 
268  public static function _getValues($a_lang_key,
269  $a_modules = array(), $a_topics = array(),
270  $a_pattern = '', $a_state = '')
271  {
272  global $ilDB, $lng;
273 
274  if (is_array($a_modules))
275  {
276  for ($i = 0; $i < count($a_modules); $i++)
277  {
278  $a_modules[$i] = $ilDB->quote($a_modules[$i]);
279  }
280  $modules_list = implode(',', $a_modules);
281  }
282  if (is_array($a_topics))
283  {
284  for ($i = 0; $i < count($a_topics); $i++)
285  {
286  $a_topics[$i] = $ilDB->quote($a_topics[$i]);
287  }
288  $topics_list = implode(',', $a_topics);
289  }
290 
291  $q = "SELECT * FROM lng_data WHERE".
292  " lang_key =".$ilDB->quote($a_lang_key);
293  if ($modules_list)
294  {
295  $q .= " AND module in (". $modules_list. ")";
296  }
297  if ($topics_list)
298  {
299  $q .= " AND identifier in (". $topics_list. ")";
300  }
301  if ($a_pattern)
302  {
303  $q .= " AND value like ". $ilDB->quote("%".$a_pattern."%");
304  }
305  if ($a_state == "changed")
306  {
307  $q .= " AND local_change <> '0000-00-00 00:00:00'";
308  }
309  if ($a_state == "unchanged")
310  {
311  $q .= " AND local_change = '0000-00-00 00:00:00'";
312  }
313  $q .= " ORDER BY module, identifier";
314  $set = $ilDB->query($q);
315 
316  $values = array();
317  while ($rec = $set->fetchRow(DB_FETCHMODE_ASSOC))
318  {
319  $values[$rec["module"].$lng->separator.$rec["identifier"]] = $rec["value"];
320  }
321  return $values;
322  }
323 
331  public static function _saveValues($a_lang_key, $a_values = array())
332  {
333  global $ilDB, $lng;
334 
335  if (!is_array($a_values))
336  {
337  return;
338  }
339  $save_array = array();
340  $save_date = date("Y-m-d H:i:s", time());
341 
342  // read and get the global values
343  require_once "./Services/Language/classes/class.ilLanguageFile.php";
344  $global_file_obj = ilLanguageFile::_getGlobalLanguageFile($a_lang_key);
345  $global_values = $global_file_obj->getAllValues();
346 
347  // save the single translations in lng_data
348  foreach ($a_values as $key => $value)
349  {
350  $keys = explode($lng->separator, $key);
351  if (count($keys) == 2)
352  {
353  $module = $keys[0];
354  $topic = $keys[1];
355  $save_array[$module][$topic] = $value;
356  $local_change = $global_values[$key] == $value ?
357  "0000-00-00 00:00:00" : $save_date;
358 
359  $q = "REPLACE INTO lng_data(lang_key, module, identifier, value, local_change)"
360  . " VALUES("
361  . $ilDB->quote($a_lang_key). ","
362  . $ilDB->quote($module). ","
363  . $ilDB->quote($topic). ","
364  . $ilDB->quote($value). ","
365  . $ilDB->quote($local_change). ")";
366  $ilDB->query($q);
367  }
368  }
369 
370  // save the serialized module entries in lng_modules
371  foreach ($save_array as $module => $entries)
372  {
373  $q = "SELECT * FROM lng_modules WHERE ".
374  " lang_key = ".$ilDB->quote($a_lang_key).
375  " AND module = ".$ilDB->quote($module);
376  $set = $ilDB->query($q);
377  $row = $set->fetchRow(DB_FETCHMODE_ASSOC);
378  $arr = unserialize($row["lang_array"]);
379  if (is_array($arr))
380  {
381  $entries = array_merge($arr, $entries);
382  }
383  $q = "REPLACE INTO lng_modules (lang_key, module, lang_array) VALUES ".
384  "(".$ilDB->quote($a_lang_key).", " .
385  " ".$ilDB->quote($module).", " .
386  " ".$ilDB->quote(serialize($entries)).") ";
387  $ilDB->query($q);
388  }
389  }
390 } // END class.ilObjLanguageExt
391 ?>