ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilTabsGUI.php
Go to the documentation of this file.
1 <?php
2 
25 class ilTabsGUI
26 {
27  protected ilCtrl $ctrl;
28 
29  public string $target_script;
30  public string $obj_type;
32  public ilLanguage $lng;
33  public array $tabs;
34  public array $target = array();
35  public array $sub_target = array();
36  public array $non_tabbed_link = array();
37  public bool $setup_mode = false;
38  protected bool $force_one_tab = false;
39  protected bool $manual_activation;
40  protected bool $subtab_manual_activation;
41  protected bool $sub_tabs;
42  protected string $temp_var;
43  protected string $back_title;
44  public string $back_target;
45  protected string $back_2_target;
46  protected string $back_2_title;
47  protected string $back_2_frame;
48  protected string $back_frame;
49 
50  public function __construct()
51  {
53  global $DIC;
54 
55  $this->ctrl = $DIC->ctrl();
56  $this->tpl = $DIC->ui()->mainTemplate();
57  $lng = $DIC->language();
58  $this->lng = $lng;
59  $this->manual_activation = false;
60  $this->subtab_manual_activation = false;
61  $this->temp_var = "TABS";
62  $this->sub_tabs = false;
63  $this->back_title = "";
64  $this->back_target = "";
65  $this->back_2_target = "";
66  $this->back_2_title = "";
67  }
68 
69  protected function symbol(): \ILIAS\Repository\Symbol\SymbolAdapterGUI
70  {
71  global $DIC;
72  return $DIC->repository()->internal()->gui()->symbol();
73  }
74 
75  public function setSetupMode(bool $a_val): void
76  {
77  $this->setup_mode = $a_val;
78  }
79 
80  public function getSetupMode(): bool
81  {
82  return $this->setup_mode;
83  }
84 
85  public function setBackTarget(
86  string $a_title,
87  string $a_target,
88  string $a_frame = ""
89  ): void {
90  $this->back_title = $a_title;
91  $this->back_target = $a_target;
92  $this->back_frame = $a_frame;
93  }
94 
95  public function setBack2Target(
96  string $a_title,
97  string $a_target,
98  string $a_frame = ""
99  ): void {
100  $this->back_2_title = $a_title;
101  $this->back_2_target = $a_target;
102  $this->back_2_frame = $a_frame;
103  }
104 
105  public function setForcePresentationOfSingleTab(bool $a_val): void
106  {
107  $this->force_one_tab = $a_val;
108  }
109 
110  public function getForcePresentationOfSingleTab(): bool
111  {
112  return $this->force_one_tab;
113  }
114 
120  public function addTarget(
121  string $a_text,
122  string $a_link,
123  $a_cmd = "",
124  $a_cmdClass = "",
125  string $a_frame = "",
126  bool $a_activate = false,
127  bool $a_dir_text = false
128  ): void {
129  if (!$a_cmdClass) {
130  $a_cmdClass = array();
131  }
132  $a_cmdClass = !is_array($a_cmdClass) ? array(strtolower($a_cmdClass)) : $a_cmdClass;
133 
134  if ($a_activate) {
135  $this->manual_activation = true;
136  }
137  $this->target[] = array("text" => $a_text, "link" => $a_link,
138  "cmd" => $a_cmd, "cmdClass" => $a_cmdClass, "frame" => $a_frame,
139  "activate" => $a_activate, "dir_text" => $a_dir_text, "id" => $a_text);
140  }
141 
145  public function addTab(
146  string $a_id,
147  string $a_text,
148  string $a_link,
149  string $a_frame = ""
150  ): void {
151  $this->target[] = array("text" => $a_text, "link" => $a_link,
152  "frame" => $a_frame, "dir_text" => true, "id" => $a_id, "cmdClass" => array());
153  }
154 
158  public function removeTab(string $a_id): void
159  {
160  foreach ($this->target as $key => $target) {
161  if ($target['id'] == $a_id) {
162  unset($this->target[$key]);
163  }
164  }
165  }
166 
170  public function removeSubTab(string $a_id): void
171  {
172  foreach ($this->sub_target as $i => $sub_target) {
173  if ($this->sub_target[$i]['id'] == $a_id) {
174  unset($this->sub_target[$i]);
175  }
176  }
177  }
178 
182  public function replaceTab(
183  string $a_old_id,
184  string $a_new_id,
185  string $a_text,
186  string $a_link,
187  string $a_frame = ''
188  ): void {
189  for ($i = 0, $iMax = count($this->target); $i < $iMax; $i++) {
190  if ($this->target[$i]['id'] == $a_old_id) {
191  $this->target[$i] = array(
192  "text" => $a_text,
193  "link" => $a_link,
194  "frame" => $a_frame,
195  "dir_text" => true,
196  "id" => $a_new_id,
197  "cmdClass" => array());
198  }
199  }
200  }
201 
205  public function clearTargets(): void
206  {
207  global $DIC;
208 
209  $ilHelp = $DIC["ilHelp"] ?? null;
210 
211  if (!$this->getSetupMode()) {
212  $ilHelp->setScreenIdComponent("");
213  }
214 
215  $this->target = array();
216  $this->sub_target = array();
217  $this->non_tabbed_link = array();
218  $this->back_title = "";
219  $this->back_target = "";
220  $this->back_2_target = "";
221  $this->back_2_title = "";
222  $this->setTabActive("");
223  $this->setSubTabActive("");
224  }
225 
231  public function addSubTabTarget(
232  string $a_text,
233  string $a_link,
234  $a_cmd = "",
235  $a_cmdClass = "",
236  string $a_frame = "",
237  bool $a_activate = false,
238  bool $a_dir_text = false
239  ): void {
240  if (!$a_cmdClass) {
241  $a_cmdClass = array();
242  }
243  $a_cmdClass = !is_array($a_cmdClass) ? array(strtolower($a_cmdClass)) : $a_cmdClass;
244  #$a_cmdClass = strtolower($a_cmdClass);
245 
246  if ($a_activate) {
247  $this->subtab_manual_activation = true;
248  }
249  $this->sub_target[] = array("text" => $a_text, "link" => $a_link,
250  "cmd" => $a_cmd, "cmdClass" => $a_cmdClass, "frame" => $a_frame,
251  "activate" => $a_activate, "dir_text" => $a_dir_text, "id" => $a_text);
252  }
253 
254  public function addSubTab(
255  string $a_id,
256  string $a_text,
257  string $a_link,
258  string $a_frame = ""
259  ): void {
260  $this->sub_target[] = array("text" => $a_text, "link" => $a_link,
261  "frame" => $a_frame, "dir_text" => true, "id" => $a_id, "cmdClass" => array());
262  }
263 
267  public function setTabActive(string $a_id): void
268  {
269  foreach ($this->target as $key => $target) {
270  $this->target[$key]['activate'] = $this->target[$key]['id'] === $a_id;
271  }
272  if ($a_id !== "") {
273  $this->manual_activation = true;
274  } else {
275  $this->manual_activation = false;
276  }
277  }
278 
279  public function activateTab(string $a_id): void
280  {
281  $this->setTabActive($a_id);
282  }
283 
287  public function setSubTabActive(string $a_text): void
288  {
289  for ($i = 0, $iMax = count($this->sub_target); $i < $iMax; $i++) {
290  $this->sub_target[$i]['activate'] = $this->sub_target[$i]['id'] === $a_text;
291  }
292  $this->subtab_manual_activation = true;
293  }
294 
295  public function activateSubTab(string $a_id): void
296  {
297  $this->setSubTabActive($a_id);
298  }
299 
300  public function clearSubTabs(): void
301  {
302  $this->sub_target = array();
303  }
304 
305  public function getHTML(bool $a_after_tabs_anchor = false): string
306  {
307  return $this->__getHTML(false, $a_after_tabs_anchor);
308  }
309 
310  public function getSubTabHTML(): string
311  {
312  return $this->__getHTML(true);
313  }
314 
315  public function addNonTabbedLink(
316  string $a_id,
317  string $a_text,
318  string $a_link,
319  string $a_frame = ""
320  ): void {
321  $this->non_tabbed_link[] = array("text" => $a_text, "link" => $a_link,
322  "frame" => $a_frame, "dir_text" => true, "id" => $a_id, "cmdClass" => array());
323  }
324 
325  public function removeNonTabbedLinks(): void
326  {
327  $this->non_tabbed_link = [];
328  }
329 
330  private function __getHTML(
331  bool $a_get_sub_tabs,
332  bool $a_after_tabs_anchor = false
333  ): string {
335  global $DIC;
336 
337  $cmd = null;
338  $cmdClass = null;
339  $sr_pre = "";
340  $hash = "";
341 
342  $ilHelp = $DIC["ilHelp"] ?? null;
343 
344  $ilCtrl = $this->ctrl;
345  $lng = $this->lng;
346  $ilUser = null;
347  if (isset($DIC["ilUser"])) {
348  $ilUser = $DIC->user();
349  }
350  $component_factory = $DIC["component.factory"] ?? null;
351 
352  // user interface hook [uihk]
353  if ($component_factory && !$this->getSetupMode()) {
354  foreach ($component_factory->getActivePluginsInSlot("uihk") as $plugin) {
355  $gui_class = $plugin->getUIClassInstance();
356  $resp = $gui_class->modifyGUI(
357  "",
358  $a_get_sub_tabs ? "sub_tabs" : "tabs",
359  array("tabs" => $this)
360  );
361  }
362  }
363 
364 
365  // user interface hook [uihk]
366  if (!$this->getSetupMode()) {
367  $cmd = $ilCtrl->getCmd();
368  $cmdClass = $ilCtrl->getCmdClass();
369  }
370 
371  if ($a_get_sub_tabs) {
372  $tpl = new ilTemplate("tpl.sub_tabs.html", true, true, "Services/UIComponent/Tabs");
373  $pre = "sub";
374  $pre2 = "SUB_";
375  $sr_pre = "sub_";
376  } else {
377  $tpl = new ilTemplate("tpl.tabs.html", true, true, "Services/UIComponent/Tabs");
378  if ($a_after_tabs_anchor) {
379  $tpl->touchBlock("after_tabs");
380  }
381  $pre = $pre2 = "";
382 
383  // back 2 tab
384  if ($this->back_2_title !== "") {
385  $tpl->setCurrentBlock("back_2_tab");
386  $tpl->setVariable("BACK_2_ICON", $this->symbol()->glyph("back")->render());
387  $tpl->setVariable("BACK_2_TAB_LINK", ilUtil::secureUrl($this->back_2_target));
388  $tpl->setVariable("BACK_2_TAB_TEXT", $this->back_2_title);
389  if ($this->back_2_frame !== "") {
390  $tpl->setVariable("BACK_2_TAB_TARGET", ' target="' . $this->back_2_frame . '" ');
391  }
392 
393  $tpl->parseCurrentBlock();
394  }
395 
396  // back tab
397  if ($this->back_title !== "") {
398  $tpl->setCurrentBlock("back_tab");
399  $tpl->setVariable("BACK_ICON", $this->symbol()->glyph("back")->render());
400  $tpl->setVariable("BACK_TAB_LINK", ilUtil::secureUrl($this->back_target));
401  $tpl->setVariable("BACK_TAB_TEXT", $this->back_title);
402  if ($this->back_frame !== "") {
403  $tpl->setVariable("BACK_TAB_TARGET", ' target="' . $this->back_frame . '" ');
404  }
405  $tpl->parseCurrentBlock();
406  }
407  }
408 
409  $targets = $a_get_sub_tabs ? $this->sub_target : $this->target;
410 
411  $i = 0;
412 
413  // do not display one tab only
414  if ((count($targets) > 1 || $this->force_one_tab) || ($this->back_title !== "" && !$a_get_sub_tabs)
415  || (count($this->non_tabbed_link) > 0 && !$a_get_sub_tabs)) {
416  foreach ($targets as $target) {
417  $i++;
418 
419  if (isset($target['cmd'])) {
420  if (!is_array($target['cmd'])) {
421  $target['cmd'] = [$target['cmd']];
422  }
423  } else {
424  $target['cmd'] = [];
425  }
426 
427  if ($this->isTabActive($a_get_sub_tabs, $target, $cmd, $cmdClass)) {
428  $tabtype = $pre . "tabactive";
429  } else {
430  $tabtype = $pre . "tabinactive";
431  }
432 
433  if (($a_get_sub_tabs ? $this->subtab_manual_activation : $this->manual_activation) && ($target["activate"] ?? false)) {
434  $tabtype = $pre . "tabactive";
435  }
436 
437  if ($tabtype === "tabactive" || $tabtype === "subtabactive") {
438  $tpl->setCurrentBlock("sel_text");
439  $tpl->setVariable("TXT_SELECTED", $lng->txt("stat_selected"));
440  $tpl->parseCurrentBlock();
441 
442  if (!$this->getSetupMode()) {
443  if ($a_get_sub_tabs) {
445  } else {
447  }
448  $ilHelp->setDefaultScreenId($part, $target["id"]);
449  }
450  }
451 
452  $tpl->setCurrentBlock($pre . "tab");
453  $tpl->setVariable("ID", $pre . "tab_" . $target["id"]);
454 
455  // tooltip
456  if (!$this->getSetupMode()) {
457  $ttext = $ilHelp->getTabTooltipText($target["id"]);
458  if ($ttext !== "") {
460  $pre . "tab_" . $target["id"],
461  $ttext,
462  "",
463  "bottom center",
464  "top center",
465  false
466  );
467  }
468  }
469 
470  // bs-patch: start
471  $tabtype = in_array($tabtype, array("tabactive", "subtabactive"))
472  ? "active"
473  : "";
474  // bs-patch: end
475 
476  $tpl->setVariable($pre2 . "TAB_TYPE", $tabtype);
477  $hash = "";
478  $tpl->setVariable($pre2 . "TAB_LINK", $target["link"] . $hash);
479  if ($target["dir_text"]) {
480  $tpl->setVariable($pre2 . "TAB_TEXT", $target["text"]);
481  } else {
482  $tpl->setVariable($pre2 . "TAB_TEXT", $lng->txt($target["text"]));
483  }
484  if ($target["frame"] != "") {
485  $tpl->setVariable($pre2 . "TAB_TARGET", ' target="' . $target["frame"] . '" ');
486  }
487  $tpl->parseCurrentBlock();
488  }
489 
490  if ($a_get_sub_tabs) {
491  $tpl->setVariable("TXT_SUBTABS", $this->getTabTextOfId($this->getActiveTab()) . ": " . $lng->txt("subtabs"));
492  } else {
493  $tpl->setVariable("TXT_TABS", $lng->txt("tabs"));
494  $tpl->setVariable("LAST_TAB_LABEL", $lng->txt("show_more"));
495 
496  // non tabbed links
497  foreach ($this->non_tabbed_link as $link) {
498  $tpl->setCurrentBlock("tab");
499  $tpl->setVariable("TAB_TYPE", "nontabbed");
500  $tpl->setVariable("TAB_ICON", " " . $this->symbol()->glyph("next")->render());
501  $tpl->setVariable("TAB_TEXT", $link["text"]);
502  $tpl->setVariable("TAB_LINK", $link["link"]);
503  $tpl->setVariable("TAB_TARGET", $link["frame"]);
504  $tpl->setVariable("ID", "nontab_" . $link["id"]);
505  $tpl->parseCurrentBlock();
506 
507  // tooltip
508  if (!$this->getSetupMode()) {
509  $ttext = $ilHelp->getTabTooltipText($link["id"]);
510  if ($ttext !== "") {
512  "nontab_" . $link["id"],
513  $ttext,
514  "",
515  "bottom center",
516  "top center",
517  false
518  );
519  }
520  }
521  }
522  }
523 
524  return $tpl->get();
525  } else {
526  return "";
527  }
528  }
529 
530  protected function getTabTextOfId(string $id): string
531  {
532  foreach ($this->target as $i => $target) {
533  if ($this->target[$i]['id'] == $id) {
534  if ($target["dir_text"]) {
535  return $target["text"];
536  } else {
537  return $this->lng->txt($target["text"]);
538  }
539  }
540  }
541  return "";
542  }
543 
544  public function getActiveTab(): string
545  {
546  foreach ($this->target as $i => $target) {
547  if ($this->target[$i]['activate'] ?? null) {
548  return $this->target[$i]['id'];
549  }
550  }
551  return "";
552  }
553 
554  public function hasTabs(): bool
555  {
556  return $this->target !== [];
557  }
558 
559  private function isTabActive(bool $isSubTabsContext, array $target, ?string $cmd, ?string $cmdClass): bool
560  {
561  if (($isSubTabsContext && $this->subtab_manual_activation) || (!$isSubTabsContext && $this->manual_activation)) {
562  return false;
563  }
564 
565  $cmdClass = (string) $cmdClass;
566  $cmd = (string) $cmd;
567 
568  $targetMatchesCmdClass = (
569  !$target['cmdClass'] ||
570  in_array(strtolower($cmdClass), array_map('strtolower', $target['cmdClass']), true)
571  );
572 
573  $targetMatchesCmd = (
574  in_array(strtolower($cmd), array_map('strtolower', $target['cmd']), true) ||
575  (count($target['cmd']) === 1 && $target['cmd'][0] === '')
576  );
577 
578  return $targetMatchesCmd && $targetMatchesCmdClass;
579  }
580 }
removeSubTab(string $a_id)
Remove a subtab identified by its id.
bool $force_one_tab
setBack2Target(string $a_title, string $a_target, string $a_frame="")
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
addTarget(string $a_text, string $a_link, $a_cmd="", $a_cmdClass="", string $a_frame="", bool $a_activate=false, bool $a_dir_text=false)
touchBlock(string $block)
overwrites ITX::touchBlock.
string $temp_var
Class ChatMainBarProvider .
activateSubTab(string $a_id)
get(string $part=self::DEFAULT_BLOCK)
Renders the given block and returns the html string.
ilLanguage $lng
setVariable(string $variable, $value='')
Sets the given variable to the given value.
addNonTabbedLink(string $a_id, string $a_text, string $a_link, string $a_frame="")
const ID_PART_SUB_SCREEN
static secureUrl(string $url)
getForcePresentationOfSingleTab()
getHTML(bool $a_after_tabs_anchor=false)
setTabActive(string $a_id)
addSubTabTarget(string $a_text, string $a_link, $a_cmd="", $a_cmdClass="", string $a_frame="", bool $a_activate=false, bool $a_dir_text=false)
global $DIC
Definition: feed.php:28
replaceTab(string $a_old_id, string $a_new_id, string $a_text, string $a_link, string $a_frame='')
Replace a tab.
string $back_2_frame
addSubTab(string $a_id, string $a_text, string $a_link, string $a_frame="")
string $back_target
__construct(VocabulariesInterface $vocabularies)
array $sub_target
setBackTarget(string $a_title, string $a_target, string $a_frame="")
string $key
Consumer key/client ID value.
Definition: System.php:193
getTabTextOfId(string $id)
bool $subtab_manual_activation
clearTargets()
clear all targets
ilGlobalTemplateInterface $tpl
static addTooltip(string $a_el_id, string $a_text, string $a_container="", string $a_my="bottom center", string $a_at="top center", bool $a_use_htmlspecialchars=true)
setSetupMode(bool $a_val)
string $back_2_target
string $target_script
string $back_2_title
setCurrentBlock(string $part=self::DEFAULT_BLOCK)
Sets the template to the given block.
bool $manual_activation
activateTab(string $a_id)
string $back_title
removeTab(string $a_id)
Remove a tab identified by its id.
setForcePresentationOfSingleTab(bool $a_val)
setSubTabActive(string $a_text)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
parseCurrentBlock(string $block_name=self::DEFAULT_BLOCK)
Parses the given block.
array $non_tabbed_link
string $back_frame
const ID_PART_SCREEN
isTabActive(bool $isSubTabsContext, array $target, ?string $cmd, ?string $cmdClass)
string $obj_type
addTab(string $a_id, string $a_text, string $a_link, string $a_frame="")
Add a Tab.