src/Service/RenderBloc.php line 114

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Entity\Faqs;
  4. use App\Entity\Post;
  5. use App\Entity\Forms;
  6. use App\Entity\Videos;
  7. use App\Entity\CpField;
  8. use App\Entity\Product;
  9. use App\Entity\MediaCms;
  10. use App\Entity\PostBloc;
  11. use App\Entity\BlocService;
  12. use App\Entity\CpPostField;
  13. use App\Entity\FormsFields;
  14. use App\Entity\Partenaires;
  15. use App\Entity\CategoryBloc;
  16. use App\Entity\MediaGallery;
  17. use App\Entity\Testimonials;
  18. use Spatie\SchemaOrg\Schema;
  19. use App\Entity\ParametrePost;
  20. use App\Entity\ParametreSite;
  21. use App\Entity\ProduitOption;
  22. use App\Entity\GallerieBlocPost;
  23. use App\Entity\CategoriesProduct;
  24. use Symfony\Component\Intl\Locale;
  25. use App\Entity\ParametrePartenaires;
  26. use Doctrine\ORM\EntityManagerInterface;
  27. use Symfony\Component\HttpClient\HttpClient;
  28. use Symfony\Component\HttpFoundation\RequestStack;
  29. use Symfony\Contracts\HttpClient\HttpClientInterface;
  30. class RenderBloc
  31. {
  32.     private $em;
  33.     private $templating;
  34.     private $theme;
  35.     private $client;
  36.     private $requestStack;
  37.     public function __construct(EntityManagerInterface $em,\Twig\Environment $templating,string $theme,
  38.                                 HttpClientInterface $clientRequestStack $requestStack)
  39.     {
  40.         $this->templating $templating;
  41.         $this->em $em;
  42.         $this->theme $theme;
  43.         $this->client $client;
  44.         $this->requestStack $requestStack;
  45.     }
  46.     /**
  47.      * Retourne l'objet Post
  48.      */
  49.     public function RenderBloc($post,$schemaOrg,$type 'post') {
  50.         $_locale Locale::getDefault();
  51.    
  52.         $data_post = [];
  53.         $data_Bloc = [];
  54.         $blocs null;
  55.         if($type == 'post'){
  56.             if ($post != null) {
  57.               $blocs $this->em->getRepository(PostBloc::class)->findBlocNotDeleted($post->getId());
  58.             }
  59.         }else{
  60.             if($post != null){
  61.               $blocs $this->em->getRepository(CategoryBloc::class)->findBlocNotDeleted($post->getId());
  62.             }            
  63.         }
  64.     
  65.         if($blocs != null){
  66.             foreach($blocs as $bloc){
  67.                 $data = [];
  68.                 if($bloc->getTypeBloc() == '5' && $bloc->getActif() == true){
  69.                   $Media $this->em->getRepository(MediaGallery::class)->findGalleryActif($bloc->getMediaGallery());
  70.                   $data_gallerie = [];
  71.                   if($Media){
  72.                       $media_cms $this->em->getRepository(MediaCms::class)->findBy(['module'=> 'gallerie','module_id'=>$Media->getId()],['position'=> 'ASC']);
  73.                       foreach($media_cms as $gallerie){
  74.                         $data_gallerie[] = [
  75.                                            'id'=> $gallerie->getId(),
  76.                                            'position'=> $gallerie->getPosition(),
  77.                                            'path'=> $gallerie->getLocation().$gallerie->getPath().$gallerie->getFileName(),  
  78.                                            'title'=> $gallerie->translate($_locale)->gettitre(), 
  79.                                            'fileExtension' => $gallerie->getType(),
  80.                                            'description'=> $gallerie->translate($_locale)->getDescription(),
  81.                                            'description2'=> $gallerie->translate($_locale)->getDescription2(),  
  82.                                           ];
  83.                       }
  84.                       $data = [
  85.                                 'id' => $Media->getId(),
  86.                                 'actif'=> $Media->getActif(),
  87.                                 'type' => 'slider',
  88.                                 'title'=> $Media->translate($_locale)->getTitre(),     
  89.                                 'description'=> $Media->translate($_locale)->getDescription(),    
  90.                                 'images'=> $data_gallerie,  
  91.                                 'mobile'=> $bloc->getActifMobile(), 
  92.                                 'desktop'=> $bloc->getActifDesktop(), 
  93.                       ];
  94.                   }
  95.                   
  96.                 }elseif($bloc->getTypeBloc() == '6' && $bloc->getActif() == true){
  97.                     $form $this->em->getRepository(Forms::class)->findOneBy(['id'=>$bloc->getForm()]);
  98.                     $formField $this->em->getRepository(FormsFields::class)->findOneBy(['form'=>$form,'local'=>$_locale]);
  99.    
  100.                     $data_Form = [
  101.                           'id'=> $form->getId(),
  102.                           'name'=> $form->getName(),
  103.                           'methode'=> $form->getMethode(),   
  104.                           'success_message'=>  $form->getSuccessMessage(), 
  105.                           'identifier'=>  $form->getIdentifier(), 
  106.                           'type_form'=> $form->getFormBuilder(),
  107.                           'data'=> $formField?unserialize($formField->getData()):null,
  108.                           'data_form_step'=> $formField?unserialize($formField->getDataFormMutiStep()):null,
  109.                           'titre'=> $form->getTitreContent(),
  110.                           'texte'=> $form->getTexteContent(),
  111.                     ]; 
  112.                        
  113.                     $data = [ 'id' => $bloc->getId(),
  114.                               'actif' => $bloc->getActif(),
  115.                               'type'  => $bloc->getTypeBloc() == ?'form':$bloc->getTypeBloc(),
  116.                               'title' => $bloc->translate($_locale)->getTitle(),
  117.                               'content' => $bloc->translate($_locale)->getContent(),
  118.                               'content2' => $bloc->translate($_locale)->getContent2(),
  119.                               'link'=> $bloc->translate($_locale)->getLink(),
  120.                               'form'  => $data_Form
  121.                               'mobile'=> $bloc->getActifMobile(), 
  122.                               'desktop'=> $bloc->getActifDesktop(), 
  123.                             ];
  124.                 }elseif($bloc->getTypeBloc() == '7' && $bloc->getActif()  == true){
  125.   
  126.                   $data = [ 'id' => $bloc->getId(),
  127.                             'actif' => $bloc->getActif(),
  128.                             'type'  => 'template',
  129.                             'template' => $bloc->translate($_locale)->getTemplate(),
  130.                             'mobile'=> $bloc->getActifMobile(), 
  131.                             'desktop'=> $bloc->getActifDesktop(), 
  132.                           ];
  133.                 }elseif($bloc->getTypeBloc() == '8' && $bloc->getActif() == true){
  134.                   
  135.                   $template $this->renderTemplate($bloc,$schemaOrg);
  136.                   $data = [ 'id' => $bloc->getId(),
  137.                             'actif' => $bloc->getActif(),
  138.                             'type'  => 'template',
  139.                             'template' => $template,
  140.                             'mobile'=> $bloc->getActifMobile(), 
  141.                             'desktop'=> $bloc->getActifDesktop(), 
  142.                           ];
  143.                 }elseif($bloc->getTypeBloc() == '9' && $bloc->getActif()  == true){
  144.                   $data = [ 'id' => $bloc->getId(),
  145.                             'actif' => $bloc->getActif(),
  146.                             'type'  => 'video',
  147.                             'url_video' => $bloc->getVideoUrl(),
  148.                             'mobile'=> $bloc->getActifMobile(), 
  149.                             'desktop'=> $bloc->getActifDesktop(), 
  150.                           ];
  151.                 }elseif($bloc->getTypeBloc() == '3' && $bloc->getActif()  == true){
  152.                           $data = [ 'id' => $bloc->getId(),
  153.                                     'actif' => $bloc->getActif(),
  154.                                     'type'  => 'image',
  155.                                     'title' => $bloc->translate($_locale)->getTitle(),
  156.                                     'bgimage' => $bloc->getImage(),
  157.                                     'mobile'=> $bloc->getActifMobile(), 
  158.                                     'desktop'=> $bloc->getActifDesktop(), 
  159.                                   ];
  160.                 }elseif($bloc->getTypeBloc() == '4' && $bloc->getActif()  == true){
  161.                     $data = [ 'id' => $bloc->getId(),
  162.                               'actif' => $bloc->getActif(),
  163.                               'type'  => 'titre',
  164.                               'title' => $bloc->translate($_locale)->getContent2(),
  165.                               'mobile'=> $bloc->getActifMobile(), 
  166.                               'desktop'=> $bloc->getActifDesktop(), 
  167.                             ];
  168.                   }elseif($bloc->getTypeBloc() == '1' && $bloc->getActif()  == true){
  169.                     $data = [ 'id' => $bloc->getId(),
  170.                               'actif' => $bloc->getActif(),
  171.                               'type'  => 'text',
  172.                               'bg_color'=> $bloc->getBgColor(),
  173.                               'title' => $bloc->translate($_locale)->getTitle(),
  174.                               'content' => $bloc->translate($_locale)->getContent(),
  175.                               'libelle_lien'=> $bloc->translate($_locale)->getTitreLien(),
  176.                               'type_lien'=> $bloc->translate($_locale)->getTypeLien(),
  177.                               'lien_interne'=> $bloc->translate($_locale)->getLienInterne(),
  178.                               'lien_externe'=> $bloc->translate($_locale)->getLienExterne(),
  179.                               'telephone'=> $bloc->translate($_locale)->getTelephone(),
  180.                               'mobile'=> $bloc->getActifMobile(), 
  181.                               'desktop'=> $bloc->getActifDesktop(), 
  182.                             ];
  183.                   }elseif($bloc->getTypeBloc() == '10' && $bloc->getActif()  == true){
  184.                     $data = [ 'id' => $bloc->getId(),
  185.                               'actif' => $bloc->getActif(),
  186.                               'type'  => 'text2',
  187.                               'title' => $bloc->translate($_locale)->getTitle(),
  188.                               'content' => $bloc->translate($_locale)->getContent(),
  189.                               'position'=> $bloc->getPositionText(),
  190.                               'image'=> $bloc->getImage(),
  191.                               'url_video' => $bloc->getVideoUrl(),
  192.                               'grid'=> $bloc->getGridCarousel(),
  193.                               'mobile'=> $bloc->getActifMobile(), 
  194.                               'desktop'=> $bloc->getActifDesktop(), 
  195.                             ];
  196.                   }elseif($bloc->getTypeBloc() == '11' && $bloc->getActif() == true){
  197.                      $bloc_service $this->em->getRepository(BlocService::class)->findOneBy(['actif'=> true,'id'=>$bloc->getIdBlocService()]);
  198.                      $data = [  'id' => $bloc->getId(),
  199.                                 'actif' => $bloc->getActif(),
  200.                                 'type'  => 'bloc-service',
  201.                                 'data' => $bloc_service,
  202.                                 'mobile'=> $bloc->getActifMobile(), 
  203.                                 'desktop'=> $bloc->getActifDesktop(), 
  204.                               ];
  205.                   }elseif($bloc->getTypeBloc() == '12' && $bloc->getActif() == true){
  206.                       $parametreSite $this->em->getRepository(ParametreSite::class)->findOneBy([],['id'=> 'ASC']);
  207.                       $param_partenaire $this->em->getRepository(ParametrePartenaires::class)->findOneBy(['actif'=> true]);
  208.                       $liste_partenaire $this->em->getRepository(Partenaires::class)->findBy(['actif'=> true]);
  209.                       $client HttpClient::create();
  210.                       $request $this->requestStack->getCurrentRequest();
  211.                       $baseurl $request->getScheme() . '://' $request->getHttpHost() . $request->getBasePath();
  212.                       $partenaires null;
  213.                       try {
  214.                           $response_api $client->request('GET',
  215.                               $parametreSite->getDomainApiHrz().'/api/custom/all-partners-domain',
  216.                               [ 'headers' => ['Accept' => 'application/json'],
  217.                                 'query' => [
  218.                                   'domain' => $baseurl,
  219.                                   'token' => $parametreSite->getTokenApiHrz(),
  220.                                   ],
  221.                               ]);
  222.                           if ($response_api->getStatusCode() === 200) {
  223.                               $partenaires $response_api->toArray()['data'];
  224.                           }
  225.                       } catch (\Throwable $th) {
  226.                           //throw $th;
  227.                           //dump($th);
  228.                       }
  229.                       $data = [ 'id' => $bloc->getId(),
  230.                                 'actif' => $bloc->getActif(),
  231.                                 'type'  => 'bloc-partenaire',
  232.                                 'data' => ['parametre'=> $param_partenaire,'liste_partenaire'=> $liste_partenaire,'partenaires'=> $partenaires,'baseurl' => $baseurl],
  233.                                 'mobile'=> $bloc->getActifMobile(), 
  234.                                 'desktop'=> $bloc->getActifDesktop(), 
  235.                                 
  236.                               ];
  237.                   }else{
  238.                     if($bloc->getActif() == true){
  239.                       $data = [ 'id' => $bloc->getId(),
  240.                                 'actif' => $bloc->getActif(),
  241.                                 'type'  => $bloc->getTypeBloc(),
  242.                                 'title' => $bloc->translate($_locale)->getTitle(),
  243.                                 'content' => $bloc->translate($_locale)->getContent(),
  244.                                 'content2' => $bloc->translate($_locale)->getContent2(),
  245.                                 'link'=> $bloc->translate($_locale)->getLink(),
  246.                                 'mobile'=> $bloc->getActifMobile(), 
  247.                                 'desktop'=> $bloc->getActifDesktop(), 
  248.                               ];
  249.                     }
  250.    
  251.                   }
  252.   
  253.                 if(!empty($data)){
  254.                   $data_Bloc[] = $data
  255.                 }
  256.             }
  257.         }
  258.         if($post){
  259.             if ($type == 'post') {
  260.               $data_post = [
  261.                 'id' => $post->getId(),
  262.                 'actif'=> $post->getActif(),
  263.                 'image'=> $post->getImage(),
  264.                 'title'=> $post->translate($_locale)->getTitle(),
  265.                 'slug'=> $post->translate($_locale)->getSlug(),
  266.                 'summary'=> $post->translate($_locale)->getSummary(),
  267.                 'content'=> $post->translate($_locale)->getContent(),
  268.                 'MetaTitle'=> $post->translate($_locale)->getMetaTitle(),
  269.                 'MetaDescription'=> $post->translate($_locale)->getMetaDescription(),
  270.                 'MetaKeys'=> $post->translate($_locale)->getMetaKeys(),
  271.                 'TagManagerhead'=> $post->getTagManagerhead(),
  272.                 'TagManagerbody'=> $post->getTagManagerbody(),
  273.                 'Bloc'=> $data_Bloc,
  274.               ];
  275.             }else{
  276.               $data_post = [
  277.                 'id' => $post->getId(),
  278.                 'actif'=> $post->getActif(),
  279.                 'image'=> $post->getImage(),
  280.                 'title'=> $post->translate($_locale)->getTitreCategorie(),
  281.                 'slug'=> $post->translate($_locale)->getSlugUrl(),
  282.                 'summary'=> $post->translate($_locale)->getChapeauCategorie(),
  283.                 'content'=> $post->translate($_locale)->getTexteCategorie(),
  284.                 'MetaTitle'=> $post->translate($_locale)->getMetaTitle(),
  285.                 'MetaDescription'=> $post->translate($_locale)->getMetaDescription(),
  286.                 'MetaKeys'=> $post->translate($_locale)->getMetaKeywords(),
  287.                 'TagManagerhead'=> $post->getTagManagerhead(),
  288.                 'TagManagerbody'=> $post->getTagManagerbody(),
  289.                 'Bloc'=> $data_Bloc,
  290.               ];
  291.             }
  292.         }
  293.         return $data_post;
  294.     }
  295.     public function renderTemplate($bloc,$schemaOrg){
  296.         $_locale Locale::getDefault();
  297.         $widget $bloc->getWidget();
  298.         $singel_post  $widget->getSingelPost();
  299.         $module $widget->getModules();
  300.         $template null;
  301.         $parametre $this->em->getRepository(ParametreSite::class)->findOneBy(['Site'=>'1']);
  302.         
  303.         if ($module == 'Poste') {
  304.             if($singel_post === false){
  305.               $type $widget->getTypePost();
  306.               $option $widget->getOptionAffichage(); 
  307.               $nb $widget->getNbPost();
  308.               $resultat = [];
  309.               $posts $this->em->getRepository(Post::class)->findPostActifsHomeByType($type,$option,$nb,$widget->getAllArticle());
  310.               if ($posts) {
  311.                   foreach ($posts as $value) {
  312.                       $resultat[] = ['image'=> $value->getImage(),
  313.                                     'imagealt'=> $value->translate($_locale)->getImageAlt(),
  314.                                     'summary'=> $value->translate($_locale)->getSummary(),
  315.                                     'slug'=> $value->translate($_locale)->getSlug(),
  316.                                     'titre'=> $value->translate($_locale)->getTitle(),
  317.                                     'title_affichage'=> $value->translate($_locale)->getTitleAffichage(),
  318.                                     'typelien'=> $value->translate($_locale)->getTypeLien(),
  319.                                     'titrelien'=> $value->translate($_locale)->getTitreLien(),
  320.                                     'telephone'=> $value->translate($_locale)->getTelephone(),
  321.                                     'lieninterne'=> $value->translate($_locale)->getLienInterne(),
  322.                                     'lienexterne'=> $value->translate($_locale)->getLienExterne(),
  323.                                     'content'=> $value->translate($_locale)->getContent(),
  324.                                     ];
  325.                   }
  326.               }
  327.              
  328.               if ($widget->getWidgetModelId() == 1) {
  329.                   $affiche $widget->getTypeAffichageHome();
  330.                   
  331.                   $template $this->templating->render('front/'.$this->theme.'/modele/bloc_articles.html.twig',[
  332.                     'resultats'=> $resultat,
  333.                     'widget'=> $widget
  334.                     'parametre' => $parametre,
  335.                     'template' =>$affiche
  336.                   ]);
  337.               }elseif($widget->getWidgetModelId() == 18){
  338.                   $template $this->templating->render('front/'.$this->theme.'/modele/bloc_associes.html.twig',[
  339.                     'widget'=> $widget
  340.                     'resultat'=> $resultat,
  341.                     'widget'=> $widget,      
  342.                   ]);
  343.               }elseif($widget->getWidgetModelId() == 19){
  344.                   $template $this->templating->render('front/'.$this->theme.'/modele/bloc_faqs_random.html.twig',[
  345.                     'widget'=> $widget
  346.                     'resultat'=> $resultat,
  347.                     'widget'=> $widget,      
  348.                   ]);
  349.               }
  350.            
  351.             }elseif ($singel_post === true &&  $widget->getWidgetModelId() == 2) {
  352.               $template $this->templating->render('front/'.$this->theme.'/modele/parallax.html.twig',[
  353.                 'widget'=> $widget,      
  354.               ]);
  355.             }elseif($singel_post === true &&  $widget->getWidgetModelId() == 3){
  356.                 $template $this->templating->render('front/'.$this->theme.'/modele/bloc_content.html.twig',[
  357.                   'widget'=> $widget,      
  358.                 ]);
  359.             }elseif($singel_post === true &&  $widget->getWidgetModelId() == 4){
  360.             $template $this->templating->render('front/'.$this->theme.'/modele/bloc_presentation.html.twig',[
  361.               'widget'=> $widget,      
  362.             ]);
  363.             }
  364.         }elseif($module == 'Faqs') {
  365.             $option $widget->getOptionAffichage(); 
  366.             $nb $widget->getNbPost();
  367.             $id_module $widget->getIdModule();
  368.             if ($singel_post) {
  369.               $faqs $this->em->getRepository(Faqs::class)->findFaqsByWidget($option,$nb,$id_module);
  370.             }else{
  371.               $faqs $this->em->getRepository(Faqs::class)->findFaqsByWidget($option,$nb,null,$widget->getAllArticle());
  372.             }
  373.             if ($faqs && $schemaOrg != null) {
  374.                 $schemaOrg_faq_items = [];
  375.                 foreach ($faqs as $value) {
  376.                   $schemaOrg_faq_items[] = Schema::Question()
  377.                                                 ->name($value->getQuestion())
  378.                                                 ->acceptedAnswer(Schema::Answer()->text(str_replace("\"""'"$value->getReponse())));
  379.                 }
  380.                 if (!empty($schemaOrg_faq_items)) {
  381.                     $schemaOrg->add(Schema::FAQPage()->mainEntity($schemaOrg_faq_items));
  382.                 }  
  383.             }
  384.             if ($widget->getWidgetModelId() == 15) {
  385.               $template $this->templating->render('front/'.$this->theme.'/modele/bloc_questions_reponse_carousel.html.twig',[
  386.                 'faqs'=> $faqs,
  387.                 'widget'=> $widget
  388.               ]);
  389.             }elseif ($widget->getWidgetModelId() == 17) {
  390.               $template $this->templating->render('front/'.$this->theme.'/modele/bloc_questions_reponse_collapse.html.twig',[
  391.                 'faqs'=> $faqs,
  392.                 'widget'=> $widget
  393.               ]);
  394.             }
  395.         }elseif($module == 'Videos'){
  396.             $option $widget->getOptionAffichage(); 
  397.             $nb $widget->getNbPost();
  398.             $id_module $widget->getIdModule();
  399.             if ($singel_post) {
  400.               $videos $this->em->getRepository(Videos::class)->findVideosByWidget($option,$nb,$id_module);
  401.             }else{
  402.               $videos $this->em->getRepository(Videos::class)->findVideosByWidget($option,$nb,null,$widget->getAllArticle());
  403.             }
  404.             $key_api_youtube 'AIzaSyCEZzFBZ_SGHsZgPrBgq4wYhx2YlgvHpUA';
  405.             if ($videos && $schemaOrg != null) {
  406.                 $list_items_video = [];
  407.                 $position_items_video 1;
  408.                 foreach ($videos as $value) {
  409.                   preg_match("/.*v=(.*)/"$value->getVideo(),$match);
  410.                   if (isset($match[1])) {
  411.                        $id_video $match[1];
  412.                        try {
  413.                         $response_api_youtube $this->client->request(
  414.                             'GET',
  415.                             'https://www.googleapis.com/youtube/v3/videos?id='.$id_video.'&key='.$key_api_youtube.'&part=snippet,statistics&fields=items(id,snippet,statistics)'
  416.                         );
  417.                         
  418.                         if ($response_api_youtube->getStatusCode() == 200) {
  419.                                 $content $response_api_youtube->toArray();
  420.                                 $data_video $content['items'][0]['snippet'];
  421.                                 $list_items_video[] = Schema::ListItem()
  422.                                                     ->position($position_items_video)
  423.                                                     ->item(Schema::VideoObject()
  424.                                                             ->name($data_video['title'])
  425.                                                             ->description($data_video['description'])
  426.                                                             ->thumbnailUrl($data_video['thumbnails']['default']['url'])
  427.                                                             ->uploadDate($data_video['publishedAt'])
  428.                                                             ->embedUrl("https://www.youtube.com/embed/".$id_video)
  429.                                                             ->contentUrl("https://www.youtube.com/watch?v=".$id_video));
  430.                                 $position_items_video++;
  431.                             }
  432.                         } catch (\Throwable $th) {
  433.                         }
  434.                   }
  435.                 }
  436.                 if (!empty($list_items_video)) {
  437.                    $schemaOrg->add(Schema::ItemList()->itemListElement($list_items_video));
  438.                 }  
  439.             }
  440.             $template $this->templating->render('front/'.$this->theme.'/modele/bloc_videos.html.twig',[
  441.               'videos'=> $videos,
  442.               'widget'=> $widget
  443.             ]);
  444.         }
  445.         return $template;
  446.     }
  447. }