src/AppBundle/Controller/ControllerExtensionTrait.php line 15

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace AppBundle\Controller;
  4. use Pimcore\Model\Document\Page;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. trait ControllerExtensionTrait
  9. {
  10.     // Redirect based on the child documents of the current document
  11.     public function langredirecttodocumentAction(Request $request)
  12.     {
  13.         // Language used, when browser languages do not match available languages
  14.         // is set in the current page
  15.         $fallbackLocale $this->document->getProperty('language');
  16.         // List of enabled languages
  17.         $enabledLanguagesSetting $this->document->getProperty('enabledLanguages');
  18.         $enabledLanguages = array();
  19.         if (is_string$enabledLanguagesSetting)) {
  20.             $enabledLanguages explode','$enabledLanguagesSetting);
  21.         }
  22.         // The matching subfolder
  23.         $subFolder null;
  24.         // The current site
  25.         $currentSite \Pimcore\Tool\Frontend::getSiteForDocument($this->document);
  26.         // Fetch available languages
  27.         $langMap = [];
  28.         $landMapDisabled = [];
  29.         /** @var Page $child */
  30.         foreach ($this->document->getChildren(true) as $child) {
  31.             if (!count($enabledLanguages) || in_array($child->getProperty('language'),$enabledLanguages)) {
  32.                 if ($child->isPublished() && ! $child->getProperty('navigation_exclude')) {
  33.                     $childPath $child->getFullPath();
  34.                     $childSite \Pimcore\Tool\Frontend::getSiteForDocument($child);
  35.                     if ($childSite && $childSite !== $currentSite) {
  36.                         if ($request->isSecure()) {
  37.                             $childPath 'https://'.$childSite->getMainDomain();
  38.                         } else {
  39.                             $childPath 'http://'.$childSite->getMainDomain();
  40.                         }
  41.                     }
  42.                     if ($child->getProperty('language')) {
  43.                         $langMap[strtolower($child->getProperty('language'))] = $childPath;
  44.                     }
  45.                 } else if ( ! $child->isPublished()) {
  46.                     $landMapDisabled[$child->getFullPath()] = 'unpublished';
  47.                 } else if ($child->getProperty('navigation_exclude')) {
  48.                     $landMapDisabled[$child->getFullPath()] = 'navigation_exclude';
  49.                 }
  50.             } else {
  51.                 $landMapDisabled[$child->getFullPath()] = 'not in enabledLanguages';
  52.             }
  53.         }
  54.         // Search for browser language in the list of available languages
  55.         $langSearches = array();
  56.         foreach ($request->getLanguages() as $lang) {
  57.             if (array_key_exists(strtolower($lang), $langMap)) {
  58.                 $langSearches[strtolower($lang)] = 'match';
  59.                 $subFolder $langMap[strtolower($lang)];
  60.                 break;
  61.             }
  62.             $langSearches[strtolower($lang)] = '-';
  63.         }
  64.         // In case no language has been matches
  65.         if (! $subFolder) {
  66.             if (array_key_exists($fallbackLocale$langMap)) {
  67.                 // Use fallback locale
  68.                 $subFolder $langMap[$fallbackLocale];
  69.             } else {
  70.                 // Use first of the available languages
  71.                 $arrayKeysTmp array_keys($langMap);
  72.                 $subFolder $langMap[array_shift($arrayKeysTmp)];
  73.             }
  74.         }
  75.         if (!$this->editmode) {
  76.             // Redirect to the language version
  77.             if (preg_match'@^https?://.+@'$subFolder)) {
  78.                 // Contains complete url to a site
  79.                 $response = new RedirectResponse($subFolder302);
  80.             } else {
  81.                 // Add the domain to the folder path
  82.                 $response = new RedirectResponse($request->getSchemeAndHttpHost().$subFolder302);
  83.             }
  84.         } else {
  85.             // In Edit mode output some debugging infos
  86.             $text "Automatic redirect to subfolder matching the Browser language\n";
  87.             $text .= "\nAvailable languages:\n";
  88.             foreach ($langMap as $k=>$v) {
  89.                 $text .= "   • ".$k."     => ".$v."\n";
  90.             }
  91.             if ($landMapDisabled) {
  92.                 $text .= "\nDisabled languages:\n";
  93.                 foreach ($landMapDisabled as $k=>$v) {
  94.                     $text .= "   • ".$k."     => ".$v."\n";
  95.                 }
  96.             }
  97.             $text .= "\nBrowser language: ".$request->headers->get('accept-language')."\n";
  98.             $text .= "\nChecking languages:\n";
  99.             foreach ($langSearches as $k=>$v) {
  100.                 $text .= "   • ".$k."     => ".$v."\n";
  101.             }
  102.             $text .= "\nSelected language: ".$subFolder;
  103.             $response = new Response(
  104.                 $text,
  105.                 Response::HTTP_OK,
  106.                 ['content-type' => 'text/plain']
  107.             );
  108.         }
  109.         return $response;
  110.     }
  111. }