1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Factory; |
4
|
|
|
|
5
|
|
|
use LAG\AdminBundle\Configuration\ActionConfiguration; |
6
|
|
|
use LAG\AdminBundle\Configuration\AdminConfiguration; |
7
|
|
|
use LAG\AdminBundle\Configuration\ApplicationConfigurationStorage; |
8
|
|
|
use LAG\AdminBundle\Exception\Exception; |
9
|
|
|
use LAG\AdminBundle\Routing\RoutingLoader; |
10
|
|
|
use LAG\AdminBundle\Utils\RedirectionUtils; |
11
|
|
|
use LAG\AdminBundle\View\RedirectView; |
12
|
|
|
use LAG\AdminBundle\View\View; |
13
|
|
|
use LAG\AdminBundle\View\ViewInterface; |
14
|
|
|
use Symfony\Component\Form\FormInterface; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
use Symfony\Component\Routing\RouterInterface; |
17
|
|
|
|
18
|
|
|
class ViewFactory |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var FieldFactory |
22
|
|
|
*/ |
23
|
|
|
private $fieldFactory; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var RouterInterface |
27
|
|
|
*/ |
28
|
|
|
private $router; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var ApplicationConfigurationStorage |
32
|
|
|
*/ |
33
|
|
|
private $storage; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* ViewFactory constructor. |
37
|
|
|
*/ |
38
|
10 |
|
public function __construct( |
39
|
|
|
FieldFactory $fieldFactory, |
40
|
|
|
RouterInterface $router, |
41
|
|
|
ApplicationConfigurationStorage $storage |
42
|
|
|
) { |
43
|
10 |
|
$this->fieldFactory = $fieldFactory; |
44
|
10 |
|
$this->router = $router; |
45
|
10 |
|
$this->storage = $storage; |
46
|
10 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Create a view for a given Admin and Action. |
50
|
|
|
* |
51
|
|
|
* @param string $actionName |
52
|
|
|
* @param string $adminName |
53
|
|
|
* @param mixed $entities |
54
|
|
|
* @param FormInterface[] $forms |
55
|
|
|
*/ |
56
|
6 |
|
public function create( |
57
|
|
|
Request $request, |
58
|
|
|
$actionName, |
59
|
|
|
$adminName, |
60
|
|
|
AdminConfiguration $adminConfiguration, |
61
|
|
|
ActionConfiguration $actionConfiguration, |
62
|
|
|
$entities, |
63
|
|
|
array $forms = [] |
64
|
|
|
): ViewInterface { |
65
|
6 |
|
if (key_exists('entity', $forms)) { |
66
|
4 |
|
$form = $forms['entity']; |
67
|
|
|
|
68
|
4 |
|
if ($this->shouldRedirect($form, $request, $adminConfiguration)) { |
69
|
2 |
|
return $this->createRedirection( |
70
|
2 |
|
$actionName, |
71
|
|
|
$adminName, |
72
|
|
|
$request, |
73
|
|
|
$adminConfiguration, |
74
|
|
|
$actionConfiguration, |
75
|
|
|
$form |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
$fields = $this |
80
|
4 |
|
->fieldFactory |
81
|
4 |
|
->createFields($actionConfiguration); |
82
|
4 |
|
$formViews = []; |
83
|
|
|
|
84
|
4 |
|
foreach ($forms as $identifier => $form) { |
85
|
4 |
|
$formViews[$identifier] = $form->createView(); |
86
|
|
|
} |
87
|
|
|
|
88
|
4 |
|
if ($request->isXmlHttpRequest()) { |
89
|
2 |
|
$template = $this->storage->getConfiguration()->get('ajax_template'); |
90
|
|
|
} else { |
91
|
2 |
|
$template = $this->storage->getConfiguration()->get('base_template'); |
92
|
|
|
} |
93
|
|
|
|
94
|
4 |
|
$view = new View( |
95
|
4 |
|
$actionName, |
96
|
|
|
$adminName, |
97
|
|
|
$actionConfiguration, |
98
|
|
|
$adminConfiguration, |
99
|
|
|
$template, |
100
|
|
|
$fields, |
101
|
|
|
$formViews |
102
|
|
|
); |
103
|
4 |
|
$view->setEntities($entities); |
104
|
|
|
|
105
|
4 |
|
return $view; |
106
|
|
|
} |
107
|
|
|
|
108
|
6 |
|
public function createRedirection( |
109
|
|
|
$actionName, |
110
|
|
|
$adminName, |
111
|
|
|
Request $request, |
112
|
|
|
AdminConfiguration $adminConfiguration, |
113
|
|
|
ActionConfiguration $actionConfiguration, |
114
|
|
|
FormInterface $form |
115
|
|
|
): RedirectView { |
116
|
6 |
|
if (RedirectionUtils::shouldRedirectToEdit($form, $request, $adminConfiguration)) { |
117
|
2 |
|
$view = new RedirectView( |
118
|
2 |
|
$actionName, |
119
|
|
|
$adminName, |
120
|
|
|
$actionConfiguration, |
121
|
|
|
$adminConfiguration |
122
|
|
|
); |
123
|
2 |
|
$view->setUrl($request->getUri()); |
124
|
|
|
|
125
|
2 |
|
return $view; |
126
|
|
|
} |
127
|
|
|
|
128
|
4 |
|
if (RedirectionUtils::shouldRedirectToList($form, $request, $adminConfiguration)) { |
129
|
2 |
|
$routeName = RoutingLoader::generateRouteName( |
130
|
2 |
|
$adminName, |
131
|
2 |
|
'list', |
132
|
2 |
|
$adminConfiguration->getParameter('routing_name_pattern') |
133
|
|
|
); |
134
|
2 |
|
$url = $this->router->generate($routeName); |
135
|
2 |
|
$view = new RedirectView( |
136
|
2 |
|
$actionName, |
137
|
|
|
$adminName, |
138
|
|
|
$actionConfiguration, |
139
|
|
|
$adminConfiguration |
140
|
|
|
); |
141
|
2 |
|
$view->setUrl($url); |
142
|
|
|
|
143
|
2 |
|
return $view; |
144
|
|
|
} |
145
|
|
|
|
146
|
2 |
|
throw new Exception('Unable to find a url to redirect'); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Return true if a redirection view should be created. |
151
|
|
|
*/ |
152
|
4 |
|
private function shouldRedirect(FormInterface $form, Request $request, AdminConfiguration $configuration): bool |
153
|
|
|
{ |
154
|
4 |
|
if (RedirectionUtils::shouldRedirectToEdit($form, $request, $configuration)) { |
155
|
|
|
return true; |
156
|
|
|
} |
157
|
|
|
|
158
|
4 |
|
if (RedirectionUtils::shouldRedirectToList($form, $request, $configuration)) { |
159
|
2 |
|
return true; |
160
|
|
|
} |
161
|
|
|
|
162
|
2 |
|
return false; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|