ILIAS  Release_4_1_x_branch Revision 61804
 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  $lang_name = $lng->txtlng('common','lang_'.$this->lang_key,'en');
293  $this->params["module"] = "language file ". $lang_name;
294  $this->params["created"] = date('Y-m-d H:i:s');
295  $this->params["created_by"] = $ilUser->getFullname()." <".$ilUser->getEmail().">";
296 
297  // build the header
298  $tpl = new ilTemplate("tpl.lang_file_header.html", true,true, "Services/Language");
299  foreach ($this->getAllParams() as $name => $value)
300  {
301  $tabs = ceil((20 - 3 - strlen($name)) / 4);
302  $tabs = $tabs > 0 ? $tabs : 1;
303 
304  $tpl->setCurrentBlock('param');
305  $tpl->setVariable('PAR_NAME', $name);
306  $tpl->setVariable('PAR_SPACE', str_repeat("\t", $tabs));
307  $tpl->setVariable('PAR_VALUE', $value);
308  $tpl->parseCurrentBlock();
309  }
310  $txt_scope = $lng->txtlng('administration','language_scope_'.$this->scope, 'en');
311  $tpl->setVariable('SCOPE', $txt_scope);
312 
313  $content = $tpl->get();
314  }
315 
316  // fault tolerant check for adding newline
317  $add_newline = (substr($content, strlen($content)-1, 1) != "\n");
318 
319  // build the content
320  foreach ($this->values as $key => $value)
321  {
322  // add the newline before the line!
323  // a valid lang file should not have a newline at the end!
324  if ($add_newline)
325  {
326  $content .= "\n";
327  }
328  $add_newline = true;
329 
330  $content .= $key . $this->separator . $value;
331 
332  if ($this->comments[$key])
333  {
334  $content .= $this->comment_separator . $this->comments[$key];
335  }
336  }
337  return $content;
338  }
339 
340 
345  public function getErrorMessage()
346  {
347  return $this->error_message;
348  }
349 
350 
355  public function getHeader()
356  {
357  return $this->header;
358  }
359 
360 
365  public function getAllParams()
366  {
367  return $this->params;
368  }
369 
374  public function getAllValues()
375  {
376  return $this->values;
377  }
378 
383  public function getAllComments()
384  {
385  return $this->comments;
386  }
387 
393  public function getParam($a_name)
394  {
395  return $this->params[$a_name];
396  }
397 
404  public function getValue($a_module, $a_identifier)
405  {
406  return $this->values[$a_module.$this->separator.$a_identifier];
407  }
408 
415  public function getComment($a_module, $a_identifier)
416  {
417  return $this->comments[$a_module.$this->separator.$a_identifier];
418  }
419 
425  public function setParam($a_name, $a_value)
426  {
427  $this->params[$a_name] = $a_value;
428  }
429 
436  public function setValue($a_module, $a_identifier, $a_value)
437  {
438  $this->values[$a_module.$this->separator.$a_identifier] = $a_value;
439  }
440 
445  public function setAllValues($a_values)
446  {
447  $this->values = $a_values;
448  }
449 
454  public function setAllComments($a_comments)
455  {
456  $this->comments = $a_comments;
457  }
458 
459 
466  public function setComment($a_module, $a_identifier, $a_value)
467  {
468  return $this->comments[$a_module.$this->separator.$a_identifier] = $a_comment;
469  }
470 
476  public static function _getGlobalLanguageFile($a_lang_key)
477  {
478  global $lng;
479 
480  if (!isset(self::$global_file_objects[$a_lang_key]))
481  {
482  $file_object = new ilLanguageFile(
483  $lng->lang_path . "/ilias_" . $a_lang_key . ".lang",
484  $a_lang_key, 'global');
485  $file_object->read();
486 
487  self::$global_file_objects[$a_lang_key] = $file_object;
488  }
489 
490  return self::$global_file_objects[$a_lang_key];
491  }
492 }
493 ?>