ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules 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 
4 include_once("./Services/UICore/lib/html-it/IT.php");
5 include_once("./Services/UICore/lib/html-it/ITX.php");
6 
14 {
19  public $vars;
20 
27  public $activeBlock;
28 
32  protected static $il_cache = array();
33 
37  protected $il_use_cache;
38 
42  protected $il_cur_key;
43 
47  protected $tplName;
48 
52  protected $tplPath;
53 
57  protected $tplIdentifier;
58 
71  public function __construct(
72  string $file,
73  bool $flag1,
74  bool $flag2,
75  string $in_module = "",
76  string $vars = "DEFAULT",
77  bool $plugin = false,
78  bool $a_use_cache = true
79  ) {
80  $this->activeBlock = "__global__";
81  $this->vars = array();
82 
83  $this->il_use_cache = $a_use_cache;
84  $this->il_cur_key = $file . "/" . $in_module;
85 
86  $fname = $this->getTemplatePath($file, $in_module, $plugin);
87 
88  $this->tplName = basename($fname);
89  $this->tplPath = dirname($fname);
90  $this->tplIdentifier = $this->getTemplateIdentifier($file, $in_module);
91 
92  if (!file_exists($fname)) {
93  throw new \LogicException("Template '$fname' was not found.");
94  }
95 
97  $this->loadTemplatefile($fname, $flag1, $flag2);
98  //add tplPath to replacevars
99  $this->vars["TPLPATH"] = $this->tplPath;
100 
101  // Option for baseclass HTML_Template_IT
102  $this->setOption('use_preg', false);
103 
104  return true;
105  }
106 
107  // overwrite their init function
108  protected function init()
109  {
110  $this->free();
111  $this->buildFunctionlist();
112 
113  $cache_hit = false;
114  if ($this->il_use_cache) {
115  // cache hit
116  if (isset(self::$il_cache[$this->il_cur_key]) && is_array(self::$il_cache[$this->il_cur_key])) {
117  $cache_hit = true;
118  //echo "cache hit";
119  $this->err = self::$il_cache[$this->il_cur_key]["err"];
120  $this->flagBlocktrouble = self::$il_cache[$this->il_cur_key]["flagBlocktrouble"];
121  $this->blocklist = self::$il_cache[$this->il_cur_key]["blocklist"];
122  $this->blockdata = self::$il_cache[$this->il_cur_key]["blockdata"];
123  $this->blockinner = self::$il_cache[$this->il_cur_key]["blockinner"];
124  $this->blockparents = self::$il_cache[$this->il_cur_key]["blockparents"];
125  $this->blockvariables = self::$il_cache[$this->il_cur_key]["blockvariables"];
126  }
127  }
128 
129  if (!$cache_hit) {
130  $this->findBlocks($this->template);
131  $this->template = '';
132  $this->buildBlockvariablelist();
133  if ($this->il_use_cache) {
134  self::$il_cache[$this->il_cur_key]["err"] = $this->err;
135  self::$il_cache[$this->il_cur_key]["flagBlocktrouble"] = $this->flagBlocktrouble;
136  self::$il_cache[$this->il_cur_key]["blocklist"] = $this->blocklist;
137  self::$il_cache[$this->il_cur_key]["blockdata"] = $this->blockdata;
138  self::$il_cache[$this->il_cur_key]["blockinner"] = $this->blockinner;
139  self::$il_cache[$this->il_cur_key]["blockparents"] = $this->blockparents;
140  self::$il_cache[$this->il_cur_key]["blockvariables"] = $this->blockvariables;
141  }
142  }
143 
144  // we don't need it any more
145  $this->template = '';
146  } // end func init
147 
154  public function blockExists($a_blockname)
155  {
156  // added second evaluation to the return statement because the first one only works for the content block (Helmut Schottmüller, 2007-09-14)
157  return (isset($this->blockvariables["content"][$a_blockname]) ? true : false) | (isset($this->blockvariables[$a_blockname]) ? true : false);
158  }
159 
164  public function get($part = "DEFAULT")
165  {
166  global $DIC;
167 
168  $html = $this->getUnmodified($part);
169 
170  // include the template output hook
171  $ilPluginAdmin = $DIC["ilPluginAdmin"];
172  $pl_names = $ilPluginAdmin->getActivePluginsForSlot(IL_COMP_SERVICE, "UIComponent", "uihk");
173  foreach ($pl_names as $pl) {
174  $ui_plugin = ilPluginAdmin::getPluginObject(IL_COMP_SERVICE, "UIComponent", "uihk", $pl);
175  $gui_class = $ui_plugin->getUIClassInstance();
176 
177  $resp = $gui_class->getHTML(
178  "",
179  "template_get",
180  array("tpl_id" => $this->tplIdentifier, "tpl_obj" => $this, "html" => $html)
181  );
182 
183  if ($resp["mode"] != ilUIHookPluginGUI::KEEP) {
184  $html = $gui_class->modifyHTML($html, $resp);
185  }
186  }
187 
188  return $html;
189  }
190 
195  public function getUnmodified($part = "DEFAULT")
196  {
197  global $DIC;
198 
199  if ($part == "DEFAULT") {
200  return parent::get();
201  }
202  return parent::get($part);
203  }
204 
205 
206 
213  public function setCurrentBlock($part = "DEFAULT")
214  {
215  $this->activeBlock = $part;
216 
217  if ($part == "DEFAULT") {
218  return parent::setCurrentBlock();
219  } else {
220  return parent::setCurrentBlock($part);
221  }
222  }
223 
230  public function touchBlock($block)
231  {
232  $this->setCurrentBlock($block);
233  $count = $this->fillVars();
234  $this->parseCurrentBlock();
235 
236  if ($count == 0) {
237  parent::touchBlock($block);
238  }
239  }
240 
247  public function parseCurrentBlock($part = "DEFAULT")
248  {
249  // Hier erst noch ein replace aufrufen
250  if ($part != "DEFAULT") {
251  $tmp = $this->activeBlock;
252  $this->activeBlock = $part;
253  }
254 
255  if ($part != "DEFAULT") {
256  $this->activeBlock = $tmp;
257  }
258 
259  $this->fillVars();
260 
261  $this->activeBlock = "__global__";
262 
263  if ($part == "DEFAULT") {
264  return parent::parseCurrentBlock();
265  } else {
266  return parent::parseCurrentBlock($part);
267  }
268  }
269 
279  public function addBlockFile($var, $block, $tplname, $in_module = false)
280  {
281  global $DIC;
282 
283  if (DEBUG) {
284  echo "<br/>Template '" . $this->tplPath . "/" . $tplname . "'";
285  }
286 
287  $tplfile = $this->getTemplatePath($tplname, $in_module);
288  if (file_exists($tplfile) == false) {
289  echo "<br/>Template '" . $tplfile . "' doesn't exist! aborting...";
290  return false;
291  }
292 
293  $id = $this->getTemplateIdentifier($tplname, $in_module);
294  $template = $this->getFile($tplfile);
295 
296  // include the template input hook
297  $ilPluginAdmin = $DIC["ilPluginAdmin"];
298  $pl_names = $ilPluginAdmin->getActivePluginsForSlot(IL_COMP_SERVICE, "UIComponent", "uihk");
299  foreach ($pl_names as $pl) {
300  $ui_plugin = ilPluginAdmin::getPluginObject(IL_COMP_SERVICE, "UIComponent", "uihk", $pl);
301  $gui_class = $ui_plugin->getUIClassInstance();
302 
303  $resp = $gui_class->getHTML(
304  "",
305  "template_add",
306  array("tpl_id" => $id, "tpl_obj" => $this, "html" => $template)
307  );
308 
309  if ($resp["mode"] != ilUIHookPluginGUI::KEEP) {
310  $template = $gui_class->modifyHTML($template, $resp);
311  }
312  }
313 
314  return $this->addBlock($var, $block, $template);
315  }
316 
323  private function fillVars()
324  {
325  $count = 0;
326  reset($this->vars);
327 
328  foreach ($this->vars as $key => $val) {
329  if (is_array($this->blockvariables[$this->activeBlock])) {
330  if (array_key_exists($key, $this->blockvariables[$this->activeBlock])) {
331  $count++;
332 
333  $this->setVariable($key, $val);
334  }
335  }
336  }
337 
338  return $count;
339  }
340 
354  public function loadTemplatefile(
355  $filename,
357  $removeEmptyBlocks = true
358  ) {
359  global $DIC;
360 
361  // copied from IT:loadTemplateFile
362  $template = '';
363  if (!$this->flagCacheTemplatefile ||
364  $this->lastTemplatefile != $filename
365  ) {
366  $template = $this->getFile($filename);
367  }
368  $this->lastTemplatefile = $filename;
369  // copied.
370 
371  // new code to include the template input hook:
372  $ilPluginAdmin = $DIC["ilPluginAdmin"];
373  $pl_names = $ilPluginAdmin->getActivePluginsForSlot(IL_COMP_SERVICE, "UIComponent", "uihk");
374  foreach ($pl_names as $pl) {
375  $ui_plugin = ilPluginAdmin::getPluginObject(IL_COMP_SERVICE, "UIComponent", "uihk", $pl);
376  $gui_class = $ui_plugin->getUIClassInstance();
377 
378  $resp = $gui_class->getHTML(
379  "",
380  "template_load",
381  array("tpl_id" => $this->tplIdentifier, "tpl_obj" => $this, "html" => $template)
382  );
383 
384  if ($resp["mode"] != ilUIHookPluginGUI::KEEP) {
385  $template = $gui_class->modifyHTML($template, $resp);
386  }
387  }
388  // new.
389 
390  // copied from IT:loadTemplateFile
391  return $template != '' ?
392  $this->setTemplate(
393  $template,
396  ) : false;
397  // copied.
398  }
399 
400 
409  protected function getTemplatePath($a_tplname, $a_in_module = false, $a_plugin = false)
410  {
411  global $DIC;
412 
413  $ilCtrl = null;
414  if (isset($DIC["ilCtrl"])) {
415  $ilCtrl = $DIC->ctrl();
416  }
417 
418  $fname = "";
419 
420  if (strpos($a_tplname, "/") === false) {
421  $module_path = "";
422 
423  if ($a_in_module != "") {
424  $module_path = $a_in_module . "/";
425  }
426 
427  // use ilStyleDefinition instead of account to get the current skin
428  include_once "Services/Style/System/classes/class.ilStyleDefinition.php";
429  if (ilStyleDefinition::getCurrentSkin() != "default") {
431 
432  $fname = "./Customizing/global/skin/" .
433  ilStyleDefinition::getCurrentSkin() . "/" . $style . "/" . $module_path
434  . basename($a_tplname);
435 
436  if ($fname == "" || !file_exists($fname)) {
437  $fname = "./Customizing/global/skin/" .
438  ilStyleDefinition::getCurrentSkin() . "/" . $module_path . basename($a_tplname);
439  }
440  }
441 
442  if ($fname == "" || !file_exists($fname)) {
443  $fname = "./" . $module_path . "templates/default/" . basename($a_tplname);
444  }
445  } elseif (strpos($a_tplname, "src/UI") === 0) {
446  if (class_exists("ilStyleDefinition") // for testing
447  && ilStyleDefinition::getCurrentSkin() != "default") {
450  $base_path = "./Customizing/global/skin/";
451  $ui_path = "/" . str_replace("src/UI/templates/default", "UI", $a_tplname);
452  $fname = $base_path . ilStyleDefinition::getCurrentSkin() . "/" . $style . "/" . $ui_path;
453 
454  if (!file_exists($fname)) {
455  $fname = $base_path . $skin . "/" . $ui_path;
456  }
457  }
458 
459  if ($fname == "" || !file_exists($fname)) {
460  $fname = $a_tplname;
461  }
462  } else {
463  $fname = $a_tplname;
464  }
465  return $fname;
466  }
467 
482  private function getTemplateIdentifier($a_tplname, $a_in_module = false)
483  {
484  global $DIC;
485 
486  $ilCtrl = null;
487  if (isset($DIC["ilCtrl"])) {
488  $ilCtrl = $DIC->ctrl();
489  }
490 
491 
492  // if baseClass functionality is used (ilias.php):
493  // get template directory from ilCtrl
494  if (!empty($_GET["baseClass"]) && $a_in_module === true) {
495  $a_in_module = $ilCtrl->getModuleDir();
496  }
497 
498  if (strpos($a_tplname, "/") === false) {
499  if ($a_in_module) {
500  if ($a_in_module === true) {
501  $module_path = ILIAS_MODULE . "/";
502  } else {
503  $module_path = $a_in_module . "/";
504  }
505  } else {
506  $module_path = "";
507  }
508 
509  return $module_path . basename($a_tplname);
510  } else {
511  return $a_tplname;
512  }
513  }
514 
515  public function variableExists($a_variablename)
516  {
517  return (isset($this->blockvariables["content"][$a_variablename]) ? true : false);
518  }
519 }
getTemplatePath($a_tplname, $a_in_module=false, $a_plugin=false)
builds a full template path with template and module name
$_GET["client_id"]
__construct(string $file, bool $flag1, bool $flag2, string $in_module="", string $vars="DEFAULT", bool $plugin=false, bool $a_use_cache=true)
constructor ilTemplate constructor.
$removeUnknownVariables
Definition: IT.php:195
addBlock($placeholder, $blockname, $template)
Adds a block to the template changing a variable placeholder to a block placeholder.
Definition: ITX.php:241
getUnmodified($part="DEFAULT")
getTemplateIdentifier($a_tplname, $a_in_module=false)
get a unique template identifier
loadTemplatefile( $filename, $removeUnknownVariables=true, $removeEmptyBlocks=true)
Reads a template file from the disk.
free()
Clears all datafields of the object.
Definition: IT.php:716
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:615
static getCurrentSkin()
get the current skin
touchBlock($block)
overwrites ITX::touchBlock.
get(string $class_name)
global $DIC
Definition: goto.php:24
static getPluginObject($a_ctype, $a_cname, $a_slot_id, $a_pname)
Get Plugin Object.
addBlockFile($var, $block, $tplname, $in_module=false)
overwrites ITX::addBlockFile public
$filename
Definition: buildRTE.php:89
setCurrentBlock($part="DEFAULT")
Überladene Funktion, die sich hier lokal noch den aktuellen Block merkt.
parseCurrentBlock()
Parses the current block.
Definition: IT.php:596
buildBlockvariablelist()
Build a list of all variables within of a block.
Definition: IT.php:824
blockExists($a_blockname)
check if block exists in actual template private
__construct(Container $dic, ilPlugin $plugin)
$removeEmptyBlocks
Definition: IT.php:202
setTemplate( $template, $removeUnknownVariables=true, $removeEmptyBlocks=true)
Sets the template.
Definition: IT.php:743
fillVars()
all template vars defined in $vars will be replaced automatically without setting and parsing them wi...
const ILIAS_MODULE
Definition: server.php:14
setOption($option, $value)
Sets the option for the template class.
Definition: IT.php:399
static getCurrentStyle()
get the current style or sub style
variableExists($a_variablename)
findBlocks($string)
Recusively builds a list of all blocks within the template.
Definition: IT.php:865
parseCurrentBlock($part="DEFAULT")
Überladene Funktion, die auf den aktuelle Block vorher noch ein replace ausführt public...
const IL_COMP_SERVICE
getFile($filename)
Reads a file from disk and returns its content.
Definition: IT.php:914
buildFunctionlist()
Builds a functionlist from the template.
Definition: ITX.php:591