1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller\Trick; |
4
|
|
|
|
5
|
|
|
use App\FlashMessage\AddFlashTrait; |
6
|
|
|
use App\FlashMessage\FlashMessageCategory; |
7
|
|
|
use App\Repository\CategoryRepository; |
8
|
|
|
use App\Repository\TrickRepository; |
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
11
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
12
|
|
|
|
13
|
|
|
class SearchTrickController extends AbstractController |
14
|
|
|
{ |
15
|
|
|
use AddFlashTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var TrickRepository |
19
|
|
|
*/ |
20
|
|
|
private $trickRepository; |
21
|
|
|
/** |
22
|
|
|
* @var CategoryRepository |
23
|
|
|
*/ |
24
|
|
|
private $categoryRepository; |
25
|
|
|
|
26
|
|
|
public function __construct(TrickRepository $trickRepository, CategoryRepository $categoryRepository) |
27
|
|
|
{ |
28
|
|
|
$this->trickRepository = $trickRepository; |
29
|
|
|
$this->categoryRepository = $categoryRepository; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @Route("/search", name="trick.searchTrick", methods={"POST"}) |
34
|
|
|
* @param Request $request |
35
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
36
|
|
|
* Get the results from the search query |
37
|
|
|
*/ |
38
|
|
|
public function searchTrick(Request $request) |
39
|
|
|
{ |
40
|
|
|
$submittedToken = $request->request->get('_token'); |
41
|
|
|
|
42
|
|
|
if ($this->isCsrfTokenValid('search-trick', $submittedToken)) { |
43
|
|
|
$categories = $this->categoryRepository->findAll(); |
44
|
|
|
$searchTerm = $request->request->get('search_trick'); |
45
|
|
|
$tricks = $this->trickRepository->findBySearchQuery($searchTerm); |
46
|
|
|
return $this->render('trick/search.html.twig', [ |
47
|
|
|
'tricks' => $tricks, |
48
|
|
|
'categories' => $categories, |
49
|
|
|
]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$this->addFlash(FlashMessageCategory::ERROR, "Bad CSRF Token"); |
53
|
|
|
return $this->redirectToRoute('home'); |
54
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @Route("/search/{categoryId}", name="trick.search", methods={"GET"}) |
59
|
|
|
* @param string $categoryId |
60
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
61
|
|
|
* show tricks in category |
62
|
|
|
*/ |
63
|
|
|
public function search($categoryId = "") |
64
|
|
|
{ |
65
|
|
|
$categories = $this->categoryRepository->findAll(); |
66
|
|
|
$criteria = array(); |
67
|
|
|
if ($categoryId !== "") { |
68
|
|
|
$criteria = array('category' => $categoryId); |
69
|
|
|
} |
70
|
|
|
$tricks = $this->trickRepository->findBy($criteria); |
71
|
|
|
return $this->render('trick/search.html.twig', [ |
72
|
|
|
'tricks' => $tricks, |
73
|
|
|
'categories' => $categories, |
74
|
|
|
]); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|