ILIAS  release_7 Revision v7.30-3-g800a261c036
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 = rawurlencode($target); // see #32789
44 $target_str = 'target=' . $target . '&';
45 }
46
47 $login_glyph = $factory->symbol()->glyph()->login();
48 $login = $this->meta_bar->topLinkItem($if('login'))
49 ->withAction("login.php?" . $target_str . "client_id=" . rawurlencode(CLIENT_ID) . "&cmd=force_login&lang=" . $this->dic->user()->getCurrentLanguage())
50 ->withSymbol($login_glyph)
51 ->withPosition(2)
52 ->withTitle($txt('log_in'))
53 ->withAvailableCallable(function () {
54 return !$this->isUserLoggedIn();
55 })
56 ->withVisibilityCallable(function () {
57 return !$this->isUserOnLoginPage();
58 });
59
60 // Language-Selection
61 $language_selection = $this->meta_bar->topParentItem($if('language_selection'))
62 ->withSymbol($factory->symbol()->glyph()->language())
63 ->withPosition(1)
64 ->withAvailableCallable(function () {
65 return !$this->isUserLoggedIn();
66 })
67 ->withVisibilityCallable(function () use ($languages) {
68 return count($languages) > 1;
69 })
70 ->withTitle($txt('language'));
71
72 $base = $this->getBaseURL();
73
77 foreach ($languages as $lang_key) {
78 $link = $this->appendUrlParameterString($base, "lang=" . $lang_key);
79 $language_name = $this->dic->language()->_lookupEntry($lang_key, "meta", "meta_l_" . $lang_key);
80
81 $language_icon = $factory->symbol()->icon()->standard("none", $language_name)
82 ->withAbbreviation($lang_key);
83
84 $s = $this->meta_bar->linkItem($if($lang_key))
85 ->withSymbol($language_icon)
86 ->withAction($link)
87 ->withTitle($language_name);
88
89 $language_selection->appendChild($s);
90 }
91
92 return [
93 $login,
94 $language_selection,
95 ];
96 }
97
98
99 private function isUserLoggedIn() : bool
100 {
101 return (!$this->dic->user()->isAnonymous() && $this->dic->user()->getId() != 0);
102 }
103
104
105 private function isUserOnLoginPage() : bool
106 {
107 $b = preg_match("%^.*/login.php$%", $_SERVER["SCRIPT_NAME"]) === 1;
108
109 return $b;
110 }
111
112
113 private function appendUrlParameterString(string $existing_url, string $addition) : string
114 {
115 $url = (is_int(strpos($existing_url, "?")))
116 ? $existing_url . "&" . $addition
117 : $existing_url . "?" . $addition;
118
119 $url = str_replace("?&", "?", $url);
120
121 return $url;
122 }
123
124
125 private function getBaseURL() : string
126 {
127 $base = substr($_SERVER["REQUEST_URI"], strrpos($_SERVER["REQUEST_URI"], "/") + 1);
128
129 return preg_replace("/&*lang=[a-z]{2}&*/", "", $base);
130 }
131}
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
const CLIENT_ID
Definition: constants.php:39
$login
Definition: cron.php:13
$txt
Definition: error.php:13
$factory
Definition: metadata.php:58
withSymbol(Symbol $symbol)
@inheritDoc
$base
Definition: index.php:4
$type
$url
$_SERVER['HTTP_HOST']
Definition: raiseError.php:10