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

Services/Language/classes/class.ilObjLanguageExt.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 "./Services/Language/classes/class.ilObjLanguage.php";
00025 
00034 class ilObjLanguageExt extends ilObjLanguage
00035 {
00036         
00043         var $lang_file_param = array();
00044         var $lang_file_content = array();
00045         var $lang_file_comments = array();
00046 
00047 
00051         function ilObjLanguageExt($a_id = 0, $a_call_by_reference = false)
00052         {
00053                 $this->ilObjLanguage($a_id, $a_call_by_reference);
00054         }
00055         
00056         
00062         function setLocal($a_local = true)
00063         {
00064                 if ($this->isInstalled())
00065                 {
00066                         if ($a_local == true)
00067                         {
00068                                 $this->setDescription("installed_local");
00069                         }
00070                         else
00071                         {
00072                 $this->setDescription("installed");
00073                         }
00074                         $this->update();
00075                 }
00076         }
00077 
00078         
00084         function getLongDescription()
00085         {
00086                 return $this->lng->txt($this->desc);
00087         }
00088         
00094         function getLangPath()
00095         {
00096                 return $this->lang_path;
00097         }
00098 
00104         function getCustLangPath()
00105         {
00106                 return $this->cust_lang_path;
00107         }
00108 
00116         function getAllTranslations($a_modules = array(), $a_pattern = '')
00117         {
00118                 return $this->_getTranslations($this->key, $a_modules, NULL, $a_pattern);
00119         }
00120         
00121         
00131         function getChangedTranslations($a_modules = array(), $a_pattern = '')
00132         {
00133                 $translations = $this->_getTranslations($this->key, $a_modules, NULL, $a_pattern);
00134                 $changes = array();
00135                 
00136                 foreach ($translations as $key => $value)
00137                 {
00138                         if ($this->lang_file_content[$key] != $value)
00139                         {
00140                                 $changes[$key] = $value;
00141                         }
00142                 }
00143                 
00144                 return $changes;
00145         }
00146 
00147 
00157         function getUnchangedTranslations($a_modules = array(), $a_pattern = '')
00158         {
00159                 $translations = $this->_getTranslations($this->key, $a_modules, NULL, $a_pattern);
00160                 $unchanged = array();
00161 
00162                 foreach ($translations as $key => $value)
00163                 {
00164                         if ($this->lang_file_content[$key] == $value)
00165                         {
00166                                 $unchanged[$key] = $value;
00167                         }
00168                 }
00169 
00170                 return $unchanged;
00171         }
00172 
00173 
00183         function getCommentedTranslations($a_modules = array(), $a_pattern = '')
00184         {
00185                 $translations = $this->_getTranslations($this->key, $a_modules, NULL, $a_pattern);
00186                 $commented = array();
00187 
00188                 foreach ($translations as $key => $value)
00189                 {
00190                         if ($this->lang_file_comments[$key] != "")
00191                         {
00192                                 $commented[$key] = $value;
00193                         }
00194                 }
00195 
00196                 return $commented;
00197         }
00198 
00199 
00206         function getLangFileContent()
00207         {
00208                 return $this->lang_file_content;
00209         }
00210         
00217         function getLangFileComments()
00218         {
00219                 return $this->lang_file_comments;
00220         }
00221 
00231         function getLangFileParam($a_param)
00232         {
00233                 return $this->lang_file_param[$a_param];
00234         }
00235         
00236         
00248         function getLangFileValue($a_module, $a_topic)
00249         {
00250                 return $this->lang_file_content[$a_module.$this->separator.$a_topic];
00251         }
00252 
00253 
00260         function importLanguageFile($a_file, $a_mode_existing = 'keepnew')
00261         {
00262                 global $ilDB;
00263                 
00264                 switch($a_mode_existing)
00265                 {
00266                         // keep all existing entries
00267                         case 'keepall':
00268                                 $to_keep = $this->getAllTranslations();
00269                                 break;
00270 
00271                         // keep existing online changes
00272                         case 'keepnew':
00273                             // read the original language file
00274                             $this->readLanguageFile();
00275                                 $to_keep = $this->getChangedTranslations();
00276                                 break;
00277 
00278                         // replace all existing definitions
00279                         case 'replace':
00280                             $to_keep = array();
00281                             break;
00282 
00283            // delete all existing entries
00284                         case 'delete':
00285                                 $query = "DELETE FROM lng_data WHERE lang_key='".$this->key."'";
00286                                 $ilDB->query($query);
00287                                 $query = "DELETE FROM lng_modules WHERE lang_key='".$this->key."'";
00288                                 $ilDB->query($query);
00289                                 $to_keep = array();
00290                                 break;
00291                                 
00292                         default:
00293                             return;
00294                 }
00295                 
00296                 // read the new language file and process content
00297                 $this->readLanguageFile($a_file);
00298                 $to_save = array();
00299                 foreach ($this->lang_file_content as $key => $value)
00300                 {
00301                         if (!isset($to_keep[$key]))
00302                         {
00303                                 $to_save[$key] = $value;
00304                         }
00305                 }
00306                 $this->_saveTranslations($this->key, $to_save);
00307         }
00308 
00309 
00313         function readLanguageFile($a_lang_file = '')
00314         {
00315                 $this->lang_file_param = array();
00316                 $this->lang_file_content = array();
00317                 $this->lang_file_comments = array();
00318 
00319                 if ($a_lang_file == '')
00320                 {
00321                         $a_lang_file = $this->lang_path . "/ilias_" . $this->key . ".lang";
00322                 }
00323                 $content = file($a_lang_file);
00324                 
00325                 $in_header = true;
00326                 foreach ($content as $dummy => $line)
00327                 {
00328                         if ($in_header)
00329                         {
00330                                 // check header end
00331                                 if (trim($line) == "<!-- language file start -->")
00332                                 {
00333                                         $in_header = false;
00334                                         continue;
00335                                 }
00336                                 else
00337                                 {
00338                                         // get header params
00339                                         $pos_par = strpos($line, "* @");
00340                                         
00341                                         if ($pos_par !== false)
00342                                         {
00343                                         $pos_par += 3;
00344                                                 $pos_space = strpos($line, " ", $pos_par);
00345                                                 $pos_tab = strpos($line, "\t", $pos_par);
00346                                                 $pos_white = min($pos_space, $pos_tab);
00347                                         
00348                                                 $param = substr($line, $pos_par, $pos_white-$pos_par);
00349                                                 $value = trim(substr($line, $pos_white));
00350                                                 
00351                                                 $this->lang_file_param[$param] = $value;
00352                                         }
00353                                 }
00354                         }
00355                         else
00356                         {
00357                                 // separate the lang file entry
00358                                 $separated = explode($this->separator, trim($line));
00359                                 
00360                                 if (count($separated) == 3)
00361                                 {
00362                                         $key = $separated[0].$this->separator.$separated[1];
00363                                         $value = $separated[2];
00364 
00365                                         // cut off comment
00366                                         $pos = strpos($value, $this->comment_separator);
00367                                         if ($pos !== false)
00368                                         {
00369                                                 $this->lang_file_comments[$key]
00370                                                         = substr($value , $pos + strlen($this->comment_separator));
00371                                                         
00372                                                 $value = substr($value , 0 , $pos);
00373                                         }
00374                                         $this->lang_file_content[$key] = $value;
00375                                 }
00376                         }
00377                 }
00378         }
00379 
00380 
00381         //
00382         // STATIC FUNCTIONS
00383         //
00384 
00392         function _getModules($a_lang_key)
00393         {
00394                 global $ilDB;
00395                 
00396                 $q = "SELECT DISTINCT module FROM lng_data WHERE ".
00397                         " lang_key = ".$ilDB->quote($a_lang_key).
00398                         " order by module";
00399                 $set = $ilDB->query($q);
00400 
00401                 while ($rec = $set->fetchRow(DB_FETCHMODE_ASSOC))
00402                 {
00403                         $modules[] = $rec["module"];
00404                 }
00405                 return $modules;
00406         }
00407 
00418         function _getTranslations($a_lang_key, $a_modules = array(), $a_topics = array(), $a_pattern = '')
00419         {
00420                 global $ilDB, $lng;
00421 
00422                 if (is_array($a_modules))
00423                 {
00424                         for ($i = 0; $i < count($a_modules); $i++)
00425                         {
00426                                 $a_modules[$i] = $ilDB->quote($a_modules[$i]);
00427                         }
00428             $modules_list = implode(',', $a_modules);
00429                 }
00430                 if (is_array($a_topics))
00431                 {
00432                         for ($i = 0; $i < count($a_topics); $i++)
00433                         {
00434                                 $a_topics[$i] = $ilDB->quote($a_topics[$i]);
00435                         }
00436                         $topics_list = implode(',', $a_topics);
00437                 }
00438 
00439                 $q = "SELECT * FROM lng_data WHERE".
00440                         " lang_key =".$ilDB->quote($a_lang_key);
00441                 if ($modules_list)
00442                 {
00443                         $q .= " AND module in (". $modules_list. ")";
00444                 }
00445                 if ($topics_list)
00446                 {
00447                         $q .= " AND identifier in (". $topics_list. ")";
00448                 }
00449                 if ($a_pattern)
00450                 {
00451                         $q .= " AND value like ". $ilDB->quote("%".$a_pattern."%");
00452                 }
00453                 $q .= " ORDER BY module, identifier";
00454                 $set = $ilDB->query($q);
00455 
00456                 $trans = array();
00457                 while ($rec = $set->fetchRow(DB_FETCHMODE_ASSOC))
00458                 {
00459                         $trans[$rec["module"].$lng->separator.$rec["identifier"]] = $rec["value"];
00460                 }
00461                 return $trans;
00462         }
00463 
00471         function _saveTranslations($a_lang_key, $a_translations = array())
00472         {
00473                 global $ilDB, $lng;
00474                 
00475                 if (!is_array($a_translations))
00476                 {
00477                         return;
00478                 }
00479                 $save_array = array();
00480                 
00481                 // save the single translations in lng_data
00482                 foreach ($a_translations as $key => $value)
00483                 {
00484                         $keys = explode($lng->separator, $key);
00485                         if (count($keys) == 2)
00486                         {
00487                                 $module = $keys[0];
00488                                 $topic = $keys[1];
00489                                 $save_array[$module][$topic] = $value;
00490                         
00491                                 $q = "REPLACE INTO lng_data(lang_key, module, identifier, value)"
00492                                 . " VALUES("
00493                                 . $ilDB->quote($a_lang_key). ","
00494                                 . $ilDB->quote($module). ","
00495                                 . $ilDB->quote($topic). ","
00496                                 . $ilDB->quote($value).")";
00497                                 $ilDB->query($q);
00498                         }
00499                 }
00500 
00501                 // save the serialized module entries in lng_modules
00502                 foreach ($save_array as $module => $entries)
00503                 {
00504                         $q = "SELECT * FROM lng_modules WHERE ".
00505                                 " lang_key = ".$ilDB->quote($a_lang_key).
00506                                 " AND module = ".$ilDB->quote($module);
00507                         $set = $ilDB->query($q);
00508                         $row = $set->fetchRow(DB_FETCHMODE_ASSOC);
00509                         $arr = unserialize($row["lang_array"]);
00510                         if (is_array($arr))
00511                         {
00512                                 $entries = array_merge($arr, $entries);
00513                         }
00514                         $q = "REPLACE INTO lng_modules (lang_key, module, lang_array) VALUES ".
00515                                  "(".$ilDB->quote($a_lang_key).", " .
00516                                  " ".$ilDB->quote($module).", " .
00517                                  " ".$ilDB->quote(serialize($entries)).") ";
00518                         $ilDB->query($q);
00519                 }
00520         }
00521 } // END class.ilObjLanguageExt
00522 ?>

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