|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Event\Subscriber; |
|
4
|
|
|
|
|
5
|
|
|
use LAG\AdminBundle\Admin\ActionInterface; |
|
6
|
|
|
use LAG\AdminBundle\Admin\AdminInterface; |
|
7
|
|
|
use LAG\AdminBundle\Event\Events; |
|
8
|
|
|
use LAG\AdminBundle\Event\Events\FormEvent; |
|
9
|
|
|
use LAG\AdminBundle\Factory\DataProviderFactory; |
|
10
|
|
|
use LAG\AdminBundle\LAGAdminBundle; |
|
11
|
|
|
use LAG\AdminBundle\Utils\StringUtils; |
|
12
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
13
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
|
14
|
|
|
use Symfony\Component\Form\FormInterface; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
|
17
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
|
18
|
|
|
|
|
19
|
|
|
class FormSubscriber implements EventSubscriberInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var FormFactoryInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
private $formFactory; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var DataProviderFactory |
|
28
|
|
|
*/ |
|
29
|
|
|
private $dataProviderFactory; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var Session |
|
33
|
|
|
*/ |
|
34
|
|
|
private $session; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var TranslatorInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
private $translator; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return array |
|
43
|
|
|
*/ |
|
44
|
|
|
public static function getSubscribedEvents() |
|
45
|
|
|
{ |
|
46
|
|
|
return [ |
|
47
|
|
|
Events::CREATE_FORM => 'onCreateForm', |
|
48
|
|
|
Events::HANDLE_FORM => 'onHandleForm', |
|
49
|
|
|
]; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* FormSubscriber constructor. |
|
54
|
|
|
* |
|
55
|
|
|
* @param FormFactoryInterface $formFactory |
|
56
|
|
|
* @param DataProviderFactory $dataProviderFactory |
|
57
|
|
|
* @param Session $session |
|
58
|
|
|
* @param TranslatorInterface $translator |
|
59
|
|
|
*/ |
|
60
|
|
|
public function __construct( |
|
61
|
|
|
FormFactoryInterface $formFactory, |
|
62
|
|
|
DataProviderFactory $dataProviderFactory, |
|
63
|
|
|
Session $session, |
|
64
|
|
|
TranslatorInterface $translator |
|
65
|
|
|
) { |
|
66
|
|
|
$this->formFactory = $formFactory; |
|
67
|
|
|
$this->dataProviderFactory = $dataProviderFactory; |
|
68
|
|
|
$this->session = $session; |
|
69
|
|
|
$this->translator = $translator; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Create a form for the loaded entity. |
|
74
|
|
|
* |
|
75
|
|
|
* @param FormEvent $event |
|
76
|
|
|
*/ |
|
77
|
|
|
public function onCreateForm(FormEvent $event): void |
|
78
|
|
|
{ |
|
79
|
|
|
$admin = $event->getAdmin(); |
|
80
|
|
|
$action = $admin->getAction(); |
|
81
|
|
|
$configuration = $action->getConfiguration(); |
|
82
|
|
|
|
|
83
|
|
|
if (!$configuration->getParameter('use_form')) { |
|
84
|
|
|
return; |
|
85
|
|
|
} |
|
86
|
|
|
$entity = null; |
|
87
|
|
|
|
|
88
|
|
|
if (LAGAdminBundle::LOAD_STRATEGY_UNIQUE === $configuration->get('load_strategy')) { |
|
89
|
|
|
if (!$admin->getEntities()->isEmpty()) { |
|
90
|
|
|
$entity = $admin->getEntities()->first(); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
if ('create' === $action->getName()) { |
|
95
|
|
|
$form = $this->createEntityForm($admin, $entity); |
|
96
|
|
|
$event->addForm($form, 'entity'); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
if ('edit' === $action->getName()) { |
|
100
|
|
|
$form = $this->createEntityForm($admin, $entity); |
|
101
|
|
|
$event->addForm($form, 'entity'); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
if ('delete' === $action->getName()) { |
|
105
|
|
|
$form = $this->createDeleteForm($action, $entity); |
|
106
|
|
|
$event->addForm($form, 'delete'); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* When the HANDLE_FORM event is dispatched, we handle the form according to the current action. |
|
112
|
|
|
* |
|
113
|
|
|
* @param FormEvent $event |
|
114
|
|
|
*/ |
|
115
|
|
|
public function onHandleForm(FormEvent $event): void |
|
116
|
|
|
{ |
|
117
|
|
|
$admin = $event->getAdmin(); |
|
118
|
|
|
$action = $admin->getAction(); |
|
119
|
|
|
|
|
120
|
|
|
if ('delete' === $action->getName()) { |
|
121
|
|
|
|
|
122
|
|
|
if (!$admin->hasForm('delete')) { |
|
123
|
|
|
return; |
|
124
|
|
|
} |
|
125
|
|
|
$form = $admin->getForm('delete'); |
|
126
|
|
|
$this->handleDeleteForm($event->getRequest(), $form, $admin); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
private function createEntityForm(AdminInterface $admin, $entity = null): FormInterface |
|
131
|
|
|
{ |
|
132
|
|
|
if (!$entity) { |
|
133
|
|
|
$dataProvider = $this |
|
134
|
|
|
->dataProviderFactory |
|
135
|
|
|
->get($admin->getConfiguration()->get('data_provider')); |
|
136
|
|
|
$entity = $dataProvider->create($admin); |
|
137
|
|
|
} |
|
138
|
|
|
$form = $this |
|
139
|
|
|
->formFactory |
|
140
|
|
|
->create($admin->getConfiguration()->getParameter('form'), $entity) |
|
141
|
|
|
; |
|
142
|
|
|
|
|
143
|
|
|
return $form; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
private function createDeleteForm(ActionInterface $action, $entity): FormInterface |
|
147
|
|
|
{ |
|
148
|
|
|
$form = $this |
|
149
|
|
|
->formFactory |
|
150
|
|
|
->create($action->getConfiguration()->getParameter('form'), $entity) |
|
151
|
|
|
; |
|
152
|
|
|
|
|
153
|
|
|
return $form; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
private function handleDeleteForm(Request $request, FormInterface $form, AdminInterface $admin) |
|
157
|
|
|
{ |
|
158
|
|
|
$form->handleRequest($request); |
|
159
|
|
|
|
|
160
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
|
161
|
|
|
$dataProvider = $this |
|
162
|
|
|
->dataProviderFactory |
|
163
|
|
|
->get($admin->getConfiguration()->get('data_provider')) |
|
164
|
|
|
; |
|
165
|
|
|
$dataProvider->delete($admin); |
|
166
|
|
|
|
|
167
|
|
|
$message = StringUtils::getTranslationKey( |
|
168
|
|
|
$admin->getConfiguration()->get('translation_pattern'), |
|
169
|
|
|
$admin->getName(), |
|
170
|
|
|
'delete_success' |
|
171
|
|
|
); |
|
172
|
|
|
$this->session->getFlashBag()->add('success', $this->translator->trans($message)); |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|