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