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")
241 $tpl = 'pages/configuration_dynamic_fields.html.twig';
242 //Render directly template if we called from ajax,
243 //render in a full page otherwise
245 ($request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest')
246 ||
isset($request->getQueryParams()['ajax'])
247 && $request->getQueryParams()['ajax'] == 'true'
249 $tpl = 'elements/edit_dynamic_fields.html.twig';
251 $all_forms = DynamicField
::getFormsNames();
252 $params['all_forms'] = $all_forms;
267 * @param Request $request PSR Request
268 * @param Response $response PSR Response
272 public function filter(Request
$request, Response
$response): Response
281 * @param Request $request PSR Request
282 * @param Response $response PSR Response
283 * @param string $form_name Form name
284 * @param integer $id Object ID
285 * @param integer $fid Dynamic fields ID
286 * @param integer $pos Dynamic field position
287 * @param string $name File name
291 public function getDynamicFile(
300 $object_class = DynamicFieldsSet
::getClasses()[$form_name];
301 if ($object_class === 'Galette\Entity\Adherent') {
302 $object = new $object_class($this->zdb
);
304 $object = new $object_class($this->zdb
, $this->login
);
309 ->enableDep('dynamics')
311 $fields = $object->getDynamicFields()->getFields();
312 $field = $fields[$fid] ??
null;
315 if (!$object->canShow($this->login
)) {
316 if (!isset($fields[$fid])) {
317 //field does not exist or access is forbidden
324 if ($denied === true) {
325 $this->flash
->addMessage(
327 _T("You do not have permission for requested URL.")
330 $route_name = 'member';
331 if ($form_name == 'contrib') {
332 $route_name = 'contribution';
333 } elseif ($route_name == 'trans') {
334 $route_name = 'transaction';
339 $this->routeparser
->urlFor(
346 $filename = $field->getFileName($id, $pos);
348 if ($form_name !== 'member' && !file_exists(GALETTE_FILES_PATH
. $filename)) {
349 //handle old names for non adh dynamic files
350 $test_filename = $field->getFileName($id, $pos, 'member');
351 if (file_exists(GALETTE_FILES_PATH
. $test_filename)) {
352 //rename old file to new name
353 rename(GALETTE_FILES_PATH
. $test_filename, GALETTE_FILES_PATH
. $filename);
357 if (file_exists(GALETTE_FILES_PATH
. $filename)) {
358 $type = File
::getMimeType(GALETTE_FILES_PATH
. $filename);
360 $response = $response->withHeader('Content-Description', 'File Transfer')
361 ->withHeader('Content-Type', $type)
362 ->withHeader('Content-Disposition', 'attachment;filename="' . $name . '"')
363 ->withHeader('Pragma', 'no-cache')
364 ->withHeader('Content-Transfer-Encoding', 'binary')
365 ->withHeader('Expires', '0')
366 ->withHeader('Cache-Control', 'must-revalidate')
367 ->withHeader('Pragma', 'public');
369 $stream = fopen('php://memory', 'r+');
370 fwrite($stream, file_get_contents(GALETTE_FILES_PATH
. $filename));
373 return $response->withBody(new \Slim\Psr7\
Stream($stream));
376 'A request has been made to get a dynamic file named `' .
377 $filename . '` that does not exists.',
381 $this->flash
->addMessage(
383 _T("The file does not exists or cannot be read :(")
389 $this->routeparser
->urlFor('member', ['id' => $id])
400 * @param Request $request PSR Request
401 * @param Response $response PSR Response
402 * @param integer $id Dynamic field id
403 * @param string $form_name Form name
407 public function edit(Request
$request, Response
$response, int $id, $form_name = null): Response
410 if ($this->session
->dynamicfieldtype
) {
411 $df = $this->session
->dynamicfieldtype
;
412 $this->session
->dynamicfieldtype
= null;
414 $df = DynamicField
::loadFieldType($this->zdb
, $id);
416 $this->flash
->addMessage(
418 _T("Unable to retrieve field information.")
422 ->withHeader('Location', $this->routeparser
->urlFor('configureDynamicFields'));
427 'page_title' => _T("Edit field"),
429 'form_name' => $form_name,
430 'perm_names' => DynamicField
::getPermsNames(),
431 'mode' => (($request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest') ?
'ajax' : ''),
438 'pages/configuration_dynamic_field_form.html.twig',
447 * @param Request $request PSR Request
448 * @param Response $response PSR Response
449 * @param integer $id Dynamic field id
450 * @param string $form_name Form name
454 public function doEdit(Request
$request, Response
$response, int $id = null, string $form_name = null): Response
456 $post = $request->getParsedBody();
457 $post['form_name'] = $form_name;
459 if (isset($post['cancel'])) {
462 ->withHeader('Location', $this->cancelUri($this->getArgs($request)));
465 $error_detected = [];
466 $warning_detected = [];
469 $df = DynamicField
::loadFieldType($this->zdb
, $field_id);
473 $error_detected = $df->getErrors();
474 $warning_detected = $df->getWarnings();
475 } catch (Throwable
$e) {
476 $msg = 'An error occurred storing dynamic field ' . $df->getId() . '.';
482 if (Galette
::isDebugEnabled()) {
485 $error_detected[] = _T('An error occurred editing dynamic field :(');
489 if (count($error_detected) > 0) {
490 foreach ($error_detected as $error) {
491 $this->flash
->addMessage(
497 $this->flash
->addMessage(
499 _T('Dynamic field has been successfully stored!')
503 if (count($warning_detected) > 0) {
504 foreach ($warning_detected as $warning) {
505 $this->flash
->addMessage(
512 //handle redirections
513 if (count($error_detected) > 0) {
514 //something went wrong :'(
515 $this->session
->dynamicfieldtype
= $df;
520 $this->routeparser
->urlFor(
523 'form_name' => $form_name,
533 $this->routeparser
->urlFor(
534 'configureDynamicFields',
535 ['form_name' => $form_name]
545 * Get redirection URI
547 * @param array $args Route arguments
551 public function redirectUri(array $args)
553 return $this->routeparser
->urlFor('configureDynamicFields', ['form_name' => $args['form_name']]);
559 * @param array $args Route arguments
563 public function formUri(array $args)
565 return $this->routeparser
->urlFor(
566 'doRemoveDynamicField',
567 ['id' => $args['id'], 'form_name' => $args['form_name']]
572 * Get confirmation removal page title
574 * @param array $args Route arguments
578 public function confirmRemoveTitle(array $args)
580 $field = DynamicField
::loadFieldType($this->zdb
, (int)$args['id']);
581 if ($field === false) {
582 $this->flash
->addMessage(
584 _T("Requested field does not exists!")
586 return _T("Requested field does not exists!");
590 _T('Remove dynamic field %1$s'),
598 * @param array $args Route arguments
599 * @param array $post POST values
603 protected function doDelete(array $args, array $post)
605 $field_id = (int)$post['id'];
606 $field = DynamicField
::loadFieldType($this->zdb
, $field_id);
607 return $field->remove();
616 * @param Request $request PSR Request
617 * @param Response $response PSR Response
618 * @param integer $id Field id
619 * @param string $form_name Form name
620 * @param string $direction One of DynamicField::MOVE_*
624 public function move(
631 $field = DynamicField
::loadFieldType($this->zdb
, $id);
632 if ($field->move($direction)) {
633 $this->flash
->addMessage(
635 _T("Field has been successfully moved")
638 $this->flash
->addMessage(
640 _T("An error occurred moving field :(")
646 ->withHeader('Location', $this->routeparser
->urlFor('configureDynamicFields', ['form_name' => $form_name]));