ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilLanguageFile.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2008 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 
36 {
41  private static $global_file_objects = array();
42 
47  private $lang_file;
48 
53  private $lang_key;
54 
59  private $scope;
60 
61 
66  private $header;
67 
72  private $file_start = "<!-- language file start -->";
73 
74 
79  private $separator;
80 
86 
91  private $params = array();
92 
97  private $values = array();
98 
103  private $comments = array();
104 
109  private $error_message = "";
110 
117  function __construct($a_file, $a_key = "", $a_scope = 'global')
118  {
119  global $lng;
120  $this->separator = $lng->separator;
121  $this->comment_separator = $lng->comment_separator;
122 
123  $this->lang_file = $a_file;
124  $this->lang_key = $a_key;
125  $this->scope = $a_scope;
126 
127  // initialize the header of a blank file
128  $this->header = $file_start;
129 
130  // Set the default parameters to be written in a new file.
131  // This ensures the correct order of parameters
132 
133  $this->params["module"] = "language file";
134  $this->params["modulegroup"] = "language";
135 
136  if ($this->scope == "local")
137  {
138  $this->params["based_on"] = "";
139  }
140  else
141  {
142  $this->params["author"] = "";
143  $this->params["version"] = "";
144  }
145 
146  $this->params["il_server"] = ILIAS_HTTP_PATH;
147  $this->params["il_version"] = ILIAS_VERSION;
148  $this->params["created"] = "";
149  $this->params["created_by"] = "";
150  }
151 
156  public function read()
157  {
158  global $lng;
159 
160  $this->header = '';
161  $this->params = array();
162  $this->values = array();
163  $this->comments = array();
164  $this->error_message = "";
165 
166  $content = file($this->lang_file);
167  $in_header = true;
168 
169  foreach ($content as $line_num => $line)
170  {
171  if ($in_header)
172  {
173  // store the header line
174  $this->header .= $line . "\n";
175 
176  // check header end
177  if (trim($line) == $this->file_start)
178  {
179  $in_header = false;
180  continue;
181  }
182  else
183  {
184  // get header params
185  $pos_par = strpos($line, "* @");
186 
187  if ($pos_par !== false)
188  {
189  $pos_par += 3;
190  $pos_space = strpos($line, " ", $pos_par);
191  $pos_tab = strpos($line, "\t", $pos_par);
192  if ($pos_space !== false and $pos_tab !== false)
193  {
194  $pos_white = min($pos_space, $pos_tab);
195  }
196  elseif ($pos_space !== false)
197  {
198  $pos_white = $pos_space;
199  }
200  elseif ($pos_tab !== false)
201  {
202  $pos_white = $pos_tab;
203  }
204  else
205  {
206  $pos_white = false;
207  }
208  if ($pos_white)
209  {
210  $param = substr($line, $pos_par, $pos_white-$pos_par);
211  $value = trim(substr($line, $pos_white));
212 
213  $this->params[$param] = $value;
214  }
215  }
216  }
217  }
218  else
219  {
220  // separate the lang file entry
221  $separated = explode($this->separator, trim($line));
222 
223  // not a valid line with module, identifier and value?
224  if (count($separated) != 3)
225  {
226  $this->error_message =
227  $lng->txt("file_not_valid"). " "
228  .$lng->txt("err_in_line")." ". $line_num . ". "
229  .$lng->txt("err_count_param");
230  return false;
231  }
232  else
233  {
234  $key = $separated[0].$this->separator.$separated[1];
235  $value = $separated[2];
236 
237  // cut off comment
238  $pos = strpos($value, $this->comment_separator);
239  if ($pos !== false)
240  {
241  $this->comments[$key]
242  = substr($value , $pos + strlen($this->comment_separator));
243 
244  $value = substr($value , 0 , $pos);
245  }
246  $this->values[$key] = $value;
247  }
248  }
249  }
250  // still in header after parsing the whole file?
251  if ($in_header)
252  {
253  $this->error_message = $lng->txt("file_not_valid")." ".$lng->txt("err_wrong_header");
254  return false;
255  }
256  else
257  {
258  return true;
259  }
260  }
261 
267  public function write($a_header = '')
268  {
269  $fp = fopen($this->lang_file, "w");
270  fwrite($fp, $this->build($a_header));
271  fclose($fp);
272  }
273 
280  public function build($a_header = '')
281  {
282  global $ilUser, $lng;
283 
284  if ($a_header)
285  {
286  // take the given header
287  $content = $a_header;
288  }
289  else
290  {
291  // set default params
292  $lng->loadLanguageModule('meta');
293  $lang_name = $lng->txtlng('meta','meta_l_'.$this->lang_key,'en');
294  $this->params["module"] = "language file ". $lang_name;
295  $this->params["created"] = date('Y-m-d H:i:s');
296  $this->params["created_by"] = $ilUser->getFullname()." <".$ilUser->getEmail().">";
297 
298  // build the header
299  $tpl = new ilTemplate("tpl.lang_file_header.html", true,true, "Services/Language");
300  foreach ($this->getAllParams() as $name => $value)
301  {
302  $tabs = ceil((20 - 3 - strlen($name)) / 4);
303  $tabs = $tabs > 0 ? $tabs : 1;
304 
305  $tpl->setCurrentBlock('param');
306  $tpl->setVariable('PAR_NAME', $name);
307  $tpl->setVariable('PAR_SPACE', str_repeat("\t", $tabs));
308  $tpl->setVariable('PAR_VALUE', $value);
309  $tpl->parseCurrentBlock();
310  }
311  $txt_scope = $lng->txtlng('administration','language_scope_'.$this->scope, 'en');
312  $tpl->setVariable('SCOPE', $txt_scope);
313 
314  $content = $tpl->get();
315  }
316 
317  // fault tolerant check for adding newline
318  $add_newline = (substr($content, strlen($content)-1, 1) != "\n");
319 
320  // build the content
321  foreach ($this->values as $key => $value)
322  {
323  // add the newline before the line!
324  // a valid lang file should not have a newline at the end!
325  if ($add_newline)
326  {
327  $content .= "\n";
328  }
329  $add_newline = true;
330 
331  $content .= $key . $this->separator . $value;
332 
333  if ($this->comments[$key])
334  {
335  $content .= $this->comment_separator . $this->comments[$key];
336  }
337  }
338  return $content;
339  }
340 
341 
346  public function getErrorMessage()
347  {
348  return $this->error_message;
349  }
350 
351 
356  public function getHeader()
357  {
358  return $this->header;
359  }
360 
361 
366  public function getAllParams()
367  {
368  return $this->params;
369  }
370 
375  public function getAllValues()
376  {
377  return $this->values;
378  }
379 
384  public function getAllComments()
385  {
386  return $this->comments;
387  }
388 
394  public function getParam($a_name)
395  {
396  return $this->params[$a_name];
397  }
398 
405  public function getValue($a_module, $a_identifier)
406  {
407  return $this->values[$a_module.$this->separator.$a_identifier];
408  }
409 
416  public function getComment($a_module, $a_identifier)
417  {
418  return $this->comments[$a_module.$this->separator.$a_identifier];
419  }
420 
426  public function setParam($a_name, $a_value)
427  {
428  $this->params[$a_name] = $a_value;
429  }
430 
437  public function setValue($a_module, $a_identifier, $a_value)
438  {
439  $this->values[$a_module.$this->separator.$a_identifier] = $a_value;
440  }
441 
446  public function setAllValues($a_values)
447  {
448  $this->values = $a_values;
449  }
450 
455  public function setAllComments($a_comments)
456  {
457  $this->comments = $a_comments;
458  }
459 
460 
467  public function setComment($a_module, $a_identifier, $a_value)
468  {
469  return $this->comments[$a_module.$this->separator.$a_identifier] = $a_comment;
470  }
471 
477  public static function _getGlobalLanguageFile($a_lang_key)
478  {
479  global $lng;
480 
481  if (!isset(self::$global_file_objects[$a_lang_key]))
482  {
483  $file_object = new ilLanguageFile(
484  $lng->lang_path . "/ilias_" . $a_lang_key . ".lang",
485  $a_lang_key, 'global');
486  $file_object->read();
487 
488  self::$global_file_objects[$a_lang_key] = $file_object;
489  }
490 
491  return self::$global_file_objects[$a_lang_key];
492  }
493 }
494 ?>