src/Controller/SitemapController.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Component\HttpFoundation\Response;
  4. use App\Controller\AbstractController;
  5. use App\Entity\Departement;
  6. use Symfony\Component\Routing\Router;
  7. use App\Entity\Prestataire;
  8. use App\Misc\StringUtils;
  9. use App\Entity\Page;
  10. use Symfony\Component\HttpFoundation\Request;
  11. class SitemapController extends AbstractController
  12. {
  13. public function sitemap()
  14. {
  15. $categories = $this->getPageRepository()->getCategorieTree();
  16. $locales = $this->getPageRepository()->getLocalPagesForSitemap();
  17. foreach ($locales as $key => $locale) {
  18. // Sanitized Url Ville
  19. $locales[$key]['slug'] = StringUtils::sanitizeString($locale['title']);
  20. }
  21. $departements = $this->getDoctrine()->getRepository(Departement::class)->findBy([], ['nom' => 'ASC']);
  22. foreach ($departements as $key => $departement) {
  23. $dept = $departement->getCode();
  24. $prestataires = $this->getPrestataireRepository()->getBySecteur(
  25. $dept,
  26. $this->getParameter('group_diagnostics')
  27. );
  28. if (count($prestataires) <= 0) {
  29. unset($departements[$key]);
  30. }
  31. }
  32. return $this->render('Public/sitemap.html.twig', array(
  33. 'categories' => $categories,
  34. 'locales' => $locales,
  35. 'departements' => $departements
  36. ));
  37. }
  38. public function index()
  39. {
  40. /** @var RouterInterface $router */
  41. $router = $this->getRouter();
  42. $sitemaps = array();
  43. $sitemaps[] = array(
  44. 'loc' => $router->generate('sitemap_pages', array(), Router::ABSOLUTE_URL),
  45. 'modified' => $this->getPageRepository()->getLastModifiedDate()
  46. );
  47. // $sitemaps[] = array(
  48. // 'loc' => $router->generate('sitemap_actus', array(), Router::ABSOLUTE_URL),
  49. // 'modified' => $this->getPageRepository()->getLastModifiedDate(Page::TYPE_ACTU)
  50. // );
  51. $sitemaps[] = array(
  52. 'loc' => $router->generate('sitemap_locales', array(), Router::ABSOLUTE_URL),
  53. 'modified' => $this->getPageRepository()->getLastModifiedDate(Page::TYPE_LOCAL)
  54. );
  55. $sitemaps[] = array(
  56. 'loc' => $router->generate('sitemap_prestataires', array(), Router::ABSOLUTE_URL),
  57. 'modified' => $this->getPrestataireRepository()->getLastModifiedDate()
  58. );
  59. // $sitemaps[] = array(
  60. // 'loc' => $router->generate('sitemap_demandes', array(), Router::ABSOLUTE_URL),
  61. // 'modified' => $this->getDemandeRepository()->getLastModifiedDate()
  62. // );
  63. return $this->render('Sitemap/sitemap_index.xml.twig', array(
  64. 'sitemaps' => $sitemaps
  65. ), $this->getResponse());
  66. }
  67. private function getResponse()
  68. {
  69. $response = new Response();
  70. $response->headers->set('Content-Type', 'text/xml');
  71. return $response;
  72. }
  73. private function renderMap($urls)
  74. {
  75. return $this->render('Sitemap/sitemap.xml.twig', array(
  76. 'urls' => $urls,
  77. ), $this->getResponse());
  78. }
  79. public function pages()
  80. {
  81. $urls = array();
  82. $urls[] = array(
  83. 'loc' => $this->getRouter()->generate('public_index', array(), Router::ABSOLUTE_URL),
  84. 'changefreq' => 'hourly',
  85. 'priority' => '1'
  86. );
  87. $urls[] = array(
  88. 'loc' => $this->getRouter()->generate('public_categories', array(), Router::ABSOLUTE_URL),
  89. 'changefreq' => 'hourly',
  90. 'priority' => '1'
  91. );
  92. $urls[] = array(
  93. 'loc' => $this->getRouter()->generate('public_prestataires', array(), Router::ABSOLUTE_URL),
  94. 'changefreq' => 'monthly',
  95. 'priority' => '0.7'
  96. );
  97. $urls[] = array(
  98. 'loc' => $this->getRouter()->generate('public_demandes', array(), Router::ABSOLUTE_URL),
  99. 'changefreq' => 'monthly',
  100. 'priority' => '0.7'
  101. );
  102. $urls[] = array(
  103. 'loc' => $this->getRouter()->generate('public_local_pages', array(), Router::ABSOLUTE_URL),
  104. 'changefreq' => 'monthly',
  105. 'priority' => '0.7'
  106. );
  107. $urls[] = array(
  108. 'loc' => $this->getRouter()->generate('public_sitemap', array(), Router::ABSOLUTE_URL),
  109. 'changefreq' => 'monthly',
  110. 'priority' => '0.5'
  111. );
  112. $urls[] = array(
  113. 'loc' => $this->getRouter()->generate('public_demande_form', array(), Router::ABSOLUTE_URL),
  114. 'changefreq' => 'monthly',
  115. 'priority' => '0.7'
  116. );
  117. $urls[] = array(
  118. 'loc' => $this->getRouter()->generate('presta_access', array(), Router::ABSOLUTE_URL),
  119. 'changefreq' => 'monthly',
  120. 'priority' => '0.7'
  121. );
  122. // $urls[] = array(
  123. // 'loc' => $this->getRouter()->generate('public_actus', array(), Router::ABSOLUTE_URL),
  124. // 'changefreq' => 'monthly',
  125. // 'priority' => '0.7'
  126. // );
  127. $page = $this->getPageRepository()->getByType('prix');
  128. $urls[] = array(
  129. 'loc' => $this->getRouter()->generate('public_page', array('url' => $page->getUrl()->getUrl()), Router::ABSOLUTE_URL),
  130. 'changefreq' => 'monthly',
  131. 'priority' => '0.8',
  132. 'lastmod' => $page->getModifiedDate()
  133. );
  134. /** @var Page[] $pages */
  135. foreach ($this->getPageRepository()->getForXmlSiteMap() ?? [] as $page) {
  136. if ($page->getType() != 'prix'):
  137. $priority = '0.5';
  138. switch ($page->getType()) {
  139. case Page::TYPE_CATEGORIE:
  140. $priority = '0.8';
  141. break;
  142. case Page::TYPE_AIDE:
  143. case Page::TYPE_QUALITY:
  144. $priority = '0.7';
  145. break;
  146. }
  147. if ($page->getId() === 122 || ($page->getParent() && $page->getParent()->getId() === 122)):
  148. $order = 10;
  149. elseif ($page->getId() === 140 || ($page->getParent() && $page->getParent()->getId() == 140)):
  150. $order = 100;
  151. elseif ($page->getId() === 163 || ($page->getParent() && $page->getParent()->getId() == 163)):
  152. $order = 1000;
  153. elseif ($page->getId() === 178 || ($page->getParent() && $page->getParent()->getId() == 178)):
  154. $order = 10000;
  155. elseif ($page->getId() === 189 || ($page->getParent() && $page->getParent()->getId() == 189)):
  156. $order = 100000;
  157. endif;
  158. $pages[] = array(
  159. 'loc' => $this->getRouter()->generate('public_page', array(
  160. 'url' => $page->getUrl()->getUrl()
  161. ), Router::ABSOLUTE_URL),
  162. 'priority' => $priority,
  163. 'changefreq' => 'monthly',
  164. 'lastmod' => $page->getModifiedDate(),
  165. 'order' => $order + $page->getOrder(),
  166. );
  167. endif;
  168. }
  169. usort($pages, function ($a, $b) {
  170. return $a['order'] <=> $b['order'];
  171. });
  172. $urls = array_merge($urls, $pages);
  173. $urls[] = array(
  174. 'loc' => $this->getRouter()->generate('public_contact', array(), Router::ABSOLUTE_URL),
  175. 'changefreq' => 'monthly',
  176. 'priority' => '0.5'
  177. );
  178. return $this->renderMap($urls);
  179. }
  180. // public function actus()
  181. // {
  182. // $urls = array();
  183. // /** @var Page[] $actus */
  184. // $actus = $this->getPageRepository()->findBy(array('type' => Page::TYPE_ACTU));
  185. // foreach ($actus as $actu) {
  186. // $urls[] = array(
  187. // 'loc' => $this->getUrlGenerator()->getUrlFor($actu, true),
  188. // 'priority' => '0.8',
  189. // 'changefreq' => 'monthly',
  190. // 'lastmod' => $actu->getModifiedDate()
  191. // );
  192. // }
  193. // return $this->renderMap($urls);
  194. // }
  195. public function locales(Request $request)
  196. {
  197. $urls = array();
  198. // Pages locales (diagnostic)
  199. $locales = $this->getPageRepository()->getLocalPagesForSitemap();
  200. foreach ($locales as $locale) {
  201. $urls[] = array(
  202. 'loc' => $request->getScheme() . '://' . $request->getHttpHost() . '/diagnostic-immobilier-' . StringUtils::sanitizeString($locale['title']) . '-' . $locale['cp'] . '.html',
  203. 'priority' => '0.8',
  204. 'changefreq' => 'monthly',
  205. 'lastmod' => $locale['modified']
  206. );
  207. }
  208. // Pages departement (diagnostiqueur immobilier)
  209. $departements = $this->getEm()->getRepository(Departement::class)->findBy([], ['nom' => 'ASC']);
  210. foreach ($departements as $departement) {
  211. $dept = $departement->getCode();
  212. $prestataires = $this->getPrestataireRepository()->getBySecteur(
  213. $dept,
  214. $this->getParameter('group_diagnostics')
  215. );
  216. if (count($prestataires) > 0) {
  217. $urlsDepartements[] = array(
  218. 'loc' => $this->getRouter()->generate('public_local_page_departement', array(
  219. 'departement' => StringUtils::sanitizeString($departement->getSlug()),
  220. 'cp' => $departement->getCode()
  221. ), Router::ABSOLUTE_URL),
  222. 'priority' => '0.8',
  223. 'changefreq' => 'monthly',
  224. 'lastmod' => '2018-05-31 11:17'
  225. );
  226. }
  227. }
  228. $urls = array_merge($urls, $urlsDepartements);
  229. return $this->renderMap($urls);
  230. }
  231. public function prestataires()
  232. {
  233. $urls = array();
  234. /** @var Prestataire[] $prestataires */
  235. $prestataires = $this->getPrestataireRepository()->findBy(array(
  236. 'status' => Prestataire::STATUS_VALIDATED
  237. ));
  238. foreach ($prestataires as $prestataire) {
  239. $url = array(
  240. 'loc' => $this->getUrlGenerator()->getUrlFor($prestataire, true),
  241. 'priority' => '0.7',
  242. 'changefreq' => 'weekly',
  243. 'lastmod' => $prestataire->getModifiedDate()
  244. );
  245. if ($prestataire->getLogoPath()) {
  246. $url['image'] = array(
  247. 'loc' => 'data/logos/'. $prestataire->getLogoPath(),
  248. 'caption' => $prestataire->getEntreprise()
  249. );
  250. }
  251. $urls[] = $url;
  252. }
  253. return $this->renderMap($urls);
  254. }
  255. // public function demandes()
  256. // {
  257. // $urls = array();
  258. // /** @var Demande[] $demandes */
  259. // $demandes = $this->getDemandeRepository()->getForXmlSiteMap();
  260. // foreach ($demandes as $demande) {
  261. // $urls[] = array(
  262. // 'loc' => $this->getUrlGenerator()->getUrlFor($demande, true),
  263. // 'priority' => '0.7',
  264. // 'changefreq' => 'weekly',
  265. // 'lastmod' => $demande->getModifiedDate()
  266. // );
  267. // }
  268. // return $this->renderMap($urls);
  269. // }
  270. }