• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

Services/Language/classes/class.ilObjLanguage.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003         +-----------------------------------------------------------------------------+
00004         | ILIAS open source                                                           |
00005         +-----------------------------------------------------------------------------+
00006         | Copyright (c) 1998-2001 ILIAS open source, University of Cologne            |
00007         |                                                                             |
00008         | This program is free software; you can redistribute it and/or               |
00009         | modify it under the terms of the GNU General Public License                 |
00010         | as published by the Free Software Foundation; either version 2              |
00011         | of the License, or (at your option) any later version.                      |
00012         |                                                                             |
00013         | This program is distributed in the hope that it will be useful,             |
00014         | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00015         | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
00016         | GNU General Public License for more details.                                |
00017         |                                                                             |
00018         | You should have received a copy of the GNU General Public License           |
00019         | along with this program; if not, write to the Free Software                 |
00020         | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
00021         +-----------------------------------------------------------------------------+
00022 */
00023 
00024 require_once "./classes/class.ilObject.php";
00025 
00034 class ilObjLanguage extends ilObject
00035 {
00043         var $separator;
00044         var $comment_separator;
00045         var $lang_default;
00046         var $lang_user;
00047         var $lang_path;
00048 
00049         var $key;
00050         var $status;
00051 
00052 
00060         function ilObjLanguage($a_id = 0, $a_call_by_reference = false)
00061         {
00062                 global $lng;
00063 
00064                 $this->type = "lng";
00065                 $this->ilObject($a_id,$a_call_by_reference);
00066 
00067                 $this->type = "lng";
00068                 $this->key = $this->title;
00069                 $this->status = $this->desc;
00070                 $this->lang_default = $lng->lang_default;
00071                 $this->lang_user = $lng->lang_user;
00072                 $this->lang_path = $lng->lang_path;
00073                 $this->cust_lang_path = $lng->cust_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                                 // lang-file is ok. Flush data in db and...
00186                                 if (empty($scope))
00187                                 {
00188                                         $this->flush();
00189                                 }
00190 
00191                                 // ...re-insert data from lang-file
00192                                 $this->insert($scope);
00193 
00194                                 // update information in db-table about available/installed languages
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                 global $ilDB;
00240                 
00241                 $query = "DELETE FROM lng_data WHERE lang_key=".
00242                         $ilDB->quote($this->key);
00243                 $this->ilias->db->query($query);
00244         }
00245 
00246 
00252         function insert($scope = '')
00253         {
00254                 global $ilDB;
00255                 
00256                 if (!empty($scope))
00257                 {
00258                         if ($scope == 'global')
00259                         {
00260                                 $scope = ''; 
00261                         }
00262                         else
00263                         {
00264                                 $scopeExtension = '.' . $scope;
00265                         }
00266                 }
00267                 
00268                 $path = $this->lang_path;
00269                 if ($scope == "local")
00270                 {
00271                         $path = $this->cust_lang_path;
00272                 }
00273 
00274                 $tmpPath = getcwd();
00275                 chdir($path);
00276 
00277                 $lang_file = "ilias_" . $this->key . ".lang" . $scopeExtension;
00278 
00279                 if ($lang_file)
00280                 {
00281                         // remove header first
00282                         if ($content = $this->cut_header(file($lang_file)))
00283                         {
00284                                 foreach ($content as $key => $val)
00285                                 {
00286                                         $separated = explode($this->separator,trim($val));
00287                                         
00288                                         //get position of the comment_separator
00289                                         $pos = strpos($separated[2], $this->comment_separator);
00290                                 
00291                                         if ($pos !== false)
00292                                         { 
00293                                                 //cut comment of
00294                                                 $separated[2] = substr($separated[2] , 0 , $pos);
00295                                         }
00296 
00297                                         if (empty($scope))
00298                                         {
00299                                                 $query = "INSERT INTO lng_data " .
00300                                                                 "(module, identifier, lang_key, value) " .
00301                                                                 "VALUES " .
00302                                                                 "(".$ilDB->quote($separated[0]).",".
00303                                                                 $ilDB->quote($separated[1]).",".
00304                                                                 $ilDB->quote($this->key).",".
00305                                                                 $ilDB->quote($separated[2]).")";
00306                                                 $lang_array[$separated[0]][$separated[1]] = $separated[2];
00307                                         }
00308                                         else if ($scope == 'local')
00309                                         {
00310                                                 // UPDATE because the global values have already been INSERTed
00311                                                 $query = "UPDATE lng_data SET ".
00312                                                                  "module = ".$ilDB->quote($separated[0]).", " .
00313                                                                  "identifier = ".$ilDB->quote($separated[1]).", " . 
00314                                                                  "lang_key = ".$ilDB->quote($this->key).", " .
00315                                                                  "value = ".$ilDB->quote($separated[2])." " .
00316                                                                  "WHERE module = ".$ilDB->quote($separated[0])." " .
00317                                                                  "AND identifier = ".$ilDB->quote($separated[1])." " .
00318                                                                  "AND lang_key = ".$ilDB->quote($this->key);
00319                                                 $lang_array[$separated[0]][$separated[1]] = $separated[2];
00320                                         }
00321                                         $this->ilias->db->query($query);
00322                                 }
00323 
00324                                 if (empty($scope))
00325                                 {
00326                                         $query = "UPDATE object_data SET " .
00327                                                         "description = 'installed', " .
00328                                                         "last_update = now() " .
00329                                                         "WHERE title = ".$ilDB->quote($this->key)." " .
00330                                                         "AND type = 'lng'";
00331                                 }
00332                                 else if ($scope == 'local')
00333                                 {
00334                                         $query = "UPDATE object_data SET " .
00335                                                         "description = 'installed_local', " .
00336                                                         "last_update = now() " .
00337                                                         "WHERE title = ".$ilDB->quote($this->key)." " .
00338                                                         "AND type = 'lng'";
00339                                 }
00340                                 $this->ilias->db->query($query);
00341                         }
00342                         
00343                         // insert module data
00344                         $lang_array[$separated[0]][$separated[1]] = $separated[2];
00345 
00346                         foreach($lang_array as $module => $lang_arr)
00347                         {
00348                                 if ($scope == "local")
00349                                 {
00350                                         $q = "SELECT * FROM lng_modules WHERE ".
00351                                                 " lang_key = ".$ilDB->quote($this->key).
00352                                                 " AND module = ".$ilDB->quote($module);
00353                                         $set = $ilDB->query($q);
00354                                         $row = $set->fetchRow(DB_FETCHMODE_ASSOC);
00355                                         $arr2 = unserialize($row["lang_array"]);
00356                                         if (is_array($arr2))
00357                                         {
00358                                                 $lang_arr = array_merge($arr2, $lang_arr);
00359                                         }
00360                                 }
00361                                 $query = "REPLACE INTO lng_modules (lang_key, module, lang_array) VALUES ".
00362                                          "(".$ilDB->quote($this->key).", " .
00363                                          " ".$ilDB->quote($module).", " . 
00364                                          " ".$ilDB->quote(serialize($lang_arr)).") ";
00365                                 $ilDB->query($query);
00366 
00367                         }
00368                 }
00369 
00370                 chdir($tmpPath);
00371         }
00372 
00379         function resetUserLanguage($lang_key)
00380         {
00381                 global $ilDB;
00382                 
00383                 $query = "UPDATE usr_pref SET " .
00384                                 "value = ".$ilDB->quote($this->lang_default)." " .
00385                                 "WHERE keyword = 'language' " .
00386                                 "AND value = ".$ilDB->quote($lang_key);
00387                 $this->ilias->db->query($query);
00388         }
00389 
00398         function cut_header($content)
00399         {
00400                 foreach ($content as $key => $val)
00401                 {
00402                         if (trim($val) == "<!-- language file start -->")
00403                         {
00404                                 return array_slice($content,$key +1);
00405                         }
00406                 }
00407 
00408                 return false;
00409         }
00410 
00416         function optimizeData()
00417         {
00418                 // optimize
00419                 $query = "OPTIMIZE TABLE lng_data";
00420                 $this->ilias->db->query($query);
00421 
00422                 return true;
00423         }
00424 
00434         function check($scope = '')
00435         {
00436                 if (!empty($scope))
00437                 {
00438                         if ($scope == 'global')
00439                         {
00440                                 $scope = ''; 
00441                         }
00442                         else
00443                         {
00444                                 $scopeExtension = '.' . $scope;
00445                         }
00446                 }
00447 
00448                 $path = $this->lang_path;
00449                 if ($scope == "local")
00450                 {
00451                         $path = $this->cust_lang_path;
00452                 }
00453                 
00454                 $tmpPath = getcwd();
00455                 
00456                 // dir check
00457                 if (!is_dir($path))
00458                 {
00459                         $this->ilias->raiseError("Directory not found: ".$path, $this->ilias->error_obj->MESSAGE);
00460                 }
00461 
00462                 chdir($path);
00463 
00464                 // compute lang-file name format
00465                 $lang_file = "ilias_" . $this->key . ".lang" . $scopeExtension;
00466 
00467                 // file check
00468                 if (!is_file($lang_file))
00469                 {
00470                         $this->ilias->raiseError("File not found: ".$lang_file,$this->ilias->error_obj->MESSAGE);
00471                 }
00472 
00473                 // header check
00474                 if (!$content = $this->cut_header(file($lang_file)))
00475                 {
00476                         $this->ilias->raiseError("Wrong Header in ".$lang_file,$this->ilias->error_obj->MESSAGE);
00477                 }
00478 
00479                 // check (counting) elements of each lang-entry
00480                 $line = 0;
00481                 foreach ($content as $key => $val)
00482                 {
00483                         $separated = explode($this->separator, trim($val));
00484                         $num = count($separated);
00485                         ++$n;
00486                         if ($num != 3)
00487                         {
00488                                 $line = $n + 36;
00489                                 $this->ilias->raiseError("Wrong parameter count in ".$lang_file." in line $line (Value: $val)! Please check your language file!",$this->ilias->error_obj->MESSAGE);
00490                         }
00491                 }
00492 
00493                 chdir($tmpPath);
00494 
00495                 // no error occured
00496                 return true;
00497         }
00498 } // END class.LanguageObject
00499 ?>

Generated on Fri Dec 13 2013 17:56:57 for ILIAS Release_3_9_x_branch .rev 46835 by  doxygen 1.7.1