src/Controller/PageController.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Controller\AbstractController;
  4. use App\Entity\Page;
  5. use App\Misc\EntityUrlGenerator;
  6. use App\Misc\StringUtils;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Knp\Component\Pager\PaginatorInterface;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Mailer\MailerInterface;
  13. use Symfony\Component\Routing\RouterInterface;
  14. use Twig\Environment;
  15. class PageController extends AbstractController
  16. {
  17. public function __construct(
  18. private Environment $twig,
  19. protected MailerInterface $mailer,
  20. private EntityUrlGenerator $entityUrlGenerator,
  21. private RouterInterface $router,
  22. private RequestStack $requestStack,
  23. private EntityManagerInterface $entityManager,
  24. ) {
  25. parent::__construct($mailer, $entityUrlGenerator, $router, $requestStack, $entityManager);
  26. }
  27. public function localPage($ville, $cp)
  28. {
  29. // Debug url routing for city with "-"
  30. while (str_contains($cp, '-')):
  31. $ville .= '-' . $error = strtok($cp, '-');
  32. $cp = str_replace($error . '-', '', $cp);
  33. endwhile;
  34. $page = $this->getPageRepository()->getByMeta('cp', $cp);
  35. if (!$page) {
  36. return $this->throw404();
  37. }
  38. $sanitized = StringUtils::sanitizeString($page->getTitle());
  39. if ($ville != $sanitized) {
  40. return $this->redirectToRoute('public_local_page', array(
  41. 'ville' => $sanitized,
  42. 'cp' => $page->getMeta('cp')
  43. ));
  44. }
  45. $deptRepo = $this->getDepartementRepository();
  46. $departement = $deptRepo->getByCode($page->getMeta('departement'));
  47. $departements = $deptRepo->findBy(['region' => $departement->getRegion()]);
  48. $prestataires = [];
  49. foreach ($departements as $dept) {
  50. $presta = (array) $this->getPrestataireRepository()->getByCodePostal(
  51. $dept->getCode(),
  52. $this->getParameter('group_diagnostics')
  53. );
  54. $prestataires = array_merge($prestataires, $presta);
  55. }
  56. return $this->render('Public/local_page.html.twig', array(
  57. 'page' => $page,
  58. 'departement' => $departement,
  59. 'prestataires' => $prestataires
  60. ));
  61. }
  62. public function localPages()
  63. {
  64. $repo = $this->getPageRepository();
  65. $page = $repo->getByType(Page::TYPE_LOCAL_INDEX);
  66. $pages = $repo->getLocalPagesList();
  67. // Get Prestataires by page
  68. foreach ($pages as $key => $item) {
  69. $prestataires = $this->getPrestataireRepository()->getBySecteur(
  70. $item['code'],
  71. $this->getParameter('group_diagnostics')
  72. );
  73. $pages[$key]['prestataires'] = count($prestataires);
  74. }
  75. return $this->render('Public/local_pages.html.twig', array(
  76. 'page' => $page,
  77. 'pages' => $pages
  78. ));
  79. }
  80. public function localPageDepartement($departement, $cp, Request $request, PaginatorInterface $paginator)
  81. {
  82. $deptRepo = $this->getDepartementRepository();
  83. $departement = $deptRepo->getByCode($cp);
  84. $repo = $this->getPrestataireRepository();
  85. $prestatairesAll = $repo->getBySecteur(
  86. $departement->getCode(),
  87. $this->getParameter('group_diagnostics')
  88. );
  89. $prestataires = $paginator->paginate(
  90. $prestatairesAll,
  91. $request->query->getInt('page', 1),
  92. 20
  93. );
  94. $categories = $repo->getCategoriesForAll($prestataires);
  95. return $this->render('Public/local_page_departement.html.twig', array(
  96. 'departement' => $departement,
  97. 'prestataires' => $prestataires,
  98. 'prestatairesAll' => $prestatairesAll,
  99. 'categories' => $categories
  100. ));
  101. }
  102. public function page($url)
  103. {
  104. /** @var Url $url */
  105. $url = $this->getUrlRepository()->findOneBy(array('url' => $url));
  106. if ($url && $url->getRedirectOn()) {
  107. return $this->redirectToRoute('public_page', array(
  108. 'url' => $url->getRedirectOn()->getUrl()
  109. ), Response::HTTP_MOVED_PERMANENTLY);
  110. }
  111. $page = $url ? $url->getPage() : null;
  112. if (!$page) {
  113. return $this->throw404();
  114. }
  115. $categorieTree = $this->getPageRepository()->getCategorieTree();
  116. $groups = array_flip([
  117. 'diagnostics' => 72,
  118. 'expertises' => 73,
  119. 'etudes' => 74,
  120. 'solutions' => 75,
  121. 'energies' => 76
  122. ]);
  123. // echo '<pre>';
  124. // print_r($groups);
  125. // print_r($categorieTree);
  126. // echo '</pre>';
  127. $data = array(
  128. 'page' => $page,
  129. // 'groups' => array_flip($this->container->getParameter('category_groups')),
  130. 'cid' => $this->getCategorieRepository()->getByPageId($page->getId())
  131. );
  132. $template = 'Public/page_'. $page->getType() .'.html.twig';
  133. if ($this->twig->getLoader()->exists($template)) {
  134. return $this->render($template, $data);
  135. } else {
  136. // echo '<pre>';
  137. // print_r($page->getRobots());
  138. // echo '</pre>';
  139. return $this->render('Public/page.html.twig', $data);
  140. }
  141. }
  142. }