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