<?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\Product;
use App\Entity\MediaCms;
use App\Entity\PostBloc;
use App\Entity\BlocService;
use App\Entity\CpPostField;
use App\Entity\FormsFields;
use App\Entity\Partenaires;
use App\Entity\CategoryBloc;
use App\Entity\MediaGallery;
use App\Entity\Testimonials;
use Spatie\SchemaOrg\Schema;
use App\Entity\ParametrePost;
use App\Entity\ParametreSite;
use App\Entity\ProduitOption;
use App\Entity\GallerieBlocPost;
use App\Entity\CategoriesProduct;
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;
public function __construct(EntityManagerInterface $em,\Twig\Environment $templating,string $theme,
HttpClientInterface $client, RequestStack $requestStack)
{
$this->templating = $templating;
$this->em = $em;
$this->theme = $theme;
$this->client = $client;
$this->requestStack = $requestStack;
}
/**
* Retourne l'objet Post
*/
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){
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]);
$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(),
];
$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(),
];
}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]);
$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/liste-of-partenaire',
[ '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],
'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) {
$resultat[] = ['image'=> $value->getImage(),
'imagealt'=> $value->translate($_locale)->getImageAlt(),
'summary'=> $value->translate($_locale)->getSummary(),
'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(),
];
}
}
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,
'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 ($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,
]);
}
}elseif($module == 'Faqs') {
$option = $widget->getOptionAffichage();
$nb = $widget->getNbPost();
$id_module = $widget->getIdModule();
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(str_replace("\"", "'", $value->getReponse())));
}
if (!empty($schemaOrg_faq_items)) {
$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;
}
}