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