ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilTemplate.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
10 class ilTemplate extends ilTemplateX
11 {
16  var $vars;
17  var $js_files = array(0 => "./Services/JavaScript/js/Basic.js"); // list of JS files that should be included
18 
25  var $activeBlock;
26 
36  /*function ilTemplate($root)
37  {
38 
39  $this->callConstructor();
40 
41  $this->setRoot($root);
42 
43  return true;
44  }*/
45  function ilTemplate($file,$flag1,$flag2,$in_module = false, $vars = "DEFAULT")
46  {
47  $this->activeBlock = "__global__";
48  $this->vars = array();
49 
50  $fname = $this->getTemplatePath($file, $in_module);
51 
52  $this->tplName = basename($fname);
53  $this->tplPath = dirname($fname);
54  // set default content-type to text/html
55  $this->contenttype = "text/html";
56  if (!file_exists($fname))
57  {
58  die("template ".$fname." was not found.");
59  return false;
60  }
61 
62  //$this->IntegratedTemplateExtension(dirname($fname));
63  $this->callConstructor();
64  //$this->loadTemplatefile(basename($fname), $flag1, $flag2);
65  $this->loadTemplatefile($fname, $flag1, $flag2);
66  //add tplPath to replacevars
67  $this->vars["TPLPATH"] = $this->tplPath;
68 
69  // set Options
70  if (method_exists($this, "setOption"))
71  {
72  $this->setOption('use_preg', false);
73  }
74 
75  return true;
76  }
77 
86  function getTemplatePath($a_tplname, $a_in_module = false, $a_plugin = false)
87  {
88  global $ilias, $ilCtrl;
89 
90  // if baseClass functionality is used (ilias.php):
91  // get template directory from ilCtrl
92  if (!empty($_GET["baseClass"]) && $a_in_module === true)
93  {
94  $a_in_module = $ilCtrl->getModuleDir();
95  }
96 
97  if (strpos($a_tplname,"/") === false)
98  {
99  $module_path = "";
100 
101  //$fname = $ilias->tplPath;
102  if ($a_in_module)
103  {
104  if ($a_in_module === true)
105  {
106  $module_path = ILIAS_MODULE."/";
107  }
108  else
109  {
110  $module_path = $a_in_module."/";
111  }
112  }
113 
114  if($fname == "" || !file_exists($fname))
115  {
116  if ($a_in_module == "setup")
117  {
118  $fname = "./".$module_path."templates/".basename($a_tplname);
119  }
120  else
121  {
122  $fname = "./".$module_path."templates/default/".basename($a_tplname);
123  }
124  }
125  }
126  else
127  {
128  $fname = $a_tplname;
129  }
130 
131  return $fname;
132  }
133 
134  function addBlockFile($var, $block, $tplname, $in_module = false)
135  {
136  if (DEBUG)
137  {
138  echo "<br/>Template '".$this->tplPath."/".$tplname."'";
139  }
140 
141  $tplfile = $this->getTemplatePath($tplname, $in_module);
142  if (file_exists($tplfile) == false)
143  {
144  echo "<br/>Template '".$tplfile."' doesn't exist! aborting...";
145  return false;
146  }
147 
148  return parent::addBlockFile($var, $block, $tplfile);
149  }
150 
155  function show($part = "DEFAULT")
156  {
157  header('Content-type: text/html; charset=UTF-8');
158 
159  $this->fillJavaScriptFiles();
160 
161  // ERROR HANDLER SETS $_GET["message"] IN CASE OF $error_obj->MESSAGE
162  $ms = array("info", "success", "failure", "question");
163  $out = "";
164 
165  foreach ($ms as $m)
166  {
167  if ($m == "question")
168  {
169  $m = "mess_question";
170  }
171 
172  $txt = (ilSession::get($m) != "")
173  ? ilSession::get($m)
174  : $this->message[$m];
175 
176  if ($m == "mess_question")
177  {
178  $m = "question";
179  }
180 
181  if ($txt != "")
182  {
183  $out.= $this->getMessageHTML($txt, $m);
184  }
185 
186  if ($m == "question")
187  {
188  $m = "mess_question";
189  }
190 
191  if (ilSession::get($m))
192  {
193  ilSession::clear($m);
194  }
195  }
196 
197  if ($this->blockExists("MESSAGE") && $out != "")
198  {
199  $this->setVariable("MESSAGE", $out);
200  }
201 
202  if ($part == "DEFAULT")
203  {
204  parent::show();
205  }
206  else
207  {
208  parent::show($part);
209  }
210 
211  if (((substr(strrchr($_SERVER["PHP_SELF"],"/"),1) != "error.php")
212  && (substr(strrchr($_SERVER["PHP_SELF"],"/"),1) != "adm_menu.php")))
213  {
214  ilSession::set("post_vars", $_POST);
215 
216  // referer is modified if query string contains cmd=gateway and $_POST is not empty.
217  // this is a workaround to display formular again in case of error and if the referer points to another page
218  $url_parts = parse_url($_SERVER["REQUEST_URI"]);
219  if(!$url_parts)
220  {
221  $protocol = (isset($_SERVER['HTTPS']) ? 'https' : 'http').'://';
222  $host = $_SERVER['HTTP_HOST'];
223  $path = $_SERVER['REQUEST_URI'];
224  $url_parts = @parse_url($protocol.$host.$path);
225  }
226 
227  if (preg_match("/cmd=gateway/",$url_parts["query"]))
228  {
229  foreach ($_POST as $key => $val)
230  {
231  if (is_array($val))
232  {
233  $val = key($val);
234  }
235 
236  $str .= "&".$key."=".$val;
237  }
238 
239  ilSession::set("referer",
240  preg_replace("/cmd=gateway/",substr($str,1),$_SERVER["REQUEST_URI"]));
241  ilSession::set("referer_ref_id",
242  (int) $_GET['ref_id']);
243  }
244  else
245  {
246  ilSession::set("referer", $_SERVER["REQUEST_URI"]);
247  ilSession::set("referer_ref_id",
248  (int) $_GET['ref_id']);
249  }
250 
251  ilSession::clear("error_post_vars");
252  }
253  }
254 
258  public function getMessageHTML($a_txt, $a_type = "info")
259  {
260  global $lng;
261 
262  $mtpl = new ilTemplate("tpl.message.html", true, true, "Services/Utilities");
263  $mtpl->setCurrentBlock($a_type."_message");
264  $mtpl->setVariable("TEXT", $a_txt);
265  $mtpl->setVariable("MESSAGE_HEADING", $lng->txt($a_type."_message"));
266  $mtpl->setVariable("ALT_IMAGE", $lng->txt("icon")." ".$lng->txt($a_type."_message"));
267  $mtpl->setVariable("SRC_IMAGE", ilUtil::getImagePath("mess_".$a_type.".png"));
268  $mtpl->parseCurrentBlock();
269 
270  return $mtpl->get();
271  }
272 
279  function setCurrentBlock ($part = "DEFAULT")
280  {
281  $this->activeBlock = $part;
282 
283  if ($part == "DEFAULT")
284  {
285  return parent::setCurrentBlock();
286  }
287  else
288  {
289  return parent::setCurrentBlock($part);
290  }
291  }
292 
299  function touchBlock($block)
300  {
301  $this->setCurrentBlock($block);
302  //$count = $this->fillVars();
303  $this->parseCurrentBlock();
304 
305  if ($count == 0)
306  {
307  parent::touchBlock($block);
308  }
309  }
310 
317  function parseCurrentBlock($part = "DEFAULT")
318  {
319  // Hier erst noch ein replace aufrufen
320  if ($part != "DEFAULT")
321  {
322  $tmp = $this->activeBlock;
323  $this->activeBlock = $part;
324  }
325 
326  if ($part != "DEFAULT")
327  {
328  $this->activeBlock = $tmp;
329  }
330 
331  //$this->fillVars();
332 
333  $this->activeBlock = "__global__";
334 
335  if ($part == "DEFAULT")
336  {
337  return parent::parseCurrentBlock();
338  }
339  else
340  {
341  return parent::parseCurrentBlock($part);
342  }
343  }
348  function setMessage($a_type, $a_txt, $a_keep = false)
349  {
350  if (!in_array($a_type, array("info", "success", "failure", "question")) || $a_txt == "")
351  {
352  return;
353  }
354  if ($a_type == "question")
355  {
356  $a_type = "mess_question";
357  }
358  if (!$a_keep)
359  {
360  $this->message[$a_type] = $a_txt;
361  }
362  else
363  {
364  ilSession::set($a_type, $a_txt);
365  }
366  }
367 
368  function fillMessage()
369  {
370  global $lng;
371 
372  $ms = array("info", "success", "failure", "question");
373  $out = "";
374 
375  foreach ($ms as $m)
376  {
377  if ($m == "question")
378  {
379  $m = "mess_question";
380  }
381 
382  $txt = (ilSession::get($m) != "")
383  ? ilSession::get($m)
384  : $this->message[$m];
385 
386  if ($m == "mess_question")
387  {
388  $m = "question";
389  }
390 
391  if ($txt != "")
392  {
393  $mtpl = new ilTemplate("tpl.message.html", true, true, "Services/Utilities");
394  $mtpl->setCurrentBlock($m."_message");
395  $mtpl->setVariable("TEXT", $txt);
396  $mtpl->setVariable("MESSAGE_HEADING", $lng->txt($m."_message"));
397  $mtpl->setVariable("ALT_IMAGE", $lng->txt("icon")." ".$lng->txt($m."_message"));
398  $mtpl->setVariable("SRC_IMAGE", ilUtil::getImagePath("mess_".$m.".png"));
399  $mtpl->parseCurrentBlock();
400  $out.= $mtpl->get();
401  }
402 
403  if ($m == "question")
404  {
405  $m = "mess_question";
406  }
407 
408  if (ilSession::get($m))
409  {
410  ilSession::clear($m);
411  }
412  }
413 
414  if ($out != "")
415  {
416  $this->setVariable("MESSAGE", $out);
417  }
418  }
419 
426  function blockExists($a_blockname)
427  {
428  // added second evaluation to the return statement because the first one only works for the content block (Helmut Schottmüller, 2007-09-14)
429  return (isset($this->blockvariables["content"][$a_blockname]) ? true : false) | (isset($this->blockvariables[$a_blockname]) ? true : false);
430  }
431 
435  function addJavaScript($a_js_file)
436  {
437  if (!in_array($a_js_file, $this->js_files))
438  {
439  $this->js_files[] = $a_js_file;
440  }
441  }
442 
444  {
445  global $ilias,$ilTabs;
446  if ($this->blockExists("js_file"))
447  {
448  foreach($this->js_files as $file)
449  {
450  if (is_file($file) || substr($file, 0, 4) == "http")
451  {
452  $this->setCurrentBlock("js_file");
453  $this->setVariable("JS_FILE", $file);
454  $this->parseCurrentBlock();
455  }
456  }
457  }
458  }
459 
460  function get($part = "DEFAULT")
461  {
462  if ($part == "DEFAULT")
463  {
464  return parent::get();
465  }
466  else
467  {
468  return parent::get($part);
469  }
470  }
471 
472 
473 }
474 ?>