ILIAS  Release_4_0_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 
65  private $separator;
66 
72 
77  private $params = array();
78 
83  private $values = array();
84 
89  private $comments = array();
90 
95  private $error_message = "";
96 
103  function __construct($a_file, $a_key = "", $a_scope = 'global')
104  {
105  global $lng;
106  $this->separator = $lng->separator;
107  $this->comment_separator = $lng->comment_separator;
108 
109  $this->lang_file = $a_file;
110  $this->lang_key = $a_key;
111  $this->scope = $a_scope;
112 
113  // Set the default parameters to be written in a new file.
114  // This ensures the correct order of parameters
115 
116  $this->params["module"] = "language file";
117  $this->params["modulegroup"] = "language";
118 
119  if ($this->scope == "local")
120  {
121  $this->params["based_on"] = "";
122  }
123  else
124  {
125  $this->params["author"] = "";
126  $this->params["version"] = "";
127  }
128 
129  $this->params["il_server"] = ILIAS_HTTP_PATH;
130  $this->params["il_version"] = ILIAS_VERSION;
131  $this->params["created"] = "";
132  $this->params["created_by"] = "";
133  }
134 
139  public function read()
140  {
141  global $lng;
142 
143  $this->params = array();
144  $this->values = array();
145  $this->comments = array();
146  $this->error_message = "";
147 
148  $content = file($this->lang_file);
149  $in_header = true;
150 
151  foreach ($content as $line_num => $line)
152  {
153  if ($in_header)
154  {
155  // check header end
156  if (trim($line) == "<!-- language file start -->")
157  {
158  $in_header = false;
159  continue;
160  }
161  else
162  {
163  // get header params
164  $pos_par = strpos($line, "* @");
165 
166  if ($pos_par !== false)
167  {
168  $pos_par += 3;
169  $pos_space = strpos($line, " ", $pos_par);
170  $pos_tab = strpos($line, "\t", $pos_par);
171  $pos_white = min($pos_space, $pos_tab);
172 
173  $param = substr($line, $pos_par, $pos_white-$pos_par);
174  $value = trim(substr($line, $pos_white));
175 
176  $this->params[$param] = $value;
177  }
178  }
179  }
180  else
181  {
182  // separate the lang file entry
183  $separated = explode($this->separator, trim($line));
184 
185  // not a valid line with module, identifier and value?
186  if (count($separated) != 3)
187  {
188  $this->error_message =
189  $lng->txt("file_not_valid"). " "
190  .$lng->txt("err_in_line")." ". $line_num . ". "
191  .$lng->txt("err_count_param");
192  return false;
193  }
194  else
195  {
196  $key = $separated[0].$this->separator.$separated[1];
197  $value = $separated[2];
198 
199  // cut off comment
200  $pos = strpos($value, $this->comment_separator);
201  if ($pos !== false)
202  {
203  $this->comments[$key]
204  = substr($value , $pos + strlen($this->comment_separator));
205 
206  $value = substr($value , 0 , $pos);
207  }
208  $this->values[$key] = $value;
209  }
210  }
211  }
212  // still in header after parsing the whole file?
213  if ($in_header)
214  {
215  $this->error_message = $lng->txt("file_not_valid")." ".$lng->txt("err_wrong_header");
216  return false;
217  }
218  else
219  {
220  return true;
221  }
222  }
223 
227  public function write()
228  {
229  $fp = fopen($this->lang_file, "w");
230  fwrite($fp, $this->build());
231  fclose($fp);
232  }
233 
238  public function build()
239  {
240  global $ilUser, $lng;
241 
242  // set default params
243  $lang_name = $lng->txtlng('common','lang_'.$this->lang_key,'en');
244  $this->params["module"] = "language file ". $lang_name;
245  $this->params["created"] = date('Y-m-d H:i:s');
246  $this->params["created_by"] = $ilUser->getFullname()." <".$ilUser->getEmail().">";
247 
248  // build the header
249  $tpl = new ilTemplate("tpl.lang_file_header.html", true,true, "Services/Language");
250  foreach ($this->getAllParams() as $name => $value)
251  {
252  $tpl->setCurrentBlock('param');
253  $tpl->setVariable('PAR_NAME', $name);
254  $tpl->setVariable('PAR_VALUE', $value);
255  $tpl->parseCurrentBlock();
256  }
257  $txt_scope = $lng->txtlng('administration','language_scope_'.$this->scope,'en');
258  $tpl->setVariable('SCOPE', $txt_scope, 'en');
259 
260  // build the content
261  $content = $tpl->get();
262  foreach ($this->getAllValues() as $key => $value)
263  {
264  $content .= $key . $this->separator . $value . "\n";
265  }
266  return $content;
267  }
268 
269 
274  public function getErrorMessage()
275  {
276  return $this->error_message;
277  }
278 
283  public function getAllParams()
284  {
285  return $this->params;
286  }
287 
292  public function getAllValues()
293  {
294  return $this->values;
295  }
296 
301  public function getAllComments()
302  {
303  return $this->comments;
304  }
305 
311  public function getParam($a_name)
312  {
313  return $this->params[$a_name];
314  }
315 
322  public function getValue($a_module, $a_identifier)
323  {
324  return $this->values[$a_module.$this->separator.$a_identifier];
325  }
326 
333  public function getComment($a_module, $a_identifier)
334  {
335  return $this->comments[$a_module.$this->separator.$a_identifier];
336  }
337 
343  public function setParam($a_name, $a_value)
344  {
345  $this->params[$a_name] = $a_value;
346  }
347 
354  public function setValue($a_module, $a_identifier, $a_value)
355  {
356  $this->values[$a_module.$this->separator.$a_identifier] = $a_value;
357  }
358 
363  public function setAllValues($a_values)
364  {
365  $this->values = $a_values;
366  }
367 
374  public function setComment($a_module, $a_identifier, $a_value)
375  {
376  return $this->comments[$a_module.$this->separator.$a_identifier] = $a_comment;
377  }
378 
384  public static function _getGlobalLanguageFile($a_lang_key)
385  {
386  global $lng;
387 
388  if (!isset(self::$global_file_objects[$a_lang_key]))
389  {
390  $file_object = new ilLanguageFile(
391  $lng->lang_path . "/ilias_" . $a_lang_key . ".lang",
392  $a_lang_key, 'global');
393  $file_object->read();
394 
395  self::$global_file_objects[$a_lang_key] = $file_object;
396  }
397 
398  return self::$global_file_objects[$a_lang_key];
399  }
400 }
401 ?>