src/Service/TraduireContenu.php line 59

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Entity\AdapterContent;
  4. use App\Entity\Category;
  5. use App\Entity\Package;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. class TraduireContenu{
  10.     private $doctrine;
  11.     function __construct(ManagerRegistry $doctrine)
  12.     {
  13.         $this->doctrine=$doctrine;
  14.     }
  15.     function retourTrad($instance$langue){
  16.     
  17.         //récupérer la liste des adapter
  18.         $adapters $instance->getAdapterContent();
  19.         //renvoie l'instance s'il n'y a pas d'adapter, car il n'y a pas de traduction
  20.         if(!$adapters) return $instance;
  21.     
  22.         //récupérer la liste des webContent via l'adapter
  23.         foreach($adapters->getContents() as $webContent){
  24.             //récupération de la langue
  25.             $langueFromWebContent $webContent->getLangueContent()->getName();
  26.             if($langueFromWebContent == $langue){
  27.                 //récupérer l'entité catégorie traduit dans la langue voulu
  28.                 $entity=$webContent->getContentTranslated()->getEntityNotNull();
  29.                 
  30.                 return $this->getReadEntity($entity);
  31.             }    
  32.         }
  33.         // il n'y a pas de traduction dans la langue voulue, donc on renvoie l'instance de base           
  34.         return $instance;
  35.     }
  36.     function getReadEntity($entity){
  37.         if($entity instanceof Category){
  38.             $entity=$this->doctrine->getRepository(Category::class)->find($entity->getId());
  39.             return $entity;
  40.         }
  41.         else if ($entity instanceof Package){
  42.             $entity=$this->doctrine->getRepository(Package::class)->find($entity->getId());
  43.             return $entity;
  44.         }
  45.     }
  46.     function nouveau_chemin(Request $request$path$langues){
  47.         if($path==null){
  48.             $session $request->getSession();
  49.             $langue_preferee=$request->getPreferredLanguage($langues);
  50.             $session->set("_locale",$langue_preferee);
  51.             return "/".$langue_preferee."/".$path;
  52.         }
  53.         else{
  54.             $session $request->getSession();
  55.             $langue=$session->get("_locale");
  56.             $langue_preferee=$request->getPreferredLanguage($langues);
  57.             
  58.             if($langue==null){
  59.                 $session->set("_locale",$langue_preferee);
  60.                 if(preg_match("/".$langue_preferee."\\/.*"."/",$path)){
  61.                     return "/".$path;
  62.                 }
  63.                 return "/".$langue_preferee."/".$path;
  64.             }
  65.             else{  
  66.                 $session->set("_locale",$langue);
  67.                 if(preg_match("/".$langue."\\/.*"."/",$path)){
  68.                     return "/".$path;
  69.                 }              
  70.                 return "/".$langue."/".$path;
  71.             }
  72.         }
  73.     }
  74. }