|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PiedWeb\CMSBundle\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
7
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
|
8
|
|
|
use PiedWeb\CMSBundle\Entity\PageInterface as Page; |
|
9
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; |
|
10
|
|
|
|
|
11
|
|
|
class PageController extends AbstractController |
|
12
|
|
|
{ |
|
13
|
|
|
protected $translator; |
|
14
|
|
|
|
|
15
|
|
|
public function show(string $slug = 'homepage', Request $request, TranslatorInterface $translator, ParameterBagInterface $params) |
|
16
|
|
|
{ |
|
17
|
|
|
$slug = '' == $slug ? 'homepage' : rtrim(strtolower($slug), '/'); |
|
18
|
|
|
$page = $this->getDoctrine()->getRepository($this->container->getParameter('app.entity_page'))->findOneBySlug($slug, $request->getLocale()); |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
// Check if page exist |
|
21
|
|
|
if (null === $page) { |
|
22
|
|
|
throw $this->createNotFoundException(); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
// Check if page is public |
|
26
|
|
|
if ($page->getCreatedAt() > new \DateTimeImmutable() && !$this->isGranted('ROLE_ADMIN')) { |
|
27
|
|
|
throw $this->createNotFoundException(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
// SEO redirection if we are not on the good URI (for exemple /fr/tagada instead of /tagada) |
|
31
|
|
|
$redirect = $this->checkIfUriIsCanonical($request, $page); |
|
32
|
|
|
if (false !== $redirect) { |
|
33
|
|
|
return $this->redirect($redirect[0], $redirect[1]); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
// Maybe the page is a redirection |
|
37
|
|
|
if (false !== $page->getRedirection()) { |
|
38
|
|
|
return $this->redirect($page->getRedirection(), $page->getRedirectionCode()); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$template = method_exists($this->container->getParameter('app.entity_page'), 'getTemplate') && null !== $page->getTemplate() ? $page->getTemplate() : $params->get('app.default_page_template'); |
|
42
|
|
|
|
|
43
|
|
|
return $this->render($template, ['page' => $page]); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
protected function checkIfUriIsCanonical($request, $page) |
|
47
|
|
|
{ |
|
48
|
|
|
$real = $request->getRequestUri(); |
|
49
|
|
|
|
|
50
|
|
|
$defaultLocale = $this->container->getParameter('locale'); |
|
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
$expected = 'homepage' == $page->getSlug() ? |
|
53
|
|
|
$this->get('piedweb.page_canonical')->generatePathForHomepage() : |
|
54
|
|
|
$this->get('piedweb.page_canonical')->generatePathForPage($page->getRealSlug()) |
|
55
|
|
|
; |
|
56
|
|
|
|
|
57
|
|
|
if ($real != $expected) { |
|
58
|
|
|
return [$request->getBasePath().$expected, 301]; |
|
59
|
|
|
// may log ? |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return false; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|