ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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
4include_once("./Services/UICore/lib/html-it/IT.php");
5include_once("./Services/UICore/lib/html-it/ITX.php");
6
13class ilTemplate extends HTML_Template_ITX
14{
19 public $vars;
20 //var $js_files = array(0 => "./Services/JavaScript/js/Basic.js"); // list of JS files that should be included
21 public $js_files = array(); // list of JS files that should be included
22 public $css_files = array(); // list of css files that should be included
23
30 public $activeBlock;
31
41 /*function ilTemplate($root)
42 {
43
44 $this->callConstructor();
45
46 $this->setRoot($root);
47
48 return true;
49 }*/
50 public 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 die("template " . $fname . " was not found.");
63 return false;
64 }
65
66 //$this->IntegratedTemplateExtension(dirname($fname));
67 parent::__construct();
68 //$this->loadTemplatefile(basename($fname), $flag1, $flag2);
69 $this->loadTemplatefile($fname, $flag1, $flag2);
70 //add tplPath to replacevars
71 $this->vars["TPLPATH"] = $this->tplPath;
72
73 // set Options
74 if (method_exists($this, "setOption")) {
75 $this->setOption('use_preg', false);
76 }
77
78 return true;
79 }
80
89 public function getTemplatePath($a_tplname, $a_in_module = false, $a_plugin = false)
90 {
91 global $ilias, $ilCtrl;
92
93 // if baseClass functionality is used (ilias.php):
94 // get template directory from ilCtrl
95 if (!empty($_GET["baseClass"]) && $a_in_module === true) {
96 $a_in_module = $ilCtrl->getModuleDir();
97 }
98
99 if (strpos($a_tplname, "/") === false) {
100 $module_path = "";
101
102 //$fname = $ilias->tplPath;
103 if ($a_in_module) {
104 if ($a_in_module === true) {
105 $module_path = ILIAS_MODULE . "/";
106 } else {
107 $module_path = $a_in_module . "/";
108 }
109 }
110
111 if ($fname == "" || !file_exists($fname)) {
112 if ($a_in_module == "setup") {
113 $fname = "./" . $module_path . "templates/" . basename($a_tplname);
114 } else {
115 $fname = "./" . $module_path . "templates/default/" . basename($a_tplname);
116 }
117 }
118 } else {
119 $fname = $a_tplname;
120 }
121
122 return $fname;
123 }
124
125 public function addBlockFile($var, $block, $tplname, $in_module = false)
126 {
127 if (DEBUG) {
128 echo "<br/>Template '" . $this->tplPath . "/" . $tplname . "'";
129 }
130
131 $tplfile = $this->getTemplatePath($tplname, $in_module);
132 if (file_exists($tplfile) == false) {
133 echo "<br/>Template '" . $tplfile . "' doesn't exist! aborting...";
134 return false;
135 }
136
137 return parent::addBlockFile($var, $block, $tplfile);
138 }
139
144 public function show($part = "DEFAULT")
145 {
146 header('Content-type: text/html; charset=UTF-8');
147
148 $this->fillJavaScriptFiles();
149 $this->fillCssFiles();
150
151 // ERROR HANDLER SETS $_GET["message"] IN CASE OF $error_obj->MESSAGE
152 $ms = array("info", "success", "failure", "question");
153 $out = "";
154
155 foreach ($ms as $m) {
156 if ($m == "question") {
157 $m = "mess_question";
158 }
159
160 $txt = (ilSession::get($m) != "")
162 : $this->message[$m];
163
164 if ($m == "mess_question") {
165 $m = "question";
166 }
167
168 if ($txt != "") {
169 $out.= $this->getMessageHTML($txt, $m);
170 }
171
172 if ($m == "question") {
173 $m = "mess_question";
174 }
175
176 if (ilSession::get($m)) {
178 }
179 }
180
181 if ($this->blockExists("MESSAGE") && $out != "") {
182 $this->setVariable("MESSAGE", $out);
183 }
184
185 if ($part == "DEFAULT") {
186 parent::show();
187 } else {
188 parent::show($part);
189 }
190
191 if (((substr(strrchr($_SERVER["PHP_SELF"], "/"), 1) != "error.php")
192 && (substr(strrchr($_SERVER["PHP_SELF"], "/"), 1) != "adm_menu.php"))) {
193 ilSession::set("post_vars", $_POST);
194
195 // referer is modified if query string contains cmd=gateway and $_POST is not empty.
196 // this is a workaround to display formular again in case of error and if the referer points to another page
197 $url_parts = parse_url($_SERVER["REQUEST_URI"]);
198 if (!$url_parts) {
199 $protocol = (isset($_SERVER['HTTPS']) ? 'https' : 'http') . '://';
200 $host = $_SERVER['HTTP_HOST'];
201 $path = $_SERVER['REQUEST_URI'];
202 $url_parts = @parse_url($protocol . $host . $path);
203 }
204
205 if (preg_match("/cmd=gateway/", $url_parts["query"])) {
206 foreach ($_POST as $key => $val) {
207 if (is_array($val)) {
208 $val = key($val);
209 }
210
211 $str .= "&" . $key . "=" . $val;
212 }
213
215 "referer",
216 preg_replace("/cmd=gateway/", substr($str, 1), $_SERVER["REQUEST_URI"])
217 );
219 "referer_ref_id",
220 (int) $_GET['ref_id']
221 );
222 } else {
223 ilSession::set("referer", $_SERVER["REQUEST_URI"]);
225 "referer_ref_id",
226 (int) $_GET['ref_id']
227 );
228 }
229
230 ilSession::clear("error_post_vars");
231 }
232 }
233
237 public function getMessageHTML($a_txt, $a_type = "info")
238 {
239 global $lng;
240
241 $mtpl = new ilTemplate("tpl.message.html", true, true, "Services/Utilities");
242 $mtpl->setCurrentBlock($a_type . "_message");
243 $mtpl->setVariable("TEXT", $a_txt);
244 $mtpl->setVariable("MESSAGE_HEADING", $lng->txt($a_type . "_message"));
245 $mtpl->parseCurrentBlock();
246
247 return $mtpl->get();
248 }
249
256 public function setCurrentBlock($part = "DEFAULT")
257 {
258 $this->activeBlock = $part;
259
260 if ($part == "DEFAULT") {
261 return parent::setCurrentBlock();
262 } else {
263 return parent::setCurrentBlock($part);
264 }
265 }
266
273 public function touchBlock($block)
274 {
275 $this->setCurrentBlock($block);
276 //$count = $this->fillVars();
277 $this->parseCurrentBlock();
278
279 if ($count == 0) {
280 parent::touchBlock($block);
281 }
282 }
283
290 public function parseCurrentBlock($part = "DEFAULT")
291 {
292 // Hier erst noch ein replace aufrufen
293 if ($part != "DEFAULT") {
294 $tmp = $this->activeBlock;
295 $this->activeBlock = $part;
296 }
297
298 if ($part != "DEFAULT") {
299 $this->activeBlock = $tmp;
300 }
301
302 //$this->fillVars();
303
304 $this->activeBlock = "__global__";
305
306 if ($part == "DEFAULT") {
307 return parent::parseCurrentBlock();
308 } else {
309 return parent::parseCurrentBlock($part);
310 }
311 }
316 public function setMessage($a_type, $a_txt, $a_keep = false)
317 {
318 if (!in_array($a_type, array("info", "success", "failure", "question")) || $a_txt == "") {
319 return;
320 }
321 if ($a_type == "question") {
322 $a_type = "mess_question";
323 }
324 if (!$a_keep) {
325 $this->message[$a_type] = $a_txt;
326 } else {
327 ilSession::set($a_type, $a_txt);
328 }
329 }
330
331 public function fillMessage()
332 {
333 global $lng;
334
335 $ms = array("info", "success", "failure", "question");
336 $out = "";
337
338 foreach ($ms as $m) {
339 if ($m == "question") {
340 $m = "mess_question";
341 }
342
343 $txt = (ilSession::get($m) != "")
345 : $this->message[$m];
346
347 if ($m == "mess_question") {
348 $m = "question";
349 }
350
351 if ($txt != "") {
352 $mtpl = new ilTemplate("tpl.message.html", true, true, "Services/Utilities");
353 $mtpl->setCurrentBlock($m . "_message");
354 $mtpl->setVariable("TEXT", $txt);
355 $mtpl->setVariable("MESSAGE_HEADING", $lng->txt($m . "_message"));
356 $mtpl->parseCurrentBlock();
357 $out.= $mtpl->get();
358 }
359
360 if ($m == "question") {
361 $m = "mess_question";
362 }
363
364 if (ilSession::get($m)) {
366 }
367 }
368
369 if ($out != "") {
370 $this->setVariable("MESSAGE", $out);
371 }
372 }
373
380 public function blockExists($a_blockname)
381 {
382 // added second evaluation to the return statement because the first one only works for the content block (Helmut Schottmüller, 2007-09-14)
383 return (isset($this->blockvariables["content"][$a_blockname]) ? true : false) | (isset($this->blockvariables[$a_blockname]) ? true : false);
384 }
385
389 public function addJavaScript($a_js_file)
390 {
391 if (!in_array($a_js_file, $this->js_files)) {
392 $this->js_files[] = $a_js_file;
393 }
394 }
395
396 public function fillJavaScriptFiles()
397 {
398 global $ilias,$ilTabs;
399 if ($this->blockExists("js_file")) {
400 foreach ($this->js_files as $file) {
401 if (is_file($file) || substr($file, 0, 4) == "http") {
402 $this->setCurrentBlock("js_file");
403 $this->setVariable("JS_FILE", $file);
404 $this->parseCurrentBlock();
405 }
406 }
407 }
408 }
409
413 public function addCss($a_css_file, $media = "screen")
414 {
415 if (!array_key_exists($a_css_file . $media, $this->css_files)) {
416 $this->css_files[$a_css_file . $media] = array("file" => $a_css_file, "media" => $media);
417 }
418 }
419
425 public function fillCssFiles($a_force = false)
426 {
427 if (!$this->blockExists("css_file")) {
428 return;
429 }
430 foreach ($this->css_files as $css) {
431 $filename = $css["file"];
432 if (strpos($filename, "?") > 0) {
433 $filename = substr($filename, 0, strpos($filename, "?"));
434 }
435 if (is_file($filename) || $a_force) {
436 $this->setCurrentBlock("css_file");
437 $this->setVariable("CSS_FILE", $css["file"]);
438 $this->setVariable("CSS_MEDIA", $css["media"]);
439 $this->parseCurrentBlock();
440 }
441 }
442 }
443
444
445 public function get($part = "DEFAULT")
446 {
447 if ($part == "DEFAULT") {
448 return parent::get();
449 } else {
450 return parent::get($part);
451 }
452 }
453}
$_GET["client_id"]
$_POST["username"]
An exception for terminatinating execution or to throw for unit testing.
setOption($option, $value)
Sets the option for the template class.
Definition: IT.php:397
parseCurrentBlock()
Parses the current block.
Definition: IT.php:594
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:613
show()
returns the content of IniFile @access public
static set($a_var, $a_val)
Set a value.
static clear($a_var)
Unset a value.
static get($a_var)
Get a value.
special template class to simplify handling of ITX/PEAR
touchBlock($block)
overwrites ITX::touchBlock.
show($part="DEFAULT")
@access public
loadTemplatefile( $filename, $removeUnknownVariables=true, $removeEmptyBlocks=true)
Reads a template file from the disk.
setMessage($a_type, $a_txt, $a_keep=false)
Set message.
blockExists($a_blockname)
check if block exists in actual template @access private
addBlockFile($var, $block, $tplname, $in_module=false)
parseCurrentBlock($part="DEFAULT")
Überladene Funktion, die auf den aktuelle Block vorher noch ein replace ausführt @access public.
fillCssFiles($a_force=false)
Fill in the css file tags.
addJavaScript($a_js_file)
Add a javascript file that should be included in the header.
getMessageHTML($a_txt, $a_type="info")
Get HTML for a system message.
addCss($a_css_file, $media="screen")
Add a css file that should be included in the header.
setCurrentBlock($part="DEFAULT")
Überladene Funktion, die sich hier lokal noch den aktuellen Block merkt.
__construct($file, $flag1, $flag2, $in_module=false, $vars="DEFAULT")
constructor
getTemplatePath($a_tplname, $a_in_module=false, $a_plugin=false)
builds a full template path with template and module name
$key
Definition: croninfo.php:18
$txt
Definition: error.php:11
global $ilCtrl
Definition: ilias.php:18
const DEBUG
global $lng
Definition: privfeed.php:17
if(!file_exists("$old.txt")) if( $old===$new) if(file_exists("$new.txt")) $file
if((!isset($_SERVER['DOCUMENT_ROOT'])) OR(empty($_SERVER['DOCUMENT_ROOT']))) $_SERVER['DOCUMENT_ROOT']
const ILIAS_MODULE
Definition: server.php:14
$a_type
Definition: workflow.php:92