ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
StartUpMetaBarProvider.php
Go to the documentation of this file.
1<?php
2
4
8
15{
16
20 public function getMetaBarItems() : array
21 {
22 $factory = $this->dic->ui()->factory();
23 $request = $this->dic->http()->request();
24 $languages = $this->dic->language()->getInstalledLanguages();
25
26 $if = function (string $id) : IdentificationInterface {
27 return $this->if->identifier($id);
28 };
29
30 $txt = function (string $id) : string {
31 return $this->dic->language()->txt($id);
32 };
33
34 // Login-Button
35 // Only visible, if not on login-page but not logged in
36 $target_str = '';
37 if ($ref_id = $request->getQueryParams()['ref_id']) {
38 $type = \ilObject::_lookupType($ref_id, true);
39 if ($type != "root") { // see bug #30710
40 $target_str = 'target=' . \ilObject::_lookupType($ref_id, true) . '_' . (int) $ref_id . '&';
41 }
42 } elseif ($target = $request->getQueryParams()['target']) {
43 $target_str = 'target=' . $target . '&';
44 }
45
46 $login_glyph = $factory->symbol()->glyph()->login();
47 $login = $this->meta_bar->topLinkItem($if('login'))
48 ->withAction("login.php?" . $target_str . "client_id=" . rawurlencode(CLIENT_ID) . "&cmd=force_login&lang=" . $this->dic->user()->getCurrentLanguage())
49 ->withSymbol($login_glyph)
50 ->withPosition(2)
51 ->withTitle($txt('log_in'))
52 ->withAvailableCallable(function () {
53 return !$this->isUserLoggedIn();
54 })
55 ->withVisibilityCallable(function () {
56 return !$this->isUserOnLoginPage();
57 });
58
59 // Language-Selection
60 $language_selection = $this->meta_bar->topParentItem($if('language_selection'))
61 ->withSymbol($factory->symbol()->glyph()->language())
62 ->withPosition(1)
63 ->withAvailableCallable(function () {
64 return !$this->isUserLoggedIn();
65 })
66 ->withVisibilityCallable(function () use ($languages) {
67 return count($languages) > 1;
68 })
69 ->withTitle($txt('language'));
70
71 $base = $this->getBaseURL();
72
76 foreach ($languages as $lang_key) {
77 $link = $this->appendUrlParameterString($base, "lang=" . $lang_key);
78 $language_name = $this->dic->language()->_lookupEntry($lang_key, "meta", "meta_l_" . $lang_key);
79
80 $language_icon = $factory->symbol()->icon()->standard("none", $language_name)
81 ->withAbbreviation($lang_key);
82
83 $s = $this->meta_bar->linkItem($if($lang_key))
84 ->withSymbol($language_icon)
85 ->withAction($link)
86 ->withTitle($language_name);
87
88 $language_selection->appendChild($s);
89 }
90
91 return [
92 $login,
93 $language_selection,
94 ];
95 }
96
97
98 private function isUserLoggedIn() : bool
99 {
100 return (!$this->dic->user()->isAnonymous() && $this->dic->user()->getId() != 0);
101 }
102
103
104 private function isUserOnLoginPage() : bool
105 {
106 $b = preg_match("%^.*/login.php$%", $_SERVER["SCRIPT_NAME"]) === 1;
107
108 return $b;
109 }
110
111
112 private function appendUrlParameterString(string $existing_url, string $addition) : string
113 {
114 $url = (is_int(strpos($existing_url, "?")))
115 ? $existing_url . "&" . $addition
116 : $existing_url . "?" . $addition;
117
118 $url = str_replace("?&", "?", $url);
119
120 return $url;
121 }
122
123
124 private function getBaseURL() : string
125 {
126 $base = substr($_SERVER["REQUEST_URI"], strrpos($_SERVER["REQUEST_URI"], "/") + 1);
127
128 return preg_replace("/&*lang=[a-z]{2}&*/", "", $base);
129 }
130}
An exception for terminatinating execution or to throw for unit testing.
appendUrlParameterString(string $existing_url, string $addition)
static _lookupType($a_id, $a_reference=false)
lookup object type
$login
Definition: cron.php:13
$txt
Definition: error.php:13
$base
Definition: index.php:4
$factory
Definition: metadata.php:58
withSymbol(Symbol $symbol)
@inheritDoc
$type
$url
$_SERVER['HTTP_HOST']
Definition: raiseError.php:10