|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Action\Responder; |
|
4
|
|
|
|
|
5
|
|
|
use LAG\AdminBundle\Action\Configuration\ActionConfiguration; |
|
6
|
|
|
use LAG\AdminBundle\Admin\AdminInterface; |
|
7
|
|
|
use Symfony\Component\Form\FormInterface; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
10
|
|
|
|
|
11
|
|
View Code Duplication |
class EditResponder extends AbstractResponder |
|
|
|
|
|
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Display the edit form and redirect if required when the form is submitted. |
|
15
|
|
|
* |
|
16
|
|
|
* @param ActionConfiguration $configuration |
|
17
|
|
|
* @param AdminInterface $admin |
|
18
|
|
|
* @param FormInterface $form |
|
19
|
|
|
* @param $submitButtonName |
|
20
|
|
|
* |
|
21
|
|
|
* @return Response|RedirectResponse |
|
22
|
|
|
*/ |
|
23
|
3 |
|
public function respond( |
|
24
|
|
|
ActionConfiguration $configuration, |
|
25
|
|
|
AdminInterface $admin, |
|
26
|
|
|
FormInterface $form, |
|
27
|
|
|
$submitButtonName |
|
28
|
|
|
) { |
|
29
|
3 |
|
$template = $configuration->getParameter('template'); |
|
30
|
|
|
|
|
31
|
|
|
// if the form is submitted and validated, the user should be redirected |
|
32
|
3 |
|
if ($form->isSubmitted() && $form->isValid()) { |
|
33
|
|
|
// if the save button is pressed, the user will stay on the edit view |
|
34
|
2 |
|
if ('save' === $submitButtonName) { |
|
35
|
|
|
$url = $this |
|
36
|
1 |
|
->router |
|
37
|
1 |
|
->generate($admin->generateRouteName('edit'), [ |
|
38
|
1 |
|
'id' => $admin->getUniqueEntity()->getId(), |
|
39
|
|
|
]) |
|
40
|
|
|
; |
|
41
|
|
|
|
|
42
|
1 |
|
return new RedirectResponse($url); |
|
43
|
|
|
} else { |
|
44
|
|
|
// otherwise the user will be redirected to the list view |
|
45
|
|
|
$url = $this |
|
46
|
1 |
|
->router |
|
47
|
1 |
|
->generate($admin->generateRouteName('list')) |
|
48
|
|
|
; |
|
49
|
|
|
|
|
50
|
1 |
|
return new RedirectResponse($url); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
1 |
|
return $this->render($template, [ |
|
55
|
1 |
|
'admin' => $admin, |
|
56
|
1 |
|
'form' => $form->createView(), |
|
57
|
|
|
]); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.