1 <?php
declare(strict_types
=1);
4 * This file is part of the Rapsys BlogBundle package.
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Rapsys\BlogBundle\Controller
;
14 use Symfony\Component\HttpFoundation\Request
;
15 use Symfony\Component\HttpFoundation\Response
;
17 use Rapsys\BlogBundle\Entity\Article
;
18 use Rapsys\BlogBundle\Entity\Keyword
;
23 class KeywordController
extends AbstractController
{
29 * @param Request $request The request instance
30 * @return Response The rendered view
32 public function index(Request
$request): Response
{
34 if ($count = $this->doctrine
->getRepository(Keyword
::class)->findCountAsInt()) {
35 //Negative page or over page
36 if (($page = (int) $request->get('page', 0)) < 0 || $page > $count / $this->limit
) {
38 throw $this->createNotFoundException($this->translator
->trans('Unable to find keywords (page: %page%)', ['%page%' => $page]));
42 if (empty($this->context
['keywords'] = $this->doctrine
->getRepository(Keyword
::class)->findAllAsArray($page, $this->limit
))) {
44 throw $this->createNotFoundException($this->translator
->trans('Unable to find keywords'));
50 $this->context
['head']['prev'] = $this->context
['keywords_prev'] = $this->generateUrl($request->attributes
->get('_route'), ['page' => $page - 1]+
$request->attributes
->get('_route_params'));
54 if ($count > ($page +
1) * $this->limit
) {
56 $this->context
['head']['next'] = $this->context
['keywords_next'] = $this->generateUrl($request->attributes
->get('_route'), ['page' => $page +
1]+
$request->attributes
->get('_route_params'));
60 $this->modified
= max(array_map(function ($v) { return $v
['modified']; }, $this->context
['keywords']));
64 $this->context
['keywords'] = [];
67 $this->modified
= new \
DateTime('-1 year');
71 $response = new Response();
74 if ($this->checker
->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
76 $response->setLastModified(new \
DateTime('-1 year'));
79 $response->setPrivate();
83 //XXX: only for public to force revalidation by last modified
84 $response->setEtag(md5(serialize($this->context
['keywords'])));
87 $response->setLastModified($this->modified
);
90 $response->setPublic();
92 //Without role and modification
93 if ($response->isNotModified($request)) {
100 $this->context
['head']['keywords'] = implode(
102 //Use closure to extract each unique article keywords sorted
107 //Iterate on keywords
110 $r[$k['title']] = $k['title'];
118 })($this->context
['keywords'])
122 $this->context
['title'] = $this->translator
->trans('Keywords list');
125 $this->context
['description'] = $this->translator
->trans('Welcome to raphaël\'s developer diary keyword listing');
128 return $this->render('@RapsysBlog/keyword/index.html.twig', $this->context
, $response);
134 * Display keyword articles
136 * @param Request $request The request instance
137 * @param integer $id The keyword id
138 * @param ?string $slug The keyword slug
139 * @return Response The rendered view
141 public function view(Request
$request, int $id, ?string $slug): Response
{
143 if (empty($this->context
['keyword'] = $this->doctrine
->getRepository(Keyword
::class)->findByIdAsArray($id))) {
145 throw $this->createNotFoundException($this->translator
->trans('Unable to find keyword: %id%', ['%id%' => $id]));
149 if ($slug !== $this->context
['keyword']['slug']) {
150 //Redirect on correctly spelled keyword
151 return $this->redirectToRoute('rapsys_blog_keyword_view', ['id' => $this->context
['keyword']['id'], 'slug' => $this->context
['keyword']['slug']], Response
::HTTP_MOVED_PERMANENTLY
);
155 $this->modified
= $this->context
['keyword']['modified'];
158 $response = new Response();
161 if ($this->checker
->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
163 $response->setLastModified(new \
DateTime('-1 year'));
166 $response->setPrivate();
167 //Without logged user
170 //XXX: only for public to force revalidation by last modified
171 $response->setEtag(md5(serialize($this->context
['keyword'])));
174 $response->setLastModified($this->modified
);
177 $response->setPublic();
179 //Without role and modification
180 if ($response->isNotModified($request)) {
181 //Return 304 response
187 $this->context
['head']['keywords'] = implode(
189 //Use closure to extract each unique article keywords sorted
194 //Iterate on articles
197 if (!empty($a['keywords'])) {
198 //Iterate on keywords
199 foreach($a['keywords'] as $k) {
201 $r[$k['title']] = $k['title'];
211 })($this->context
['keyword']['articles'])
215 $this->context
['title'] = $this->context
['keyword']['title'];
218 $this->context
['description'] = $this->context
['keyword']['description'];
221 return $this->render('@RapsysBlog/keyword/view.html.twig', $this->context
, $response);