|
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 App\Search\TrickSearch; |
|
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
12
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
13
|
|
|
|
|
14
|
|
|
class SearchTrickController extends AbstractController |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var TrickRepository |
|
19
|
|
|
*/ |
|
20
|
|
|
private $trickRepository; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var CategoryRepository |
|
23
|
|
|
*/ |
|
24
|
|
|
private $categoryRepository; |
|
25
|
|
|
/** |
|
26
|
|
|
* @var TrickSearch |
|
27
|
|
|
*/ |
|
28
|
|
|
private $search; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct(TrickRepository $trickRepository, CategoryRepository $categoryRepository, TrickSearch $search) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->trickRepository = $trickRepository; |
|
33
|
|
|
$this->categoryRepository = $categoryRepository; |
|
34
|
|
|
$this->search = $search; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @Route("/search", name="trick.searchTrick", methods={"POST"}) |
|
39
|
|
|
* @param Request $request |
|
40
|
|
|
* @return Response |
|
41
|
|
|
* Get the results from the search query |
|
42
|
|
|
*/ |
|
43
|
|
|
public function searchTrick(Request $request) |
|
44
|
|
|
{ |
|
45
|
|
|
$submittedToken = $request->request->get('_token'); |
|
46
|
|
|
|
|
47
|
|
|
if (!$this->isCsrfTokenValid('search-trick', $submittedToken)) { |
|
48
|
|
|
throw new RedirectException($this->generateUrl('home'), 'Bad CSRF Token'); |
|
49
|
|
|
} |
|
50
|
|
|
$categories = $this->categoryRepository->findAll(); |
|
51
|
|
|
$searchTerm = $request->request->get('search_trick'); |
|
52
|
|
|
|
|
53
|
|
|
$tricks = $this->search->searchTricks($searchTerm); |
|
54
|
|
|
|
|
55
|
|
|
return $this->render('trick/search.html.twig', [ |
|
56
|
|
|
'tricks' => $tricks, |
|
57
|
|
|
'categories' => $categories, |
|
58
|
|
|
'categoryId' =>"", |
|
59
|
|
|
'searchTerm' =>$searchTerm, |
|
60
|
|
|
]); |
|
61
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|