1 | <?php |
||
2 | |||
3 | namespace PiedWeb\CMSBundle\Controller; |
||
4 | |||
5 | use PiedWeb\CMSBundle\Entity\PageInterface as Page; |
||
6 | use PiedWeb\CMSBundle\Repository\PageRepository; |
||
7 | use PiedWeb\CMSBundle\Service\App; |
||
8 | use PiedWeb\CMSBundle\Service\Repository; |
||
9 | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
||
10 | use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; |
||
11 | use Symfony\Component\HttpFoundation\Request; |
||
12 | use Symfony\Component\HttpFoundation\Response; |
||
13 | use Twig\Environment as Twig; |
||
14 | |||
15 | class PageController extends AbstractController |
||
16 | { |
||
17 | /** |
||
18 | * @var ParameterBagInterface |
||
19 | */ |
||
20 | protected $params; |
||
21 | |||
22 | /** |
||
23 | * @var App |
||
24 | */ |
||
25 | protected $app; |
||
26 | protected $twig; |
||
27 | |||
28 | public function __construct( |
||
29 | ParameterBagInterface $params, |
||
30 | Twig $twig |
||
31 | ) { |
||
32 | $this->params = $params; |
||
33 | $this->twig = $twig; |
||
34 | $this->app = App::load(null, $this->params); |
||
35 | } |
||
36 | |||
37 | public function show(?string $slug, ?string $host, Request $request): Response |
||
38 | { |
||
39 | $page = $this->getPage($slug, $host, $request); |
||
40 | |||
41 | // SEO redirection if we are not on the good URI (for exemple /fr/tagada instead of /tagada) |
||
42 | if ((null === $host || $host == $request->getHost()) |
||
43 | && false !== $redirect = $this->checkIfUriIsCanonical($request, $page)) { |
||
44 | return $this->redirect($redirect[0], $redirect[1]); |
||
45 | } |
||
46 | |||
47 | // Maybe the page is a redirection |
||
48 | if ($page->getRedirection()) { |
||
49 | return $this->redirect($page->getRedirection(), $page->getRedirectionCode()); |
||
50 | } |
||
51 | |||
52 | return $this->render( |
||
53 | $this->getTemplate($page->getTemplate() ?: '/page/page.html.twig'), |
||
54 | array_merge(['page' => $page], $this->app->getParamsForRendering()) |
||
55 | ); |
||
56 | } |
||
57 | |||
58 | public function preview(?string $slug, ?string $host, Request $request): Response |
||
59 | { |
||
60 | $page = $this->getPage($slug, $host, $request); |
||
61 | $page->setMainContent($request->request->get('plaintext')); |
||
62 | // todo update all fields to avoid errors with autosave |
||
63 | // (getting them via json?!) |
||
64 | // And not getPage but create a new Page !!! (else, error on unexisting Page) |
||
65 | |||
66 | return $this->render( |
||
67 | $this->getTemplate('/page/preview.html.twig'), |
||
68 | array_merge(['page' => $page], $this->app->getParamsForRendering()) |
||
69 | ); |
||
70 | } |
||
71 | |||
72 | protected function getTemplate($path) |
||
73 | { |
||
74 | return $this->app->getTemplate($path, $this->twig); |
||
75 | } |
||
76 | |||
77 | public function showFeed(?string $slug, ?string $host, Request $request) |
||
78 | { |
||
79 | $page = $this->getPage($slug, $host, $request); |
||
80 | |||
81 | if ('homepage' == $slug) { |
||
82 | return $this->redirect($this->generateUrl('piedweb_cms_page_feed', ['slug' => 'index']), 301); |
||
83 | } |
||
84 | |||
85 | $response = new Response(); |
||
86 | $response->headers->set('Content-Type', 'text/xml'); |
||
87 | |||
88 | return $this->render( |
||
89 | $this->getTemplate('/page/rss.xml.twig'), |
||
90 | array_merge(['page' => $page], $this->app->getParamsForRendering()), |
||
91 | $response |
||
92 | ); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Show Last created page in an XML Feed. |
||
97 | */ |
||
98 | public function showMainFeed(?string $host, Request $request) |
||
99 | { |
||
100 | $this->app->switchCurrentApp($host); |
||
101 | $locale = $request->getLocale() ? rtrim($request->getLocale(), '/') : $this->params->get('locale'); |
||
102 | $LocaleHomepage = $this->getPage($locale, $host, $request, false); |
||
103 | $slug = 'homepage'; |
||
104 | $page = $LocaleHomepage ?: $this->getPage($slug, $host, $request); |
||
105 | |||
106 | $params = [ |
||
107 | 'pages' => $this->getPages(5, $request), |
||
108 | 'page' => $page, |
||
109 | 'feedUri' => ($this->params->get('locale') == $locale ? '' : $locale.'/').'feed.xml', |
||
110 | ]; |
||
111 | |||
112 | return $this->render( |
||
113 | $this->getTemplate('/page/rss.xml.twig'), |
||
114 | array_merge($params, $this->app->getParamsForRendering()) |
||
115 | ); |
||
116 | } |
||
117 | |||
118 | public function showSitemap($_format, ?string $host, Request $request) |
||
119 | { |
||
120 | $this->app->switchCurrentApp($host); |
||
121 | $pages = $this->getPages(null, $request); |
||
122 | |||
123 | if (! $pages) { |
||
124 | throw $this->createNotFoundException(); |
||
125 | } |
||
126 | |||
127 | return $this->render( |
||
128 | $this->getTemplate('/page/sitemap.'.$_format.'.twig'), |
||
129 | [ |
||
130 | 'pages' => $pages, |
||
131 | 'app_base_url' => $this->app->getBaseUrl(), |
||
132 | ] |
||
133 | ); |
||
134 | } |
||
135 | |||
136 | protected function getPages(?int $limit = null, Request $request) |
||
137 | { |
||
138 | $requestedLocale = rtrim($request->getLocale(), '/'); |
||
139 | |||
140 | $pages = $this->getPageRepository()->getIndexablePages( |
||
141 | $this->app->getHost(), |
||
142 | $this->app->isFirstApp(), |
||
143 | $requestedLocale, |
||
144 | $this->params->get('locale'), |
||
145 | $limit |
||
146 | )->getQuery()->getResult(); |
||
147 | |||
148 | return $pages; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * @return PageRepository |
||
153 | */ |
||
154 | protected function getPageRepository() |
||
155 | { |
||
156 | return Repository::getPageRepository($this->getDoctrine(), $this->params->get('pwc.entity_page')); |
||
157 | } |
||
158 | |||
159 | protected function getPage(?string &$slug, ?string $host = null, ?Request $request, $throwException = true): ?Page |
||
0 ignored issues
–
show
|
|||
160 | { |
||
161 | $this->app->switchCurrentApp($host); |
||
162 | $slug = $this->noramlizeSlug($slug); |
||
163 | $page = $this->getPageRepository()->getPage($slug, $this->app->getHost(), $this->app->isFirstApp()); |
||
164 | |||
165 | // Check if page exist |
||
166 | if (null === $page) { |
||
167 | if ($throwException) { |
||
168 | throw $this->createNotFoundException(); |
||
169 | } else { |
||
170 | return null; |
||
171 | } |
||
172 | } |
||
173 | |||
174 | if (! $page->getLocale()) { // avoid bc break |
||
175 | $page->setLocale($this->params->get('pwc.locale')); |
||
176 | } |
||
177 | |||
178 | //if (null !== $request) { $request->setLocale($page->getLocale()); } |
||
179 | $this->get('translator')->setLocale($page->getLocale()); |
||
180 | |||
181 | // Check if page is public |
||
182 | if ($page->getCreatedAt() > new \DateTimeImmutable() && ! $this->isGranted('ROLE_EDITOR')) { |
||
183 | if ($throwException) { |
||
184 | throw $this->createNotFoundException(); |
||
185 | } else { |
||
186 | return null; |
||
187 | } |
||
188 | } |
||
189 | |||
190 | return $page; |
||
191 | } |
||
192 | |||
193 | protected function noramlizeSlug($slug) |
||
194 | { |
||
195 | return (null === $slug || '' === $slug) ? 'homepage' : rtrim(strtolower($slug), '/'); |
||
196 | } |
||
197 | |||
198 | protected function checkIfUriIsCanonical(Request $request, Page $page) |
||
199 | { |
||
200 | $real = $request->getRequestUri(); |
||
201 | |||
202 | $expected = 'homepage' == $page->getSlug() ? |
||
203 | $this->get('piedweb.page_canonical')->generatePathForHomepage() : |
||
204 | $this->get('piedweb.page_canonical')->generatePathForPage($page->getRealSlug()); |
||
205 | |||
206 | if ($real != $expected) { |
||
207 | return [$request->getBasePath().$expected, 301]; |
||
208 | } |
||
209 | |||
210 | return false; |
||
211 | } |
||
212 | } |
||
213 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.