src/Controller/Front/PageController.php line 451

Open in your IDE?
  1. <?php 
  2. namespace App\Controller\Front;
  3. use App\Entity\Menu;
  4. use App\Entity\Post;
  5. use App\Entity\UrlRef;
  6. use App\Entity\UrlPost;
  7. use App\Entity\Category;
  8. use App\Entity\Language;
  9. use App\Entity\MediaCms;
  10. use App\Entity\Secteurs;
  11. use App\Entity\TypePost;
  12. use App\Entity\ListeMenu;
  13. use App\Service\BuildTree;
  14. use App\Entity\CpPostField;
  15. use App\Entity\ModelGallerie;
  16. use App\Entity\PostArchive;
  17. use App\Service\RenderBloc;
  18. use Spatie\SchemaOrg\Graph;
  19. use App\Entity\ParametreRef;
  20. use App\Entity\PostCategory;
  21. use App\Service\MetaService;
  22. use Spatie\SchemaOrg\Schema;
  23. use App\Entity\ParametreSite;
  24. use App\Entity\ReseauSociaux;
  25. use App\Service\MetaClePageRef;
  26. use App\Service\FunctionService;
  27. use App\Service\MetaWithoutCategoryService;
  28. use Symfony\Component\HttpFoundation\Request;
  29. use Symfony\Component\HttpFoundation\Response;
  30. use Symfony\Component\Routing\Annotation\Route;
  31. use Symfony\Component\HttpFoundation\JsonResponse;
  32. use Leogout\Bundle\SeoBundle\Seo\Og\OgSeoGenerator;
  33. use Symfony\Component\String\Slugger\SluggerInterface;
  34. use Leogout\Bundle\SeoBundle\Seo\Basic\BasicSeoGenerator;
  35. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  36. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  37. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  38. class PageController extends AbstractController
  39. {
  40.     public $basicSeo;
  41.     public $seo;
  42.     private $slugger;
  43.     private $projectDir;
  44.     private $metaService;
  45.     private $metaClePageRef;
  46.     private $renderBloc;
  47.     private $buildTree;
  48.     private $functionService;
  49.     private $metaPostService;
  50.     private $theme;
  51.     
  52.     public function __construct(string $projectDir,string $theme,
  53.                                 BasicSeoGenerator $basicSeo,
  54.                                 OgSeoGenerator $seo,
  55.                                 RenderBloc $renderBloc,
  56.                                 SluggerInterface $slugger,
  57.                                 BuildTree $buildTree,
  58.                                 MetaService $metaService,
  59.                                 MetaWithoutCategoryService $metaPostService,
  60.                                 MetaClePageRef $metaClePageRef,
  61.                                 FunctionService $functionService){
  62.         $this->metaService $metaService;
  63.         $this->metaPostService $metaPostService;
  64.         $this->basicSeo $basicSeo;
  65.         $this->seo $seo;
  66.         $this->buildTree $buildTree;
  67.         $this->slugger $slugger;
  68.         $this->metaClePageRef $metaClePageRef;
  69.         $this->functionService $functionService;
  70.         $this->renderBloc $renderBloc;
  71.         $this->projectDir $projectDir;
  72.         $this->theme $theme;
  73.     }
  74.     /**
  75.      * @Route("/{slug}/{page<[1-9]\d*>}", name="page")
  76.      */
  77.     public function Page(Request $request,$slug,$page 1)
  78.     {
  79.         $slug_info $this->getDoctrine()->getRepository(UrlPost::class)->findUrlBySlug($slug);
  80.         if(!$slug_info){
  81.             throw new NotFoundHttpException('Sorry not existing!');
  82.         }
  83.         if($slug_info->getType() === "Post"){
  84.  
  85.             $this->MetaModulePostOrCategory($request,$slug,$slug_info);
  86.             $render $this->pagePost($request,$slug);
  87.            
  88.         }elseif ($slug_info->getType() === "Categorie"){
  89.     
  90.             $this->MetaModulePostOrCategory($request,$slug,$slug_info);
  91.             $render $this->pageCatgorie($request,$slug,$page);
  92.         }
  93.  
  94.         return $render;
  95.     }
  96.     public function pagePost($request,$slug){
  97.         $schemaOrg = new Graph();
  98.         $_locale $request->getLocale();
  99.         $post $this->getDoctrine()->getRepository(Post::class)->findPostBySlug($slug);
  100.         $modele_galerie_choisie $post->getModeleGallerie() ? $post->getModeleGallerie()->getId() : 1;
  101.         $modele_gallerie $this->getDoctrine()->getRepository(ModelGallerie::class)->findById($modele_galerie_choisie);
  102.         $baseurl $request->getScheme() . '://' $request->getHttpHost() . $request->getBasePath();
  103.         $seo_image '';
  104.         $url $this->generateUrl('page', ['slug'=> $post->translate($_locale)->getSlug()], UrlGeneratorInterface::ABSOLUTE_URL);
  105.         if ($this->functionService->parametreGenerale()) {
  106.             if ($this->functionService->parametreGenerale()->getApercuImageLien()) {
  107.                 $seo_image $baseurl.'/public/uploads/images/'.$this->functionService->parametreGenerale()->getApercuImageLien();
  108.             }
  109.         }
  110.         $galleries_images = [];
  111.         $bloc_post $this->renderBloc->RenderBloc($post,$schemaOrg);
  112.         $module $post->getTypePost()->getSystemName();
  113.         $galleries $this->getDoctrine()->getRepository(MediaCms::class)->findFileByModule($module,$post->getId(),'media');
  114.         if($galleries){
  115.             foreach ($galleries as $value) {
  116.   
  117.                 $extension_file pathinfo($value->getFileName(), PATHINFO_EXTENSION);
  118.                 $new_galleries_images = ['id'=> $value->getId(),
  119.                                             'path'=> '',
  120.                                             'lienexterne' => $value->getLienExterne(),
  121.                                             'titre'=> $value->translate($_locale)->getTitre(),
  122.                                             'description'=> $value->translate($_locale)->getDescription(),
  123.                                             'extension_file' => $extension_file
  124.                                         ];
  125.                 if($extension_file != 'mp4'){
  126.                     $new_galleries_images['path'] = 'storage/galleries/'.$value->getFileName();
  127.                 }else{
  128.                     $new_galleries_images['path'] = 'uploads/media/'.$value->getFileName();
  129.                 }
  130.                 
  131.                 $galleries_images[] = $new_galleries_images;
  132.             }
  133.         }
  134.         $docs $this->getDoctrine()->getRepository(MediaCms::class)->findFileByModule($module,$post->getId(),'doc');
  135.         
  136.         $ListId = [];
  137.         if($post->getPostCategory()){
  138.             foreach($post->getPostCategory() as $value){
  139.                 $ListId[]= $value->getCategory()->getId();
  140.             }
  141.         }
  142.         $categorieTree null;
  143.         if (!empty($ListId)) {
  144.             $categories $this->getDoctrine()->getRepository(Category::class)->findCategoriePost($ListId);
  145.             $categorieTree $this->functionService->buildTree($categories); 
  146.         }
  147.         $meta_robots $post->getMetaRobots();
  148.         $custom_schemaOrg null;
  149.         if ($post->translate($_locale)->getSchema() != '') {
  150.             $custom_schemaOrg $post->translate($_locale)->getSchema();
  151.         }
  152.         $breadcrumb_item 1;
  153.         $schema_breadcrumb_items = [];
  154.         $schema_breadcrumb_items[] = Schema::ListItem()
  155.                                    ->position(1)
  156.                                    ->item(["@id"=> $this->generateUrl('index_home', [], UrlGeneratorInterface::ABSOLUTE_URL),"name"=>"Accueil"]);
  157.     
  158.         if($categorieTree){
  159.             foreach ($categorieTree as $value) {
  160.                 $breadcrumb_item $breadcrumb_item 1;
  161.                 $schema_breadcrumb_items[] = Schema::ListItem()
  162.                                             ->position($breadcrumb_item)
  163.                                             ->item(["@id"=> $this->generateUrl('page', ['slug'=> $value['slug']], UrlGeneratorInterface::ABSOLUTE_URL),"name"=>$value['titre']]);
  164.             }
  165.         }
  166.         $schema_breadcrumb_items[] = Schema::ListItem()
  167.                                     ->position($breadcrumb_item 1)
  168.                                     ->item(["@id"=> $this->generateUrl('page', ['slug'=> $post->translate($_locale)->getSlug()], UrlGeneratorInterface::ABSOLUTE_URL),"name"=>$post->translate($_locale)->getTitle()]);
  169.         if ($schema_breadcrumb_items) {
  170.             $schemaOrg->add(Schema::BreadcrumBList()->itemListElement($schema_breadcrumb_items));
  171.         }
  172.         $schemaOrg->add(Schema::Article()->headline($post->translate($_locale)->getTitle())
  173.                                          ->datePublished($post->getDatePublication()?$post->getDatePublication():$post->getCreatedAt())
  174.                                          ->dateModified($post->getUpdatedAt())
  175.                                          ->image($post->getImage()!= null?$baseurl.$post->getImage():'')
  176.                                          ->publisher(Schema::Organization()
  177.                                             ->name($this->functionService->parametreGenerale()?$this->functionService->parametreGenerale()->translate($_locale)->getTitre():'')
  178.                                             ->logo($this->functionService->parametreGenerale()?$baseurl.'/public/uploads/images/'.$this->functionService->parametreGenerale()->getImage():''))
  179.                                          ->mainEntityOfPage(Schema::WebPage()->url($url))
  180.                                         );
  181.         
  182.         $url_social = [];
  183.         $social $this->getDoctrine()->getRepository(ReseauSociaux::class)->findAll();
  184.         if($social){
  185.             foreach ($social as $value) {
  186.                 $url_social[] = $value->getUrl();
  187.             }
  188.         }
  189.         // id categorie related with post 
  190.         $categoiesPosts $this->getDoctrine()->getRepository(PostCategory::class)->findCategorieByPost($post->getId());
  191.         $next_article $this->getDoctrine()->getRepository(Post::class)->findPostPrevOrNext($categoiesPosts,$post->getId());
  192.         $prev_article $this->getDoctrine()->getRepository(Post::class)->findPostPrevOrNext($categoiesPosts,$post->getId(),'prev');
  193.         $template_model_galerie $modele_gallerie[0]->getTwig();
  194.         $parametre $this->getDoctrine()->getRepository(ParametreSite::class)->findOneBy(['Site'=>'1']);
  195.  
  196.         if($post->getTemplateCms()){
  197.             $template $this->render('front/'.$this->theme.'/dynamique-page-cms.html.twig',[
  198.                  'template'=> $post->getTemplateCms()->getTwig(),
  199.                  'post'=> $post,
  200.                  'galleries'=> $galleries_images,
  201.                  'bloc_post'=> $bloc_post,
  202.                  'categorieTree'=> $categorieTree,
  203.                  'meta_robots'=> $meta_robots,
  204.                  'schemaOrg'=> $schemaOrg,
  205.                  'custom_schemaOrg'=> $custom_schemaOrg,
  206.                  'parametre'=> $parametre,
  207.                  'next_article'=> $next_article,
  208.                  'prev_article'=> $prev_article,
  209.                  'modele_galerie' => $modele_gallerie,
  210.                  'template_model_gallerie' => $template_model_galerie,
  211.            ]);
  212.            
  213.         }else{
  214.             // Champs personnalisés
  215.             $valsChamp = [];
  216.             $labelsChamp = [];
  217.             $vCpFields $this->getDoctrine()->getRepository(CpPostField::class)->findBy(['IdPost'=>$post->getId()]);
  218.             $labelFiels "";
  219.             if($vCpFields){
  220.             foreach ($vCpFields as $vCpField) {
  221.                 $labelFiels $vCpField->getCpField()->translate($_locale)->getLabel();
  222.                 $TypeFiels $vCpField->getCpField()->translate($_locale)->getType();
  223.                 //dump($TypeFiels);
  224.                 $labelsChamp += [
  225.                     $vCpField->getID()=> $labelFiels
  226.                 ];
  227.   
  228.                 if($vCpField){
  229.                 $valChamp $vCpField->getLinkValue() ? $vCpField->getLinkValue() : $vCpField->getTextareaValue();
  230.                 
  231.                 $valsChamp += [
  232.                     $vCpField->getID()=> $valChamp
  233.                 ];  
  234.                 
  235.                 }
  236.             }
  237.             }
  238.             
  239.             $template =  $this->render('front/'.$this->theme.'/page_cms.html.twig',[
  240.                 'post'=> $post,
  241.                 'galleries'=> $galleries_images,
  242.                 'docs'=> $docs,
  243.                 'blocs'=> $bloc_post,
  244.                 'categorieTree'=> $categorieTree,
  245.                 'meta_robots'=> $meta_robots,
  246.                 'schemaOrg'=> $schemaOrg,
  247.                 'custom_schemaOrg'=> $custom_schemaOrg,
  248.                 'champsPersonnalises' => $valsChamp,
  249.                 'lblchampsPersonnalises' => $labelsChamp,
  250.                 'parametre'=> $parametre,
  251.                 'next_article'=> $next_article,
  252.                 'prev_article'=> $prev_article,
  253.                 'modele_galerie' => $modele_gallerie,
  254.                 'template_model_gallerie' => $template_model_galerie,
  255.             ]);
  256.         }
  257.         return $template;
  258.     }
  259.     public function pageCatgorie($request,$slug,$page){
  260.         $module 'category';
  261.         $resultats = [];
  262.         $schemaOrg = new Graph();
  263.         $_locale $request->getLocale();
  264.         $categorie $this->getDoctrine()->getRepository(Category::class)->findCategoryBySlug($slug);
  265.         $sousCategorie $this->getDoctrine()->getRepository(Category::class)->findBy(['parent'=> $categorie->getId()],['order_by'=>'ASC']);
  266.         
  267.         $template $categorie->getTypeAffichageHome();
  268.         $bloc_categorie $this->renderBloc->RenderBloc($categorie,$schemaOrg,'categorie');
  269.         // Galleries
  270.         $galleries_images = [];
  271.         $modele_galerie_choisie $categorie->getModeleGallerie() ? $categorie->getModeleGallerie()->getId() : 1;
  272.         $modele_gallerie $this->getDoctrine()->getRepository(ModelGallerie::class)->findById($modele_galerie_choisie);
  273.         $galleries $this->getDoctrine()->getRepository(MediaCms::class)->findFileByModule($module,$categorie->getId(),'media');
  274.         if($galleries){
  275.             foreach ($galleries as $value) {
  276.   
  277.                 $extension_file pathinfo($value->getFileName(), PATHINFO_EXTENSION);
  278.                 $new_galleries_images = ['id'=> $value->getId(),
  279.                                             'path'=> '',
  280.                                             'lienexterne' => $value->getLienExterne(),
  281.                                             'titre'=> $value->translate($_locale)->getTitre(),
  282.                                             'description'=> $value->translate($_locale)->getDescription(),
  283.                                             'extension_file' => $extension_file
  284.                                         ];
  285.                 if($extension_file != 'mp4'){
  286.                     $new_galleries_images['path'] = 'storage/galleries/'.$value->getFileName();
  287.                 }else{
  288.                     $new_galleries_images['path'] = 'uploads/media/'.$value->getFileName();
  289.                 }
  290.                 
  291.                 $galleries_images[] = $new_galleries_images;
  292.             }
  293.         }
  294.         $template_model_galerie $modele_gallerie[0]->getTwig();
  295.         if ($categorie->getPagination() == true) {
  296.             $posts $this->getDoctrine()->getRepository(Post::class)->findPostByCategory($categorie->getId(),$categorie->getPagination(),$page,$categorie->getPageSize());
  297.             if($posts->getResults()){
  298.                 foreach ($posts->getResults() as $value) {
  299.                     $resultats[] = [ 'id'=> $value->getId(),
  300.                                      'slug'=> $value->getSlug(),
  301.                                      'titre'=> $value->getTitle(),
  302.                                      'title_affichage'=> $value->getTitleAffichage(),
  303.                                      'content'=> $value->getContent(),
  304.                                      'chapeau'=> $value->getSummary(),
  305.                                      'image'=> $value->getImage(),
  306.                                      'typelien'=> $value->getTypeLien(),
  307.                                      'titrelien'=> $value->getTitreLien(),
  308.                                      'telephone'=> $value->getTelephone(),
  309.                                      'lieninterne'=> $value->getLienInterne(),
  310.                                      'lienexterne'=> $value->getLienExterne(),
  311.                                     ];
  312.                 }
  313.             }
  314.         }else{
  315.             $posts $this->getDoctrine()->getRepository(Post::class)->findPostByCategory($categorie->getId(),false,$page,$categorie->getPageSize());
  316.             if($posts){
  317.                 foreach ($posts as $value) {
  318.                     // Champs personnalisés
  319.                     $valsChamp = [];
  320.                     $vCpFields $this->getDoctrine()->getRepository(CpPostField::class)->findBy(['IdPost'=>$value->getId()]);
  321.                     if($vCpFields){
  322.                     foreach ($vCpFields as $vCpField) {
  323.                         if($vCpField){
  324.                         $valChamp $vCpField->getLinkValue() ? $vCpField->getLinkValue() : $vCpField->getTextareaValue();
  325.                         $valsChamp += [
  326.                             $vCpField->getID()=> $valChamp
  327.                         ];  
  328.                         
  329.                         }
  330.                     }
  331.                     }
  332.                     $resultats[] = [ 'id'=> $value->getId(),
  333.                                      'slug'=> $value->getSlug(),
  334.                                      'titre'=> $value->getTitle(),
  335.                                      'title_affichage'=> $value->getTitleAffichage(),
  336.                                      'content'=> $value->getContent(),
  337.                                      'chapeau'=> $value->getSummary(),
  338.                                      'image'=> $value->getImage(),
  339.                                      'typelien'=> $value->getTypeLien(),
  340.                                      'titrelien'=> $value->getTitreLien(),
  341.                                      'telephone'=> $value->getTelephone(),
  342.                                      'lieninterne'=> $value->getLienInterne(),
  343.                                      'lienexterne'=> $value->getLienExterne(),
  344.                                      'champsPersonnalises' => $valsChamp,
  345.                                     ];
  346.                 }
  347.             }
  348.         }
  349.        
  350.         $meta_robots $categorie->getMetaRobots();
  351.         $schema_breadcrumb_items = [];
  352.         $schema_breadcrumb_items[] = Schema::ListItem()
  353.                                    ->position(1)
  354.                                    ->item(["@id"=> $this->generateUrl('index_home', [], UrlGeneratorInterface::ABSOLUTE_URL),"name"=>"Accueil"]);
  355.         $schema_breadcrumb_items[] = Schema::ListItem()
  356.                                     ->position(2)
  357.                                     ->item(["@id"=> $this->generateUrl('page', ['slug'=> $categorie->translate($_locale)->getSlugUrl()], UrlGeneratorInterface::ABSOLUTE_URL),"name"=>$categorie->translate($_locale)->getTitreCategorie()]);
  358.         if ($schema_breadcrumb_items) {
  359.             $schemaOrg->add(Schema::BreadcrumBList()->itemListElement($schema_breadcrumb_items));
  360.         }
  361.         $baseurl $request->getScheme() . '://' $request->getHttpHost() . $request->getBasePath();
  362.         $schemaOrg->add(Schema::Article()
  363.                             ->headline($categorie->translate($_locale)->getTitreCategorie())
  364.                             ->datePublished($categorie->getCreatedAt())
  365.                             ->dateModified($categorie->getUpdatedAt())
  366.                             ->image($categorie->getImage()!= null?$baseurl.$categorie->getImage():'')
  367.                             ->publisher(Schema::Organization()
  368.                             ->name($this->functionService->parametreGenerale()?$this->functionService->parametreGenerale()->translate($_locale)->getTitre():'')
  369.                             ->logo($this->functionService->parametreGenerale()?$baseurl.'/public/uploads/images/'.$this->functionService->parametreGenerale()->getImage():''))
  370.                             ->mainEntityOfPage(Schema::WebPage()->url($this->generateUrl('page', ['slug'=> $categorie->translate($_locale)->getSlugUrl()], UrlGeneratorInterface::ABSOLUTE_URL)))
  371.                         );
  372.         $module $categorie->getTypePost()->translate($_locale)->getSystemName();
  373.         $galleries $this->getDoctrine()->getRepository(MediaCms::class)->findFileByModule($module,$categorie->getId(),'media');
  374.         $docs $this->getDoctrine()->getRepository(MediaCms::class)->findFileByModule($module,$categorie->getId(),'doc');
  375.         $parametre $this->functionService->parametreGenerale();
  376.         
  377.         return $this->render('front/'.$this->theme.'/page_categorie.html.twig',[
  378.             'template'=> $template,
  379.             'categorie'=> $categorie,
  380.             'resultats'=> $resultats,
  381.             'blocs'=> $bloc_categorie,
  382.             'posts'=> $posts,
  383.             'meta_robots'=> $meta_robots,
  384.             'pagination'=> $categorie->getPagination(),
  385.             'schemaOrg'=> $schemaOrg,
  386.             'galleries'=> $galleries_images,
  387.             'docs'=> $docs,
  388.             'parametre'=> $parametre,
  389.             'modele_galerie' => $modele_gallerie,
  390.             'template_model_gallerie' => $template_model_galerie,
  391.         ]);
  392.     }
  393.     function MetaModulePostOrCategory($request,$slug,$slug_info){
  394.         $baseurl $request->getScheme() . '://' $request->getHttpHost() . $request->getBasePath();
  395.         $url $this->generateUrl('page', ['slug'=> $slug], UrlGeneratorInterface::ABSOLUTE_URL);
  396.         $metaService $this->metaService->meta($slug,$slug_info->getType());
  397.         $title_seo =  isset($metaService['title'])?$metaService['title']:null;
  398.         $description_seo = isset($metaService['description'])?$metaService['description']:null;
  399.         $metacle = isset($metaService['metacle'])?$metaService['metacle']:null;
  400.         if($title_seo != '' || $description_seo != ''){
  401.             $this->basicSeo->setTitle($title_seo)
  402.                         ->setDescription($description_seo)
  403.                         ->setkeywords($metacle);
  404.             $this->seo->setTitle($title_seo)
  405.                     ->setDescription($description_seo)
  406.                     ->setUrl($url);
  407.             if ($this->functionService->parametreGenerale()) {
  408.                 if ($this->functionService->parametreGenerale()->getApercuImageLien()) {
  409.                     $this->seo->setImage($baseurl.'/public/uploads/images/'.$this->functionService->parametreGenerale()->getApercuImageLien());
  410.                 }
  411.             }
  412.         }
  413.     }
  414. }