src/Service/RenderBloc.php line 141

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/liste-of-partenaire',
  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],
  233.                                 'mobile'=> $bloc->getActifMobile(), 
  234.                                 'desktop'=> $bloc->getActifDesktop(), 
  235.                               ];
  236.                   }else{
  237.                     if($bloc->getActif() == true){
  238.                       $data = [ 'id' => $bloc->getId(),
  239.                                 'actif' => $bloc->getActif(),
  240.                                 'type'  => $bloc->getTypeBloc(),
  241.                                 'title' => $bloc->translate($_locale)->getTitle(),
  242.                                 'content' => $bloc->translate($_locale)->getContent(),
  243.                                 'content2' => $bloc->translate($_locale)->getContent2(),
  244.                                 'link'=> $bloc->translate($_locale)->getLink(),
  245.                                 'mobile'=> $bloc->getActifMobile(), 
  246.                                 'desktop'=> $bloc->getActifDesktop(), 
  247.                               ];
  248.                     }
  249.    
  250.                   }
  251.   
  252.                 if(!empty($data)){
  253.                   $data_Bloc[] = $data
  254.                 }
  255.             }
  256.         }
  257.         if($post){
  258.             if ($type == 'post') {
  259.               $data_post = [
  260.                 'id' => $post->getId(),
  261.                 'actif'=> $post->getActif(),
  262.                 'image'=> $post->getImage(),
  263.                 'title'=> $post->translate($_locale)->getTitle(),
  264.                 'slug'=> $post->translate($_locale)->getSlug(),
  265.                 'summary'=> $post->translate($_locale)->getSummary(),
  266.                 'content'=> $post->translate($_locale)->getContent(),
  267.                 'MetaTitle'=> $post->translate($_locale)->getMetaTitle(),
  268.                 'MetaDescription'=> $post->translate($_locale)->getMetaDescription(),
  269.                 'MetaKeys'=> $post->translate($_locale)->getMetaKeys(),
  270.                 'TagManagerhead'=> $post->getTagManagerhead(),
  271.                 'TagManagerbody'=> $post->getTagManagerbody(),
  272.                 'Bloc'=> $data_Bloc,
  273.               ];
  274.             }else{
  275.               $data_post = [
  276.                 'id' => $post->getId(),
  277.                 'actif'=> $post->getActif(),
  278.                 'image'=> $post->getImage(),
  279.                 'title'=> $post->translate($_locale)->getTitreCategorie(),
  280.                 'slug'=> $post->translate($_locale)->getSlugUrl(),
  281.                 'summary'=> $post->translate($_locale)->getChapeauCategorie(),
  282.                 'content'=> $post->translate($_locale)->getTexteCategorie(),
  283.                 'MetaTitle'=> $post->translate($_locale)->getMetaTitle(),
  284.                 'MetaDescription'=> $post->translate($_locale)->getMetaDescription(),
  285.                 'MetaKeys'=> $post->translate($_locale)->getMetaKeywords(),
  286.                 'TagManagerhead'=> $post->getTagManagerhead(),
  287.                 'TagManagerbody'=> $post->getTagManagerbody(),
  288.                 'Bloc'=> $data_Bloc,
  289.               ];
  290.             }
  291.         }
  292.         return $data_post;
  293.     }
  294.     public function renderTemplate($bloc,$schemaOrg){
  295.         $_locale Locale::getDefault();
  296.         $widget $bloc->getWidget();
  297.         $singel_post  $widget->getSingelPost();
  298.         $module $widget->getModules();
  299.         $template null;
  300.         $parametre $this->em->getRepository(ParametreSite::class)->findOneBy(['Site'=>'1']);
  301.         
  302.         if ($module == 'Poste') {
  303.             if($singel_post === false){
  304.               $type $widget->getTypePost();
  305.               $option $widget->getOptionAffichage(); 
  306.               $nb $widget->getNbPost();
  307.               $resultat = [];
  308.               $posts $this->em->getRepository(Post::class)->findPostActifsHomeByType($type,$option,$nb,$widget->getAllArticle());
  309.               if ($posts) {
  310.                   foreach ($posts as $value) {
  311.                       $resultat[] = ['image'=> $value->getImage(),
  312.                                     'imagealt'=> $value->translate($_locale)->getImageAlt(),
  313.                                     'summary'=> $value->translate($_locale)->getSummary(),
  314.                                     'slug'=> $value->translate($_locale)->getSlug(),
  315.                                     'titre'=> $value->translate($_locale)->getTitle(),
  316.                                     'title_affichage'=> $value->translate($_locale)->getTitleAffichage(),
  317.                                     'typelien'=> $value->translate($_locale)->getTypeLien(),
  318.                                     'titrelien'=> $value->translate($_locale)->getTitreLien(),
  319.                                     'telephone'=> $value->translate($_locale)->getTelephone(),
  320.                                     'lieninterne'=> $value->translate($_locale)->getLienInterne(),
  321.                                     'lienexterne'=> $value->translate($_locale)->getLienExterne(),
  322.                                     'content'=> $value->translate($_locale)->getContent(),
  323.                                     ];
  324.                   }
  325.               }
  326.              
  327.               if ($widget->getWidgetModelId() == 1) {
  328.                   $affiche $widget->getTypeAffichageHome();
  329.                   
  330.                   $template $this->templating->render('front/'.$this->theme.'/modele/bloc_articles.html.twig',[
  331.                     'resultats'=> $resultat,
  332.                     'widget'=> $widget
  333.                     'parametre' => $parametre,
  334.                     'template' =>$affiche
  335.                   ]);
  336.               }elseif($widget->getWidgetModelId() == 18){
  337.                   $template $this->templating->render('front/'.$this->theme.'/modele/bloc_associes.html.twig',[
  338.                     'widget'=> $widget
  339.                     'resultat'=> $resultat,
  340.                     'widget'=> $widget,      
  341.                   ]);
  342.               }elseif($widget->getWidgetModelId() == 19){
  343.                   $template $this->templating->render('front/'.$this->theme.'/modele/bloc_faqs_random.html.twig',[
  344.                     'widget'=> $widget
  345.                     'resultat'=> $resultat,
  346.                     'widget'=> $widget,      
  347.                   ]);
  348.               }
  349.            
  350.             }elseif ($singel_post === true &&  $widget->getWidgetModelId() == 2) {
  351.               $template $this->templating->render('front/'.$this->theme.'/modele/parallax.html.twig',[
  352.                 'widget'=> $widget,      
  353.               ]);
  354.             }elseif($singel_post === true &&  $widget->getWidgetModelId() == 3){
  355.                 $template $this->templating->render('front/'.$this->theme.'/modele/bloc_content.html.twig',[
  356.                   'widget'=> $widget,      
  357.                 ]);
  358.             }
  359.         }elseif($module == 'Faqs') {
  360.             $option $widget->getOptionAffichage(); 
  361.             $nb $widget->getNbPost();
  362.             $id_module $widget->getIdModule();
  363.             if ($singel_post) {
  364.               $faqs $this->em->getRepository(Faqs::class)->findFaqsByWidget($option,$nb,$id_module);
  365.             }else{
  366.               $faqs $this->em->getRepository(Faqs::class)->findFaqsByWidget($option,$nb,null,$widget->getAllArticle());
  367.             }
  368.             if ($faqs && $schemaOrg != null) {
  369.                 $schemaOrg_faq_items = [];
  370.                 foreach ($faqs as $value) {
  371.                   $schemaOrg_faq_items[] = Schema::Question()
  372.                                                 ->name($value->getQuestion())
  373.                                                 ->acceptedAnswer(Schema::Answer()->text(str_replace("\"""'"$value->getReponse())));
  374.                 }
  375.                 if (!empty($schemaOrg_faq_items)) {
  376.                     $schemaOrg->add(Schema::FAQPage()->mainEntity($schemaOrg_faq_items));
  377.                 }  
  378.             }
  379.             if ($widget->getWidgetModelId() == 15) {
  380.               $template $this->templating->render('front/'.$this->theme.'/modele/bloc_questions_reponse_carousel.html.twig',[
  381.                 'faqs'=> $faqs,
  382.                 'widget'=> $widget
  383.               ]);
  384.             }elseif ($widget->getWidgetModelId() == 17) {
  385.               $template $this->templating->render('front/'.$this->theme.'/modele/bloc_questions_reponse_collapse.html.twig',[
  386.                 'faqs'=> $faqs,
  387.                 'widget'=> $widget
  388.               ]);
  389.             }
  390.         }elseif($module == 'Videos'){
  391.             $option $widget->getOptionAffichage(); 
  392.             $nb $widget->getNbPost();
  393.             $id_module $widget->getIdModule();
  394.             if ($singel_post) {
  395.               $videos $this->em->getRepository(Videos::class)->findVideosByWidget($option,$nb,$id_module);
  396.             }else{
  397.               $videos $this->em->getRepository(Videos::class)->findVideosByWidget($option,$nb,null,$widget->getAllArticle());
  398.             }
  399.             $key_api_youtube 'AIzaSyCEZzFBZ_SGHsZgPrBgq4wYhx2YlgvHpUA';
  400.             if ($videos && $schemaOrg != null) {
  401.                 $list_items_video = [];
  402.                 $position_items_video 1;
  403.                 foreach ($videos as $value) {
  404.                   preg_match("/.*v=(.*)/"$value->getVideo(),$match);
  405.                   if (isset($match[1])) {
  406.                        $id_video $match[1];
  407.                        try {
  408.                         $response_api_youtube $this->client->request(
  409.                             'GET',
  410.                             'https://www.googleapis.com/youtube/v3/videos?id='.$id_video.'&key='.$key_api_youtube.'&part=snippet,statistics&fields=items(id,snippet,statistics)'
  411.                         );
  412.                         
  413.                         if ($response_api_youtube->getStatusCode() == 200) {
  414.                                 $content $response_api_youtube->toArray();
  415.                                 $data_video $content['items'][0]['snippet'];
  416.                                 $list_items_video[] = Schema::ListItem()
  417.                                                     ->position($position_items_video)
  418.                                                     ->item(Schema::VideoObject()
  419.                                                             ->name($data_video['title'])
  420.                                                             ->description($data_video['description'])
  421.                                                             ->thumbnailUrl($data_video['thumbnails']['default']['url'])
  422.                                                             ->uploadDate($data_video['publishedAt'])
  423.                                                             ->embedUrl("https://www.youtube.com/embed/".$id_video)
  424.                                                             ->contentUrl("https://www.youtube.com/watch?v=".$id_video));
  425.                                 $position_items_video++;
  426.                             }
  427.                         } catch (\Throwable $th) {
  428.                         }
  429.                   }
  430.                 }
  431.                 if (!empty($list_items_video)) {
  432.                    $schemaOrg->add(Schema::ItemList()->itemListElement($list_items_video));
  433.                 }  
  434.             }
  435.             $template $this->templating->render('front/'.$this->theme.'/modele/bloc_videos.html.twig',[
  436.               'videos'=> $videos,
  437.               'widget'=> $widget
  438.             ]);
  439.         }
  440.         return $template;
  441.     }
  442. }