Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 require_once "class.ilObject.php";
00025
00035 class ilObjLanguage extends ilObject
00036 {
00044 var $separator;
00045 var $comment_separator;
00046 var $lang_default;
00047 var $lang_user;
00048 var $lang_path;
00049
00050 var $key;
00051 var $status;
00052
00053
00061 function ilObjLanguage($a_id = 0, $a_call_by_reference = false)
00062 {
00063 global $lng;
00064
00065 $this->type = "lng";
00066 $this->ilObject($a_id,$a_call_by_reference);
00067
00068 $this->type = "lng";
00069 $this->key = $this->title;
00070 $this->status = $this->desc;
00071 $this->lang_default = $lng->lang_default;
00072 $this->lang_user = $lng->lang_user;
00073 $this->lang_path = $lng->lang_path;
00074 $this->separator = $lng->separator;
00075 $this->comment_separator = $lng->comment_separator;
00076 }
00077
00083 function getKey()
00084 {
00085 return $this->key;
00086 }
00087
00093 function getStatus()
00094 {
00095 return $this->status;
00096 }
00097
00101 function isSystemLanguage()
00102 {
00103 if ($this->key == $this->lang_default)
00104 return true;
00105 else
00106 return false;
00107 }
00108
00112 function isUserLanguage()
00113 {
00114 if ($this->key == $this->lang_user)
00115 {
00116 return true;
00117 }
00118 else
00119 {
00120 return false;
00121 }
00122 }
00123
00124
00130 function isInstalled()
00131 {
00132 if (substr($this->getStatus(), 0, 9) == "installed")
00133 {
00134 return true;
00135 }
00136 else
00137 {
00138 return false;
00139 }
00140 }
00141
00148 function isLocal()
00149 {
00150 if (substr($this->getStatus(), 10) == "local")
00151 {
00152 return true;
00153 }
00154 else
00155 {
00156 return false;
00157 }
00158 }
00159
00166 function install($scope = '')
00167 {
00168 if (!empty($scope))
00169 {
00170 if ($scope == 'global')
00171 {
00172 $scope = '';
00173 }
00174 else
00175 {
00176 $scopeExtension = '.' . $scope;
00177 }
00178 }
00179
00180 if (($this->isInstalled() == false) ||
00181 ($this->isInstalled() == true && $this->isLocal() == false && !empty($scope)))
00182 {
00183 if ($this->check($scope))
00184 {
00185
00186 if (empty($scope))
00187 {
00188 $this->flush();
00189 }
00190
00191
00192 $this->insert($scope);
00193
00194
00195 if (empty($scope))
00196 {
00197 $newDesc = 'installed';
00198 }
00199 else if ($scope == 'local')
00200 {
00201 $newDesc = 'installed_local';
00202 }
00203 $this->setDescription($newDesc);
00204 $this->update();
00205 $this->optimizeData();
00206 return $this->getKey();
00207 }
00208 }
00209 return "";
00210 }
00211
00212
00218 function uninstall()
00219 {
00220 if ((substr($this->status, 0, 9) == "installed") && ($this->key != $this->lang_default) && ($this->key != $this->lang_user))
00221 {
00222 $this->flush();
00223 $this->setTitle($this->key);
00224 $this->setDescription("not_installed");
00225 $this->update();
00226 $this->resetUserLanguage($this->key);
00227
00228 return $this->key;
00229 }
00230 return "";
00231 }
00232
00233
00237 function flush()
00238 {
00239 $query = "DELETE FROM lng_data WHERE lang_key='".$this->key."'";
00240 $this->ilias->db->query($query);
00241 }
00242
00243
00249 function insert($scope = '')
00250 {
00251 global $ilDB;
00252
00253 if (!empty($scope))
00254 {
00255 if ($scope == 'global')
00256 {
00257 $scope = '';
00258 }
00259 else
00260 {
00261 $scopeExtension = '.' . $scope;
00262 }
00263 }
00264
00265 $tmpPath = getcwd();
00266 chdir($this->lang_path);
00267
00268 $lang_file = "ilias_" . $this->key . ".lang" . $scopeExtension;
00269
00270 if ($lang_file)
00271 {
00272
00273 if ($content = $this->cut_header(file($lang_file)))
00274 {
00275 foreach ($content as $key => $val)
00276 {
00277 $separated = explode($this->separator,trim($val));
00278
00279
00280 $pos = strpos($separated[2], $this->comment_separator);
00281
00282 if ($pos !== false)
00283 {
00284
00285 $separated[2] = substr($separated[2] , 0 , $pos);
00286 }
00287
00288 if (empty($scope))
00289 {
00290 $query = "INSERT INTO lng_data " .
00291 "(module, identifier, lang_key, value) " .
00292 "VALUES " .
00293 "('" . $separated[0] . "','" . $separated[1] . "','" . $this->key . "','" . addslashes($separated[2]) . "')";
00294 $lang_array[$separated[0]][$separated[1]] = $separated[2];
00295 }
00296 else if ($scope == 'local')
00297 {
00298
00299 $query = "UPDATE lng_data SET ".
00300 "module = '" . $separated[0] . "', " .
00301 "identifier = '" . $separated[1] . "', " .
00302 "lang_key = '" . $this->key . "', " .
00303 "value = '" . addslashes($separated[2]) . "' " .
00304 "WHERE module = '" . $separated[0] . "' " .
00305 "AND identifier = '" . $separated[1] . "' " .
00306 "AND lang_key = '" . $this->key . "'";
00307 $lang_array[$separated[0]][$separated[1]] = $separated[2];
00308 }
00309 $this->ilias->db->query($query);
00310 }
00311
00312 if (empty($scope))
00313 {
00314 $query = "UPDATE object_data SET " .
00315 "description = 'installed', " .
00316 "last_update = now() " .
00317 "WHERE title = '".$this->key."' " .
00318 "AND type = 'lng'";
00319 }
00320 else if ($scope == 'local')
00321 {
00322 $query = "UPDATE object_data SET " .
00323 "description = 'installed_local', " .
00324 "last_update = now() " .
00325 "WHERE title = '".$this->key."' " .
00326 "AND type = 'lng'";
00327 }
00328 $this->ilias->db->query($query);
00329 }
00330
00331
00332 $lang_array[$separated[0]][$separated[1]] = $separated[2];
00333
00334 foreach($lang_array as $module => $lang_arr)
00335 {
00336 if ($scope == "local")
00337 {
00338 $q = "SELECT * FROM lng_modules WHERE ".
00339 " lang_key = ".$ilDB->quote($this->key).
00340 " AND module = ".$ilDB->quote($module);
00341 $set = $ilDB->query($q);
00342 $row = $set->fetchRow(DB_FETCHMODE_ASSOC);
00343 $arr2 = unserialize($row["lang_array"]);
00344 if (is_array($arr2))
00345 {
00346 $lang_arr = array_merge($arr2, $lang_arr);
00347 }
00348 }
00349 $query = "REPLACE INTO lng_modules (lang_key, module, lang_array) VALUES ".
00350 "(".$ilDB->quote($this->key).", " .
00351 " ".$ilDB->quote($module).", " .
00352 " ".$ilDB->quote(serialize($lang_arr)).") ";
00353 $ilDB->query($query);
00354
00355 }
00356 }
00357
00358 chdir($tmpPath);
00359 }
00360
00367 function resetUserLanguage($lang_key)
00368 {
00369 $query = "UPDATE usr_pref SET " .
00370 "value = '" . $this->lang_default."' " .
00371 "WHERE keyword = 'language' " .
00372 "AND value = '" . $lang_key . "'";
00373 $this->ilias->db->query($query);
00374 }
00375
00384 function cut_header($content)
00385 {
00386 foreach ($content as $key => $val)
00387 {
00388 if (trim($val) == "<!-- language file start -->")
00389 {
00390 return array_slice($content,$key +1);
00391 }
00392 }
00393
00394 return false;
00395 }
00396
00402 function optimizeData()
00403 {
00404
00405 $query = "OPTIMIZE TABLE lng_data";
00406 $this->ilias->db->query($query);
00407
00408 return true;
00409 }
00410
00420 function check($scope = '')
00421 {
00422 if (!empty($scope))
00423 {
00424 if ($scope == 'global')
00425 {
00426 $scope = '';
00427 }
00428 else
00429 {
00430 $scopeExtension = '.' . $scope;
00431 }
00432 }
00433
00434 $tmpPath = getcwd();
00435 chdir($this->lang_path);
00436
00437
00438 $lang_file = "ilias_" . $this->key . ".lang" . $scopeExtension;
00439
00440
00441 if (!is_file($lang_file))
00442 {
00443 $this->ilias->raiseError("File not found: ".$lang_file,$this->ilias->error_obj->MESSAGE);
00444 }
00445
00446
00447 if (!$content = $this->cut_header(file($lang_file)))
00448 {
00449 $this->ilias->raiseError("Wrong Header in ".$lang_file,$this->ilias->error_obj->MESSAGE);
00450 }
00451
00452
00453 $line = 0;
00454 foreach ($content as $key => $val)
00455 {
00456 $separated = explode($this->separator, trim($val));
00457 $num = count($separated);
00458 ++$n;
00459 if ($num != 3)
00460 {
00461 $line = $n + 36;
00462 $this->ilias->raiseError("Wrong parameter count in ".$lang_file." in line $line (Value: $val)! Please check your language file!",$this->ilias->error_obj->MESSAGE);
00463 }
00464 }
00465
00466 chdir($tmpPath);
00467
00468
00469 return true;
00470 }
00471 }
00472 ?>