src/Controller/Front/DefaultController.php line 194

Open in your IDE?
  1. <?php 
  2. namespace App\Controller\Front;
  3. use App\Entity\Post;
  4. use App\Entity\UrlRef;
  5. use App\Entity\MediaCms;
  6. use App\Entity\ModelGallerie;
  7. use App\Entity\Secteurs;
  8. use League\Glide\Server;
  9. use App\Service\BuildTree;
  10. use App\Entity\PostArchive;
  11. use App\Service\RenderBloc;
  12. use Spatie\SchemaOrg\Graph;
  13. use App\Entity\ParametreRef;
  14. use App\Service\MetaService;
  15. use Spatie\SchemaOrg\Schema;
  16. use App\Entity\ParametreSite;
  17. use App\Entity\Popup;
  18. use App\Entity\ReseauSociaux;
  19. use App\Service\MetaClePageRef;
  20. use App\Service\FunctionService;
  21. use App\Service\MetaWithoutCategoryService;
  22. use League\Glide\Signatures\SignatureFactory;
  23. use Symfony\Component\HttpFoundation\Request;
  24. use Symfony\Component\HttpFoundation\Response;
  25. use League\Glide\Signatures\SignatureException;
  26. use Symfony\Component\Routing\Annotation\Route;
  27. use League\Glide\Filesystem\FileNotFoundException;
  28. use League\Glide\Responses\SymfonyResponseFactory;
  29. use Symfony\Component\HttpFoundation\JsonResponse;
  30. use Leogout\Bundle\SeoBundle\Seo\Og\OgSeoGenerator;
  31. use Symfony\Component\String\Slugger\SluggerInterface;
  32. use Leogout\Bundle\SeoBundle\Seo\Basic\BasicSeoGenerator;
  33. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  34. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  35. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  36. class DefaultController extends AbstractController
  37. {
  38.     public $basicSeo;
  39.     public $seo;
  40.     private $slugger;
  41.     private $projectDir;
  42.     private $metaService;
  43.     private $metaClePageRef;
  44.     private $renderBloc;
  45.     private $buildTree;
  46.     private $functionService;
  47.     private $metaPostService;
  48.     private $theme;
  49.     
  50.     public function __construct(string $projectDir,string $theme,
  51.                                 BasicSeoGenerator $basicSeo,
  52.                                 OgSeoGenerator $seo,
  53.                                 RenderBloc $renderBloc,
  54.                                 SluggerInterface $slugger,
  55.                                 BuildTree $buildTree,
  56.                                 MetaService $metaService,
  57.                                 MetaWithoutCategoryService $metaPostService,
  58.                                 MetaClePageRef $metaClePageRef,
  59.                                 FunctionService $functionService){
  60.         $this->metaService $metaService;
  61.         $this->metaPostService $metaPostService;
  62.         $this->basicSeo $basicSeo;
  63.         $this->seo $seo;
  64.         $this->buildTree $buildTree;
  65.         $this->slugger $slugger;
  66.         $this->metaClePageRef $metaClePageRef;
  67.         $this->functionService $functionService;
  68.         $this->renderBloc $renderBloc;
  69.         $this->projectDir $projectDir;
  70.         $this->theme $theme;
  71.     }
  72.     /**
  73.      * @Route("/", name="index_page_home", options={"sitemap" = true})
  74.      */
  75.     public function index(Request $request)
  76.     {
  77.         $schemaOrg = new Graph();
  78.         $header_images = [];
  79.         $preload_images '';
  80.         $cdn_image '';
  81.         //$cdn_image = 'https://assets.webspirit.ovh';
  82.         $custom_schemaOrg null;
  83.         $_locale $request->getLocale();
  84.         $home_page $this->getDoctrine()->getRepository(Post::class)->findPostBySlug('Home');
  85.         $modele_galerie_choisie $home_page->getModeleGallerie() ? $home_page->getModeleGallerie()->getId() : 1;
  86.         $modele_gallerie $this->getDoctrine()->getRepository(ModelGallerie::class)->findById($modele_galerie_choisie);
  87.         $popup $this->getDoctrine()->getRepository(Popup::class)->findOneBy([],['id'=>'ASC']);
  88.         if ($home_page) {
  89.             $media_cms $this->getDoctrine()->getRepository(MediaCms::class)->findBy(['module'=> $home_page->getTypePost()->getSystemName(),'module_id'=>$home_page->getId()],['position'=> 'ASC']);
  90.             
  91.             if($media_cms){
  92.                 foreach ($media_cms as $value) {
  93.       
  94.                     $extension_file pathinfo($value->getFileName(), PATHINFO_EXTENSION);
  95.     
  96.                     $new_galleries_images = ['id'=> $value->getId(),
  97.                                                 'path'=> '',
  98.                                                 'lienexterne' => $value->getLienExterne(),
  99.                                                 'titre'=> $value->translate($_locale)->getTitre(),
  100.                                                 'description'=> $value->translate($_locale)->getDescription(),
  101.                                                 'description2'=> $value->translate($_locale)->getDescription2(),
  102.                                                 'extension_file' => $extension_file
  103.                                             ];
  104.     
  105.                     if($extension_file != 'mp4'){
  106.                         // On récupère le nom de fichier sans son extension
  107.                         $nom_sans_extension pathinfo($value->getFileName(), PATHINFO_FILENAME);
  108.                         // On modifie l'extension du fichier
  109.                         $nouvelle_extension 'webp';
  110.                         $nouveau_nom_fichier $nom_sans_extension '.' $nouvelle_extension;
  111.     
  112.                         $new_galleries_images['path'] = 'storage/galleries/'.$nouveau_nom_fichier;
  113.                     }else{
  114.                         $new_galleries_images['path'] = 'uploads/media/'.$value->getFileName();
  115.                     }
  116.                     
  117.                     $header_images[] = $new_galleries_images;
  118.     
  119.                 }
  120.             }
  121.         }
  122.         
  123.         $bloc_post $this->renderBloc->RenderBloc($home_page,$schemaOrg);
  124.         $metaService $this->metaPostService->meta($home_page);
  125.         $meta_robots $home_page->getMetaRobots();     
  126.         $baseurl $request->getScheme() . '://' $request->getHttpHost() . $request->getBasePath();
  127.         $url $this->generateUrl('index_home', [], UrlGeneratorInterface::ABSOLUTE_URL);
  128.         $title_seo =  isset($metaService['title'])?$metaService['title']:null;
  129.         $description_seo = isset($metaService['description'])?$metaService['description']:null;
  130.         $metacle = isset($metaService['metacle'])?$metaService['metacle']:null;
  131.       
  132.         if ($home_page->translate($_locale)->getSchema() != '') {
  133.             $custom_schemaOrg $home_page->translate($_locale)->getSchema();
  134.         }
  135.         if($title_seo != '' || $description_seo != ''){
  136.             $this->basicSeo->setTitle($title_seo)
  137.                            ->setDescription($description_seo)
  138.                            ->setkeywords($metacle);
  139.             $this->seo->setTitle($title_seo)
  140.                       ->setDescription($description_seo)
  141.                       ->setUrl($url);
  142.                       
  143.             if ($this->functionService->parametreGenerale()) {
  144.                 if ($this->functionService->parametreGenerale()->getApercuImageLien()) {
  145.                     $this->seo->setImage($baseurl.'/public/uploads/images/'.$this->functionService->parametreGenerale()->getApercuImageLien());
  146.                 }
  147.             }
  148.             $schemaOrg->add(Schema::WebSite()->name($title_seo)->url($url));
  149.         }
  150.         // preload image page home 
  151.         if($bloc_post && isset($bloc_post['Bloc'])){
  152.             foreach ($bloc_post['Bloc'] as $value) {
  153.                 isset($value['template']) ? preg_match_all('@src="([^"]+)"@',$value['template'],$image_bloc) : null;
  154.                 isset($value['template']) ? preg_match_all('/url\((.*?)\)/s',$value['template'],$image_parallax) : null;
  155.                 
  156.                 if (isset($image_bloc)) {
  157.                    foreach ($image_bloc[1] as $value) {
  158.                         if (str_contains($value$cdn_image)) {
  159.                             $preload_images.='<link rel="preload" as="image" href="'.$value.'">'."\n";
  160.                         }else{
  161.                             $preload_images.='<link rel="preload" as="image" href="'.$value.'">'."\n";
  162.                         }
  163.                    }
  164.                 }
  165.                 if (isset($image_parallax)) {
  166.                    foreach ($image_parallax[1] as $value) {
  167.                         if (str_contains($value$cdn_image)) {
  168.                             $preload_images.='<link rel="preload" as="image" href="'.$value.'">'."\n";
  169.                         }else{
  170.                             $preload_images.='<link rel="preload" as="image" href="'.$value.'">'."\n";
  171.                         }
  172.                    }
  173.                 }
  174.             }
  175.         }
  176.         $template_model_galerie $modele_gallerie[0]->getTwig();
  177.         return $this->render('front/'.$this->theme.'/index.html.twig',[
  178.                'galleries'=> $header_images,
  179.                'bloc_post'=> $bloc_post,
  180.                'meta_robots'=> $meta_robots,
  181.                'schemaOrg'=> $schemaOrg,
  182.                'custom_schemaOrg'=> $custom_schemaOrg,
  183.                'preload_images'=> $preload_images,
  184.                'popup'=> $popup,
  185.                'modele_galerie' => $modele_gallerie,
  186.                'template_model_gallerie' => $template_model_galerie,
  187.         ]);
  188.     }
  189.     /**
  190.      * @Route("/assets/{path<(.+)>}", methods={"GET"}, name="asset_link")
  191.      */
  192.     public function asset(string $pathstring $secretRequest $requestServer $glide)
  193.     {
  194.         $parameters $request->query->all();
  195.         if (\count($parameters) > 0) {
  196.             try {
  197.                 SignatureFactory::create($secret)->validateRequest($path$parameters);
  198.             } catch (SignatureException $e) {
  199.                 throw $this->createNotFoundException(''$e);
  200.             }
  201.         }
  202.         $glide->setResponseFactory(new SymfonyResponseFactory($request));
  203.         try {
  204.             $response $glide->getImageResponse($path$parameters);
  205.         } catch (\InvalidArgumentException FileNotFoundException $e) {
  206.             throw $this->createNotFoundException(''$e);
  207.         }
  208.         return $response;
  209.     }
  210. }