<?php
namespace App\Controller\Front;
use App\Entity\Menu;
use App\Entity\Post;
use App\Entity\UrlRef;
use App\Entity\UrlPost;
use App\Entity\Category;
use App\Entity\Language;
use App\Entity\MediaCms;
use App\Entity\Secteurs;
use App\Entity\TypePost;
use App\Entity\ListeMenu;
use App\Service\BuildTree;
use App\Entity\CpPostField;
use App\Entity\ModelGallerie;
use App\Entity\PostArchive;
use App\Service\RenderBloc;
use Spatie\SchemaOrg\Graph;
use App\Entity\ParametreRef;
use App\Entity\PostCategory;
use App\Service\MetaService;
use Spatie\SchemaOrg\Schema;
use App\Entity\ParametreSite;
use App\Entity\ReseauSociaux;
use App\Service\MetaClePageRef;
use App\Service\FunctionService;
use App\Service\MetaWithoutCategoryService;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use Leogout\Bundle\SeoBundle\Seo\Og\OgSeoGenerator;
use Symfony\Component\String\Slugger\SluggerInterface;
use Leogout\Bundle\SeoBundle\Seo\Basic\BasicSeoGenerator;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class PageController extends AbstractController
{
public $basicSeo;
public $seo;
private $slugger;
private $projectDir;
private $metaService;
private $metaClePageRef;
private $renderBloc;
private $buildTree;
private $functionService;
private $metaPostService;
private $theme;
public function __construct(string $projectDir,string $theme,
BasicSeoGenerator $basicSeo,
OgSeoGenerator $seo,
RenderBloc $renderBloc,
SluggerInterface $slugger,
BuildTree $buildTree,
MetaService $metaService,
MetaWithoutCategoryService $metaPostService,
MetaClePageRef $metaClePageRef,
FunctionService $functionService){
$this->metaService = $metaService;
$this->metaPostService = $metaPostService;
$this->basicSeo = $basicSeo;
$this->seo = $seo;
$this->buildTree = $buildTree;
$this->slugger = $slugger;
$this->metaClePageRef = $metaClePageRef;
$this->functionService = $functionService;
$this->renderBloc = $renderBloc;
$this->projectDir = $projectDir;
$this->theme = $theme;
}
/**
* @Route("/{slug}/{page<[1-9]\d*>}", name="page")
*/
public function Page(Request $request,$slug,$page = 1)
{
$slug_info = $this->getDoctrine()->getRepository(UrlPost::class)->findUrlBySlug($slug);
if(!$slug_info){
throw new NotFoundHttpException('Sorry not existing!');
}
if($slug_info->getType() === "Post"){
$this->MetaModulePostOrCategory($request,$slug,$slug_info);
$render = $this->pagePost($request,$slug);
}elseif ($slug_info->getType() === "Categorie"){
$this->MetaModulePostOrCategory($request,$slug,$slug_info);
$render = $this->pageCatgorie($request,$slug,$page);
}
return $render;
}
public function pagePost($request,$slug){
$schemaOrg = new Graph();
$_locale = $request->getLocale();
$post = $this->getDoctrine()->getRepository(Post::class)->findPostBySlug($slug);
$modele_galerie_choisie = $post->getModeleGallerie() ? $post->getModeleGallerie()->getId() : 1;
$modele_gallerie = $this->getDoctrine()->getRepository(ModelGallerie::class)->findById($modele_galerie_choisie);
$baseurl = $request->getScheme() . '://' . $request->getHttpHost() . $request->getBasePath();
$seo_image = '';
$url = $this->generateUrl('page', ['slug'=> $post->translate($_locale)->getSlug()], UrlGeneratorInterface::ABSOLUTE_URL);
if ($this->functionService->parametreGenerale()) {
if ($this->functionService->parametreGenerale()->getApercuImageLien()) {
$seo_image = $baseurl.'/public/uploads/images/'.$this->functionService->parametreGenerale()->getApercuImageLien();
}
}
$galleries_images = [];
$bloc_post = $this->renderBloc->RenderBloc($post,$schemaOrg);
$module = $post->getTypePost()->getSystemName();
$galleries = $this->getDoctrine()->getRepository(MediaCms::class)->findFileByModule($module,$post->getId(),'media');
if($galleries){
foreach ($galleries as $value) {
$extension_file = pathinfo($value->getFileName(), PATHINFO_EXTENSION);
$new_galleries_images = ['id'=> $value->getId(),
'path'=> '',
'lienexterne' => $value->getLienExterne(),
'titre'=> $value->translate($_locale)->getTitre(),
'description'=> $value->translate($_locale)->getDescription(),
'extension_file' => $extension_file
];
if($extension_file != 'mp4'){
$new_galleries_images['path'] = 'storage/galleries/'.$value->getFileName();
}else{
$new_galleries_images['path'] = 'uploads/media/'.$value->getFileName();
}
$galleries_images[] = $new_galleries_images;
}
}
$docs = $this->getDoctrine()->getRepository(MediaCms::class)->findFileByModule($module,$post->getId(),'doc');
$ListId = [];
if($post->getPostCategory()){
foreach($post->getPostCategory() as $value){
$ListId[]= $value->getCategory()->getId();
}
}
$categorieTree = null;
if (!empty($ListId)) {
$categories = $this->getDoctrine()->getRepository(Category::class)->findCategoriePost($ListId);
$categorieTree = $this->functionService->buildTree($categories);
}
$meta_robots = $post->getMetaRobots();
$custom_schemaOrg = null;
if ($post->translate($_locale)->getSchema() != '') {
$custom_schemaOrg = $post->translate($_locale)->getSchema();
}
$breadcrumb_item = 1;
$schema_breadcrumb_items = [];
$schema_breadcrumb_items[] = Schema::ListItem()
->position(1)
->item(["@id"=> $this->generateUrl('index_home', [], UrlGeneratorInterface::ABSOLUTE_URL),"name"=>"Accueil"]);
if($categorieTree){
foreach ($categorieTree as $value) {
$breadcrumb_item = $breadcrumb_item + 1;
$schema_breadcrumb_items[] = Schema::ListItem()
->position($breadcrumb_item)
->item(["@id"=> $this->generateUrl('page', ['slug'=> $value['slug']], UrlGeneratorInterface::ABSOLUTE_URL),"name"=>$value['titre']]);
}
}
$schema_breadcrumb_items[] = Schema::ListItem()
->position($breadcrumb_item + 1)
->item(["@id"=> $this->generateUrl('page', ['slug'=> $post->translate($_locale)->getSlug()], UrlGeneratorInterface::ABSOLUTE_URL),"name"=>$post->translate($_locale)->getTitle()]);
if ($schema_breadcrumb_items) {
$schemaOrg->add(Schema::BreadcrumBList()->itemListElement($schema_breadcrumb_items));
}
$schemaOrg->add(Schema::Article()->headline($post->translate($_locale)->getTitle())
->datePublished($post->getDatePublication()?$post->getDatePublication():$post->getCreatedAt())
->dateModified($post->getUpdatedAt())
->image($post->getImage()!= null?$baseurl.$post->getImage():'')
->publisher(Schema::Organization()
->name($this->functionService->parametreGenerale()?$this->functionService->parametreGenerale()->translate($_locale)->getTitre():'')
->logo($this->functionService->parametreGenerale()?$baseurl.'/public/uploads/images/'.$this->functionService->parametreGenerale()->getImage():''))
->mainEntityOfPage(Schema::WebPage()->url($url))
);
$url_social = [];
$social = $this->getDoctrine()->getRepository(ReseauSociaux::class)->findAll();
if($social){
foreach ($social as $value) {
$url_social[] = $value->getUrl();
}
}
// id categorie related with post
$categoiesPosts = $this->getDoctrine()->getRepository(PostCategory::class)->findCategorieByPost($post->getId());
$next_article = $this->getDoctrine()->getRepository(Post::class)->findPostPrevOrNext($categoiesPosts,$post->getId());
$prev_article = $this->getDoctrine()->getRepository(Post::class)->findPostPrevOrNext($categoiesPosts,$post->getId(),'prev');
$template_model_galerie = $modele_gallerie[0]->getTwig();
$parametre = $this->getDoctrine()->getRepository(ParametreSite::class)->findOneBy(['Site'=>'1']);
if($post->getTemplateCms()){
$template = $this->render('front/'.$this->theme.'/dynamique-page-cms.html.twig',[
'template'=> $post->getTemplateCms()->getTwig(),
'post'=> $post,
'galleries'=> $galleries_images,
'bloc_post'=> $bloc_post,
'categorieTree'=> $categorieTree,
'meta_robots'=> $meta_robots,
'schemaOrg'=> $schemaOrg,
'custom_schemaOrg'=> $custom_schemaOrg,
'parametre'=> $parametre,
'next_article'=> $next_article,
'prev_article'=> $prev_article,
'modele_galerie' => $modele_gallerie,
'template_model_gallerie' => $template_model_galerie,
]);
}else{
// Champs personnalisés
$valsChamp = [];
$labelsChamp = [];
$vCpFields = $this->getDoctrine()->getRepository(CpPostField::class)->findBy(['IdPost'=>$post->getId()]);
$labelFiels = "";
if($vCpFields){
foreach ($vCpFields as $vCpField) {
$labelFiels = $vCpField->getCpField()->translate($_locale)->getLabel();
$TypeFiels = $vCpField->getCpField()->translate($_locale)->getType();
//dump($TypeFiels);
$labelsChamp += [
$vCpField->getID()=> $labelFiels
];
if($vCpField){
$valChamp = $vCpField->getLinkValue() ? $vCpField->getLinkValue() : $vCpField->getTextareaValue();
$valsChamp += [
$vCpField->getID()=> $valChamp
];
}
}
}
$template = $this->render('front/'.$this->theme.'/page_cms.html.twig',[
'post'=> $post,
'galleries'=> $galleries_images,
'docs'=> $docs,
'blocs'=> $bloc_post,
'categorieTree'=> $categorieTree,
'meta_robots'=> $meta_robots,
'schemaOrg'=> $schemaOrg,
'custom_schemaOrg'=> $custom_schemaOrg,
'champsPersonnalises' => $valsChamp,
'lblchampsPersonnalises' => $labelsChamp,
'parametre'=> $parametre,
'next_article'=> $next_article,
'prev_article'=> $prev_article,
'modele_galerie' => $modele_gallerie,
'template_model_gallerie' => $template_model_galerie,
]);
}
return $template;
}
public function pageCatgorie($request,$slug,$page){
$module = 'category';
$resultats = [];
$schemaOrg = new Graph();
$_locale = $request->getLocale();
$categorie = $this->getDoctrine()->getRepository(Category::class)->findCategoryBySlug($slug);
$sousCategorie = $this->getDoctrine()->getRepository(Category::class)->findBy(['parent'=> $categorie->getId()],['order_by'=>'ASC']);
$template = $categorie->getTypeAffichageHome();
$bloc_categorie = $this->renderBloc->RenderBloc($categorie,$schemaOrg,'categorie');
// Galleries
$galleries_images = [];
$modele_galerie_choisie = $categorie->getModeleGallerie() ? $categorie->getModeleGallerie()->getId() : 1;
$modele_gallerie = $this->getDoctrine()->getRepository(ModelGallerie::class)->findById($modele_galerie_choisie);
$galleries = $this->getDoctrine()->getRepository(MediaCms::class)->findFileByModule($module,$categorie->getId(),'media');
if($galleries){
foreach ($galleries as $value) {
$extension_file = pathinfo($value->getFileName(), PATHINFO_EXTENSION);
$new_galleries_images = ['id'=> $value->getId(),
'path'=> '',
'lienexterne' => $value->getLienExterne(),
'titre'=> $value->translate($_locale)->getTitre(),
'description'=> $value->translate($_locale)->getDescription(),
'extension_file' => $extension_file
];
if($extension_file != 'mp4'){
$new_galleries_images['path'] = 'storage/galleries/'.$value->getFileName();
}else{
$new_galleries_images['path'] = 'uploads/media/'.$value->getFileName();
}
$galleries_images[] = $new_galleries_images;
}
}
$template_model_galerie = $modele_gallerie[0]->getTwig();
if ($categorie->getPagination() == true) {
$posts = $this->getDoctrine()->getRepository(Post::class)->findPostByCategory($categorie->getId(),$categorie->getPagination(),$page,$categorie->getPageSize());
if($posts->getResults()){
foreach ($posts->getResults() as $value) {
$resultats[] = [ 'id'=> $value->getId(),
'slug'=> $value->getSlug(),
'titre'=> $value->getTitle(),
'title_affichage'=> $value->getTitleAffichage(),
'content'=> $value->getContent(),
'chapeau'=> $value->getSummary(),
'image'=> $value->getImage(),
'typelien'=> $value->getTypeLien(),
'titrelien'=> $value->getTitreLien(),
'telephone'=> $value->getTelephone(),
'lieninterne'=> $value->getLienInterne(),
'lienexterne'=> $value->getLienExterne(),
];
}
}
}else{
$posts = $this->getDoctrine()->getRepository(Post::class)->findPostByCategory($categorie->getId(),false,$page,$categorie->getPageSize());
if($posts){
foreach ($posts as $value) {
// Champs personnalisés
$valsChamp = [];
$vCpFields = $this->getDoctrine()->getRepository(CpPostField::class)->findBy(['IdPost'=>$value->getId()]);
if($vCpFields){
foreach ($vCpFields as $vCpField) {
if($vCpField){
$valChamp = $vCpField->getLinkValue() ? $vCpField->getLinkValue() : $vCpField->getTextareaValue();
$valsChamp += [
$vCpField->getID()=> $valChamp
];
}
}
}
$resultats[] = [ 'id'=> $value->getId(),
'slug'=> $value->getSlug(),
'titre'=> $value->getTitle(),
'title_affichage'=> $value->getTitleAffichage(),
'content'=> $value->getContent(),
'chapeau'=> $value->getSummary(),
'image'=> $value->getImage(),
'typelien'=> $value->getTypeLien(),
'titrelien'=> $value->getTitreLien(),
'telephone'=> $value->getTelephone(),
'lieninterne'=> $value->getLienInterne(),
'lienexterne'=> $value->getLienExterne(),
'champsPersonnalises' => $valsChamp,
];
}
}
}
$meta_robots = $categorie->getMetaRobots();
$schema_breadcrumb_items = [];
$schema_breadcrumb_items[] = Schema::ListItem()
->position(1)
->item(["@id"=> $this->generateUrl('index_home', [], UrlGeneratorInterface::ABSOLUTE_URL),"name"=>"Accueil"]);
$schema_breadcrumb_items[] = Schema::ListItem()
->position(2)
->item(["@id"=> $this->generateUrl('page', ['slug'=> $categorie->translate($_locale)->getSlugUrl()], UrlGeneratorInterface::ABSOLUTE_URL),"name"=>$categorie->translate($_locale)->getTitreCategorie()]);
if ($schema_breadcrumb_items) {
$schemaOrg->add(Schema::BreadcrumBList()->itemListElement($schema_breadcrumb_items));
}
$baseurl = $request->getScheme() . '://' . $request->getHttpHost() . $request->getBasePath();
$schemaOrg->add(Schema::Article()
->headline($categorie->translate($_locale)->getTitreCategorie())
->datePublished($categorie->getCreatedAt())
->dateModified($categorie->getUpdatedAt())
->image($categorie->getImage()!= null?$baseurl.$categorie->getImage():'')
->publisher(Schema::Organization()
->name($this->functionService->parametreGenerale()?$this->functionService->parametreGenerale()->translate($_locale)->getTitre():'')
->logo($this->functionService->parametreGenerale()?$baseurl.'/public/uploads/images/'.$this->functionService->parametreGenerale()->getImage():''))
->mainEntityOfPage(Schema::WebPage()->url($this->generateUrl('page', ['slug'=> $categorie->translate($_locale)->getSlugUrl()], UrlGeneratorInterface::ABSOLUTE_URL)))
);
$module = $categorie->getTypePost()->translate($_locale)->getSystemName();
$galleries = $this->getDoctrine()->getRepository(MediaCms::class)->findFileByModule($module,$categorie->getId(),'media');
$docs = $this->getDoctrine()->getRepository(MediaCms::class)->findFileByModule($module,$categorie->getId(),'doc');
$parametre = $this->functionService->parametreGenerale();
return $this->render('front/'.$this->theme.'/page_categorie.html.twig',[
'template'=> $template,
'categorie'=> $categorie,
'resultats'=> $resultats,
'blocs'=> $bloc_categorie,
'posts'=> $posts,
'meta_robots'=> $meta_robots,
'pagination'=> $categorie->getPagination(),
'schemaOrg'=> $schemaOrg,
'galleries'=> $galleries_images,
'docs'=> $docs,
'parametre'=> $parametre,
'modele_galerie' => $modele_gallerie,
'template_model_gallerie' => $template_model_galerie,
]);
}
function MetaModulePostOrCategory($request,$slug,$slug_info){
$baseurl = $request->getScheme() . '://' . $request->getHttpHost() . $request->getBasePath();
$url = $this->generateUrl('page', ['slug'=> $slug], UrlGeneratorInterface::ABSOLUTE_URL);
$metaService = $this->metaService->meta($slug,$slug_info->getType());
$title_seo = isset($metaService['title'])?$metaService['title']:null;
$description_seo = isset($metaService['description'])?$metaService['description']:null;
$metacle = isset($metaService['metacle'])?$metaService['metacle']:null;
if($title_seo != '' || $description_seo != ''){
$this->basicSeo->setTitle($title_seo)
->setDescription($description_seo)
->setkeywords($metacle);
$this->seo->setTitle($title_seo)
->setDescription($description_seo)
->setUrl($url);
if ($this->functionService->parametreGenerale()) {
if ($this->functionService->parametreGenerale()->getApercuImageLien()) {
$this->seo->setImage($baseurl.'/public/uploads/images/'.$this->functionService->parametreGenerale()->getApercuImageLien());
}
}
}
}
}