|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SumoCoders\FrameworkMultiUserBundle\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
|
6
|
|
|
use SumoCoders\FrameworkCoreBundle\BreadCrumb\BreadCrumbBuilder; |
|
7
|
|
|
use SumoCoders\FrameworkMultiUserBundle\Command\Handler; |
|
8
|
|
|
use SumoCoders\FrameworkMultiUserBundle\Form\DeleteType; |
|
9
|
|
|
use SumoCoders\FrameworkMultiUserBundle\User\Interfaces\UserRepository; |
|
10
|
|
|
use Symfony\Component\Form\Form; |
|
11
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; |
|
15
|
|
|
use Symfony\Component\Routing\Router; |
|
16
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* This class handles all the user actions. |
|
20
|
|
|
* Register a service for each action. |
|
21
|
|
|
*/ |
|
22
|
|
|
class UserController |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var FormFactoryInterface */ |
|
25
|
|
|
private $formFactory; |
|
26
|
|
|
|
|
27
|
|
|
/** @var Router */ |
|
28
|
|
|
private $router; |
|
29
|
|
|
|
|
30
|
|
|
/** @var FlashBagInterface */ |
|
31
|
|
|
private $flashBag; |
|
32
|
|
|
|
|
33
|
|
|
/** @var TranslatorInterface */ |
|
34
|
|
|
private $translator; |
|
35
|
|
|
|
|
36
|
|
|
/** @var Handler */ |
|
37
|
|
|
private $handler; |
|
38
|
|
|
|
|
39
|
|
|
/** @var string */ |
|
40
|
|
|
private $form; |
|
41
|
|
|
|
|
42
|
|
|
/** @var UserRepository */ |
|
43
|
|
|
private $userRepository; |
|
44
|
|
|
|
|
45
|
|
|
/** @var BreadCrumbBuilder */ |
|
46
|
|
|
private $breadcrumbBuilder; |
|
47
|
|
|
|
|
48
|
|
|
/** @var array */ |
|
49
|
|
|
private $breadcrumbs; |
|
50
|
|
|
|
|
51
|
|
|
/** @var string */ |
|
52
|
|
|
private $redirectRoute; |
|
53
|
|
|
|
|
54
|
|
|
public function __construct( |
|
55
|
|
|
FormFactoryInterface $formFactory, |
|
56
|
|
|
Router $router, |
|
57
|
|
|
FlashBagInterface $flashBag, |
|
58
|
|
|
TranslatorInterface $translator, |
|
59
|
|
|
string $form, |
|
60
|
|
|
Handler $handler, |
|
61
|
|
|
UserRepository $userRepository, |
|
62
|
|
|
BreadCrumbBuilder $breadcrumbBuilder, |
|
63
|
|
|
array $breadcrumbs, |
|
64
|
|
|
string $redirectRoute = null |
|
65
|
|
|
) { |
|
66
|
|
|
$this->formFactory = $formFactory; |
|
67
|
|
|
$this->router = $router; |
|
68
|
|
|
$this->flashBag = $flashBag; |
|
69
|
|
|
$this->translator = $translator; |
|
70
|
|
|
$this->form = $form; |
|
71
|
|
|
$this->handler = $handler; |
|
72
|
|
|
$this->userRepository = $userRepository; |
|
73
|
|
|
$this->breadcrumbBuilder = $breadcrumbBuilder; |
|
74
|
|
|
$this->breadcrumbs = $breadcrumbs; |
|
75
|
|
|
$this->redirectRoute = $redirectRoute; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param Request $request |
|
80
|
|
|
* @param int|null $id |
|
81
|
|
|
* |
|
82
|
|
|
* @Template() |
|
83
|
|
|
* |
|
84
|
|
|
* @return array|RedirectResponse |
|
85
|
|
|
*/ |
|
86
|
|
|
public function baseAction(Request $request, int $id = null) |
|
87
|
|
|
{ |
|
88
|
|
|
foreach ($this->breadcrumbs as $breadcrumb) { |
|
89
|
|
|
$uri = ''; |
|
90
|
|
|
if ($breadcrumb['route'] !== '') { |
|
91
|
|
|
$uri = $this->router->generate($breadcrumb['route']); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$this->breadcrumbBuilder->addSimpleItem(ucfirst($this->translator->trans($breadcrumb['label'])), $uri); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$form = $this->getFormForId($id); |
|
98
|
|
|
$form->handleRequest($request); |
|
99
|
|
|
|
|
100
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
|
101
|
|
|
$command = $form->getData(); |
|
102
|
|
|
$this->handler->handle($command); |
|
103
|
|
|
|
|
104
|
|
|
$this->flashBag->add( |
|
105
|
|
|
'success', |
|
106
|
|
|
$this->translator->trans( |
|
107
|
|
|
$id === null ? 'sumocoders.multiuserbundle.flash.added' : 'sumocoders.multiuserbundle.flash.edited', |
|
108
|
|
|
['%user%' => $command->getEntity()->getDisplayName()] |
|
109
|
|
|
) |
|
110
|
|
|
); |
|
111
|
|
|
|
|
112
|
|
|
if ($this->redirectRoute !== null) { |
|
113
|
|
|
return new RedirectResponse($this->router->generate($this->redirectRoute)); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
if ($id !== null) { |
|
118
|
|
|
$deleteForm = $this->formFactory->create(DeleteType::class, $form->getData()); |
|
119
|
|
|
|
|
120
|
|
|
return [ |
|
121
|
|
|
'form' => $form->createView(), |
|
122
|
|
|
'deleteForm' => $deleteForm->createView(), |
|
123
|
|
|
'user' => $form->getData()->getEntity(), |
|
124
|
|
|
]; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return ['form' => $form->createView()]; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
private function getFormForId(?int $id = null): Form |
|
131
|
|
|
{ |
|
132
|
|
|
if ($id === null) { |
|
133
|
|
|
return $this->formFactory->create($this->form); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
$user = $this->userRepository->find((int) $id); |
|
137
|
|
|
$dataTransferObjectClass = $this->form::getDataTransferObjectClass(); |
|
|
|
|
|
|
138
|
|
|
$dataTransferObject = $dataTransferObjectClass::fromUser($user); |
|
139
|
|
|
|
|
140
|
|
|
return $this->formFactory->create($this->form, $dataTransferObject); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.