ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilTemplate.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 require_once __DIR__ . '/../lib/html-it/IT.php';
22 require_once __DIR__ . '/../lib/html-it/ITX.php';
23 
31 {
35  public array $vars = [];
36 
42  public string $activeBlock = '';
43 
44  protected static array $il_cache = [];
45 
46  protected bool $il_use_cache;
47 
48  protected string $il_cur_key;
49 
50  protected string $tplName;
51 
52  protected string $tplPath;
53 
54  protected string $tplIdentifier;
55 
68  public function __construct(
69  string $file,
70  bool $flag1,
71  bool $flag2,
72  string $in_module = "",
74  bool $plugin = false,
75  bool $a_use_cache = true
76  ) {
77  $this->activeBlock = HTML_Template_IT::IT_DEFAULT_BLOCK;
78  $this->il_use_cache = $a_use_cache;
79  $this->il_cur_key = $file . "/" . $in_module;
80 
81  $fname = $this->getTemplatePath($file, $in_module);
82  if (!file_exists($fname)) {
83  throw new ilTemplateException("Template '$fname' was not found.\nfile: " . $file . "\nmodule: " . $in_module);
84  }
85 
86  $this->tplName = basename($fname);
87  $this->tplPath = dirname($fname);
88  $this->vars["TPLPATH"] = $this->tplPath;
89  $this->tplIdentifier = $this->getTemplateIdentifier($file, $in_module);
90 
92 
93  $this->loadTemplatefile($fname, $flag1, $flag2);
94  $this->setOption('use_preg', false);
95  }
96 
97  protected function init(): void
98  {
99  $this->free();
100  $this->buildFunctionlist();
101 
102  $cache_hit = false;
103  if ($this->il_use_cache &&
104  isset(self::$il_cache[$this->il_cur_key]) &&
105  is_array(self::$il_cache[$this->il_cur_key])
106  ) {
107  $cache_hit = true;
108  $this->err = self::$il_cache[$this->il_cur_key]["err"];
109  $this->flagBlocktrouble = self::$il_cache[$this->il_cur_key]["flagBlocktrouble"];
110  $this->blocklist = self::$il_cache[$this->il_cur_key]["blocklist"];
111  $this->blockdata = self::$il_cache[$this->il_cur_key]["blockdata"];
112  $this->blockinner = self::$il_cache[$this->il_cur_key]["blockinner"];
113  $this->blockparents = self::$il_cache[$this->il_cur_key]["blockparents"];
114  $this->blockvariables = self::$il_cache[$this->il_cur_key]["blockvariables"];
115  }
116 
117  if (!$cache_hit) {
118  $this->findBlocks($this->template);
119  $this->buildBlockvariablelist();
120  if ($this->il_use_cache) {
121  self::$il_cache[$this->il_cur_key]["err"] = $this->err;
122  self::$il_cache[$this->il_cur_key]["flagBlocktrouble"] = $this->flagBlocktrouble;
123  self::$il_cache[$this->il_cur_key]["blocklist"] = $this->blocklist;
124  self::$il_cache[$this->il_cur_key]["blockdata"] = $this->blockdata;
125  self::$il_cache[$this->il_cur_key]["blockinner"] = $this->blockinner;
126  self::$il_cache[$this->il_cur_key]["blockparents"] = $this->blockparents;
127  self::$il_cache[$this->il_cur_key]["blockvariables"] = $this->blockvariables;
128  }
129  }
130 
131  // we don't need it anymore
132  $this->template = '';
133  }
134 
135  public function blockExists(string $a_blockname): bool
136  {
137  // added second evaluation to the return statement because the first one
138  // only works for the content block (Helmut Schottmüller, 2007-09-14).
139  return
140  isset($this->blockvariables["content"][$a_blockname]) ||
141  isset($this->blockvariables[$a_blockname]);
142  }
143 
144  public function get(string $part = ilGlobalTemplateInterface::DEFAULT_BLOCK): string
145  {
146  global $DIC;
147 
148  $html = $this->getUnmodified($part);
149  $component_factory = $DIC["component.factory"];
150  foreach ($component_factory->getActivePluginsInSlot("uihk") as $ui_plugin) {
151  $gui_class = $ui_plugin->getUIClassInstance();
152  $resp = $gui_class->getHTML(
153  "",
154  "template_get",
155  [
156  "tpl_id" => $this->tplIdentifier,
157  "tpl_obj" => $this,
158  "html" => $html
159  ]
160  );
161 
162  if (ilUIHookPluginGUI::KEEP !== $resp["mode"]) {
163  $html = $gui_class->modifyHTML($html, $resp);
164  }
165  }
166 
167  return $html;
168  }
169 
173  public function getUnmodified(string $part = ilGlobalTemplateInterface::DEFAULT_BLOCK): string
174  {
175  // I can't believe how garbage this is.
177  $part = self::IT_DEFAULT_BLOCK;
178  }
179 
180  return parent::get($part);
181  }
182 
186  public function setCurrentBlock(string $part = ilGlobalTemplateInterface::DEFAULT_BLOCK): bool
187  {
188  // I can't believe how garbage this is.
190  $part = self::IT_DEFAULT_BLOCK;
191  }
192 
193  $this->activeBlock = $part;
194  return parent::setCurrentBlock($part);
195  }
196 
200  public function touchBlock(string $block): bool
201  {
202  $this->setCurrentBlock($block);
203  $count = $this->fillVars();
204  $this->parseCurrentBlock();
205 
206  if (0 === $count) {
207  return parent::touchBlock($block);
208  }
209 
210  return false;
211  }
212 
216  public function parseCurrentBlock(string $part = ilGlobalTemplateInterface::DEFAULT_BLOCK): bool
217  {
218  $this->fillVars();
219  $this->activeBlock = self::IT_DEFAULT_BLOCK;
220 
221  return parent::parseCurrentBlock();
222  }
223 
224  public function addBlockFile(string $var, string $block, string $tplname, ?string $in_module = null): bool
225  {
226  global $DIC;
227 
228  $tplfile = $this->getTemplatePath($tplname, $in_module ?? '');
229  if (file_exists($tplfile) === false) {
230  echo "<br/>Template '" . $tplfile . "' doesn't exist! aborting...";
231  return false;
232  }
233 
234  $id = $this->getTemplateIdentifier($tplname, $in_module);
235  $template = $this->getFile($tplfile);
236  $component_factory = $DIC["component.factory"];
237  foreach ($component_factory->getActivePluginsInSlot("uihk") as $ui_plugin) {
238  $gui_class = $ui_plugin->getUIClassInstance();
239  $resp = $gui_class->getHTML(
240  "",
241  "template_add",
242  [
243  "tpl_id" => $id,
244  "tpl_obj" => $this,
245  "html" => $template,
246  ]
247  );
248 
249  if ($resp["mode"] !== ilUIHookPluginGUI::KEEP) {
250  $template = $gui_class->modifyHTML($template, $resp);
251  }
252  }
253 
254  return $this->addBlock($var, $block, $template);
255  }
256 
261  private function fillVars(): int
262  {
263  $count = 0;
264  foreach ($this->vars as $key => $val) {
265  if (is_array($this->blockvariables[$this->activeBlock]) &&
266  array_key_exists($key, $this->blockvariables[$this->activeBlock])
267  ) {
268  $this->setVariable($key, $val);
269  $count++;
270  }
271  }
272 
273  return $count;
274  }
275 
276  public function loadTemplatefile(
277  string $filename,
278  bool $removeUnknownVariables = true,
279  bool $removeEmptyBlocks = true
280  ): bool {
281  global $DIC;
282 
283  $template = '';
284  if (!$this->flagCacheTemplatefile ||
285  $this->lastTemplatefile !== $filename
286  ) {
287  $template = $this->getFile($filename);
288  }
289  $this->lastTemplatefile = $filename;
290 
291  $component_factory = $DIC["component.factory"];
292  foreach ($component_factory->getActivePluginsInSlot("uihk") as $ui_plugin) {
293  $gui_class = $ui_plugin->getUIClassInstance();
294  $resp = $gui_class->getHTML(
295  "",
296  "template_load",
297  [
298  "tpl_id" => $this->tplIdentifier,
299  "tpl_obj" => $this,
300  "html" => $template,
301  ]
302  );
303 
304  if ($resp["mode"] !== ilUIHookPluginGUI::KEEP) {
305  $template = $gui_class->modifyHTML($template, $resp);
306  }
307  }
308 
309  return
310  $template !== '' &&
311  $this->setTemplate(
312  $template,
315  );
316  }
317 
318 
319  protected function getCurrentSkin(): ?string
320  {
321  // use ilStyleDefinition instead of account to get the current skin
323  }
324 
325  protected function getCurrentStyle(): ?string
326  {
328  }
329 
330  protected function fileExistsInSkin(string $path): bool
331  {
332  return file_exists($path);
333  }
334 
338  protected function getTemplatePath(string $a_tplname, string $a_in_module = ''): string
339  {
340  $ilias_root = realpath(__DIR__ . '/../../../../') . '/';
341 
342  if (str_starts_with($a_in_module, $ilias_root)) {
343  $a_in_module = str_replace($ilias_root, '', $a_in_module);
344  }
345 
346  if (str_ends_with($a_in_module, '/')) {
347  $a_in_module = rtrim($a_in_module, '/');
348  }
349 
350  $tpl_sub_path = '/templates/default/';
351  if (str_starts_with($a_tplname, 'components/ILIAS/UI/')) {
352  $a_in_module = 'components/ILIAS/UI/src';
353  $a_tplname = str_replace('components/ILIAS/UI/src/templates/default/', '', $a_tplname);
354  }
355 
356  if (str_starts_with($a_tplname, $ilias_root)) {
357  $a_tplname = str_replace($ilias_root, '', $a_tplname);
358  }
359  if (strpos($a_tplname, 'public/Customizing/global/plugins')) {
360  $tpl_sub_path = '';
361  }
362 
363  $base_path = $ilias_root;
364  $default = $base_path . $a_in_module . $tpl_sub_path . $a_tplname;
365 
366  $skin = $this->getCurrentSkin();
367  if ($skin === 'default') {
368  return $default;
369  }
370 
371  $style = $this->getCurrentStyle();
372  $base_path .= 'public/Customizing/skin/' . $skin . '/' . $style;
373 
374  if ($a_in_module === 'components/ILIAS/UI/src') {
375  $a_in_module = 'UI';
376  }
377 
378  $from_skin = $base_path . '/' . $a_in_module . '/' . $a_tplname;
379 
380  return $this->fileExistsInSkin($from_skin) ? $from_skin : $default;
381  }
382 
389  public function getTemplateIdentifier(string $a_tplname, ?string $a_in_module = null): string
390  {
391  if (strpos($a_tplname, "/") === false) {
392  if (null !== $a_in_module) {
393  $module_path = $a_in_module . "/";
394  } else {
395  $module_path = "";
396  }
397 
398  return $module_path . basename($a_tplname);
399  }
400 
401  return $a_tplname;
402  }
403 
404  public function variableExists(string $a_variablename): bool
405  {
406  return isset($this->blockvariables["content"][$a_variablename]);
407  }
408 }
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
setTemplate(string $template, bool $removeUnknownVariables=true, bool $removeEmptyBlocks=true)
Sets the template.
Definition: IT.php:643
bool $removeEmptyBlocks
Controls the handling of empty blocks, default is remove.
Definition: IT.php:184
string $template
Content of the template.
Definition: IT.php:199
array $blockparents
Array of block parents.
Definition: IT.php:219
array $blockvariables
Array of variables in a block.
Definition: IT.php:214
const IT_DEFAULT_BLOCK
Definition: IT.php:126
loadTemplatefile(string $filename, bool $removeUnknownVariables=true, bool $removeEmptyBlocks=true)
getUnmodified(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
bool $flagBlocktrouble
Internal flag indicating that a blockname was used multiple times.
Definition: IT.php:272
Integrated Template Extension - ITX With this class you get the full power of the phplib template cla...
Definition: ITX.php:44
string $activeBlock
Aktueller Block Der wird gemerkt bei der berladenen Funktion setCurrentBlock, damit beim ParseBlock v...
$path
Definition: ltiservices.php:29
bool $removeUnknownVariables
Controls the handling of unknown variables, default is remove.
Definition: IT.php:179
findBlocks(string $string)
Recusively builds a list of all blocks within the template.
Definition: IT.php:732
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
free()
Clears all datafields of the object.
Definition: IT.php:623
setOption(string $option, $value)
Sets the option for the template class.
Definition: IT.php:351
touchBlock(string $block)
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:544
getTemplatePath(string $a_tplname, string $a_in_module='')
static getCurrentSkin()
get the current skin use always this function instead of getting the account&#39;s skin the current skin ...
string $il_cur_key
get(string $class_name)
getFile(string $filename)
Reads a file from disk and returns its content.
Definition: IT.php:777
global $DIC
Definition: shib_login.php:26
blockExists(string $a_blockname)
variableExists(string $a_variablename)
$filename
Definition: buildRTE.php:78
getTemplateIdentifier(string $a_tplname, ?string $a_in_module=null)
get a unique template identifier The identifier is common for default or customized skins but distinc...
parseCurrentBlock()
Parses the current block.
Definition: IT.php:529
buildBlockvariablelist()
Build a list of all variables within of a block.
Definition: IT.php:713
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
fileExistsInSkin(string $path)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
__construct(Container $dic, ilPlugin $plugin)
fillVars()
all template vars defined in $vars will be replaced automatically without setting and parsing them wi...
string $tplIdentifier
static getCurrentStyle()
get the current style or sub style use always this function instead of getting the account&#39;s style th...
addBlockFile(string $var, string $block, string $tplname, ?string $in_module=null)
static array $il_cache
array $blockinner
Array of inner blocks of a block.
Definition: IT.php:224
array $err
Contains the error objects.
Definition: IT.php:134
array $blockdata
Array with the parsed content of a block.
Definition: IT.php:209
__construct(string $file, bool $flag1, bool $flag2, string $in_module="", string $vars=ilGlobalTemplateInterface::DEFAULT_BLOCK, bool $plugin=false, bool $a_use_cache=true)
constructor ilTemplate constructor.
array $vars
variablen die immer in jedem block ersetzt werden sollen
array $blocklist
Array of all blocks and their content.
Definition: IT.php:204
addBlock(string $placeholder, string $blockname, string $template)
Adds a block to the template changing a variable placeholder to a block placeholder.
Definition: ITX.php:186
buildFunctionlist()
Builds a functionlist from the template.
Definition: ITX.php:285