ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 
4 include_once("./Services/UICore/lib/html-it/IT.php");
5 include_once("./Services/UICore/lib/html-it/ITX.php");
6 
13 class ilTemplate extends HTML_Template_ITX
14 {
19  var $vars;
20  //var $js_files = array(0 => "./Services/JavaScript/js/Basic.js"); // list of JS files that should be included
21  var $js_files = array(); // list of JS files that should be included
22  var $css_files = array(); // list of css files that should be included
23 
30  var $activeBlock;
31 
41  /*function ilTemplate($root)
42  {
43 
44  $this->callConstructor();
45 
46  $this->setRoot($root);
47 
48  return true;
49  }*/
50  function __construct($file,$flag1,$flag2,$in_module = false, $vars = "DEFAULT")
51  {
52  $this->activeBlock = "__global__";
53  $this->vars = array();
54 
55  $fname = $this->getTemplatePath($file, $in_module);
56 
57  $this->tplName = basename($fname);
58  $this->tplPath = dirname($fname);
59  // set default content-type to text/html
60  $this->contenttype = "text/html";
61  if (!file_exists($fname))
62  {
63  die("template ".$fname." was not found.");
64  return false;
65  }
66 
67  //$this->IntegratedTemplateExtension(dirname($fname));
68  parent::__construct();
69  //$this->loadTemplatefile(basename($fname), $flag1, $flag2);
70  $this->loadTemplatefile($fname, $flag1, $flag2);
71  //add tplPath to replacevars
72  $this->vars["TPLPATH"] = $this->tplPath;
73 
74  // set Options
75  if (method_exists($this, "setOption"))
76  {
77  $this->setOption('use_preg', false);
78  }
79 
80  return true;
81  }
82 
91  function getTemplatePath($a_tplname, $a_in_module = false, $a_plugin = false)
92  {
93  global $ilias, $ilCtrl;
94 
95  // if baseClass functionality is used (ilias.php):
96  // get template directory from ilCtrl
97  if (!empty($_GET["baseClass"]) && $a_in_module === true)
98  {
99  $a_in_module = $ilCtrl->getModuleDir();
100  }
101 
102  if (strpos($a_tplname,"/") === false)
103  {
104  $module_path = "";
105 
106  //$fname = $ilias->tplPath;
107  if ($a_in_module)
108  {
109  if ($a_in_module === true)
110  {
111  $module_path = ILIAS_MODULE."/";
112  }
113  else
114  {
115  $module_path = $a_in_module."/";
116  }
117  }
118 
119  if($fname == "" || !file_exists($fname))
120  {
121  if ($a_in_module == "setup")
122  {
123  $fname = "./".$module_path."templates/".basename($a_tplname);
124  }
125  else
126  {
127  $fname = "./".$module_path."templates/default/".basename($a_tplname);
128  }
129  }
130  }
131  else
132  {
133  $fname = $a_tplname;
134  }
135 
136  return $fname;
137  }
138 
139  function addBlockFile($var, $block, $tplname, $in_module = false)
140  {
141  if (DEBUG)
142  {
143  echo "<br/>Template '".$this->tplPath."/".$tplname."'";
144  }
145 
146  $tplfile = $this->getTemplatePath($tplname, $in_module);
147  if (file_exists($tplfile) == false)
148  {
149  echo "<br/>Template '".$tplfile."' doesn't exist! aborting...";
150  return false;
151  }
152 
153  return parent::addBlockFile($var, $block, $tplfile);
154  }
155 
160  function show($part = "DEFAULT")
161  {
162  header('Content-type: text/html; charset=UTF-8');
163 
164  $this->fillJavaScriptFiles();
165  $this->fillCssFiles();
166 
167  // ERROR HANDLER SETS $_GET["message"] IN CASE OF $error_obj->MESSAGE
168  $ms = array("info", "success", "failure", "question");
169  $out = "";
170 
171  foreach ($ms as $m)
172  {
173  if ($m == "question")
174  {
175  $m = "mess_question";
176  }
177 
178  $txt = (ilSession::get($m) != "")
179  ? ilSession::get($m)
180  : $this->message[$m];
181 
182  if ($m == "mess_question")
183  {
184  $m = "question";
185  }
186 
187  if ($txt != "")
188  {
189  $out.= $this->getMessageHTML($txt, $m);
190  }
191 
192  if ($m == "question")
193  {
194  $m = "mess_question";
195  }
196 
197  if (ilSession::get($m))
198  {
199  ilSession::clear($m);
200  }
201  }
202 
203  if ($this->blockExists("MESSAGE") && $out != "")
204  {
205  $this->setVariable("MESSAGE", $out);
206  }
207 
208  if ($part == "DEFAULT")
209  {
210  parent::show();
211  }
212  else
213  {
214  parent::show($part);
215  }
216 
217  if (((substr(strrchr($_SERVER["PHP_SELF"],"/"),1) != "error.php")
218  && (substr(strrchr($_SERVER["PHP_SELF"],"/"),1) != "adm_menu.php")))
219  {
220  ilSession::set("post_vars", $_POST);
221 
222  // referer is modified if query string contains cmd=gateway and $_POST is not empty.
223  // this is a workaround to display formular again in case of error and if the referer points to another page
224  $url_parts = parse_url($_SERVER["REQUEST_URI"]);
225  if(!$url_parts)
226  {
227  $protocol = (isset($_SERVER['HTTPS']) ? 'https' : 'http').'://';
228  $host = $_SERVER['HTTP_HOST'];
229  $path = $_SERVER['REQUEST_URI'];
230  $url_parts = @parse_url($protocol.$host.$path);
231  }
232 
233  if (preg_match("/cmd=gateway/",$url_parts["query"]))
234  {
235  foreach ($_POST as $key => $val)
236  {
237  if (is_array($val))
238  {
239  $val = key($val);
240  }
241 
242  $str .= "&".$key."=".$val;
243  }
244 
245  ilSession::set("referer",
246  preg_replace("/cmd=gateway/",substr($str,1),$_SERVER["REQUEST_URI"]));
247  ilSession::set("referer_ref_id",
248  (int) $_GET['ref_id']);
249  }
250  else
251  {
252  ilSession::set("referer", $_SERVER["REQUEST_URI"]);
253  ilSession::set("referer_ref_id",
254  (int) $_GET['ref_id']);
255  }
256 
257  ilSession::clear("error_post_vars");
258  }
259  }
260 
264  public function getMessageHTML($a_txt, $a_type = "info")
265  {
266  global $lng;
267 
268  $mtpl = new ilTemplate("tpl.message.html", true, true, "Services/Utilities");
269  $mtpl->setCurrentBlock($a_type."_message");
270  $mtpl->setVariable("TEXT", $a_txt);
271  $mtpl->setVariable("MESSAGE_HEADING", $lng->txt($a_type."_message"));
272  $mtpl->parseCurrentBlock();
273 
274  return $mtpl->get();
275  }
276 
283  function setCurrentBlock ($part = "DEFAULT")
284  {
285  $this->activeBlock = $part;
286 
287  if ($part == "DEFAULT")
288  {
289  return parent::setCurrentBlock();
290  }
291  else
292  {
293  return parent::setCurrentBlock($part);
294  }
295  }
296 
303  function touchBlock($block)
304  {
305  $this->setCurrentBlock($block);
306  //$count = $this->fillVars();
307  $this->parseCurrentBlock();
308 
309  if ($count == 0)
310  {
311  parent::touchBlock($block);
312  }
313  }
314 
321  function parseCurrentBlock($part = "DEFAULT")
322  {
323  // Hier erst noch ein replace aufrufen
324  if ($part != "DEFAULT")
325  {
326  $tmp = $this->activeBlock;
327  $this->activeBlock = $part;
328  }
329 
330  if ($part != "DEFAULT")
331  {
332  $this->activeBlock = $tmp;
333  }
334 
335  //$this->fillVars();
336 
337  $this->activeBlock = "__global__";
338 
339  if ($part == "DEFAULT")
340  {
341  return parent::parseCurrentBlock();
342  }
343  else
344  {
345  return parent::parseCurrentBlock($part);
346  }
347  }
352  function setMessage($a_type, $a_txt, $a_keep = false)
353  {
354  if (!in_array($a_type, array("info", "success", "failure", "question")) || $a_txt == "")
355  {
356  return;
357  }
358  if ($a_type == "question")
359  {
360  $a_type = "mess_question";
361  }
362  if (!$a_keep)
363  {
364  $this->message[$a_type] = $a_txt;
365  }
366  else
367  {
368  ilSession::set($a_type, $a_txt);
369  }
370  }
371 
372  function fillMessage()
373  {
374  global $lng;
375 
376  $ms = array("info", "success", "failure", "question");
377  $out = "";
378 
379  foreach ($ms as $m)
380  {
381  if ($m == "question")
382  {
383  $m = "mess_question";
384  }
385 
386  $txt = (ilSession::get($m) != "")
387  ? ilSession::get($m)
388  : $this->message[$m];
389 
390  if ($m == "mess_question")
391  {
392  $m = "question";
393  }
394 
395  if ($txt != "")
396  {
397  $mtpl = new ilTemplate("tpl.message.html", true, true, "Services/Utilities");
398  $mtpl->setCurrentBlock($m."_message");
399  $mtpl->setVariable("TEXT", $txt);
400  $mtpl->setVariable("MESSAGE_HEADING", $lng->txt($m."_message"));
401  $mtpl->parseCurrentBlock();
402  $out.= $mtpl->get();
403  }
404 
405  if ($m == "question")
406  {
407  $m = "mess_question";
408  }
409 
410  if (ilSession::get($m))
411  {
412  ilSession::clear($m);
413  }
414  }
415 
416  if ($out != "")
417  {
418  $this->setVariable("MESSAGE", $out);
419  }
420  }
421 
428  function blockExists($a_blockname)
429  {
430  // added second evaluation to the return statement because the first one only works for the content block (Helmut Schottmüller, 2007-09-14)
431  return (isset($this->blockvariables["content"][$a_blockname]) ? true : false) | (isset($this->blockvariables[$a_blockname]) ? true : false);
432  }
433 
437  function addJavaScript($a_js_file)
438  {
439  if (!in_array($a_js_file, $this->js_files))
440  {
441  $this->js_files[] = $a_js_file;
442  }
443  }
444 
446  {
447  global $ilias,$ilTabs;
448  if ($this->blockExists("js_file"))
449  {
450  foreach($this->js_files as $file)
451  {
452  if (is_file($file) || substr($file, 0, 4) == "http")
453  {
454  $this->setCurrentBlock("js_file");
455  $this->setVariable("JS_FILE", $file);
456  $this->parseCurrentBlock();
457  }
458  }
459  }
460  }
461 
465  function addCss($a_css_file, $media = "screen")
466  {
467  if (!array_key_exists($a_css_file . $media, $this->css_files))
468  {
469  $this->css_files[$a_css_file . $media] = array("file" => $a_css_file, "media" => $media);
470  }
471  }
472 
478  function fillCssFiles($a_force = false)
479  {
480  if (!$this->blockExists("css_file"))
481  {
482  return;
483  }
484  foreach($this->css_files as $css)
485  {
486  $filename = $css["file"];
487  if (strpos($filename, "?") > 0) $filename = substr($filename, 0, strpos($filename, "?"));
488  if (is_file($filename) || $a_force)
489  {
490  $this->setCurrentBlock("css_file");
491  $this->setVariable("CSS_FILE", $css["file"]);
492  $this->setVariable("CSS_MEDIA", $css["media"]);
493  $this->parseCurrentBlock();
494  }
495  }
496  }
497 
498 
499  function get($part = "DEFAULT")
500  {
501  if ($part == "DEFAULT")
502  {
503  return parent::get();
504  }
505  else
506  {
507  return parent::get($part);
508  }
509  }
510 
511 
512 }
513 ?>
getTemplatePath($a_tplname, $a_in_module=false, $a_plugin=false)
builds a full template path with template and module name
$path
Definition: aliased.php:25
if((!isset($_SERVER['DOCUMENT_ROOT'])) OR(empty($_SERVER['DOCUMENT_ROOT']))) $_SERVER['DOCUMENT_ROOT']
setMessage($a_type, $a_txt, $a_keep=false)
Set message.
__construct($file, $flag1, $flag2, $in_module=false, $vars="DEFAULT")
constructor
$_GET["client_id"]
static get($a_var)
Get a value.
static set($a_var, $a_val)
Set a value.
global $ilCtrl
Definition: ilias.php:18
const DEBUG
loadTemplatefile( $filename, $removeUnknownVariables=true, $removeEmptyBlocks=true)
Reads a template file from the disk.
$a_type
Definition: workflow.php:93
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:613
touchBlock($block)
overwrites ITX::touchBlock.
special template class to simplify handling of ITX/PEAR
static clear($a_var)
Unset a value.
Add a drawing to the header
Definition: 04printing.php:69
addJavaScript($a_js_file)
Add a javascript file that should be included in the header.
$txt
Definition: error.php:12
addBlockFile($var, $block, $tplname, $in_module=false)
Create styles array
The data for the language used.
setCurrentBlock($part="DEFAULT")
Überladene Funktion, die sich hier lokal noch den aktuellen Block merkt.
parseCurrentBlock()
Parses the current block.
Definition: IT.php:594
getMessageHTML($a_txt, $a_type="info")
Get HTML for a system message.
fillCssFiles($a_force=false)
Fill in the css file tags.
blockExists($a_blockname)
check if block exists in actual template private
global $lng
Definition: privfeed.php:17
const ILIAS_MODULE
Definition: server.php:14
if(!file_exists("$old.txt")) if($old===$new) if(file_exists("$new.txt")) $file
setOption($option, $value)
Sets the option for the template class.
Definition: IT.php:397
addCss($a_css_file, $media="screen")
Add a css file that should be included in the header.
$_POST["username"]
parseCurrentBlock($part="DEFAULT")
Überladene Funktion, die auf den aktuelle Block vorher noch ein replace ausführt public...
show($part="DEFAULT")
public