Skip to content

Commit

Permalink
Init: Fix URLs in language selection dropdown
Browse files Browse the repository at this point in the history
See: https://mantis.ilias.de/view.php?id=42681
(cherry picked from commit 480920c)
(cherry picked from commit 2c45e22)
  • Loading branch information
mjansenDatabay committed Nov 13, 2024
1 parent 4384145 commit 7d6b1cf
Showing 1 changed file with 53 additions and 49 deletions.
102 changes: 53 additions & 49 deletions Services/Init/classes/Provider/StartUpMetaBarProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,10 @@
namespace ILIAS\Init\Provider;

use ILIAS\GlobalScreen\Identification\IdentificationInterface;
use ILIAS\GlobalScreen\Scope\MetaBar\Factory\TopParentItem;
use ILIAS\GlobalScreen\Scope\MetaBar\Provider\AbstractStaticMetaBarProvider;
use Psr\Http\Message\UriInterface;
use ILIAS\Data\Factory as DataFactory;

/**
* Class StartUpMetaBarProvider
* @author Fabian Schmid <[email protected]>
*/
class StartUpMetaBarProvider extends AbstractStaticMetaBarProvider
{
/**
Expand All @@ -54,7 +49,7 @@ public function getMetaBarItems(): array
$target_str = '';
if (isset($request->getQueryParams()['ref_id']) && $ref_id = $request->getQueryParams()['ref_id']) {
$type = \ilObject::_lookupType((int) $ref_id, true);
if ($type != "root") { // see bug #30710
if ($type !== 'root') { // see bug #30710
$target_str = 'target=' . \ilObject::_lookupType((int) $ref_id, true) . '_' . (int) $ref_id . '&';
}
} elseif (isset($request->getQueryParams()['target']) && $target = $request->getQueryParams()['target']) {
Expand All @@ -63,46 +58,56 @@ public function getMetaBarItems(): array
}

$login_glyph = $factory->symbol()->glyph()->login();
$login = $this->meta_bar->topLinkItem($if('login'))
->withAction("login.php?" . $target_str . "client_id=" . rawurlencode(CLIENT_ID) . "&cmd=force_login&lang=" . $this->dic->user()->getCurrentLanguage())
->withSymbol($login_glyph)
->withPosition(2)
->withTitle($txt('log_in'))
->withAvailableCallable(function () {
return !$this->isUserLoggedIn();
})
->withVisibilityCallable(function () use ($request) {
return !$this->isUserOnLoginPage($request->getUri());
});
$login = $this->meta_bar
->topLinkItem($if('login'))
->withAction(
'login.php?' . $target_str . 'client_id=' . rawurlencode(
CLIENT_ID
) . '&cmd=force_login&lang=' . $this->dic->user()->getCurrentLanguage()
)
->withSymbol($login_glyph)
->withPosition(2)
->withTitle($txt('log_in'))
->withAvailableCallable(function () {
return !$this->isUserLoggedIn();
})
->withVisibilityCallable(function () use ($request) {
return !$this->isUserOnLoginPage($request->getUri());
});

// Language-Selection
$language_selection = $this->meta_bar->topParentItem($if('language_selection'))
->withSymbol($factory->symbol()->glyph()->language())
->withPosition(1)
->withAvailableCallable(function () {
return !$this->isUserLoggedIn();
})
->withVisibilityCallable(function () use ($languages) {
return count($languages) > 1;
})
->withTitle($txt('language'));
$language_selection = $this->meta_bar
->topParentItem($if('language_selection'))
->withSymbol($factory->symbol()->glyph()->language())
->withPosition(1)
->withAvailableCallable(function () {
return !$this->isUserLoggedIn();
})
->withVisibilityCallable(function () use ($languages) {
return \count($languages) > 1;
})
->withTitle($txt('language'));

$base = $this->getBaseURL($request->getUri());
$dataFactory = new DataFactory();

foreach ($languages as $lang_key) {
$link = $this->appendUrlParameterString($base, "lang=" . $lang_key);
$language_name = $this->dic->language()->_lookupEntry($lang_key, "meta", "meta_l_" . $lang_key);

$language_icon = $factory->symbol()->icon()->standard("none", $language_name)
->withAbbreviation($lang_key);

$s = $this->meta_bar->linkItem($if($lang_key))
->withSymbol($language_icon)
->withAction($link)
->withContentLanguage($dataFactory->languageTag($lang_key))
->withLanguageForTargetedResource($dataFactory->languageTag($lang_key))
->withTitle($language_name);
$link = $this->appendUrlParameterString($base, 'lang=' . $lang_key);
$language_name = $this->dic->language()->_lookupEntry($lang_key, 'meta', 'meta_l_' . $lang_key);

$language_icon = $factory
->symbol()
->icon()
->standard('none', $language_name)
->withAbbreviation($lang_key);

$s = $this->meta_bar
->linkItem($if($lang_key))
->withSymbol($language_icon)
->withAction($link)
->withContentLanguage($dataFactory->languageTag($lang_key))
->withLanguageForTargetedResource($dataFactory->languageTag($lang_key))
->withTitle($language_name);

$language_selection->appendChild($s);
}
Expand All @@ -115,30 +120,29 @@ public function getMetaBarItems(): array

private function isUserLoggedIn(): bool
{
return (!$this->dic->user()->isAnonymous() && $this->dic->user()->getId() != 0);
return (!$this->dic->user()->isAnonymous() && $this->dic->user()->getId() !== 0);
}

private function isUserOnLoginPage(UriInterface $uri): bool
{
$b = preg_match("%^.*/login.php$%", $uri->getPath()) === 1;

return $b;
return preg_match('%^.*/login.php$%', $uri->getPath()) === 1;
}

private function appendUrlParameterString(string $existing_url, string $addition): string
{
$url = (is_int(strpos($existing_url, "?")))
? $existing_url . "&" . $addition
: $existing_url . "?" . $addition;
$url = (\is_int(strpos($existing_url, '?')))
? $existing_url . '&' . $addition
: $existing_url . '?' . $addition;

$url = str_replace("?&", "?", $url);
$url = str_replace('?&', '?', $url);

return $url;
}

private function getBaseURL(UriInterface $uri): string
{
$base = substr($uri->__toString(), strrpos($uri->__toString(), "/") + 1);
return preg_replace("/&*lang=[a-z]{2}&*/", "", $base);
$base = substr($uri->__toString(), strrpos($uri->__toString(), '/') + 1);

return rtrim(preg_replace('/([&?])lang=[a-z]{2}([&$])/', '$1', $base), '?&');
}
}

0 comments on commit 7d6b1cf

Please sign in to comment.