<?php
namespace App\Controller\FrontEnd;
use App\Entity\Post;
use App\Entity\Contact;
use App\Entity\Category;
use App\Entity\MediaCms;
use Spatie\SchemaOrg\Graph;
use App\Service\CodeService;
use App\Service\MetaService;
use Spatie\SchemaOrg\Schema;
use App\Entity\ModelGallerie;
use App\Entity\ParametreSite;
use App\Entity\ReseauSociaux;
use App\Service\HelperService;
use App\Service\HrefLangService;
use App\Service\RenderBlocService;
use App\Utils\SiteParameterService;
use App\Service\ImageGalleryService;
use App\Service\NestedTreeStructure;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
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 HomeController extends AbstractController
{
public $basicSeo;
public $seo;
private $slugger;
private $projectDir;
private $metaService;
private $renderBloc;
private $buildTree;
private $helperService;
private $theme;
private $imageGalleryService;
private $siteParameterService;
private $em;
private $codeService;
private $hrefLang;
public function __construct(string $projectDir,string $theme,
BasicSeoGenerator $basicSeo,
SiteParameterService $siteParameterService,
OgSeoGenerator $seo,
RenderBlocService $renderBloc,
SluggerInterface $slugger,
NestedTreeStructure $buildTree,
MetaService $metaService,
HelperService $helperService,
ImageGalleryService $imageGalleryService,
EntityManagerInterface $em,
HrefLangService $hrefLang,
CodeService $codeService){
$this->em = $em;
$this->metaService = $metaService;
$this->basicSeo = $basicSeo;
$this->seo = $seo;
$this->hrefLang = $hrefLang;
$this->buildTree = $buildTree;
$this->slugger = $slugger;
$this->helperService = $helperService;
$this->renderBloc = $renderBloc;
$this->projectDir = $projectDir;
$this->theme = $theme;
$this->imageGalleryService = $imageGalleryService;
$this->codeService = $codeService;
$this->siteParameterService = $siteParameterService;
}
/**
* @Route("/{_locale}", name="index_page_home", defaults={"_locale": "fr"}, requirements={"_locale"="fr|en|de|es|cn|it"})
*/
public function index(Request $request)
{
$_locale = $request->getLocale();
// $this->siteParameterService->refreshParameters();
$parametre = $this->getDoctrine()->getRepository(ParametreSite::class)->findOneBy([],['id'=> 'ASC']);
$contacts = $this->em->getRepository(Contact::class)->getActifContacts();
$page_home = $this->em->getRepository(Post::class)->findOneBy(['alias'=> 'page-home']);
if(!$page_home){
throw new NotFoundHttpException('Sorry not existing!');
}
$this->metaService->metaTags($page_home);
$this->codeService->setCodeRobots($page_home->getMetaRobots()?? '');
$this->codeService->setCodeHead($page_home->getTagManagerhead()?? '');
$this->codeService->setCodeBody($page_home->getTagManagerbody()?? '');
$this->codeService->setSchemaOrgGeneral($page_home->translate($_locale)->getSchema()?? '');
$this->codeService->sethreflang($this->hrefLang->hreflangHomePage());
$schemaOrg = $this->metaService->schemaOrg($page_home);
if($contacts){
$first_contact = $contacts[0];
$daysMapping = [
'Mo' => 'getHoraireLun',
'Tu' => 'getHoraireMard',
'We' => 'getHoraireMerc',
'Th' => 'getHoraireJeu',
'Fr' => 'getHoraireVen',
'Sa' => 'getHoraireSam',
'Su' => 'getHoraireDim',
];
$openingHours = [];
foreach ($daysMapping as $day => $method) {
if (method_exists($first_contact->translate($_locale), $method)) {
$hours = $first_contact->translate($_locale)->{$method}();
if ($hours) {
$openingHours[] = "{$day} {$hours}";
}
}
}
$openingHoursString = implode(', ', $openingHours);
$socialLinks = [];
$socials = $this->getDoctrine()->getRepository(ReseauSociaux::class)->findAll();
if($socials){
foreach ($socials as $key => $social) {
$socialLinks[] = $social->getUrl();
}
}
$telephones = unserialize($first_contact->translate($_locale)->getTel());
$telephone = $telephones ? $telephones[0]: '';
$schemaOrg->add(Schema::localBusiness()
->name($parametre->getTitre())
->address(
Schema::postalAddress()
->streetAddress($first_contact->getAdresse())
->addressLocality($first_contact->getVille())
->addressRegion($first_contact->getRegion())
->postalCode($first_contact->getCodePostal())
->addressCountry($first_contact->getPays())
)
->telephone($telephone)
->url($request->getUri())
->openingHours($openingHoursString)
->geo(
Schema::geoCoordinates()
->latitude($first_contact->getLatitude())
->longitude($first_contact->getLongitude())
)->sameAs($socialLinks) );
}
$this->codeService->setcanonicalUrl($this->metaService->canonicalUrl());
$renderService = $this->renderBloc->RenderBloc($page_home,$schemaOrg);
$blocs = $renderService['bloc'];
$schemaOrg = $renderService['schemaOrg'];
$this->codeService->setSchemaOrg($schemaOrg);
$modele_galerie_choisie = $page_home->getModeleGallerie() ? $page_home->getModeleGallerie()->getId() : 6;
$modele_gallerie = $this->em->getRepository(ModelGallerie::class)->findById($modele_galerie_choisie);
$twig_galerie = $modele_gallerie[0]->getTwig();
$media_cms = $this->getDoctrine()->getRepository(MediaCms::class)->findBy(['module'=> $page_home->getTypePost()->getSystemName(),'module_id'=> $page_home->getId()],['position'=> 'ASC']);
$header_images = $this->imageGalleryService->getGalleryImages($media_cms);
$categories = $this->getCategoryTree();
return $this->render('theme/'.$this->theme.'/index.html.twig',[
'blocs'=> $blocs,
'categories'=> $categories,
'twig_galerie'=> $twig_galerie,
'galleries'=> $header_images,
]);
}
public function getCategoryTree($parentId = null)
{
$categories = $this->em->getRepository(Category::class)->findBy(['TypePost' => 13, 'actif' => true]);
$tree = [];
foreach ($categories as $category) {
if ($category->getParent() == $parentId) {
$children = $this->getCategoryTree($category->getId());
$categoryArray = [
'categorie' => $category,
'children' => $children
];
$tree[] = $categoryArray;
}
}
return $tree;
}
}