ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
StartUpMetaBarProvider.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
25use Psr\Http\Message\UriInterface;
26use ILIAS\Data\Factory as DataFactory;
27
29{
33 public function getMetaBarItems(): array
34 {
35 $factory = $this->dic->ui()->factory();
36 $request = $this->dic->http()->request();
37 $languages = $this->dic->language()->getInstalledLanguages();
38
39 $if = function (string $id): IdentificationInterface {
40 return $this->if->identifier($id);
41 };
42
43 $txt = function (string $id): string {
44 return $this->dic->language()->txt($id);
45 };
46
47 // Login-Button
48 // Only visible, if not on login-page but not logged in
49 $target_str = '';
50 if (isset($request->getQueryParams()['ref_id']) && $ref_id = $request->getQueryParams()['ref_id']) {
51 $type = \ilObject::_lookupType((int) $ref_id, true);
52 if ($type !== 'root') { // see bug #30710
53 $target_str = 'target=' . \ilObject::_lookupType((int) $ref_id, true) . '_' . (int) $ref_id . '&';
54 }
55 } elseif (isset($request->getQueryParams()['target']) && $target = $request->getQueryParams()['target']) {
56 $target = rawurlencode($target); // see #32789
57 $target_str = 'target=' . $target . '&';
58 }
59
60 $login_glyph = $factory->symbol()->glyph()->login();
61
62 $current_language = $this->dic->user()->getCurrentLanguage() ?: $this->dic->language()->getLangKey();
63
64 $login = $this->meta_bar
65 ->topLinkItem($if('login'))
66 ->withAction(
67 'login.php?' . $target_str . 'client_id=' . rawurlencode(
69 ) . '&cmd=force_login&lang=' . $current_language
70 )
71 ->withSymbol($login_glyph)
72 ->withPosition(3)
73 ->withTitle($txt('log_in'))
74 ->withAvailableCallable(function () {
75 return !$this->isUserLoggedIn();
76 })
77 ->withVisibilityCallable(function () use ($request) {
78 return !$this->isUserOnLoginPage($request->getUri());
79 });
80
81 // Language-Selection
82 $language_selection = $this->meta_bar
83 ->topParentItem($if('language_selection'))
84 ->withSymbol($factory->symbol()->glyph()->language())
85 ->withPosition(1)
86 ->withAvailableCallable(function () {
87 return !$this->isUserLoggedIn();
88 })
89 ->withVisibilityCallable(function () use ($languages) {
90 return \count($languages) > 1;
91 })
92 ->withTitle($txt('language'));
93
94 $base = $this->getBaseURL($request->getUri());
95 $dataFactory = new DataFactory();
96
97 foreach ($languages as $lang_key) {
98 $link = $this->appendUrlParameterString($base, 'lang=' . $lang_key);
99 $language_name = $this->dic->language()->_lookupEntry($lang_key, 'meta', 'meta_l_' . $lang_key);
100
101 $language_icon = $factory
102 ->symbol()
103 ->icon()
104 ->standard('none', $language_name)
105 ->withAbbreviation($lang_key);
106
107 $s = $this->meta_bar
108 ->linkItem($if($lang_key))
109 ->withSymbol($language_icon)
110 ->withAction($link)
111 ->withContentLanguage($dataFactory->languageTag($lang_key))
112 ->withLanguageForTargetedResource($dataFactory->languageTag($lang_key))
113 ->withTitle($language_name);
114
115 $language_selection->appendChild($s);
116 }
117
118 return [
119 $login,
120 $language_selection,
121 ];
122 }
123
124 private function isUserLoggedIn(): bool
125 {
126 return (!$this->dic->user()->isAnonymous() && $this->dic->user()->getId() !== 0);
127 }
128
129 private function isUserOnLoginPage(UriInterface $uri): bool
130 {
131 return preg_match('%^.*(?:/login\.php|/)$%', $uri->getPath()) === 1;
132 }
133
134 private function appendUrlParameterString(string $existing_url, string $addition): string
135 {
136 $url = (\is_int(strpos($existing_url, '?')))
137 ? $existing_url . '&' . $addition
138 : $existing_url . '?' . $addition;
139
140 $url = str_replace('?&', '?', $url);
141
142 return $url;
143 }
144
145 private function getBaseURL(UriInterface $uri): string
146 {
147 $base = substr($uri->__toString(), strrpos($uri->__toString(), '/') + 1);
148
149 return rtrim(preg_replace('/([&?])lang=[a-z]{2}([&$])/', '$1', $base), '?&');
150 }
151}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Builds data types.
Definition: Factory.php:36
appendUrlParameterString(string $existing_url, string $addition)
static _lookupType(int $id, bool $reference=false)
const CLIENT_ID
Definition: constants.php:41
$txt
Definition: error.php:31
$ref_id
Definition: ltiauth.php:66
withSymbol(Symbol $symbol)
@inheritDoc
withVisibilityCallable(callable $is_visible)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$url
Definition: shib_logout.php:68