bundles/MultilingualBundle/Controller/DefaultController.php line 33

Open in your IDE?
  1. <?php
  2. namespace MultilingualBundle\Controller;
  3. use Pimcore\Controller\FrontendController;
  4. use Pimcore\Model\Document;
  5. use Pimcore\Tool;
  6. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  10. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  11. class DefaultController extends FrontendController
  12. {
  13.     /**
  14.      * @throws NotFoundHttpException
  15.      */
  16.     public function goToFirstChildAction()
  17.     {
  18.         if ($this->document->hasChildren()) {
  19.             $children $this->document->getChildren();
  20.             $firstChild reset($children);
  21.             return $this->redirect($firstChild->getFullPath());
  22.         } else {
  23.             throw new NotFoundHttpException('No children found. Could not redirect to first child.');
  24.         }
  25.     }
  26.     /**
  27.      * @throws NotFoundHttpException
  28.      */
  29.     public function languageDetectionAction(Request $request)
  30.     {
  31.         // $locale = $request->getLocale();
  32.         $languages Tool::getValidLanguages();
  33.         $locale reset($languages);
  34.         if(isset($_SERVER['HTTP_USER_AGENT'])){
  35.             if(!preg_match('/bot|crawl|slurp|spider|Guzzle|mediapartners/i'$_SERVER['HTTP_USER_AGENT'])){
  36.                 if($_SERVER['HTTP_ACCEPT_LANGUAGE']){
  37.                     $locale substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2);
  38.                 }
  39.             }
  40.         }
  41.         $browserLanguage $locale;
  42.         // $languageArr = explode('_', locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']));
  43.         // $browserLanguage = reset($languageArr);
  44.         // return new Response($locale.' '.$browserLanguage);
  45.         // Check if the browser language is a valid frontend language
  46.         if (in_array($browserLanguage$languages)) {
  47.             $language $browserLanguage;
  48.         } else {
  49.             // If it is not, take the first frontend language as default
  50.             $language reset($languages);
  51.         }
  52.         // Get the folder of the current language (in the current site)
  53.         $currentSitePath $this->document->getRealFullPath();
  54.         // check language
  55.         $publishedLanguages = [];
  56.         foreach ($languages as $foundLanguage){
  57.             $languageDoc Document\Page::getByPath($currentSitePath '/' $foundLanguage);
  58.             if(!$this->editmode){
  59.                 if($languageDoc->isPublished()){ // check if document is published
  60.                     $publishedLanguages[] = $foundLanguage;
  61.                 }
  62.             }
  63.         }
  64.         if(count($publishedLanguages) === 1){
  65.             $reDirectCorrect 301;
  66.         }else{
  67.             $reDirectCorrect 302// when multiple language are found
  68.         }
  69.         $folder Document\Page::getByPath($currentSitePath '/' $language);
  70.         if ($folder) {
  71.             $document $this->findFirstDocumentByParentId($folder->getId());
  72.             if ($document) {
  73.                 return $this->redirect($document->getPath() . $document->getKey(), $reDirectCorrect);
  74.             } else {
  75.                 throw new NotFoundHttpException('No document found in your browser language');
  76.             }
  77.         } else {
  78.             throw new NotFoundHttpException('No language folder found that matches your browser language');
  79.         }
  80.     }
  81.     /**
  82.      * Find the first active document for a given folder
  83.      *
  84.      * @param $folder_id
  85.      * @return mixed
  86.      */
  87.     protected function findFirstDocumentByParentId($folder_id)
  88.     {
  89.         $list = new Document\Listing();
  90.         $list->setCondition("parentId = ?", (int)$folder_id);
  91.         $list->setOrderKey("index");
  92.         $list->setOrder("asc");
  93.         $list->setLimit(1);
  94.         $childrenList $list->load();
  95.         return reset($childrenList);
  96.     }
  97. }