]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Controllers/Crud/DynamicFieldsController.php
Merge branch 'hotfix/1.0.3'
[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 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 'html_editor' => true,
437 'html_editor_active' => $this->preferences->pref_editor_enabled
438 ];
439
440 // display page
441 $this->view->render(
442 $response,
443 'pages/configuration_dynamic_field_form.html.twig',
444 $params
445 );
446 return $response;
447 }
448
449 /**
450 * Edit action
451 *
452 * @param Request $request PSR Request
453 * @param Response $response PSR Response
454 * @param integer $id Dynamic field id
455 * @param string $form_name Form name
456 *
457 * @return Response
458 */
459 public function doEdit(Request $request, Response $response, int $id = null, string $form_name = null): Response
460 {
461 $post = $request->getParsedBody();
462 $post['form_name'] = $form_name;
463
464 if (isset($post['cancel'])) {
465 return $response
466 ->withStatus(301)
467 ->withHeader('Location', $this->cancelUri($this->getArgs($request)));
468 }
469
470 $error_detected = [];
471 $warning_detected = [];
472
473 $field_id = $id;
474 $df = DynamicField::loadFieldType($this->zdb, $field_id);
475
476 try {
477 $df->store($post);
478 $error_detected = $df->getErrors();
479 $warning_detected = $df->getWarnings();
480 } catch (Throwable $e) {
481 $msg = 'An error occurred storing dynamic field ' . $df->getId() . '.';
482 Analog::log(
483 $msg . ' | ' .
484 $e->getMessage(),
485 Analog::ERROR
486 );
487 if (Galette::isDebugEnabled()) {
488 throw $e;
489 }
490 $error_detected[] = _T('An error occurred editing dynamic field :(');
491 }
492
493 //flash messages
494 if (count($error_detected) > 0) {
495 foreach ($error_detected as $error) {
496 $this->flash->addMessage(
497 'error_detected',
498 $error
499 );
500 }
501 } else {
502 $this->flash->addMessage(
503 'success_detected',
504 _T('Dynamic field has been successfully stored!')
505 );
506 }
507
508 if (count($warning_detected) > 0) {
509 foreach ($warning_detected as $warning) {
510 $this->flash->addMessage(
511 'warning_detected',
512 $warning
513 );
514 }
515 }
516
517 //handle redirections
518 if (count($error_detected) > 0) {
519 //something went wrong :'(
520 $this->session->dynamicfieldtype = $df;
521 return $response
522 ->withStatus(301)
523 ->withHeader(
524 'Location',
525 $this->routeparser->urlFor(
526 'editDynamicField',
527 [
528 'form_name' => $form_name,
529 'id' => $id
530 ]
531 )
532 );
533 } else {
534 return $response
535 ->withStatus(301)
536 ->withHeader(
537 'Location',
538 $this->routeparser->urlFor(
539 'configureDynamicFields',
540 ['form_name' => $form_name]
541 )
542 );
543 }
544 }
545
546 // /CRUD - Update
547 // CRUD - Delete
548
549 /**
550 * Get redirection URI
551 *
552 * @param array $args Route arguments
553 *
554 * @return string
555 */
556 public function redirectUri(array $args)
557 {
558 return $this->routeparser->urlFor('configureDynamicFields', ['form_name' => $args['form_name']]);
559 }
560
561 /**
562 * Get form URI
563 *
564 * @param array $args Route arguments
565 *
566 * @return string
567 */
568 public function formUri(array $args)
569 {
570 return $this->routeparser->urlFor(
571 'doRemoveDynamicField',
572 ['id' => $args['id'], 'form_name' => $args['form_name']]
573 );
574 }
575
576 /**
577 * Get confirmation removal page title
578 *
579 * @param array $args Route arguments
580 *
581 * @return string
582 */
583 public function confirmRemoveTitle(array $args)
584 {
585 $field = DynamicField::loadFieldType($this->zdb, (int)$args['id']);
586 if ($field === false) {
587 $this->flash->addMessage(
588 'error_detected',
589 _T("Requested field does not exists!")
590 );
591 return _T("Requested field does not exists!");
592 }
593
594 return sprintf(
595 _T('Remove dynamic field %1$s'),
596 $field->getName()
597 );
598 }
599
600 /**
601 * Remove object
602 *
603 * @param array $args Route arguments
604 * @param array $post POST values
605 *
606 * @return boolean
607 */
608 protected function doDelete(array $args, array $post)
609 {
610 $field_id = (int)$post['id'];
611 $field = DynamicField::loadFieldType($this->zdb, $field_id);
612 return $field->remove();
613 }
614
615 // /CRUD - Delete
616 // /CRUD
617
618 /**
619 * Move field
620 *
621 * @param Request $request PSR Request
622 * @param Response $response PSR Response
623 * @param integer $id Field id
624 * @param string $form_name Form name
625 * @param string $direction One of DynamicField::MOVE_*
626 *
627 * @return Response
628 */
629 public function move(
630 Request $request,
631 Response $response,
632 int $id,
633 string $form_name,
634 string $direction
635 ): Response {
636 $field = DynamicField::loadFieldType($this->zdb, $id);
637 if ($field->move($direction)) {
638 $this->flash->addMessage(
639 'success_detected',
640 _T("Field has been successfully moved")
641 );
642 } else {
643 $this->flash->addMessage(
644 'error_detected',
645 _T("An error occurred moving field :(")
646 );
647 }
648
649 return $response
650 ->withStatus(301)
651 ->withHeader('Location', $this->routeparser->urlFor('configureDynamicFields', ['form_name' => $form_name]));
652 }
653 }