ILIAS  eassessment Revision 61809
 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 = ($_SESSION[$m] != "")
173  ? $_SESSION[$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 ($_SESSION[$m])
192  {
193  //session_unregister($m);
194  unset($_SESSION[$m]); // backport 32334
195  }
196  }
197 
198  if ($this->blockExists("MESSAGE") && $out != "")
199  {
200  $this->setVariable("MESSAGE", $out);
201  }
202 
203  if ($part == "DEFAULT")
204  {
205  parent::show();
206  }
207  else
208  {
209  parent::show($part);
210  }
211 
212  if (((substr(strrchr($_SERVER["PHP_SELF"],"/"),1) != "error.php")
213  && (substr(strrchr($_SERVER["PHP_SELF"],"/"),1) != "adm_menu.php")))
214  {
215  $_SESSION["post_vars"] = $_POST;
216 
217  // referer is modified if query string contains cmd=gateway and $_POST is not empty.
218  // this is a workaround to display formular again in case of error and if the referer points to another page
219  $url_parts = parse_url($_SERVER["REQUEST_URI"]);
220  if(!$url_parts)
221  {
222  $protocol = (isset($_SERVER['HTTPS']) ? 'https' : 'http').'://';
223  $host = $_SERVER['HTTP_HOST'];
224  $path = $_SERVER['REQUEST_URI'];
225  $url_parts = @parse_url($protocol.$host.$path);
226  }
227 
228  if (preg_match("/cmd=gateway/",$url_parts["query"]))
229  {
230  foreach ($_POST as $key => $val)
231  {
232  if (is_array($val))
233  {
234  $val = key($val);
235  }
236 
237  $str .= "&".$key."=".$val;
238  }
239 
240  $_SESSION["referer"] = preg_replace("/cmd=gateway/",substr($str,1),$_SERVER["REQUEST_URI"]);
241  $_SESSION['referer_ref_id'] = (int) $_GET['ref_id'];
242  }
243  else
244  {
245  $_SESSION["referer"] = $_SERVER["REQUEST_URI"];
246  $_SESSION['referer_ref_id'] = (int) $_GET['ref_id'];
247  }
248 
249  unset($_SESSION["error_post_vars"]);
250  }
251  }
252 
256  public function getMessageHTML($a_txt, $a_type = "info")
257  {
258  global $lng;
259 
260  $mtpl = new ilTemplate("tpl.message.html", true, true, "Services/Utilities");
261  $mtpl->setCurrentBlock($a_type."_message");
262  $mtpl->setVariable("TEXT", $a_txt);
263  $mtpl->setVariable("MESSAGE_HEADING", $lng->txt($a_type."_message"));
264  $mtpl->setVariable("ALT_IMAGE", $lng->txt("icon")." ".$lng->txt($a_type."_message"));
265  $mtpl->setVariable("SRC_IMAGE", ilUtil::getImagePath("mess_".$a_type.".gif"));
266  $mtpl->parseCurrentBlock();
267 
268  return $mtpl->get();
269  }
270 
277  function setCurrentBlock ($part = "DEFAULT")
278  {
279  $this->activeBlock = $part;
280 
281  if ($part == "DEFAULT")
282  {
283  return parent::setCurrentBlock();
284  }
285  else
286  {
287  return parent::setCurrentBlock($part);
288  }
289  }
290 
297  function touchBlock($block)
298  {
299  $this->setCurrentBlock($block);
300  //$count = $this->fillVars();
301  $this->parseCurrentBlock();
302 
303  if ($count == 0)
304  {
305  parent::touchBlock($block);
306  }
307  }
308 
315  function parseCurrentBlock($part = "DEFAULT")
316  {
317  // Hier erst noch ein replace aufrufen
318  if ($part != "DEFAULT")
319  {
320  $tmp = $this->activeBlock;
321  $this->activeBlock = $part;
322  }
323 
324  if ($part != "DEFAULT")
325  {
326  $this->activeBlock = $tmp;
327  }
328 
329  //$this->fillVars();
330 
331  $this->activeBlock = "__global__";
332 
333  if ($part == "DEFAULT")
334  {
335  return parent::parseCurrentBlock();
336  }
337  else
338  {
339  return parent::parseCurrentBlock($part);
340  }
341  }
346  function setMessage($a_type, $a_txt, $a_keep = false)
347  {
348  if (!in_array($a_type, array("info", "success", "failure", "question")) || $a_txt == "")
349  {
350  return;
351  }
352  if ($a_type == "question")
353  {
354  $a_type = "mess_question";
355  }
356  if (!$a_keep)
357  {
358  $this->message[$a_type] = $a_txt;
359  }
360  else
361  {
362  $_SESSION[$a_type] = $a_txt;
363  }
364  }
365 
366  function fillMessage()
367  {
368  global $lng;
369 
370  $ms = array("info", "success", "failure", "question");
371  $out = "";
372 
373  foreach ($ms as $m)
374  {
375  if ($m == "question")
376  {
377  $m = "mess_question";
378  }
379 
380  $txt = ($_SESSION[$m] != "")
381  ? $_SESSION[$m]
382  : $this->message[$m];
383 
384  if ($m == "mess_question")
385  {
386  $m = "question";
387  }
388 
389  if ($txt != "")
390  {
391  $mtpl = new ilTemplate("tpl.message.html", true, true, "Services/Utilities");
392  $mtpl->setCurrentBlock($m."_message");
393  $mtpl->setVariable("TEXT", $txt);
394  $mtpl->setVariable("MESSAGE_HEADING", $lng->txt($m."_message"));
395  $mtpl->setVariable("ALT_IMAGE", $lng->txt("icon")." ".$lng->txt($m."_message"));
396  $mtpl->setVariable("SRC_IMAGE", ilUtil::getImagePath("mess_".$m.".gif"));
397  $mtpl->parseCurrentBlock();
398  $out.= $mtpl->get();
399  }
400 
401  if ($m == "question")
402  {
403  $m = "mess_question";
404  }
405 
406  if ($_SESSION[$m])
407  {
408  //session_unregister($m);
409  unset($_SESSION[$m]); // backport 32334
410  }
411  }
412 
413  if ($out != "")
414  {
415  $this->setVariable("MESSAGE", $out);
416  }
417  }
418 
425  function blockExists($a_blockname)
426  {
427  return $this->blockvariables["content"][$a_blockname] ? true : false;
428  }
429 
433  function addJavaScript($a_js_file)
434  {
435  if (!in_array($a_js_file, $this->js_files))
436  {
437  $this->js_files[] = $a_js_file;
438  }
439  }
440 
442  {
443  global $ilias,$ilTabs;
444  if ($this->blockExists("js_file"))
445  {
446  foreach($this->js_files as $file)
447  {
448  if (is_file($file) || substr($file, 0, 4) == "http")
449  {
450  $this->setCurrentBlock("js_file");
451  $this->setVariable("JS_FILE", $file);
452  $this->parseCurrentBlock();
453  }
454  }
455  }
456  }
457 
458  function get($part = "DEFAULT")
459  {
460  if ($part == "DEFAULT")
461  {
462  return parent::get();
463  }
464  else
465  {
466  return parent::get($part);
467  }
468  }
469 
470 
471 }
472 ?>