1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller\Trick\Admin; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use App\Entity\Trick; |
7
|
|
|
use App\Event\Trick\TrickDeletedEvent; |
8
|
|
|
use App\Event\Trick\TrickEditedEvent; |
9
|
|
|
use App\Form\TrickType; |
10
|
|
|
use App\History\TrickHistory; |
11
|
|
|
use App\Repository\TagRepository; |
12
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
13
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
14
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
17
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class TrickEditController |
21
|
|
|
* @package App\Controller\Edit |
22
|
|
|
* |
23
|
|
|
* Require the user to be connected for everything here |
24
|
|
|
* @IsGranted("ROLE_USER") |
25
|
|
|
*/ |
26
|
|
|
class EditTrickController extends AbstractController |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var EventDispatcherInterface |
30
|
|
|
*/ |
31
|
|
|
private $dispatcher; |
32
|
|
|
/** |
33
|
|
|
* @var TrickHistory |
34
|
|
|
*/ |
35
|
|
|
private $trickHistory; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var TagRepository |
39
|
|
|
*/ |
40
|
|
|
private $tagRepository; |
41
|
|
|
|
42
|
|
|
public function __construct(EventDispatcherInterface $dispatcher, TrickHistory $trickHistory, TagRepository $tagRepository ) |
43
|
|
|
{ |
44
|
|
|
$this->dispatcher = $dispatcher; |
45
|
|
|
$this->trickHistory = $trickHistory; |
46
|
|
|
$this->tagRepository = $tagRepository; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @Route("/trick/edit/{id}", name="trick.edit") |
51
|
|
|
*/ |
52
|
|
|
public function edit(Trick $trick, Request $request) |
53
|
|
|
{ |
54
|
|
|
|
55
|
|
|
$form = $this->createForm(TrickType::class, $trick); |
56
|
|
|
$form |
57
|
|
|
->add('delete', SubmitType::class, [ |
58
|
|
|
'label' => 'Delete', |
59
|
|
|
'attr' => [ |
60
|
|
|
'class' => 'waves-effect waves-light btn right mr-2', |
61
|
|
|
'onclick' => 'return confirm(\'are you sure?\')', |
62
|
|
|
] |
63
|
|
|
]); |
64
|
|
|
|
65
|
|
|
$form->handleRequest($request); |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
if ($form->getClickedButton() && $form->getClickedButton()->getName() === 'delete') { |
|
|
|
|
69
|
|
|
|
70
|
|
|
$event = new TrickDeletedEvent($trick); |
71
|
|
|
$this->dispatcher->dispatch(TrickDeletedEvent::NAME, $event); |
72
|
|
|
|
73
|
|
|
return $this->redirectToRoute('home'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
77
|
|
|
|
78
|
|
|
$event = new TrickEditedEvent($trick); |
79
|
|
|
$this->dispatcher->dispatch(TrickEditedEvent::NAME, $event); |
80
|
|
|
|
81
|
|
|
return $this->redirectToRoute('trick.show', [ |
82
|
|
|
'id' => $trick->getId(), |
83
|
|
|
'slug' => $trick->getSlug(), |
84
|
|
|
]); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this->render('trick/admin/edit.html.twig', [ |
88
|
|
|
'allTags' => $this->tagRepository->findAll(), |
89
|
|
|
'trick' => $trick, |
90
|
|
|
'form' => $form->createView(), |
91
|
|
|
]); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
} |