src/Controller/FrontEnd/HomeController.php line 94

Open in your IDE?
  1. <?php
  2. namespace App\Controller\FrontEnd;
  3. use App\Entity\Post;
  4. use App\Entity\Contact;
  5. use App\Entity\Category;
  6. use App\Entity\MediaCms;
  7. use Spatie\SchemaOrg\Graph;
  8. use App\Service\CodeService;
  9. use App\Service\MetaService;
  10. use Spatie\SchemaOrg\Schema;
  11. use App\Entity\ModelGallerie;
  12. use App\Entity\ParametreSite;
  13. use App\Entity\ReseauSociaux;
  14. use App\Service\HelperService;
  15. use App\Service\HrefLangService;
  16. use App\Service\RenderBlocService;
  17. use App\Utils\SiteParameterService;
  18. use App\Service\ImageGalleryService;
  19. use App\Service\NestedTreeStructure;
  20. use Doctrine\ORM\EntityManagerInterface;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\Routing\Annotation\Route;
  23. use Leogout\Bundle\SeoBundle\Seo\Og\OgSeoGenerator;
  24. use Symfony\Component\String\Slugger\SluggerInterface;
  25. use Leogout\Bundle\SeoBundle\Seo\Basic\BasicSeoGenerator;
  26. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  27. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  28. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  29. class HomeController extends AbstractController
  30. {
  31. public $basicSeo;
  32. public $seo;
  33. private $slugger;
  34. private $projectDir;
  35. private $metaService;
  36. private $renderBloc;
  37. private $buildTree;
  38. private $helperService;
  39. private $theme;
  40. private $imageGalleryService;
  41. private $siteParameterService;
  42. private $em;
  43. private $codeService;
  44. private $hrefLang;
  45. public function __construct(string $projectDir,string $theme,
  46. BasicSeoGenerator $basicSeo,
  47. SiteParameterService $siteParameterService,
  48. OgSeoGenerator $seo,
  49. RenderBlocService $renderBloc,
  50. SluggerInterface $slugger,
  51. NestedTreeStructure $buildTree,
  52. MetaService $metaService,
  53. HelperService $helperService,
  54. ImageGalleryService $imageGalleryService,
  55. EntityManagerInterface $em,
  56. HrefLangService $hrefLang,
  57. CodeService $codeService){
  58. $this->em = $em;
  59. $this->metaService = $metaService;
  60. $this->basicSeo = $basicSeo;
  61. $this->seo = $seo;
  62. $this->hrefLang = $hrefLang;
  63. $this->buildTree = $buildTree;
  64. $this->slugger = $slugger;
  65. $this->helperService = $helperService;
  66. $this->renderBloc = $renderBloc;
  67. $this->projectDir = $projectDir;
  68. $this->theme = $theme;
  69. $this->imageGalleryService = $imageGalleryService;
  70. $this->codeService = $codeService;
  71. $this->siteParameterService = $siteParameterService;
  72. }
  73. /**
  74. * @Route("/{_locale}", name="index_page_home", defaults={"_locale": "fr"}, requirements={"_locale"="fr|en|de|es|cn|it"})
  75. */
  76. public function index(Request $request)
  77. {
  78. $_locale = $request->getLocale();
  79. // $this->siteParameterService->refreshParameters();
  80. $parametre = $this->getDoctrine()->getRepository(ParametreSite::class)->findOneBy([],['id'=> 'ASC']);
  81. $contacts = $this->em->getRepository(Contact::class)->getActifContacts();
  82. $page_home = $this->em->getRepository(Post::class)->findOneBy(['alias'=> 'page-home']);
  83. if(!$page_home){
  84. throw new NotFoundHttpException('Sorry not existing!');
  85. }
  86. $this->metaService->metaTags($page_home);
  87. $this->codeService->setCodeRobots($page_home->getMetaRobots()?? '');
  88. $this->codeService->setCodeHead($page_home->getTagManagerhead()?? '');
  89. $this->codeService->setCodeBody($page_home->getTagManagerbody()?? '');
  90. $this->codeService->setSchemaOrgGeneral($page_home->translate($_locale)->getSchema()?? '');
  91. $this->codeService->sethreflang($this->hrefLang->hreflangHomePage());
  92. $schemaOrg = $this->metaService->schemaOrg($page_home);
  93. if($contacts){
  94. $first_contact = $contacts[0];
  95. $daysMapping = [
  96. 'Mo' => 'getHoraireLun',
  97. 'Tu' => 'getHoraireMard',
  98. 'We' => 'getHoraireMerc',
  99. 'Th' => 'getHoraireJeu',
  100. 'Fr' => 'getHoraireVen',
  101. 'Sa' => 'getHoraireSam',
  102. 'Su' => 'getHoraireDim',
  103. ];
  104. $openingHours = [];
  105. foreach ($daysMapping as $day => $method) {
  106. if (method_exists($first_contact->translate($_locale), $method)) {
  107. $hours = $first_contact->translate($_locale)->{$method}();
  108. if ($hours) {
  109. $openingHours[] = "{$day} {$hours}";
  110. }
  111. }
  112. }
  113. $openingHoursString = implode(', ', $openingHours);
  114. $socialLinks = [];
  115. $socials = $this->getDoctrine()->getRepository(ReseauSociaux::class)->findAll();
  116. if($socials){
  117. foreach ($socials as $key => $social) {
  118. $socialLinks[] = $social->getUrl();
  119. }
  120. }
  121. $telephones = unserialize($first_contact->translate($_locale)->getTel());
  122. $telephone = $telephones ? $telephones[0]: '';
  123. $schemaOrg->add(Schema::localBusiness()
  124. ->name($parametre->getTitre())
  125. ->address(
  126. Schema::postalAddress()
  127. ->streetAddress($first_contact->getAdresse())
  128. ->addressLocality($first_contact->getVille())
  129. ->addressRegion($first_contact->getRegion())
  130. ->postalCode($first_contact->getCodePostal())
  131. ->addressCountry($first_contact->getPays())
  132. )
  133. ->telephone($telephone)
  134. ->url($request->getUri())
  135. ->openingHours($openingHoursString)
  136. ->geo(
  137. Schema::geoCoordinates()
  138. ->latitude($first_contact->getLatitude())
  139. ->longitude($first_contact->getLongitude())
  140. )->sameAs($socialLinks) );
  141. }
  142. $this->codeService->setcanonicalUrl($this->metaService->canonicalUrl());
  143. $renderService = $this->renderBloc->RenderBloc($page_home,$schemaOrg);
  144. $blocs = $renderService['bloc'];
  145. $schemaOrg = $renderService['schemaOrg'];
  146. $this->codeService->setSchemaOrg($schemaOrg);
  147. $modele_galerie_choisie = $page_home->getModeleGallerie() ? $page_home->getModeleGallerie()->getId() : 6;
  148. $modele_gallerie = $this->em->getRepository(ModelGallerie::class)->findById($modele_galerie_choisie);
  149. $twig_galerie = $modele_gallerie[0]->getTwig();
  150. $media_cms = $this->getDoctrine()->getRepository(MediaCms::class)->findBy(['module'=> $page_home->getTypePost()->getSystemName(),'module_id'=> $page_home->getId()],['position'=> 'ASC']);
  151. $header_images = $this->imageGalleryService->getGalleryImages($media_cms);
  152. $categories = $this->getCategoryTree();
  153. return $this->render('theme/'.$this->theme.'/index.html.twig',[
  154. 'blocs'=> $blocs,
  155. 'categories'=> $categories,
  156. 'twig_galerie'=> $twig_galerie,
  157. 'galleries'=> $header_images,
  158. ]);
  159. }
  160. public function getCategoryTree($parentId = null)
  161. {
  162. $categories = $this->em->getRepository(Category::class)->findBy(['TypePost' => 13, 'actif' => true]);
  163. $tree = [];
  164. foreach ($categories as $category) {
  165. if ($category->getParent() == $parentId) {
  166. $children = $this->getCategoryTree($category->getId());
  167. $categoryArray = [
  168. 'categorie' => $category,
  169. 'children' => $children
  170. ];
  171. $tree[] = $categoryArray;
  172. }
  173. }
  174. return $tree;
  175. }
  176. }