1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller\Trick; |
4
|
|
|
|
5
|
|
|
use App\Entity\Trick; |
6
|
|
|
use App\Pagination\PagePagination; |
7
|
|
|
use App\Repository\CategoryRepository; |
8
|
|
|
use App\Repository\TrickRepository; |
9
|
|
|
use Doctrine\ORM\Tools\Pagination\Paginator; |
10
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
11
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
13
|
|
|
use Symfony\Component\HttpFoundation\Response; |
14
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
15
|
|
|
|
16
|
|
|
class TricksByCategoryController extends AbstractController |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var TrickRepository |
21
|
|
|
*/ |
22
|
|
|
private $trickRepository; |
23
|
|
|
/** |
24
|
|
|
* @var CategoryRepository |
25
|
|
|
*/ |
26
|
|
|
private $categoryRepository; |
27
|
|
|
/** |
28
|
|
|
* @var PagePagination |
29
|
|
|
*/ |
30
|
|
|
private $pagePagination; |
31
|
|
|
|
32
|
|
|
public function __construct(TrickRepository $trickRepository, CategoryRepository $categoryRepository, PagePagination $pagePagination) |
33
|
|
|
{ |
34
|
|
|
$this->trickRepository = $trickRepository; |
35
|
|
|
$this->categoryRepository = $categoryRepository; |
36
|
|
|
$this->pagePagination = $pagePagination; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @Route("/trick/category/{categoryId}-{slug}", name="trick.search", methods={"GET"}) |
41
|
|
|
* @param Request $request |
42
|
|
|
* @param string $categoryId |
43
|
|
|
* @param string $slug |
44
|
|
|
* @return Response |
45
|
|
|
* show tricks in category |
46
|
|
|
*/ |
47
|
|
|
public function search(Request $request, $categoryId = "", $slug = "") |
48
|
|
|
{ |
49
|
|
|
$category = null; |
50
|
|
|
if ($categoryId !== "") { |
51
|
|
|
$category = $this->categoryRepository->find($categoryId); |
52
|
|
|
if ($category->getSlug() !== $slug) { |
53
|
|
|
return $this->redirectToRoute('trick.search', [ |
54
|
|
|
'id' => $category->getId(), |
55
|
|
|
'slug' => $category->getSlug() |
56
|
|
|
], 301); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$page = $request->get('page') ?? 1; |
61
|
|
|
|
62
|
|
|
/** @var Paginator $tricks */ |
63
|
|
|
$tricks = $this->trickRepository->findLatestEdited($page, (int)$categoryId); |
64
|
|
|
|
65
|
|
|
$nextPage = $this->pagePagination->nextPage($tricks, $page, Trick::NUMBER_OF_DISPLAYED_TRICKS); |
66
|
|
|
|
67
|
|
|
$categories = $this->categoryRepository->findAll(); |
68
|
|
|
|
69
|
|
|
if ($request->isXmlHttpRequest()) { |
70
|
|
|
$render = $this->renderView('trick/_trick-card.html.twig', [ |
71
|
|
|
'tricks' => $tricks, |
72
|
|
|
]); |
73
|
|
|
$jsonResponse = array( |
74
|
|
|
'render' => $render, |
75
|
|
|
'nextPage' => $nextPage, |
76
|
|
|
'nextPageUrl' => $this->generateUrl( |
77
|
|
|
'trick.search', |
78
|
|
|
array( |
79
|
|
|
'page' => $nextPage, |
80
|
|
|
'categoryId' => $categoryId, |
81
|
|
|
'slug' => $slug, |
82
|
|
|
) |
83
|
|
|
), |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
return new JsonResponse($jsonResponse); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $this->render('trick/category.html.twig', [ |
90
|
|
|
'tricks' => $tricks, |
91
|
|
|
'categories' => $categories, |
92
|
|
|
'categoryId' => $categoryId, |
93
|
|
|
'slug' => $slug, |
94
|
|
|
'page' => $page, |
95
|
|
|
'nextPage' => $nextPage, |
96
|
|
|
'category' => $category, |
97
|
|
|
]); |
98
|
|
|
} |
99
|
|
|
} |