3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
6 * Galette dynamic fields controller
10 * Copyright © 2020-2023 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
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.
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.
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/>.
27 * @category Controllers
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
37 namespace Galette\Controllers\Crud
;
39 use Galette\Core\Galette
;
41 use Galette\Repository\DynamicFieldsSet
;
43 use Galette\Controllers\CrudController
;
44 use Slim\Psr7\Request
;
45 use Slim\Psr7\Response
;
46 use Galette\DynamicFields\DynamicField
;
50 * Galette dynamic fields controller
52 * @category Controllers
53 * @name DynamicFieldsController
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
62 class DynamicFieldsController
extends CrudController
69 * @param Request $request PSR Request
70 * @param Response $response PSR Response
71 * @param string $form_name Form name
75 public function add(Request
$request, Response
$response, string $form_name = null): Response
78 'page_title' => _T("Add field"),
79 'form_name' => $form_name,
81 'perm_names' => DynamicField
::getPermsNames(),
82 'mode' => (($request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest') ?
'ajax' : ''),
83 'field_type_names' => DynamicField
::getFieldsTypesNames()
86 if ($this->session
->dynamicfieldtype
) {
87 $params['df'] = $this->session
->dynamicfieldtype
;
88 $this->session
->dynamicfieldtype
= null;
94 'pages/configuration_dynamic_field_form.html.twig',
103 * @param Request $request PSR Request
104 * @param Response $response PSR Response
105 * @param string $form_name Form name
109 public function doAdd(Request
$request, Response
$response, string $form_name = null): Response
111 $post = $request->getParsedBody();
112 $post['form_name'] = $form_name;
114 $error_detected = [];
115 $warning_detected = [];
117 if (isset($post['cancel'])) {
120 ->withHeader('Location', $this->cancelUri($this->getArgs($request)));
123 $df = DynamicField
::getFieldType($this->zdb
, $post['field_type']);
127 $error_detected = $df->getErrors();
128 $warning_detected = $df->getWarnings();
129 } catch (Throwable
$e) {
130 $msg = 'An error occurred adding new dynamic field.';
136 if (Galette
::isDebugEnabled()) {
139 $error_detected[] = _T('An error occurred adding dynamic field :(');
143 if (count($error_detected) > 0) {
144 foreach ($error_detected as $error) {
145 $this->flash
->addMessage(
151 $this->flash
->addMessage(
153 _T('Dynamic field has been successfully stored!')
157 if (count($warning_detected) > 0) {
158 foreach ($warning_detected as $warning) {
159 $this->flash
->addMessage(
166 //handle redirections
167 if (count($error_detected) > 0) {
168 //something went wrong :'(
169 $this->session
->dynamicfieldtype
= $df;
174 $this->routeparser
->urlFor(
176 ['form_name' => $form_name]
180 if (!$df instanceof \Galette\DynamicFields\Separator
) {
185 $this->routeparser
->urlFor(
188 'form_name' => $form_name,
199 $this->routeparser
->urlFor(
200 'configureDynamicFields',
201 ['form_name' => $form_name]
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
221 public function list(
228 if (isset($_POST['form_name']) && trim($_POST['form_name']) != '') {
229 $form_name = $_POST['form_name'];
231 $fields = new DynamicFieldsSet($this->zdb
, $this->login
);
232 $fields_list = $fields->getList($form_name);
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
244 $tpl = 'pages/configuration_dynamic_fields.html.twig';
245 //Render directly template if we called from ajax,
246 //render in a full page otherwise
248 ($request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest')
249 ||
isset($request->getQueryParams()['ajax'])
250 && $request->getQueryParams()['ajax'] == 'true'
252 $tpl = 'elements/edit_dynamic_fields.html.twig';
254 $all_forms = DynamicField
::getFormsNames();
255 $params['all_forms'] = $all_forms;
270 * @param Request $request PSR Request
271 * @param Response $response PSR Response
275 public function filter(Request
$request, Response
$response): Response
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
294 public function getDynamicFile(
303 $object_class = DynamicFieldsSet
::getClasses()[$form_name];
304 if ($object_class === 'Galette\Entity\Adherent') {
305 $object = new $object_class($this->zdb
);
307 $object = new $object_class($this->zdb
, $this->login
);
312 ->enableDep('dynamics')
314 $fields = $object->getDynamicFields()->getFields();
315 $field = $fields[$fid] ??
null;
318 if (!$object->canShow($this->login
)) {
319 if (!isset($fields[$fid])) {
320 //field does not exist or access is forbidden
327 if ($denied === true) {
328 $this->flash
->addMessage(
330 _T("You do not have permission for requested URL.")
333 $route_name = 'member';
334 if ($form_name == 'contrib') {
335 $route_name = 'contribution';
336 } elseif ($form_name == 'trans') {
337 $route_name = 'transaction';
342 $this->routeparser
->urlFor(
349 $filename = $field->getFileName($id, $pos);
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);
360 if (file_exists(GALETTE_FILES_PATH
. $filename)) {
361 $type = File
::getMimeType(GALETTE_FILES_PATH
. $filename);
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');
372 $stream = fopen('php://memory', 'r+');
373 fwrite($stream, file_get_contents(GALETTE_FILES_PATH
. $filename));
376 return $response->withBody(new \Slim\Psr7\
Stream($stream));
379 'A request has been made to get a dynamic file named `' .
380 $filename . '` that does not exists.',
384 $this->flash
->addMessage(
386 _T("The file does not exists or cannot be read :(")
389 $route_name = 'member';
390 if ($form_name == 'contrib') {
391 $route_name = 'contribution';
392 } elseif ($form_name == 'trans') {
393 $route_name = 'transaction';
399 $this->routeparser
->urlFor($route_name, ['id' => (string)$id])
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
417 public function edit(Request
$request, Response
$response, int $id, $form_name = null): Response
420 if ($this->session
->dynamicfieldtype
) {
421 $df = $this->session
->dynamicfieldtype
;
422 $this->session
->dynamicfieldtype
= null;
424 $df = DynamicField
::loadFieldType($this->zdb
, $id);
426 $this->flash
->addMessage(
428 _T("Unable to retrieve field information.")
432 ->withHeader('Location', $this->routeparser
->urlFor('configureDynamicFields'));
437 'page_title' => _T("Edit field"),
439 'form_name' => $form_name,
440 'perm_names' => DynamicField
::getPermsNames(),
441 'mode' => (($request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest') ?
'ajax' : ''),
443 'html_editor' => true,
444 'html_editor_active' => $this->preferences
->pref_editor_enabled
450 'pages/configuration_dynamic_field_form.html.twig',
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
466 public function doEdit(Request
$request, Response
$response, int $id = null, string $form_name = null): Response
468 $post = $request->getParsedBody();
469 $post['form_name'] = $form_name;
471 if (isset($post['cancel'])) {
474 ->withHeader('Location', $this->cancelUri($this->getArgs($request)));
477 $error_detected = [];
478 $warning_detected = [];
481 $df = DynamicField
::loadFieldType($this->zdb
, $field_id);
485 $error_detected = $df->getErrors();
486 $warning_detected = $df->getWarnings();
487 } catch (Throwable
$e) {
488 $msg = 'An error occurred storing dynamic field ' . $df->getId() . '.';
494 if (Galette
::isDebugEnabled()) {
497 $error_detected[] = _T('An error occurred editing dynamic field :(');
501 if (count($error_detected) > 0) {
502 foreach ($error_detected as $error) {
503 $this->flash
->addMessage(
509 $this->flash
->addMessage(
511 _T('Dynamic field has been successfully stored!')
515 if (count($warning_detected) > 0) {
516 foreach ($warning_detected as $warning) {
517 $this->flash
->addMessage(
524 //handle redirections
525 if (count($error_detected) > 0) {
526 //something went wrong :'(
527 $this->session
->dynamicfieldtype
= $df;
532 $this->routeparser
->urlFor(
535 'form_name' => $form_name,
545 $this->routeparser
->urlFor(
546 'configureDynamicFields',
547 ['form_name' => $form_name]
557 * Get redirection URI
559 * @param array $args Route arguments
563 public function redirectUri(array $args)
565 return $this->routeparser
->urlFor('configureDynamicFields', ['form_name' => $args['form_name']]);
571 * @param array $args Route arguments
575 public function formUri(array $args)
577 return $this->routeparser
->urlFor(
578 'doRemoveDynamicField',
579 ['id' => $args['id'], 'form_name' => $args['form_name']]
584 * Get confirmation removal page title
586 * @param array $args Route arguments
590 public function confirmRemoveTitle(array $args)
592 $field = DynamicField
::loadFieldType($this->zdb
, (int)$args['id']);
593 if ($field === false) {
594 $this->flash
->addMessage(
596 _T("Requested field does not exists!")
598 return _T("Requested field does not exists!");
602 _T('Remove dynamic field %1$s'),
610 * @param array $args Route arguments
611 * @param array $post POST values
615 protected function doDelete(array $args, array $post)
617 $field_id = (int)$post['id'];
618 $field = DynamicField
::loadFieldType($this->zdb
, $field_id);
619 return $field->remove();
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_*
636 public function move(
643 $field = DynamicField
::loadFieldType($this->zdb
, $id);
644 if ($field->move($direction)) {
645 $this->flash
->addMessage(
647 _T("Field has been successfully moved")
650 $this->flash
->addMessage(
652 _T("An error occurred moving field :(")
658 ->withHeader('Location', $this->routeparser
->urlFor('configureDynamicFields', ['form_name' => $form_name]));