|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Form\Handler; |
|
4
|
|
|
|
|
5
|
|
|
use LAG\AdminBundle\Admin\AdminInterface; |
|
6
|
|
|
use LAG\AdminBundle\Routing\RouteNameGenerator; |
|
7
|
|
|
use Symfony\Bundle\FrameworkBundle\Routing\Router; |
|
8
|
|
|
use Symfony\Component\Form\FormInterface; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
12
|
|
|
use Symfony\Component\Routing\RouterInterface; |
|
13
|
|
|
use Twig_Environment; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Create a response from the form data. |
|
17
|
|
|
*/ |
|
18
|
|
|
class EditFormHandler |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var Twig_Environment |
|
22
|
|
|
*/ |
|
23
|
|
|
private $twig; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var Router |
|
27
|
|
|
*/ |
|
28
|
|
|
private $router; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var RequestStack |
|
32
|
|
|
*/ |
|
33
|
|
|
private $requestStack; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* EditFormHandler constructor. |
|
37
|
|
|
* |
|
38
|
|
|
* @param Twig_Environment $twig |
|
39
|
|
|
* @param RouterInterface $router |
|
40
|
|
|
* @param RequestStack $requestStack |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct(Twig_Environment $twig, RouterInterface $router, RequestStack $requestStack) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->twig = $twig; |
|
45
|
|
|
$this->router = $router; |
|
|
|
|
|
|
46
|
|
|
$this->requestStack = $requestStack; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Save the entity if the form is valid, and redirect to the list action if |
|
51
|
|
|
* required |
|
52
|
|
|
* |
|
53
|
|
|
* @param FormInterface $form |
|
54
|
|
|
* @param AdminInterface $admin |
|
55
|
|
|
* |
|
56
|
|
|
* @return RedirectResponse|Response |
|
57
|
|
|
*/ |
|
58
|
|
|
public function handle(FormInterface $form, AdminInterface $admin) |
|
59
|
|
|
{ |
|
60
|
|
|
$template = $admin |
|
61
|
|
|
->getView() |
|
62
|
|
|
->getConfiguration() |
|
63
|
|
|
->getParameter('template') |
|
64
|
|
|
; |
|
65
|
|
|
|
|
66
|
|
View Code Duplication |
if ($form->isValid()) { |
|
|
|
|
|
|
67
|
|
|
// if the form is valid, we save the entity |
|
68
|
|
|
$admin->save(); |
|
69
|
|
|
|
|
70
|
|
|
if ($this->shouldRedirect($admin)) { |
|
71
|
|
|
$generator = new RouteNameGenerator(); |
|
72
|
|
|
|
|
73
|
|
|
// if the redirect input is clicked and the list action exists, we redirect to the list action |
|
74
|
|
|
$url = $this |
|
75
|
|
|
->router |
|
76
|
|
|
->generate($generator->generate('list', $admin->getName(), $admin->getConfiguration())); |
|
77
|
|
|
|
|
78
|
|
|
return new RedirectResponse($url); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
// display the form after validation or not |
|
82
|
|
|
$content = $this |
|
83
|
|
|
->twig |
|
84
|
|
|
->render($template, [ |
|
85
|
|
|
'admin' => $admin, |
|
86
|
|
|
'form' => $form->createView() |
|
87
|
|
|
]) |
|
88
|
|
|
; |
|
89
|
|
|
|
|
90
|
|
|
return new Response($content); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Return true if the user should be redirected to the list action. |
|
95
|
|
|
* |
|
96
|
|
|
* @param AdminInterface $admin |
|
97
|
|
|
* |
|
98
|
|
|
* @return bool |
|
99
|
|
|
*/ |
|
100
|
|
|
private function shouldRedirect(AdminInterface $admin) |
|
101
|
|
|
{ |
|
102
|
|
|
$submit = $this |
|
103
|
|
|
->requestStack |
|
104
|
|
|
->getCurrentRequest() |
|
105
|
|
|
->get('submit') |
|
106
|
|
|
; |
|
107
|
|
|
|
|
108
|
|
|
if ('save-and-redirect' !== $submit) { |
|
109
|
|
|
return false; |
|
110
|
|
|
} |
|
111
|
|
|
$actions = $admin |
|
112
|
|
|
->getConfiguration() |
|
113
|
|
|
->getParameter('actions') |
|
114
|
|
|
; |
|
115
|
|
|
|
|
116
|
|
|
if (!key_exists('list', $actions)) { |
|
117
|
|
|
return false; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return true; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.