1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller\Trick; |
4
|
|
|
|
5
|
|
|
use App\Entity\Comment; |
6
|
|
|
use App\Entity\Trick; |
7
|
|
|
use App\Form\CommentFormType; |
8
|
|
|
use App\Pagination\PagePagination; |
9
|
|
|
use App\Repository\CommentRepository; |
10
|
|
|
use Doctrine\ORM\Tools\Pagination\Paginator; |
11
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
12
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
13
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
16
|
|
|
|
17
|
|
|
class ShowTrickController extends AbstractController |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var CommentRepository |
22
|
|
|
*/ |
23
|
|
|
private $repository; |
24
|
|
|
/** |
25
|
|
|
* @var PagePagination |
26
|
|
|
*/ |
27
|
|
|
private $pagePagination; |
28
|
|
|
|
29
|
|
|
public function __construct(CommentRepository $repository, PagePagination $pagePagination) |
30
|
|
|
{ |
31
|
|
|
$this->repository = $repository; |
32
|
|
|
$this->pagePagination = $pagePagination; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @Route("/trick/{id}-{slug}", name="trick.show", requirements={"slug": "[a-z0-9\-]*"}) |
37
|
|
|
*/ |
38
|
|
|
public function show(Trick $trick, string $slug, Request $request) |
39
|
|
|
{ |
40
|
|
|
//Checking if slug is equal to the ID. This is for SEO and external links |
41
|
|
|
if ($trick->getSlug() !== $slug) { |
42
|
|
|
return $this->correctSlug($trick); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$commentForm = $this->createForm(CommentFormType::class); |
46
|
|
|
|
47
|
|
|
$page = $request->get('page') ?? 1; |
48
|
|
|
|
49
|
|
|
/** @var Paginator $tricks */ |
50
|
|
|
$comments = $this->repository->findLatestEdited($trick->getId(), $page); |
51
|
|
|
|
52
|
|
|
$nextPage = $this->pagePagination->nextPage($comments, $page, Comment::NUMBER_OF_DISPLAYED_COMMENTS); |
53
|
|
|
|
54
|
|
|
if ($request->isXmlHttpRequest()) { |
55
|
|
|
$render = $this->renderView('comment/_comment-line.html.twig', [ |
56
|
|
|
'comments' => $comments, |
57
|
|
|
]); |
58
|
|
|
$jsonResponse = array( |
59
|
|
|
'render' => $render, |
60
|
|
|
'nextPage' => $nextPage, |
61
|
|
|
'nextPageUrl' => $this->generateUrl('trick.show', |
62
|
|
|
array('page' => $nextPage, 'id' => $trick->getId(), 'slug' => $trick->getSlug())), |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
return new JsonResponse($jsonResponse); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
return $this->render('trick/show.html.twig', [ |
70
|
|
|
'trick' => $trick, |
71
|
|
|
'commentForm' => $commentForm->createView(), |
72
|
|
|
'actionPath' => $this->generateUrl('comment.create', ['id' => $trick->getId()]), |
73
|
|
|
'comments' => $comments, |
74
|
|
|
'nextPage' => $nextPage, |
75
|
|
|
]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Adding a route with only the ID that redirects to the slugged route |
81
|
|
|
* @Route("/trick/{id}", name="trick.show.id", requirements={"id"="\d+"}) |
82
|
|
|
* @return RedirectResponse |
83
|
|
|
*/ |
84
|
|
|
public function showOnlyId(Trick $trick) |
85
|
|
|
{ |
86
|
|
|
return $this->correctSlug($trick); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Correcting the url and redirecting |
91
|
|
|
* @param Trick $trick |
92
|
|
|
* @return RedirectResponse |
93
|
|
|
*/ |
94
|
|
|
private function correctSlug(Trick $trick) |
95
|
|
|
{ |
96
|
|
|
return $this->redirectToRoute('trick.show', [ |
97
|
|
|
'id' => $trick->getId(), |
98
|
|
|
'slug' => $trick->getSlug() |
99
|
|
|
], 301); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
} |
103
|
|
|
|