|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PiedWeb\CMSBundle\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use PiedWeb\CMSBundle\Entity\PageInterface as Page; |
|
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
7
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
10
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
|
11
|
|
|
|
|
12
|
|
|
class PageController extends AbstractController |
|
13
|
|
|
{ |
|
14
|
|
|
protected $translator; |
|
15
|
|
|
|
|
16
|
|
|
public function show( |
|
17
|
|
|
?string $slug, |
|
18
|
|
|
Request $request, |
|
19
|
|
|
TranslatorInterface $translator, |
|
20
|
|
|
ParameterBagInterface $params |
|
21
|
|
|
) { |
|
22
|
|
|
$slug = (null === $slug || '' === $slug) ? 'homepage' : rtrim(strtolower($slug), '/'); |
|
23
|
|
|
$page = $this->getDoctrine() |
|
24
|
|
|
->getRepository($this->container->getParameter('app.entity_page')) |
|
|
|
|
|
|
25
|
|
|
->findOneBySlug($slug); |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
// Check if page exist |
|
28
|
|
|
if (null === $page) { |
|
29
|
|
|
throw $this->createNotFoundException(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
if (null !== $page->getLocale()) { // avoid bc break, todo remove it |
|
33
|
|
|
$request->setLocale($page->getLocale()); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
// Check if page is public |
|
37
|
|
|
if ($page->getCreatedAt() > new \DateTimeImmutable() && !$this->isGranted('ROLE_ADMIN')) { |
|
38
|
|
|
throw $this->createNotFoundException(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
// SEO redirection if we are not on the good URI (for exemple /fr/tagada instead of /tagada) |
|
42
|
|
|
$redirect = $this->checkIfUriIsCanonical($request, $page); |
|
43
|
|
|
if (false !== $redirect) { |
|
44
|
|
|
return $this->redirect($redirect[0], $redirect[1]); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
// Maybe the page is a redirection |
|
48
|
|
|
if (false !== $page->getRedirection()) { |
|
49
|
|
|
return $this->redirect($page->getRedirection(), $page->getRedirectionCode()); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
// method_exists($this->container->getParameter('app.entity_page'), 'getTemplate') && |
|
53
|
|
|
$template = null !== $page->getTemplate() ? $page->getTemplate() : $params->get('app.default_page_template'); |
|
54
|
|
|
|
|
55
|
|
|
return $this->render($template, ['page' => $page]); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
protected function checkIfUriIsCanonical($request, $page) |
|
59
|
|
|
{ |
|
60
|
|
|
$real = $request->getRequestUri(); |
|
61
|
|
|
|
|
62
|
|
|
$expected = 'homepage' == $page->getSlug() ? |
|
63
|
|
|
$this->get('piedweb.page_canonical')->generatePathForHomepage() : |
|
64
|
|
|
$this->get('piedweb.page_canonical')->generatePathForPage($page->getRealSlug()); |
|
65
|
|
|
|
|
66
|
|
|
if ($real != $expected) { |
|
67
|
|
|
return [$request->getBasePath().$expected, 301]; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return false; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function preview( |
|
74
|
|
|
?string $slug, |
|
75
|
|
|
Request $request |
|
76
|
|
|
) { |
|
77
|
|
|
$pageEntity = $this->container->getParameter('app.entity_page'); |
|
78
|
|
|
|
|
79
|
|
|
$page = (null === $slug || '' === $slug) ? |
|
80
|
|
|
new $pageEntity() |
|
81
|
|
|
: $this->getDoctrine() |
|
82
|
|
|
->getRepository($pageEntity) |
|
83
|
|
|
->findOneBySlug(rtrim(strtolower($slug), '/')); |
|
84
|
|
|
|
|
85
|
|
|
$plainText = \PiedWeb\CMSBundle\Twig\AppExtension::convertMarkdownImage($request->request->get('plaintext')); |
|
86
|
|
|
|
|
87
|
|
|
return $this->render('@PiedWebCMS/admin/preview.html.twig', ['page' => $page, 'plainText' => $plainText]); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function feed( |
|
91
|
|
|
?string $slug, |
|
92
|
|
|
Request $request, |
|
|
|
|
|
|
93
|
|
|
TranslatorInterface $translator, |
|
|
|
|
|
|
94
|
|
|
ParameterBagInterface $params |
|
|
|
|
|
|
95
|
|
|
) { |
|
96
|
|
|
$slug = (null === $slug || '' === $slug) ? 'homepage' : rtrim(strtolower($slug), '/'); |
|
97
|
|
|
$page = $this->getDoctrine() |
|
98
|
|
|
->getRepository($this->container->getParameter('app.entity_page')) |
|
99
|
|
|
->findOneBySlug($slug); |
|
100
|
|
|
|
|
101
|
|
|
// Check if page exist |
|
102
|
|
|
if (null === $page || $page->getChildrenPages()->count() < 1) { |
|
103
|
|
|
throw $this->createNotFoundException(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
// Check if page is public |
|
107
|
|
|
if ($page->getCreatedAt() > new \DateTimeImmutable() && !$this->isGranted('ROLE_ADMIN')) { |
|
108
|
|
|
throw $this->createNotFoundException(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$response = new Response(); |
|
112
|
|
|
$response->headers->set('Content-Type', 'text/xml'); |
|
113
|
|
|
|
|
114
|
|
|
return $this->render('@PiedWebCMS/page/rss.xml.twig', ['page' => $page], $response); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|