<?php
namespace App\Controller\Front;
use App\Entity\Post;
use App\Entity\UrlRef;
use App\Entity\MediaCms;
use App\Entity\ModelGallerie;
use App\Entity\Secteurs;
use League\Glide\Server;
use App\Service\BuildTree;
use App\Entity\PostArchive;
use App\Service\RenderBloc;
use Spatie\SchemaOrg\Graph;
use App\Entity\ParametreRef;
use App\Service\MetaService;
use Spatie\SchemaOrg\Schema;
use App\Entity\ParametreSite;
use App\Entity\Popup;
use App\Entity\ReseauSociaux;
use App\Service\MetaClePageRef;
use App\Service\FunctionService;
use App\Service\MetaWithoutCategoryService;
use League\Glide\Signatures\SignatureFactory;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use League\Glide\Signatures\SignatureException;
use Symfony\Component\Routing\Annotation\Route;
use League\Glide\Filesystem\FileNotFoundException;
use League\Glide\Responses\SymfonyResponseFactory;
use Symfony\Component\HttpFoundation\JsonResponse;
use Leogout\Bundle\SeoBundle\Seo\Og\OgSeoGenerator;
use Symfony\Component\String\Slugger\SluggerInterface;
use Leogout\Bundle\SeoBundle\Seo\Basic\BasicSeoGenerator;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class DefaultController extends AbstractController
{
public $basicSeo;
public $seo;
private $slugger;
private $projectDir;
private $metaService;
private $metaClePageRef;
private $renderBloc;
private $buildTree;
private $functionService;
private $metaPostService;
private $theme;
public function __construct(string $projectDir,string $theme,
BasicSeoGenerator $basicSeo,
OgSeoGenerator $seo,
RenderBloc $renderBloc,
SluggerInterface $slugger,
BuildTree $buildTree,
MetaService $metaService,
MetaWithoutCategoryService $metaPostService,
MetaClePageRef $metaClePageRef,
FunctionService $functionService){
$this->metaService = $metaService;
$this->metaPostService = $metaPostService;
$this->basicSeo = $basicSeo;
$this->seo = $seo;
$this->buildTree = $buildTree;
$this->slugger = $slugger;
$this->metaClePageRef = $metaClePageRef;
$this->functionService = $functionService;
$this->renderBloc = $renderBloc;
$this->projectDir = $projectDir;
$this->theme = $theme;
}
/**
* @Route("/", name="index_page_home", options={"sitemap" = true})
*/
public function index(Request $request)
{
$schemaOrg = new Graph();
$header_images = [];
$preload_images = '';
$cdn_image = '';
//$cdn_image = 'https://assets.webspirit.ovh';
$custom_schemaOrg = null;
$_locale = $request->getLocale();
$home_page = $this->getDoctrine()->getRepository(Post::class)->findPostBySlug('Home');
$modele_galerie_choisie = $home_page->getModeleGallerie() ? $home_page->getModeleGallerie()->getId() : 1;
$modele_gallerie = $this->getDoctrine()->getRepository(ModelGallerie::class)->findById($modele_galerie_choisie);
$popup = $this->getDoctrine()->getRepository(Popup::class)->findOneBy([],['id'=>'ASC']);
if ($home_page) {
$media_cms = $this->getDoctrine()->getRepository(MediaCms::class)->findBy(['module'=> $home_page->getTypePost()->getSystemName(),'module_id'=>$home_page->getId()],['position'=> 'ASC']);
if($media_cms){
foreach ($media_cms as $value) {
$extension_file = pathinfo($value->getFileName(), PATHINFO_EXTENSION);
$new_galleries_images = ['id'=> $value->getId(),
'path'=> '',
'lienexterne' => $value->getLienExterne(),
'titre'=> $value->translate($_locale)->getTitre(),
'description'=> $value->translate($_locale)->getDescription(),
'description2'=> $value->translate($_locale)->getDescription2(),
'extension_file' => $extension_file
];
if($extension_file != 'mp4'){
// On récupère le nom de fichier sans son extension
$nom_sans_extension = pathinfo($value->getFileName(), PATHINFO_FILENAME);
// On modifie l'extension du fichier
$nouvelle_extension = 'webp';
$nouveau_nom_fichier = $nom_sans_extension . '.' . $nouvelle_extension;
$new_galleries_images['path'] = 'storage/galleries/'.$nouveau_nom_fichier;
}else{
$new_galleries_images['path'] = 'uploads/media/'.$value->getFileName();
}
$header_images[] = $new_galleries_images;
}
}
}
$bloc_post = $this->renderBloc->RenderBloc($home_page,$schemaOrg);
$metaService = $this->metaPostService->meta($home_page);
$meta_robots = $home_page->getMetaRobots();
$baseurl = $request->getScheme() . '://' . $request->getHttpHost() . $request->getBasePath();
$url = $this->generateUrl('index_home', [], UrlGeneratorInterface::ABSOLUTE_URL);
$title_seo = isset($metaService['title'])?$metaService['title']:null;
$description_seo = isset($metaService['description'])?$metaService['description']:null;
$metacle = isset($metaService['metacle'])?$metaService['metacle']:null;
if ($home_page->translate($_locale)->getSchema() != '') {
$custom_schemaOrg = $home_page->translate($_locale)->getSchema();
}
if($title_seo != '' || $description_seo != ''){
$this->basicSeo->setTitle($title_seo)
->setDescription($description_seo)
->setkeywords($metacle);
$this->seo->setTitle($title_seo)
->setDescription($description_seo)
->setUrl($url);
if ($this->functionService->parametreGenerale()) {
if ($this->functionService->parametreGenerale()->getApercuImageLien()) {
$this->seo->setImage($baseurl.'/public/uploads/images/'.$this->functionService->parametreGenerale()->getApercuImageLien());
}
}
$schemaOrg->add(Schema::WebSite()->name($title_seo)->url($url));
}
// preload image page home
if($bloc_post && isset($bloc_post['Bloc'])){
foreach ($bloc_post['Bloc'] as $value) {
isset($value['template']) ? preg_match_all('@src="([^"]+)"@',$value['template'],$image_bloc) : null;
isset($value['template']) ? preg_match_all('/url\((.*?)\)/s',$value['template'],$image_parallax) : null;
if (isset($image_bloc)) {
foreach ($image_bloc[1] as $value) {
if (str_contains($value, $cdn_image)) {
$preload_images.='<link rel="preload" as="image" href="'.$value.'">'."\n";
}else{
$preload_images.='<link rel="preload" as="image" href="'.$value.'">'."\n";
}
}
}
if (isset($image_parallax)) {
foreach ($image_parallax[1] as $value) {
if (str_contains($value, $cdn_image)) {
$preload_images.='<link rel="preload" as="image" href="'.$value.'">'."\n";
}else{
$preload_images.='<link rel="preload" as="image" href="'.$value.'">'."\n";
}
}
}
}
}
$template_model_galerie = $modele_gallerie[0]->getTwig();
return $this->render('front/'.$this->theme.'/index.html.twig',[
'galleries'=> $header_images,
'bloc_post'=> $bloc_post,
'meta_robots'=> $meta_robots,
'schemaOrg'=> $schemaOrg,
'custom_schemaOrg'=> $custom_schemaOrg,
'preload_images'=> $preload_images,
'popup'=> $popup,
'modele_galerie' => $modele_gallerie,
'template_model_gallerie' => $template_model_galerie,
]);
}
/**
* @Route("/assets/{path<(.+)>}", methods={"GET"}, name="asset_link")
*/
public function asset(string $path, string $secret, Request $request, Server $glide)
{
$parameters = $request->query->all();
if (\count($parameters) > 0) {
try {
SignatureFactory::create($secret)->validateRequest($path, $parameters);
} catch (SignatureException $e) {
throw $this->createNotFoundException('', $e);
}
}
$glide->setResponseFactory(new SymfonyResponseFactory($request));
try {
$response = $glide->getImageResponse($path, $parameters);
} catch (\InvalidArgumentException | FileNotFoundException $e) {
throw $this->createNotFoundException('', $e);
}
return $response;
}
}