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