]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Controllers/Crud/DynamicFieldsController.php
Fix redirection when dynamic file does not exists
[galette.git] / galette / lib / Galette / Controllers / Crud / DynamicFieldsController.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Galette dynamic fields controller
7 *
8 * PHP version 5
9 *
10 * Copyright © 2020-2023 The Galette Team
11 *
12 * This file is part of Galette (http://galette.tuxfamily.org).
13 *
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
26 *
27 * @category Controllers
28 * @package Galette
29 *
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2020-2023 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
33 * @link http://galette.tuxfamily.org
34 * @since Available since 0.9.4dev - 2020-05-02
35 */
36
37 namespace Galette\Controllers\Crud;
38
39 use Galette\Core\Galette;
40 use Galette\IO\File;
41 use Galette\Repository\DynamicFieldsSet;
42 use Throwable;
43 use Galette\Controllers\CrudController;
44 use Slim\Psr7\Request;
45 use Slim\Psr7\Response;
46 use Galette\DynamicFields\DynamicField;
47 use Analog\Analog;
48
49 /**
50 * Galette dynamic fields controller
51 *
52 * @category Controllers
53 * @name DynamicFieldsController
54 * @package Galette
55 * @author Johan Cwiklinski <johan@x-tnd.be>
56 * @copyright 2020-2023 The Galette Team
57 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
58 * @link http://galette.tuxfamily.org
59 * @since Available since 0.9.4dev - 2020-05-02
60 */
61
62 class DynamicFieldsController extends CrudController
63 {
64 // CRUD - Create
65
66 /**
67 * Add page
68 *
69 * @param Request $request PSR Request
70 * @param Response $response PSR Response
71 * @param string $form_name Form name
72 *
73 * @return Response
74 */
75 public function add(Request $request, Response $response, string $form_name = null): Response
76 {
77 $params = [
78 'page_title' => _T("Add field"),
79 'form_name' => $form_name,
80 'action' => 'add',
81 'perm_names' => DynamicField::getPermsNames(),
82 'mode' => (($request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest') ? 'ajax' : ''),
83 'field_type_names' => DynamicField::getFieldsTypesNames()
84 ];
85
86 if ($this->session->dynamicfieldtype) {
87 $params['df'] = $this->session->dynamicfieldtype;
88 $this->session->dynamicfieldtype = null;
89 }
90
91 // display page
92 $this->view->render(
93 $response,
94 'pages/configuration_dynamic_field_form.html.twig',
95 $params
96 );
97 return $response;
98 }
99
100 /**
101 * Add action
102 *
103 * @param Request $request PSR Request
104 * @param Response $response PSR Response
105 * @param string $form_name Form name
106 *
107 * @return Response
108 */
109 public function doAdd(Request $request, Response $response, string $form_name = null): Response
110 {
111 $post = $request->getParsedBody();
112 $post['form_name'] = $form_name;
113
114 $error_detected = [];
115 $warning_detected = [];
116
117 if (isset($post['cancel'])) {
118 return $response
119 ->withStatus(301)
120 ->withHeader('Location', $this->cancelUri($this->getArgs($request)));
121 }
122
123 $df = DynamicField::getFieldType($this->zdb, $post['field_type']);
124
125 try {
126 $df->store($post);
127 $error_detected = $df->getErrors();
128 $warning_detected = $df->getWarnings();
129 } catch (Throwable $e) {
130 $msg = 'An error occurred adding new dynamic field.';
131 Analog::log(
132 $msg . ' | ' .
133 $e->getMessage(),
134 Analog::ERROR
135 );
136 if (Galette::isDebugEnabled()) {
137 throw $e;
138 }
139 $error_detected[] = _T('An error occurred adding dynamic field :(');
140 }
141
142 //flash messages
143 if (count($error_detected) > 0) {
144 foreach ($error_detected as $error) {
145 $this->flash->addMessage(
146 'error_detected',
147 $error
148 );
149 }
150 } else {
151 $this->flash->addMessage(
152 'success_detected',
153 _T('Dynamic field has been successfully stored!')
154 );
155 }
156
157 if (count($warning_detected) > 0) {
158 foreach ($warning_detected as $warning) {
159 $this->flash->addMessage(
160 'warning_detected',
161 $warning
162 );
163 }
164 }
165
166 //handle redirections
167 if (count($error_detected) > 0) {
168 //something went wrong :'(
169 $this->session->dynamicfieldtype = $df;
170 return $response
171 ->withStatus(301)
172 ->withHeader(
173 'Location',
174 $this->routeparser->urlFor(
175 'addDynamicField',
176 ['form_name' => $form_name]
177 )
178 );
179 } else {
180 if (!$df instanceof \Galette\DynamicFields\Separator) {
181 return $response
182 ->withStatus(301)
183 ->withHeader(
184 'Location',
185 $this->routeparser->urlFor(
186 'editDynamicField',
187 [
188 'form_name' => $form_name,
189 'id' => $df->getId()
190 ]
191 )
192 );
193 }
194
195 return $response
196 ->withStatus(301)
197 ->withHeader(
198 'Location',
199 $this->routeparser->urlFor(
200 'configureDynamicFields',
201 ['form_name' => $form_name]
202 )
203 );
204 }
205 }
206
207 // /CRUD - Create
208 // CRUD - Read
209
210 /**
211 * List page
212 *
213 * @param Request $request PSR Request
214 * @param Response $response PSR Response
215 * @param string $option One of 'page' or 'order'
216 * @param string|integer $value Value of the option
217 * @param string $form_name Form name
218 *
219 * @return Response
220 */
221 public function list(
222 Request $request,
223 Response $response,
224 $option = null,
225 $value = null,
226 $form_name = 'adh'
227 ): Response {
228 if (isset($_POST['form_name']) && trim($_POST['form_name']) != '') {
229 $form_name = $_POST['form_name'];
230 }
231 $fields = new DynamicFieldsSet($this->zdb, $this->login);
232 $fields_list = $fields->getList($form_name);
233
234 $params = [
235 'fields_list' => $fields_list,
236 'form_name' => $form_name,
237 'form_title' => DynamicField::getFormTitle($form_name),
238 'page_title' => _T("Dynamic fields configuration"),
239 'html_editor' => true,
240 'html_editor_active' => $this->preferences->pref_editor_enabled
241
242 ];
243
244 $tpl = 'pages/configuration_dynamic_fields.html.twig';
245 //Render directly template if we called from ajax,
246 //render in a full page otherwise
247 if (
248 ($request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest')
249 || isset($request->getQueryParams()['ajax'])
250 && $request->getQueryParams()['ajax'] == 'true'
251 ) {
252 $tpl = 'elements/edit_dynamic_fields.html.twig';
253 } else {
254 $all_forms = DynamicField::getFormsNames();
255 $params['all_forms'] = $all_forms;
256 }
257
258 // display page
259 $this->view->render(
260 $response,
261 $tpl,
262 $params
263 );
264 return $response;
265 }
266
267 /**
268 * Filtering
269 *
270 * @param Request $request PSR Request
271 * @param Response $response PSR Response
272 *
273 * @return Response
274 */
275 public function filter(Request $request, Response $response): Response
276 {
277 //no filtering
278 return $response;
279 }
280
281 /**
282 * Get a dynamic file
283 *
284 * @param Request $request PSR Request
285 * @param Response $response PSR Response
286 * @param string $form_name Form name
287 * @param integer $id Object ID
288 * @param integer $fid Dynamic fields ID
289 * @param integer $pos Dynamic field position
290 * @param string $name File name
291 *
292 * @return Response
293 */
294 public function getDynamicFile(
295 Request $request,
296 Response $response,
297 string $form_name,
298 int $id,
299 int $fid,
300 int $pos,
301 string $name
302 ): Response {
303 $object_class = DynamicFieldsSet::getClasses()[$form_name];
304 if ($object_class === 'Galette\Entity\Adherent') {
305 $object = new $object_class($this->zdb);
306 } else {
307 $object = new $object_class($this->zdb, $this->login);
308 }
309
310 $object
311 ->disableAllDeps()
312 ->enableDep('dynamics')
313 ->load($id);
314 $fields = $object->getDynamicFields()->getFields();
315 $field = $fields[$fid] ?? null;
316
317 $denied = null;
318 if (!$object->canShow($this->login)) {
319 if (!isset($fields[$fid])) {
320 //field does not exist or access is forbidden
321 $denied = true;
322 } else {
323 $denied = false;
324 }
325 }
326
327 if ($denied === true) {
328 $this->flash->addMessage(
329 'error_detected',
330 _T("You do not have permission for requested URL.")
331 );
332
333 $route_name = 'member';
334 if ($form_name == 'contrib') {
335 $route_name = 'contribution';
336 } elseif ($form_name == 'trans') {
337 $route_name = 'transaction';
338 }
339 return $response
340 ->withHeader(
341 'Location',
342 $this->routeparser->urlFor(
343 $route_name,
344 ['id' => $id]
345 )
346 );
347 }
348
349 $filename = $field->getFileName($id, $pos);
350
351 if ($form_name !== 'member' && !file_exists(GALETTE_FILES_PATH . $filename)) {
352 //handle old names for non adh dynamic files
353 $test_filename = $field->getFileName($id, $pos, 'member');
354 if (file_exists(GALETTE_FILES_PATH . $test_filename)) {
355 //rename old file to new name
356 rename(GALETTE_FILES_PATH . $test_filename, GALETTE_FILES_PATH . $filename);
357 }
358 }
359
360 if (file_exists(GALETTE_FILES_PATH . $filename)) {
361 $type = File::getMimeType(GALETTE_FILES_PATH . $filename);
362
363 $response = $response->withHeader('Content-Description', 'File Transfer')
364 ->withHeader('Content-Type', $type)
365 ->withHeader('Content-Disposition', 'attachment;filename="' . $name . '"')
366 ->withHeader('Pragma', 'no-cache')
367 ->withHeader('Content-Transfer-Encoding', 'binary')
368 ->withHeader('Expires', '0')
369 ->withHeader('Cache-Control', 'must-revalidate')
370 ->withHeader('Pragma', 'public');
371
372 $stream = fopen('php://memory', 'r+');
373 fwrite($stream, file_get_contents(GALETTE_FILES_PATH . $filename));
374 rewind($stream);
375
376 return $response->withBody(new \Slim\Psr7\Stream($stream));
377 } else {
378 Analog::log(
379 'A request has been made to get a dynamic file named `' .
380 $filename . '` that does not exists.',
381 Analog::WARNING
382 );
383
384 $this->flash->addMessage(
385 'error_detected',
386 _T("The file does not exists or cannot be read :(")
387 );
388
389 $route_name = 'member';
390 if ($form_name == 'contrib') {
391 $route_name = 'contribution';
392 } elseif ($form_name == 'trans') {
393 $route_name = 'transaction';
394 }
395
396 return $response
397 ->withHeader(
398 'Location',
399 $this->routeparser->urlFor($route_name, ['id' => (string)$id])
400 );
401 }
402 }
403
404 // /CRUD - Read
405 // CRUD - Update
406
407 /**
408 * Edit page
409 *
410 * @param Request $request PSR Request
411 * @param Response $response PSR Response
412 * @param integer $id Dynamic field id
413 * @param string $form_name Form name
414 *
415 * @return Response
416 */
417 public function edit(Request $request, Response $response, int $id, $form_name = null): Response
418 {
419 $df = null;
420 if ($this->session->dynamicfieldtype) {
421 $df = $this->session->dynamicfieldtype;
422 $this->session->dynamicfieldtype = null;
423 } else {
424 $df = DynamicField::loadFieldType($this->zdb, $id);
425 if ($df === false) {
426 $this->flash->addMessage(
427 'error_detected',
428 _T("Unable to retrieve field information.")
429 );
430 return $response
431 ->withStatus(301)
432 ->withHeader('Location', $this->routeparser->urlFor('configureDynamicFields'));
433 }
434 }
435
436 $params = [
437 'page_title' => _T("Edit field"),
438 'action' => 'edit',
439 'form_name' => $form_name,
440 'perm_names' => DynamicField::getPermsNames(),
441 'mode' => (($request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest') ? 'ajax' : ''),
442 'df' => $df,
443 'html_editor' => true,
444 'html_editor_active' => $this->preferences->pref_editor_enabled
445 ];
446
447 // display page
448 $this->view->render(
449 $response,
450 'pages/configuration_dynamic_field_form.html.twig',
451 $params
452 );
453 return $response;
454 }
455
456 /**
457 * Edit action
458 *
459 * @param Request $request PSR Request
460 * @param Response $response PSR Response
461 * @param integer $id Dynamic field id
462 * @param string $form_name Form name
463 *
464 * @return Response
465 */
466 public function doEdit(Request $request, Response $response, int $id = null, string $form_name = null): Response
467 {
468 $post = $request->getParsedBody();
469 $post['form_name'] = $form_name;
470
471 if (isset($post['cancel'])) {
472 return $response
473 ->withStatus(301)
474 ->withHeader('Location', $this->cancelUri($this->getArgs($request)));
475 }
476
477 $error_detected = [];
478 $warning_detected = [];
479
480 $field_id = $id;
481 $df = DynamicField::loadFieldType($this->zdb, $field_id);
482
483 try {
484 $df->store($post);
485 $error_detected = $df->getErrors();
486 $warning_detected = $df->getWarnings();
487 } catch (Throwable $e) {
488 $msg = 'An error occurred storing dynamic field ' . $df->getId() . '.';
489 Analog::log(
490 $msg . ' | ' .
491 $e->getMessage(),
492 Analog::ERROR
493 );
494 if (Galette::isDebugEnabled()) {
495 throw $e;
496 }
497 $error_detected[] = _T('An error occurred editing dynamic field :(');
498 }
499
500 //flash messages
501 if (count($error_detected) > 0) {
502 foreach ($error_detected as $error) {
503 $this->flash->addMessage(
504 'error_detected',
505 $error
506 );
507 }
508 } else {
509 $this->flash->addMessage(
510 'success_detected',
511 _T('Dynamic field has been successfully stored!')
512 );
513 }
514
515 if (count($warning_detected) > 0) {
516 foreach ($warning_detected as $warning) {
517 $this->flash->addMessage(
518 'warning_detected',
519 $warning
520 );
521 }
522 }
523
524 //handle redirections
525 if (count($error_detected) > 0) {
526 //something went wrong :'(
527 $this->session->dynamicfieldtype = $df;
528 return $response
529 ->withStatus(301)
530 ->withHeader(
531 'Location',
532 $this->routeparser->urlFor(
533 'editDynamicField',
534 [
535 'form_name' => $form_name,
536 'id' => $id
537 ]
538 )
539 );
540 } else {
541 return $response
542 ->withStatus(301)
543 ->withHeader(
544 'Location',
545 $this->routeparser->urlFor(
546 'configureDynamicFields',
547 ['form_name' => $form_name]
548 )
549 );
550 }
551 }
552
553 // /CRUD - Update
554 // CRUD - Delete
555
556 /**
557 * Get redirection URI
558 *
559 * @param array $args Route arguments
560 *
561 * @return string
562 */
563 public function redirectUri(array $args)
564 {
565 return $this->routeparser->urlFor('configureDynamicFields', ['form_name' => $args['form_name']]);
566 }
567
568 /**
569 * Get form URI
570 *
571 * @param array $args Route arguments
572 *
573 * @return string
574 */
575 public function formUri(array $args)
576 {
577 return $this->routeparser->urlFor(
578 'doRemoveDynamicField',
579 ['id' => $args['id'], 'form_name' => $args['form_name']]
580 );
581 }
582
583 /**
584 * Get confirmation removal page title
585 *
586 * @param array $args Route arguments
587 *
588 * @return string
589 */
590 public function confirmRemoveTitle(array $args)
591 {
592 $field = DynamicField::loadFieldType($this->zdb, (int)$args['id']);
593 if ($field === false) {
594 $this->flash->addMessage(
595 'error_detected',
596 _T("Requested field does not exists!")
597 );
598 return _T("Requested field does not exists!");
599 }
600
601 return sprintf(
602 _T('Remove dynamic field %1$s'),
603 $field->getName()
604 );
605 }
606
607 /**
608 * Remove object
609 *
610 * @param array $args Route arguments
611 * @param array $post POST values
612 *
613 * @return boolean
614 */
615 protected function doDelete(array $args, array $post)
616 {
617 $field_id = (int)$post['id'];
618 $field = DynamicField::loadFieldType($this->zdb, $field_id);
619 return $field->remove();
620 }
621
622 // /CRUD - Delete
623 // /CRUD
624
625 /**
626 * Move field
627 *
628 * @param Request $request PSR Request
629 * @param Response $response PSR Response
630 * @param integer $id Field id
631 * @param string $form_name Form name
632 * @param string $direction One of DynamicField::MOVE_*
633 *
634 * @return Response
635 */
636 public function move(
637 Request $request,
638 Response $response,
639 int $id,
640 string $form_name,
641 string $direction
642 ): Response {
643 $field = DynamicField::loadFieldType($this->zdb, $id);
644 if ($field->move($direction)) {
645 $this->flash->addMessage(
646 'success_detected',
647 _T("Field has been successfully moved")
648 );
649 } else {
650 $this->flash->addMessage(
651 'error_detected',
652 _T("An error occurred moving field :(")
653 );
654 }
655
656 return $response
657 ->withStatus(301)
658 ->withHeader('Location', $this->routeparser->urlFor('configureDynamicFields', ['form_name' => $form_name]));
659 }
660 }