/
FormSubmissionsController.php
205 lines (177 loc) · 8.21 KB
/
FormSubmissionsController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
namespace Kunstmaan\FormBundle\Controller;
use Doctrine\ORM\EntityManager;
use Kunstmaan\AdminBundle\FlashMessages\FlashTypes;
use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionMap;
use Kunstmaan\AdminListBundle\AdminList\AdminList;
use Kunstmaan\AdminListBundle\AdminList\ExportList;
use Kunstmaan\FormBundle\AdminList\FormPageAdminListConfigurator;
use Kunstmaan\FormBundle\AdminList\FormSubmissionAdminListConfigurator;
use Kunstmaan\FormBundle\AdminList\FormSubmissionExportListConfigurator;
use Kunstmaan\FormBundle\Entity\FormSubmission;
use Kunstmaan\FormBundle\Entity\FormSubmissionField;
use Kunstmaan\NodeBundle\Entity\Node;
use Kunstmaan\NodeBundle\Entity\NodeTranslation;
use Kunstmaan\UtilitiesBundle\Helper\SlugifierInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* The controller which will handle everything related with form pages and form submissions
*
* @final since 5.9
*/
class FormSubmissionsController extends Controller
{
/**
* The index action will use an admin list to list all the form pages
*
* @Route("/", name="KunstmaanFormBundle_formsubmissions")
* @Template("@KunstmaanAdminList/Default/list.html.twig")
*
* @return array
*/
public function indexAction(Request $request)
{
/* @var EntityManager $em */
$em = $this->getDoctrine()->getManager();
$aclHelper = $this->container->get('kunstmaan_admin.acl.helper');
/* @var AdminList $adminList */
$adminList = $this->get('kunstmaan_adminlist.factory')->createList(
new FormPageAdminListConfigurator($em, $aclHelper, PermissionMap::PERMISSION_VIEW)
);
$adminList->bindRequest($request);
return ['adminlist' => $adminList];
}
/**
* The list action will use an admin list to list all the form submissions related to the given $nodeTranslationId
*
* @param int $nodeTranslationId
*
* @Route("/list/{nodeTranslationId}", requirements={"nodeTranslationId" = "\d+"},
* name="KunstmaanFormBundle_formsubmissions_list", methods={"GET", "POST"})
* @Template("@KunstmaanForm/FormSubmissions/list.html.twig")
*
* @return array
*/
public function listAction(Request $request, $nodeTranslationId)
{
$em = $this->getDoctrine()->getManager();
$nodeTranslation = $em->getRepository(NodeTranslation::class)->find($nodeTranslationId);
/** @var AdminList $adminList */
$adminList = $this->get('kunstmaan_adminlist.factory')->createList(
new FormSubmissionAdminListConfigurator($em, $nodeTranslation, $this->getParameter('kunstmaan_form.deletable_formsubmissions'))
);
$adminList->bindRequest($request);
return ['nodetranslation' => $nodeTranslation, 'adminlist' => $adminList];
}
/**
* The edit action will be used to edit a given submission.
*
* @param int $nodeTranslationId The node translation id
* @param int $submissionId The submission id
*
* @Route("/list/{nodeTranslationId}/{submissionId}", requirements={"nodeTranslationId" = "\d+", "submissionId" =
* "\d+"}, name="KunstmaanFormBundle_formsubmissions_list_edit", methods={"GET", "POST"})
* @Template("@KunstmaanForm/FormSubmissions/edit.html.twig")
*
* @return array
*/
public function editAction($nodeTranslationId, $submissionId)
{
$em = $this->getDoctrine()->getManager();
$nodeTranslation = $em->getRepository(NodeTranslation::class)->find($nodeTranslationId);
$formSubmission = $em->getRepository(FormSubmission::class)->find($submissionId);
$request = $this->container->get('request_stack')->getCurrentRequest();
$deletableFormsubmission = $this->getParameter('kunstmaan_form.deletable_formsubmissions');
/** @var AdminList $adminList */
$adminList = $this->get('kunstmaan_adminlist.factory')->createList(
new FormSubmissionAdminListConfigurator($em, $nodeTranslation, $deletableFormsubmission)
);
$adminList->bindRequest($request);
return [
'nodetranslation' => $nodeTranslation,
'formsubmission' => $formSubmission,
'adminlist' => $adminList,
];
}
/**
* Export as CSV of all the form submissions for the given $nodeTranslationId
*
* @param int $nodeTranslationId
*
* @Route("/export/{nodeTranslationId}.{_format}", requirements={"nodeTranslationId" = "\d+","_format" =
* "csv|xlsx|ods"}, name="KunstmaanFormBundle_formsubmissions_export", methods={"GET"})
*
* @return Response
*/
public function exportAction($nodeTranslationId, $_format)
{
$em = $this->getDoctrine()->getManager();
/** @var NodeTranslation $nodeTranslation */
$nodeTranslation = $em->getRepository(NodeTranslation::class)->find($nodeTranslationId);
$translator = $this->get('translator');
/** @var ExportList $exportList */
$configurator = new FormSubmissionExportListConfigurator($em, $nodeTranslation, $translator);
$exportList = $this->get('kunstmaan_adminlist.factory')->createExportList($configurator);
return $this->get('kunstmaan_adminlist.service.export')->getDownloadableResponse($exportList, $_format);
}
/**
* @Route(
* "/{id}/delete",
* requirements={"id" = "\d+"},
* name="KunstmaanFormBundle_formsubmissions_delete",
* methods={"POST"}
* )
*
* @param int $id
*
* @return RedirectResponse
*/
public function deleteAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$submission = $em->getRepository(FormSubmission::class)->find($id);
$node = $em->getRepository(Node::class)->find($submission->getNode());
$nt = $node->getNodeTranslation($request->getLocale());
$configurator = new FormSubmissionAdminListConfigurator($em, $nt, $this->getParameter('kunstmaan_form.deletable_formsubmissions'));
/** @var SlugifierInterface $slugifier */
$slugifier = $this->container->get('kunstmaan_utilities.slugifier');
$csrfId = 'delete-' . $slugifier->slugify($configurator->getEntityName());
$hasToken = $request->request->has('token');
if (!$hasToken) {
@trigger_error(sprintf('Not passing as csrf token with id "%s" in field "token" is deprecated in KunstmaanFormBundle 5.10 and will be required in KunstmaanFormBundle 6.0. If you override the adminlist delete action template make sure to post a csrf token.', $csrfId), E_USER_DEPRECATED);
}
if ($hasToken && !$this->isCsrfTokenValid($csrfId, $request->request->get('token'))) {
$indexUrl = $configurator->getIndexUrl();
return new RedirectResponse($this->generateUrl($indexUrl['path'], $indexUrl['params'] ?? []));
}
$this->denyAccessUnlessGranted(PermissionMap::PERMISSION_DELETE, $node);
$url = $this->get('router')->generate(
'KunstmaanFormBundle_formsubmissions_list',
['nodeTranslationId' => $nt->getId()]
);
$fields = $em->getRepository(FormSubmissionField::class)->findBy(['formSubmission' => $submission]);
try {
foreach ($fields as $field) {
$em->remove($field);
}
$em->remove($submission);
$em->flush();
$this->addFlash(
FlashTypes::SUCCESS,
$this->get('translator')->trans('formsubmissions.delete.flash.success')
);
} catch (\Exception $e) {
$this->get('logger')->error($e->getMessage());
$this->addFlash(
FlashTypes::DANGER,
$this->get('translator')->trans('formsubmissions.delete.flash.error')
);
}
return new RedirectResponse($url);
}
}