<?php
declare(strict_types=1);
namespace AppBundle\Controller;
use Pimcore\Model\Document\Page;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
trait ControllerExtensionTrait
{
// Redirect based on the child documents of the current document
public function langredirecttodocumentAction(Request $request)
{
// Language used, when browser languages do not match available languages
// is set in the current page
$fallbackLocale = $this->document->getProperty('language');
// List of enabled languages
$enabledLanguagesSetting = $this->document->getProperty('enabledLanguages');
$enabledLanguages = array();
if (is_string( $enabledLanguagesSetting)) {
$enabledLanguages = explode( ',', $enabledLanguagesSetting);
}
// The matching subfolder
$subFolder = null;
// The current site
$currentSite = \Pimcore\Tool\Frontend::getSiteForDocument($this->document);
// Fetch available languages
$langMap = [];
$landMapDisabled = [];
/** @var Page $child */
foreach ($this->document->getChildren(true) as $child) {
if (!count($enabledLanguages) || in_array($child->getProperty('language'),$enabledLanguages)) {
if ($child->isPublished() && ! $child->getProperty('navigation_exclude')) {
$childPath = $child->getFullPath();
$childSite = \Pimcore\Tool\Frontend::getSiteForDocument($child);
if ($childSite && $childSite !== $currentSite) {
if ($request->isSecure()) {
$childPath = 'https://'.$childSite->getMainDomain();
} else {
$childPath = 'http://'.$childSite->getMainDomain();
}
}
if ($child->getProperty('language')) {
$langMap[strtolower($child->getProperty('language'))] = $childPath;
}
} else if ( ! $child->isPublished()) {
$landMapDisabled[$child->getFullPath()] = 'unpublished';
} else if ($child->getProperty('navigation_exclude')) {
$landMapDisabled[$child->getFullPath()] = 'navigation_exclude';
}
} else {
$landMapDisabled[$child->getFullPath()] = 'not in enabledLanguages';
}
}
// Search for browser language in the list of available languages
$langSearches = array();
foreach ($request->getLanguages() as $lang) {
if (array_key_exists(strtolower($lang), $langMap)) {
$langSearches[strtolower($lang)] = 'match';
$subFolder = $langMap[strtolower($lang)];
break;
}
$langSearches[strtolower($lang)] = '-';
}
// In case no language has been matches
if (! $subFolder) {
if (array_key_exists($fallbackLocale, $langMap)) {
// Use fallback locale
$subFolder = $langMap[$fallbackLocale];
} else {
// Use first of the available languages
$arrayKeysTmp = array_keys($langMap);
$subFolder = $langMap[array_shift($arrayKeysTmp)];
}
}
if (!$this->editmode) {
// Redirect to the language version
if (preg_match( '@^https?://.+@', $subFolder)) {
// Contains complete url to a site
$response = new RedirectResponse($subFolder, 302);
} else {
// Add the domain to the folder path
$response = new RedirectResponse($request->getSchemeAndHttpHost().$subFolder, 302);
}
} else {
// In Edit mode output some debugging infos
$text = "Automatic redirect to subfolder matching the Browser language\n";
$text .= "\nAvailable languages:\n";
foreach ($langMap as $k=>$v) {
$text .= " • ".$k." => ".$v."\n";
}
if ($landMapDisabled) {
$text .= "\nDisabled languages:\n";
foreach ($landMapDisabled as $k=>$v) {
$text .= " • ".$k." => ".$v."\n";
}
}
$text .= "\nBrowser language: ".$request->headers->get('accept-language')."\n";
$text .= "\nChecking languages:\n";
foreach ($langSearches as $k=>$v) {
$text .= " • ".$k." => ".$v."\n";
}
$text .= "\nSelected language: ".$subFolder;
$response = new Response(
$text,
Response::HTTP_OK,
['content-type' => 'text/plain']
);
}
return $response;
}
}