<?php
namespace App\Service;
use App\Entity\Faqs;
use App\Entity\Post;
use App\Entity\Forms;
use App\Entity\Videos;
use App\Entity\CpField;
use App\Entity\PostFaq;
use App\Entity\Product;
use App\Entity\MediaCms;
use App\Entity\PostBloc;
use App\Entity\Secteurs;
use App\Entity\Activites;
use App\Entity\BlocService;
use App\Entity\CategoryFaq;
use App\Entity\CpPostField;
use App\Entity\FormsFields;
use App\Entity\Partenaires;
use App\Entity\CategoryBloc;
use App\Entity\MediaGallery;
use App\Entity\ParametreRef;
use App\Entity\PostCategory;
use App\Entity\Testimonials;
use Spatie\SchemaOrg\Schema;
use App\Entity\ModelGallerie;
use App\Entity\ParametrePost;
use App\Entity\ParametreSite;
use App\Entity\ProduitOption;
use App\Service\ImageService;
use App\Entity\GallerieBlocPost;
use App\Entity\CategoriesProduct;
use App\Entity\ActiviteSecondaire;
use Symfony\Component\Intl\Locale;
use App\Entity\ParametrePartenaires;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class RenderBloc
{
private $em;
private $templating;
private $theme;
private $client;
private $requestStack;
private $imageService;
private $affichageGallerieService;
private $functionService;
private $bloc_service_renderd;
public function __construct(EntityManagerInterface $em,\Twig\Environment $templating,string $theme,
HttpClientInterface $client, RequestStack $requestStack,ImageService $imageService,
AffichageGallerieService $affichageGallerieService,FunctionService $functionService,$bloc_service_renderd = false)
{
$this->templating = $templating;
$this->em = $em;
$this->theme = $theme;
$this->client = $client;
$this->requestStack = $requestStack;
$this->imageService = $imageService;
$this->affichageGallerieService = $affichageGallerieService;
$this->functionService = $functionService;
$this->bloc_service_renderd = $bloc_service_renderd;
}
public function RenderBloc($post,$schemaOrg,$type = 'post') {
$_locale = Locale::getDefault();
$data_post = [];
$data_Bloc = [];
$blocs = null;
if($type == 'post'){
if ($post != null) {
$blocs = $this->em->getRepository(PostBloc::class)->findBlocNotDeleted($post->getId());
}
}else{
if($post != null){
$blocs = $this->em->getRepository(CategoryBloc::class)->findBlocNotDeleted($post->getId());
}
}
if($blocs != null){
array_filter($blocs, function($blocItem) {
if ($blocItem->getTypeBloc() == '11' && $blocItem->getActif() == true) {
$bloc_service_existe = $this->em->getRepository(BlocService::class)->findOneBy(['actif'=> true,'id'=> $blocItem->getIdBlocService()]);
if ($bloc_service_existe && $bloc_service_existe->getAlias() == 'bloc_travaux') {
$this->bloc_service_renderd = true;
}
}
});
foreach($blocs as $bloc){
$data = [];
if($bloc->getTypeBloc() == '5' && $bloc->getActif() == true){
$Media = $this->em->getRepository(MediaGallery::class)->findGalleryActif($bloc->getMediaGallery());
$data_gallerie = [];
if($Media){
$media_cms = $this->em->getRepository(MediaCms::class)->findBy(['module'=> 'gallerie','module_id'=>$Media->getId()],['position'=> 'ASC']);
foreach($media_cms as $gallerie){
$data_gallerie[] = [
'id'=> $gallerie->getId(),
'position'=> $gallerie->getPosition(),
'path'=> $gallerie->getLocation().$gallerie->getPath().$gallerie->getFileName(),
'title'=> $gallerie->translate($_locale)->gettitre(),
'fileExtension' => $gallerie->getType(),
'description'=> $gallerie->translate($_locale)->getDescription(),
'description2'=> $gallerie->translate($_locale)->getDescription2(),
];
}
$data = [
'id' => $Media->getId(),
'actif'=> $Media->getActif(),
'type' => 'slider',
'title'=> $Media->translate($_locale)->getTitre(),
'description'=> $Media->translate($_locale)->getDescription(),
'images'=> $data_gallerie,
'mobile'=> $bloc->getActifMobile(),
'desktop'=> $bloc->getActifDesktop(),
];
}
}elseif($bloc->getTypeBloc() == '6' && $bloc->getActif() == true){
$form = $this->em->getRepository(Forms::class)->findOneBy(['id'=>$bloc->getForm()]);
$formField = $this->em->getRepository(FormsFields::class)->findOneBy(['form'=>$form,'local'=>$_locale]);
// update nbr visit
$form->setNbrVisit($form->getNbrVisit() + 1);
$this->em->persist($form);
$this->em->flush();
$data_Form = [
'id'=> $form->getId(),
'name'=> $form->getName(),
'methode'=> $form->getMethode(),
'success_message'=> $form->getSuccessMessage(),
'identifier'=> $form->getIdentifier(),
'type_form'=> $form->getFormBuilder(),
'data'=> $formField?unserialize($formField->getData()):null,
'data_form_step'=> $formField?unserialize($formField->getDataFormMutiStep()):null,
'titre'=> $form->getTitreContent(),
'texte'=> $form->getTexteContent(),
'image'=> $form->getImage(),
'alt_image'=> $form->getAltImage(),
];
$data = [ 'id' => $bloc->getId(),
'actif' => $bloc->getActif(),
'type' => $bloc->getTypeBloc() == 6 ?'form':$bloc->getTypeBloc(),
'title' => $bloc->translate($_locale)->getTitle(),
'content' => $bloc->translate($_locale)->getContent(),
'content2' => $bloc->translate($_locale)->getContent2(),
'link'=> $bloc->translate($_locale)->getLink(),
'form' => $data_Form,
'mobile'=> $bloc->getActifMobile(),
'desktop'=> $bloc->getActifDesktop(),
];
}elseif($bloc->getTypeBloc() == '7' && $bloc->getActif() == true){
$data = [ 'id' => $bloc->getId(),
'actif' => $bloc->getActif(),
'type' => 'template',
'template' => $bloc->translate($_locale)->getTemplate(),
'mobile'=> $bloc->getActifMobile(),
'desktop'=> $bloc->getActifDesktop(),
];
}elseif($bloc->getTypeBloc() == '8' && $bloc->getActif() == true){
$template = $this->renderTemplate($bloc,$schemaOrg);
$data = [ 'id' => $bloc->getId(),
'actif' => $bloc->getActif(),
'type' => 'template',
'template' => $template,
'mobile'=> $bloc->getActifMobile(),
'desktop'=> $bloc->getActifDesktop(),
];
}elseif($bloc->getTypeBloc() == '9' && $bloc->getActif() == true){
$data = [ 'id' => $bloc->getId(),
'actif' => $bloc->getActif(),
'type' => 'video',
'url_video' => $bloc->getVideoUrl(),
'mobile'=> $bloc->getActifMobile(),
'desktop'=> $bloc->getActifDesktop(),
];
}elseif($bloc->getTypeBloc() == '3' && $bloc->getActif() == true){
$data = [ 'id' => $bloc->getId(),
'actif' => $bloc->getActif(),
'type' => 'image',
'title' => $bloc->translate($_locale)->getTitle(),
'bgimage' => $bloc->getImage(),
'mobile'=> $bloc->getActifMobile(),
'desktop'=> $bloc->getActifDesktop(),
];
}elseif($bloc->getTypeBloc() == '4' && $bloc->getActif() == true){
$data = [ 'id' => $bloc->getId(),
'actif' => $bloc->getActif(),
'type' => 'titre',
'title' => $bloc->translate($_locale)->getContent2(),
'mobile'=> $bloc->getActifMobile(),
'desktop'=> $bloc->getActifDesktop(),
];
}elseif($bloc->getTypeBloc() == '1' && $bloc->getActif() == true){
$data = [ 'id' => $bloc->getId(),
'actif' => $bloc->getActif(),
'type' => 'text',
'bg_color'=> $bloc->getBgColor(),
'title' => $bloc->translate($_locale)->getTitle(),
'content' => $bloc->translate($_locale)->getContent(),
'libelle_lien'=> $bloc->translate($_locale)->getTitreLien(),
'type_lien'=> $bloc->translate($_locale)->getTypeLien(),
'lien_interne'=> $bloc->translate($_locale)->getLienInterne(),
'lien_externe'=> $bloc->translate($_locale)->getLienExterne(),
'telephone'=> $bloc->translate($_locale)->getTelephone(),
'mobile'=> $bloc->getActifMobile(),
'desktop'=> $bloc->getActifDesktop(),
];
}elseif($bloc->getTypeBloc() == '10' && $bloc->getActif() == true){
$data = [ 'id' => $bloc->getId(),
'actif' => $bloc->getActif(),
'type' => 'text2',
'title' => $bloc->translate($_locale)->getTitle(),
'content' => $bloc->translate($_locale)->getContent(),
'position'=> $bloc->getPositionText(),
'image'=> $bloc->getImage(),
'url_video' => $bloc->getVideoUrl(),
'grid'=> $bloc->getGridCarousel(),
'mobile'=> $bloc->getActifMobile(),
'desktop'=> $bloc->getActifDesktop(),
];
}elseif($bloc->getTypeBloc() == '11' && $bloc->getActif() == true){
$bloc_service = $this->em->getRepository(BlocService::class)->findOneBy(['actif'=> true,'id'=>$bloc->getIdBlocService()]);
$data = [ 'id' => $bloc->getId(),
'actif' => $bloc->getActif(),
'type' => 'bloc-service',
'data' => $bloc_service,
'mobile'=> $bloc->getActifMobile(),
'desktop'=> $bloc->getActifDesktop(),
];
// $this->bloc_service_renderd = true;
}elseif($bloc->getTypeBloc() == '12' && $bloc->getActif() == true){
// $parametreSite = $this->em->getRepository(ParametreSite::class)->findOneBy([],['id'=> 'ASC']);
$param_partenaire = $this->em->getRepository(ParametrePartenaires::class)->findOneBy(['actif'=> true]);
$liste_partenaire = $this->em->getRepository(Partenaires::class)->findBy(['actif'=> true],null,10);
// $client = HttpClient::create();
$request = $this->requestStack->getCurrentRequest();
$baseurl = $request->getScheme() . '://' . $request->getHttpHost() . $request->getBasePath();
$partenaires = null;
// try {
// $response_api = $client->request('GET',
// $parametreSite->getDomainApiHrz().'/api/custom/all-partners-domain',
// [ 'headers' => ['Accept' => 'application/json'],
// 'query' => [
// 'domain' => $baseurl,
// 'token' => $parametreSite->getTokenApiHrz(),
// ],
// ]);
// if ($response_api->getStatusCode() === 200) {
// $partenaires = $response_api->toArray()['data'];
// }
// } catch (\Throwable $th) {
// //throw $th;
// //dump($th);
// }
$data = [ 'id' => $bloc->getId(),
'actif' => $bloc->getActif(),
'type' => 'bloc-partenaire',
'data' => ['parametre'=> $param_partenaire,'liste_partenaire'=> $liste_partenaire,'partenaires'=> $partenaires,'baseurl' => $baseurl],
'mobile'=> $bloc->getActifMobile(),
'desktop'=> $bloc->getActifDesktop(),
];
}elseif($bloc->getTypeBloc() == '13' && $bloc->getActif() == true){
$activite_principal = $this->em->getRepository(Activites::class)->findBy(['actif'=> true,'afficher_home'=> true]);
$parametre = $this->em->getRepository(ParametreSite::class)->findOneBy([],['id'=> 'DESC']);
$parametreRef = $this->em->getRepository(ParametreRef::class)->findOneBy(['alias'=> 'param_generale']);
$data = [ 'id' => $bloc->getId(),
'actif'=> $bloc->getActif(),
'type'=> 'bloc-activite',
'type_affichage'=> $bloc->getAffichageBloc(),
'activite_principal'=> $activite_principal,
'parametreRef'=> $parametreRef,
'parametre'=> $parametre,
'mobile'=> $bloc->getActifMobile(),
'desktop'=> $bloc->getActifDesktop()];
}elseif($bloc->getTypeBloc() == '14' && $bloc->getActif() == true){
$secteurs = $this->em->getRepository(Secteurs::class)->findBy(['actif'=> true]);
$parametre = $this->em->getRepository(ParametreSite::class)->findOneBy([],['id'=> 'DESC']);
$parametreRef = $this->em->getRepository(ParametreRef::class)->findOneBy(['alias'=> 'param_generale']);
$data = [ 'id' => $bloc->getId(),
'actif'=> $bloc->getActif(),
'type'=> 'bloc-secteur',
'type_affichage'=> $bloc->getAffichageBloc(),
'secteurs'=> $secteurs,
'parametreRef'=> $parametreRef,
'parametre'=> $parametre,
'mobile'=> $bloc->getActifMobile(),
'desktop'=> $bloc->getActifDesktop()];
}elseif($bloc->getTypeBloc() == '15' && $bloc->getActif() == true){
$data_ref_page = null;
$ids_activite_secondaires = $this->em->getRepository(ActiviteSecondaire::class)->findIdsActiviteSecondaireActif();
if ($ids_activite_secondaires) {
$data_ref_page = $this->em->getRepository(Post::class)->findPageRefByIdsSousActivite($ids_activite_secondaires);
}
$parametre = $this->em->getRepository(ParametreSite::class)->findOneBy([],['id'=> 'DESC']);
$parametreRef = $this->em->getRepository(ParametreRef::class)->findOneBy(['alias'=> 'param_generale']);
$data = [ 'id' => $bloc->getId(),
'actif'=> $bloc->getActif(),
'type'=> 'bloc-sous-activite',
'type_affichage'=> $bloc->getAffichageBloc(),
'activite_secondaires'=> $data_ref_page,
'parametreRef'=> $parametreRef,
'parametre'=> $parametre,
'mobile'=> $bloc->getActifMobile(),
'desktop'=> $bloc->getActifDesktop()];
}elseif($bloc->getTypeBloc() == '16' && $bloc->getActif() == true){
$images = [];
$ids_images = explode(",", $bloc->getIdsImagesGaleries());
if ($ids_images) {
foreach ($ids_images as $id) {
if(!empty($id)){
$image = $this->em->getRepository(MediaCms::class)->find($id);
if ($image) {
$path = '/'.$image->getLocation().$image->getPath().$image->getFileName();
$this->imageService->thumbnail($path);
$images[] = ['id'=> $image->getId(),
'path'=> $path,
'lienexterne' => $image->getLienExterne(),
'titre'=> $image->translate($_locale)->getTitre(),
'description'=> $image->translate($_locale)->getDescription(),
'description2'=> $image->translate($_locale)->getDescription2(),
'extension_file' => pathinfo($image->getFileName(), PATHINFO_EXTENSION),
'filter'=> 'asset_image'
];
}
}
}
}
$template_model_galerie = null;
$modele_gallerie = $this->em->getRepository(ModelGallerie::class)->findModelGallerie($bloc->getModelAffichageGaleries());
if($modele_gallerie){
$template_model_galerie = $modele_gallerie['twig'];
}
$data = [ 'id' => $bloc->getId(),
'actif'=> $bloc->getActif(),
'type'=> 'bloc-gallerie',
'model_galerie'=> $template_model_galerie,
'galleries'=> $images,
'modele_galerie'=> $bloc->getModelAffichageGaleries(),
'mobile'=> $bloc->getActifMobile(),
'desktop'=> $bloc->getActifDesktop()];
}else{
if($bloc->getActif() == true){
$data = [ 'id' => $bloc->getId(),
'actif' => $bloc->getActif(),
'type' => $bloc->getTypeBloc(),
'title' => $bloc->translate($_locale)->getTitle(),
'content' => $bloc->translate($_locale)->getContent(),
'content2' => $bloc->translate($_locale)->getContent2(),
'link'=> $bloc->translate($_locale)->getLink(),
'mobile'=> $bloc->getActifMobile(),
'desktop'=> $bloc->getActifDesktop(),
];
}
}
if(!empty($data)){
$data_Bloc[] = $data;
}
}
}
if($post){
if ($type == 'post') {
$data_post = [
'id' => $post->getId(),
'actif'=> $post->getActif(),
'image'=> $post->getImage(),
'title'=> $post->translate($_locale)->getTitle(),
'slug'=> $post->translate($_locale)->getSlug(),
'summary'=> $post->translate($_locale)->getSummary(),
'content'=> $post->translate($_locale)->getContent(),
'MetaTitle'=> $post->translate($_locale)->getMetaTitle(),
'MetaDescription'=> $post->translate($_locale)->getMetaDescription(),
'MetaKeys'=> $post->translate($_locale)->getMetaKeys(),
'TagManagerhead'=> $post->getTagManagerhead(),
'TagManagerbody'=> $post->getTagManagerbody(),
'Bloc'=> $data_Bloc,
];
}else{
$data_post = [
'id' => $post->getId(),
'actif'=> $post->getActif(),
'image'=> $post->getImage(),
'title'=> $post->translate($_locale)->getTitreCategorie(),
'slug'=> $post->translate($_locale)->getSlugUrl(),
'summary'=> $post->translate($_locale)->getChapeauCategorie(),
'content'=> $post->translate($_locale)->getTexteCategorie(),
'MetaTitle'=> $post->translate($_locale)->getMetaTitle(),
'MetaDescription'=> $post->translate($_locale)->getMetaDescription(),
'MetaKeys'=> $post->translate($_locale)->getMetaKeywords(),
'TagManagerhead'=> $post->getTagManagerhead(),
'TagManagerbody'=> $post->getTagManagerbody(),
'Bloc'=> $data_Bloc,
];
}
}
return $data_post;
}
public function renderTemplate($bloc,$schemaOrg){
$_locale = Locale::getDefault();
$widget = $bloc->getWidget();
$singel_post = $widget->getSingelPost();
$module = $widget->getModules();
$template = null;
$parametre = $this->em->getRepository(ParametreSite::class)->findOneBy(['Site'=>'1']);
if ($module == 'Poste') {
if($singel_post === false){
$type = $widget->getTypePost();
$option = $widget->getOptionAffichage();
$nb = $widget->getNbPost();
$resultat = [];
$posts = $this->em->getRepository(Post::class)->findPostActifsHomeByType($type,$option,$nb,$widget->getAllArticle());
if ($posts) {
foreach ($posts as $value) {
$categorie_principal = $this->em->getRepository(PostCategory::class)->findOneBy(['Post'=> $value->getId()],['id'=> 'asc']);
$galleries_first_image = $this->affichageGallerieService->FirstGallerieImagePost($value);
// Champs personnalisés
$champsPersonnalisesPost = [];
$champsPersonnalisesPost = $this->functionService->getChampsPersonnalisesPost($value);
$resultat[] = ['image'=> $value->getImage(),
'imagealt'=> $value->translate($_locale)->getImageAlt(),
'image_couverture'=> $value->getImageCouverture(),
'summary'=> $value->translate($_locale)->getSummary(),
'slug_categorie_parent'=> $categorie_principal?$categorie_principal->getCategory()->translate($_locale)->getSlugUrl():null,
'page_produit'=> $type->translate($_locale)->getSystemName() == 'produits'?true:false,
'slug'=> $value->translate($_locale)->getSlug(),
'titre'=> $value->translate($_locale)->getTitle(),
'title_affichage'=> $value->translate($_locale)->getTitleAffichage(),
'typelien'=> $value->translate($_locale)->getTypeLien(),
'titrelien'=> $value->translate($_locale)->getTitreLien(),
'telephone'=> $value->translate($_locale)->getTelephone(),
'lieninterne'=> $value->translate($_locale)->getLienInterne(),
'lienexterne'=> $value->translate($_locale)->getLienExterne(),
'content'=> $value->translate($_locale)->getContent(),
'galleries_first_image'=> $galleries_first_image,
'champsPersonnalisesPost' => $champsPersonnalisesPost
];
}
}
if ($widget->getWidgetModelId() == 1) {
$affiche = $widget->getTypeAffichageHome();
$template = $this->templating->render('front/'.$this->theme.'/modele/bloc_articles.html.twig',[
'resultats'=> $resultat,
'widget'=> $widget,
'parametre' => $parametre,
'template' =>$affiche
]);
}elseif($widget->getWidgetModelId() == 18){
$template = $this->templating->render('front/'.$this->theme.'/modele/bloc_associes.html.twig',[
'widget'=> $widget,
'resultat'=> $resultat,
'parametre' => $parametre,
'widget'=> $widget,
]);
}elseif($widget->getWidgetModelId() == 19){
$template = $this->templating->render('front/'.$this->theme.'/modele/bloc_faqs_random.html.twig',[
'widget'=> $widget,
'resultat'=> $resultat,
'widget'=> $widget,
]);
}elseif ($widget->getWidgetModelId() == 20) {
$affiche = $widget->getTypeAffichageHome();
$template = $this->templating->render('front/'.$this->theme.'/modele/bloc_produits.html.twig',[
'widget'=> $widget,
'resultats'=> $resultat,
'widget'=> $widget,
'template' => $affiche
]);
}
}elseif ($singel_post === true && $widget->getWidgetModelId() == 2) {
$template = $this->templating->render('front/'.$this->theme.'/modele/parallax.html.twig',[
'widget'=> $widget,
]);
}elseif($singel_post === true && $widget->getWidgetModelId() == 3){
$template = $this->templating->render('front/'.$this->theme.'/modele/bloc_content.html.twig',[
'widget'=> $widget,
'bloc_service_existe'=> $this->bloc_service_renderd
]);
}elseif($singel_post === true && $widget->getWidgetModelId() == 4){
$template = $this->templating->render('front/'.$this->theme.'/modele/bloc_presentation.html.twig',[
'widget'=> $widget,
'bloc_service_existe'=> $this->bloc_service_renderd
]);
}elseif ($singel_post === true && $widget->getWidgetModelId() == 5) {
$template = $this->templating->render('front/'.$this->theme.'/modele/bandeau_image.html.twig',[
'widget'=> $widget,
]);
}
}elseif($module == 'Faqs') {
$option = $widget->getOptionAffichage();
$nb = $widget->getNbPost();
$id_module = $widget->getIdModule();
$faqs = [];
if($widget->getAlias() == "questions-reponses-automatiques"){
if ($bloc instanceof CategoryBloc) {
$CategoryFaq = $this->em->getRepository(CategoryFaq::class)->findFaqsAutomatiqueByWidget($option, $bloc->getCategory()->getId());
if($CategoryFaq){
foreach ($CategoryFaq as $CategoryFaqItem) {
if ($CategoryFaqItem->getFaq()->translate($_locale)->getActif()) {
$faqs[] = $CategoryFaqItem->getFaq();
}
}
}
}else{
$PostFaq = $this->em->getRepository(PostFaq::class)->findFaqsAutomatiqueByWidget($option, $bloc->getPost()->getId());
if($PostFaq){
foreach ($PostFaq as $PostFaqItem) {
if ($PostFaqItem->getFaq()->translate($_locale)->getActif()) {
$faqs[] = $PostFaqItem->getFaq();
}
}
}
}
}else{
if ($singel_post) {
$faqs = $this->em->getRepository(Faqs::class)->findFaqsByWidget($option,$nb,$id_module);
}else{
$faqs = $this->em->getRepository(Faqs::class)->findFaqsByWidget($option,$nb,null,$widget->getAllArticle());
}
}
if ($faqs && $schemaOrg != null) {
$schemaOrg_faq_items = [];
foreach ($faqs as $value) {
$schemaOrg_faq_items[] = Schema::Question()
->name($value->getQuestion())
->acceptedAnswer(Schema::Answer()->text(strip_tags(str_replace("\"", "'", $value->getReponse()))));
}
if (!empty($schemaOrg_faq_items) && !array_key_exists("Spatie\SchemaOrg\FAQPage", $schemaOrg->getNodes())) {
$schemaOrg->add(Schema::FAQPage()->mainEntity($schemaOrg_faq_items));
}
}
if ($widget->getWidgetModelId() == 15) {
$template = $this->templating->render('front/'.$this->theme.'/modele/bloc_questions_reponse_carousel.html.twig',[
'faqs'=> $faqs,
'widget'=> $widget,
]);
}elseif ($widget->getWidgetModelId() == 17) {
$template = $this->templating->render('front/'.$this->theme.'/modele/bloc_questions_reponse_collapse.html.twig',[
'faqs'=> $faqs,
'widget'=> $widget,
]);
}
}elseif($module == 'Videos'){
$option = $widget->getOptionAffichage();
$nb = $widget->getNbPost();
$id_module = $widget->getIdModule();
if ($singel_post) {
$videos = $this->em->getRepository(Videos::class)->findVideosByWidget($option,$nb,$id_module);
}else{
$videos = $this->em->getRepository(Videos::class)->findVideosByWidget($option,$nb,null,$widget->getAllArticle());
}
$key_api_youtube = 'AIzaSyCEZzFBZ_SGHsZgPrBgq4wYhx2YlgvHpUA';
if ($videos && $schemaOrg != null) {
$list_items_video = [];
$position_items_video = 1;
foreach ($videos as $value) {
preg_match("/.*v=(.*)/", $value->getVideo(),$match);
if (isset($match[1])) {
$id_video = $match[1];
try {
$response_api_youtube = $this->client->request(
'GET',
'https://www.googleapis.com/youtube/v3/videos?id='.$id_video.'&key='.$key_api_youtube.'&part=snippet,statistics&fields=items(id,snippet,statistics)'
);
if ($response_api_youtube->getStatusCode() == 200) {
$content = $response_api_youtube->toArray();
$data_video = $content['items'][0]['snippet'];
$list_items_video[] = Schema::ListItem()
->position($position_items_video)
->item(Schema::VideoObject()
->name($data_video['title'])
->description($data_video['description'])
->thumbnailUrl($data_video['thumbnails']['default']['url'])
->uploadDate($data_video['publishedAt'])
->embedUrl("https://www.youtube.com/embed/".$id_video)
->contentUrl("https://www.youtube.com/watch?v=".$id_video));
$position_items_video++;
}
} catch (\Throwable $th) {
}
}
}
if (!empty($list_items_video)) {
$schemaOrg->add(Schema::ItemList()->itemListElement($list_items_video));
}
}
$template = $this->templating->render('front/'.$this->theme.'/modele/bloc_videos.html.twig',[
'videos'=> $videos,
'widget'=> $widget,
]);
}
return $template;
}
}