1 | <?php |
||
2 | |||
3 | /* |
||
4 | * @copyright 2014 Mautic Contributors. All rights reserved |
||
5 | * @author Mautic |
||
6 | * |
||
7 | * @link http://mautic.org |
||
8 | * |
||
9 | * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html |
||
10 | */ |
||
11 | |||
12 | namespace Mautic\FormBundle\Controller; |
||
13 | |||
14 | use Mautic\CoreBundle\Controller\FormController as CommonFormController; |
||
15 | use Mautic\CoreBundle\Factory\PageHelperFactoryInterface; |
||
16 | use Mautic\FormBundle\Helper\FormUploader; |
||
17 | use Mautic\FormBundle\Model\FormModel; |
||
18 | use Mautic\FormBundle\Model\SubmissionResultLoader; |
||
19 | use Symfony\Component\Filesystem\Filesystem; |
||
20 | use Symfony\Component\HttpFoundation\BinaryFileResponse; |
||
21 | use Symfony\Component\HttpFoundation\ResponseHeaderBag; |
||
22 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
||
23 | |||
24 | class ResultController extends CommonFormController |
||
25 | { |
||
26 | public function __construct() |
||
27 | { |
||
28 | $this->setStandardParameters( |
||
0 ignored issues
–
show
|
|||
29 | 'form.submission', // model name |
||
30 | 'form:forms', // permission base |
||
31 | 'mautic_form', // route base |
||
32 | 'mautic.formresult', // session base |
||
33 | 'mautic.form.result', // lang string base |
||
34 | 'MauticFormBundle:Result', // template base |
||
35 | 'mautic_form', // activeLink |
||
36 | 'formresult' // mauticContent |
||
37 | ); |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * @param int $objectId |
||
42 | * @param int $page |
||
43 | * |
||
44 | * @return \Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response |
||
45 | */ |
||
46 | public function indexAction($objectId, $page = 1) |
||
47 | { |
||
48 | /** @var FormModel $formModel */ |
||
49 | $formModel = $this->getModel('form.form'); |
||
50 | $form = $formModel->getEntity($objectId); |
||
51 | $session = $this->get('session'); |
||
52 | $formPage = $session->get('mautic.form.page', 1); |
||
53 | $returnUrl = $this->generateUrl('mautic_form_index', ['page' => $formPage]); |
||
54 | $viewOnlyFields = $formModel->getCustomComponents()['viewOnlyFields']; |
||
55 | |||
56 | if (null === $form) { |
||
57 | //redirect back to form list |
||
58 | return $this->postActionRedirect( |
||
59 | [ |
||
60 | 'returnUrl' => $returnUrl, |
||
61 | 'viewParameters' => ['page' => $formPage], |
||
62 | 'contentTemplate' => 'MauticFormBundle:Form:index', |
||
63 | 'passthroughVars' => [ |
||
64 | 'activeLink' => 'mautic_form_index', |
||
65 | 'mauticContent' => 'form', |
||
66 | ], |
||
67 | 'flashes' => [ |
||
68 | [ |
||
69 | 'type' => 'error', |
||
70 | 'msg' => 'mautic.form.error.notfound', |
||
71 | 'msgVars' => ['%id%' => $objectId], |
||
72 | ], |
||
73 | ], |
||
74 | ] |
||
75 | ); |
||
76 | } elseif (!$this->get('mautic.security')->hasEntityAccess( |
||
77 | 'form:forms:viewown', |
||
78 | 'form:forms:viewother', |
||
79 | $form->getCreatedBy() |
||
80 | ) |
||
81 | ) { |
||
82 | return $this->accessDenied(); |
||
83 | } |
||
84 | |||
85 | if ('POST' == $this->request->getMethod()) { |
||
86 | $this->setListFilters($this->request->query->get('name')); |
||
87 | } |
||
88 | |||
89 | /** @var PageHelperFactoryInterface $pageHelperFacotry */ |
||
90 | $pageHelperFacotry = $this->get('mautic.page.helper.factory'); |
||
91 | $pageHelper = $pageHelperFacotry->make("mautic.formresult.{$objectId}", $page); |
||
92 | |||
93 | //set limits |
||
94 | $limit = $pageHelper->getLimit(); |
||
95 | $start = $pageHelper->getStart(); |
||
96 | |||
97 | // Set order direction to desc if not set |
||
98 | if (!$session->get('mautic.formresult.'.$objectId.'.orderbydir', null)) { |
||
99 | $session->set('mautic.formresult.'.$objectId.'.orderbydir', 'DESC'); |
||
100 | } |
||
101 | |||
102 | $orderBy = $session->get('mautic.formresult.'.$objectId.'.orderby', 's.date_submitted'); |
||
103 | $orderByDir = $session->get('mautic.formresult.'.$objectId.'.orderbydir', 'DESC'); |
||
104 | $filters = $session->get('mautic.formresult.'.$objectId.'.filters', []); |
||
105 | $model = $this->getModel('form.submission'); |
||
106 | |||
107 | if ($this->request->query->has('result')) { |
||
108 | // Force ID |
||
109 | $filters['s.id'] = ['column' => 's.id', 'expr' => 'like', 'value' => (int) $this->request->query->get('result'), 'strict' => false]; |
||
110 | $session->set("mautic.formresult.$objectId.filters", $filters); |
||
111 | } |
||
112 | |||
113 | //get the results |
||
114 | $entities = $model->getEntities( |
||
115 | [ |
||
116 | 'start' => $start, |
||
117 | 'limit' => $limit, |
||
118 | 'filter' => ['force' => $filters], |
||
119 | 'orderBy' => $orderBy, |
||
120 | 'orderByDir' => $orderByDir, |
||
121 | 'form' => $form, |
||
122 | 'withTotalCount' => true, |
||
123 | 'viewOnlyFields' => $viewOnlyFields, |
||
124 | 'simpleResults' => true, |
||
125 | ] |
||
126 | ); |
||
127 | |||
128 | $count = $entities['count']; |
||
129 | $results = $entities['results']; |
||
130 | unset($entities); |
||
131 | |||
132 | if ($count && $count < ($start + 1)) { |
||
133 | //the number of entities are now less then the current page so redirect to the last page |
||
134 | $lastPage = $pageHelper->countPage($count); |
||
135 | $pageHelper->rememberPage($lastPage); |
||
136 | $returnUrl = $this->generateUrl('mautic_form_results', ['objectId' => $objectId, 'page' => $lastPage]); |
||
137 | |||
138 | return $this->postActionRedirect( |
||
139 | [ |
||
140 | 'returnUrl' => $returnUrl, |
||
141 | 'viewParameters' => ['page' => $lastPage], |
||
142 | 'contentTemplate' => 'MauticFormBundle:Result:index', |
||
143 | 'passthroughVars' => [ |
||
144 | 'activeLink' => 'mautic_form_index', |
||
145 | 'mauticContent' => 'formresult', |
||
146 | ], |
||
147 | ] |
||
148 | ); |
||
149 | } |
||
150 | |||
151 | //set what page currently on so that we can return here if need be |
||
152 | $pageHelper->rememberPage($page); |
||
153 | |||
154 | return $this->delegateView( |
||
155 | [ |
||
156 | 'viewParameters' => [ |
||
157 | 'items' => $results, |
||
158 | 'filters' => $filters, |
||
159 | 'form' => $form, |
||
160 | 'viewOnlyFields' => $viewOnlyFields, |
||
161 | 'page' => $page, |
||
162 | 'totalCount' => $count, |
||
163 | 'limit' => $limit, |
||
164 | 'tmpl' => $this->request->isXmlHttpRequest() ? $this->request->get('tmpl', 'index') : 'index', |
||
165 | 'canDelete' => $this->get('mautic.security')->hasEntityAccess( |
||
166 | 'form:forms:editown', |
||
167 | 'form:forms:editother', |
||
168 | $form->getCreatedBy() |
||
169 | ), |
||
170 | ], |
||
171 | 'contentTemplate' => 'MauticFormBundle:Result:list.html.php', |
||
172 | 'passthroughVars' => [ |
||
173 | 'activeLink' => 'mautic_form_index', |
||
174 | 'mauticContent' => 'formresult', |
||
175 | 'route' => $this->generateUrl( |
||
176 | 'mautic_form_results', |
||
177 | [ |
||
178 | 'objectId' => $objectId, |
||
179 | 'page' => $page, |
||
180 | ] |
||
181 | ), |
||
182 | ], |
||
183 | ] |
||
184 | ); |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * @param int $submissionId |
||
189 | * @param string $field |
||
190 | * |
||
191 | * @return BinaryFileResponse |
||
192 | */ |
||
193 | public function downloadFileAction($submissionId, $field) |
||
194 | { |
||
195 | /** @var SubmissionResultLoader $submissionResultLoader */ |
||
196 | $submissionResultLoader = $this->getModel('form.submission_result_loader'); |
||
197 | $submission = $submissionResultLoader->getSubmissionWithResult($submissionId); |
||
198 | |||
199 | if (!$submission) { |
||
200 | throw $this->createNotFoundException(); |
||
201 | } |
||
202 | |||
203 | $results = $submission->getResults(); |
||
204 | $fieldEntity = $submission->getFieldByAlias($field); |
||
205 | |||
206 | if (empty($results[$field]) || null === $fieldEntity) { |
||
207 | throw $this->createNotFoundException(); |
||
208 | } |
||
209 | |||
210 | if (empty($fieldEntity->getProperties()['public']) && !$this->get('mautic.security')->hasEntityAccess( |
||
211 | 'form:forms:viewown', |
||
212 | 'form:forms:viewother', |
||
213 | $submission->getForm()->getCreatedBy()) |
||
214 | ) { |
||
215 | return $this->accessDenied(); |
||
216 | } |
||
217 | |||
218 | /** @var FormUploader $formUploader */ |
||
219 | $formUploader = $this->get('mautic.form.helper.form_uploader'); |
||
220 | |||
221 | $fileName = $results[$field]; |
||
222 | $file = $formUploader->getCompleteFilePath($fieldEntity, $fileName); |
||
223 | |||
224 | $fs = new Filesystem(); |
||
225 | if (!$fs->exists($file)) { |
||
226 | throw $this->createNotFoundException(); |
||
227 | } |
||
228 | |||
229 | $response = new BinaryFileResponse($file); |
||
230 | $response::trustXSendfileTypeHeader(); |
||
231 | $response->setContentDisposition( |
||
232 | ResponseHeaderBag::DISPOSITION_ATTACHMENT, |
||
233 | $fileName |
||
234 | ); |
||
235 | |||
236 | return $response; |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * @param int $objectId |
||
241 | * @param string $format |
||
242 | * |
||
243 | * @return \Symfony\Component\HttpFoundation\StreamedResponse |
||
244 | * |
||
245 | * @throws \Exception |
||
246 | */ |
||
247 | public function exportAction($objectId, $format = 'csv') |
||
248 | { |
||
249 | $formModel = $this->getModel('form.form'); |
||
250 | $form = $formModel->getEntity($objectId); |
||
251 | $session = $this->get('session'); |
||
252 | $formPage = $session->get('mautic.form.page', 1); |
||
253 | $returnUrl = $this->generateUrl('mautic_form_index', ['page' => $formPage]); |
||
254 | |||
255 | if (null === $form) { |
||
256 | //redirect back to form list |
||
257 | return $this->postActionRedirect( |
||
258 | [ |
||
259 | 'returnUrl' => $returnUrl, |
||
260 | 'viewParameters' => ['page' => $formPage], |
||
261 | 'contentTemplate' => 'MauticFormBundle:Form:index', |
||
262 | 'passthroughVars' => [ |
||
263 | 'activeLink' => 'mautic_form_index', |
||
264 | 'mauticContent' => 'form', |
||
265 | ], |
||
266 | 'flashes' => [ |
||
267 | [ |
||
268 | 'type' => 'error', |
||
269 | 'msg' => 'mautic.form.error.notfound', |
||
270 | 'msgVars' => ['%id%' => $objectId], |
||
271 | ], |
||
272 | ], |
||
273 | ] |
||
274 | ); |
||
275 | } elseif (!$this->get('mautic.security')->hasEntityAccess( |
||
276 | 'form:forms:viewown', |
||
277 | 'form:forms:viewother', |
||
278 | $form->getCreatedBy() |
||
279 | ) |
||
280 | ) { |
||
281 | return $this->accessDenied(); |
||
282 | } |
||
283 | |||
284 | $orderBy = $session->get('mautic.formresult.'.$objectId.'.orderby', 's.date_submitted'); |
||
285 | $orderByDir = $session->get('mautic.formresult.'.$objectId.'.orderbydir', 'DESC'); |
||
286 | $filters = $session->get('mautic.formresult.'.$objectId.'.filters', []); |
||
287 | |||
288 | $args = [ |
||
289 | 'limit' => false, |
||
290 | 'filter' => ['force' => $filters], |
||
291 | 'orderBy' => $orderBy, |
||
292 | 'orderByDir' => $orderByDir, |
||
293 | 'form' => $form, |
||
294 | ]; |
||
295 | |||
296 | /** @var \Mautic\FormBundle\Model\SubmissionModel $model */ |
||
297 | $model = $this->getModel('form.submission'); |
||
298 | |||
299 | return $model->exportResults($format, $form, $args); |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * Delete a form result. |
||
304 | * |
||
305 | * @return array|\Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse |
||
306 | */ |
||
307 | public function deleteAction() |
||
308 | { |
||
309 | $formId = $this->request->get('formId', 0); |
||
310 | $objectId = $this->request->get('objectId', 0); |
||
311 | $session = $this->get('session'); |
||
312 | $page = $session->get("mautic.formresult.{$formId}.page", 1); |
||
313 | $flashes = []; |
||
314 | |||
315 | if ('POST' == $this->request->getMethod()) { |
||
316 | $model = $this->getModel('form.submission'); |
||
317 | |||
318 | // Find the result |
||
319 | $entity = $model->getEntity($objectId); |
||
320 | |||
321 | if (null === $entity) { |
||
322 | $flashes[] = [ |
||
323 | 'type' => 'error', |
||
324 | 'msg' => 'mautic.form.error.notfound', |
||
325 | 'msgVars' => ['%id%' => $objectId], |
||
326 | ]; |
||
327 | } elseif (!$this->get('mautic.security')->hasEntityAccess('form:forms:editown', 'form:forms:editother', $entity->getCreatedBy())) { |
||
328 | return $this->accessDenied(); |
||
329 | } else { |
||
330 | $id = $entity->getId(); |
||
331 | $model->deleteEntity($entity); |
||
332 | |||
333 | $flashes[] = [ |
||
334 | 'type' => 'notice', |
||
335 | 'msg' => 'mautic.core.notice.deleted', |
||
336 | 'msgVars' => [ |
||
337 | '%name%' => '#'.$id, |
||
338 | ], |
||
339 | ]; |
||
340 | } |
||
341 | } //else don't do anything |
||
342 | |||
343 | $viewParameters = [ |
||
344 | 'objectId' => $formId, |
||
345 | 'page' => $page, |
||
346 | ]; |
||
347 | |||
348 | return $this->postActionRedirect( |
||
349 | [ |
||
350 | 'returnUrl' => $this->generateUrl('mautic_form_results', $viewParameters), |
||
351 | 'viewParameters' => $viewParameters, |
||
352 | 'contentTemplate' => 'MauticFormBundle:Result:index', |
||
353 | 'passthroughVars' => [ |
||
354 | 'mauticContent' => 'formresult', |
||
355 | ], |
||
356 | 'flashes' => $flashes, |
||
357 | ] |
||
358 | ); |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * @return \Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse |
||
363 | */ |
||
364 | public function batchDeleteAction() |
||
365 | { |
||
366 | return $this->batchDeleteStandard(); |
||
367 | } |
||
368 | |||
369 | /** |
||
370 | * @return string |
||
371 | */ |
||
372 | protected function getModelName() |
||
373 | { |
||
374 | return 'form.submission'; |
||
375 | } |
||
376 | |||
377 | /** |
||
378 | * @return string |
||
379 | */ |
||
380 | protected function getIndexRoute() |
||
381 | { |
||
382 | return 'mautic_form_results'; |
||
383 | } |
||
384 | |||
385 | /** |
||
386 | * @return string |
||
387 | */ |
||
388 | protected function getActionRoute() |
||
389 | { |
||
390 | return 'mautic_form_results_action'; |
||
391 | } |
||
392 | |||
393 | /** |
||
394 | * Set the main form ID as the objectId. |
||
395 | * |
||
396 | * @param string $route |
||
397 | * @param array $parameters |
||
398 | * @param int $referenceType |
||
399 | */ |
||
400 | public function generateUrl($route, $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH) |
||
401 | { |
||
402 | $formId = $this->getFormIdFromRequest($parameters); |
||
403 | switch ($route) { |
||
404 | case 'mautic_form_results_action': |
||
405 | $parameters['formId'] = $formId; |
||
406 | break; |
||
407 | case 'mautic_form_results': |
||
408 | $parameters['objectId'] = $formId; |
||
409 | break; |
||
410 | } |
||
411 | |||
412 | return parent::generateUrl($route, $parameters, $referenceType); |
||
413 | } |
||
414 | |||
415 | /** |
||
416 | * @param $action |
||
417 | */ |
||
418 | public function getPostActionRedirectArguments(array $args, $action) |
||
419 | { |
||
420 | switch ($action) { |
||
421 | case 'batchDelete': |
||
422 | $formId = $this->getFormIdFromRequest(); |
||
423 | $args['viewParameters']['objectId'] = $formId; |
||
424 | break; |
||
425 | } |
||
426 | |||
427 | return $args; |
||
428 | } |
||
429 | |||
430 | /** |
||
431 | * @param array $parameters |
||
432 | * |
||
433 | * @return mixed |
||
434 | */ |
||
435 | protected function getFormIdFromRequest($parameters = []) |
||
436 | { |
||
437 | if ($this->request->attributes->has('formId')) { |
||
438 | $formId = $this->request->attributes->get('formId'); |
||
439 | } elseif ($this->request->request->has('formId')) { |
||
440 | $formId = $this->request->request->get('formId'); |
||
441 | } else { |
||
442 | $objectId = isset($parameters['objectId']) ? $parameters['objectId'] : 0; |
||
443 | $formId = (isset($parameters['formId'])) ? $parameters['formId'] : $this->request->query->get('formId', $objectId); |
||
444 | } |
||
445 | |||
446 | return $formId; |
||
447 | } |
||
448 | } |
||
449 |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.