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
;
40 use Galette\Repository\DynamicFieldsSet
;
42 use Galette\Controllers\CrudController
;
43 use Slim\Psr7\Request
;
44 use Slim\Psr7\Response
;
45 use Galette\DynamicFields\DynamicField
;
49 * Galette dynamic fields controller
51 * @category Controllers
52 * @name DynamicFieldsController
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
61 class DynamicFieldsController
extends CrudController
68 * @param Request $request PSR Request
69 * @param Response $response PSR Response
70 * @param string $form_name Form name
74 public function add(Request
$request, Response
$response, string $form_name = null): Response
77 'page_title' => _T("Add field"),
78 'form_name' => $form_name,
80 'perm_names' => DynamicField
::getPermsNames(),
81 'mode' => (($request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest') ?
'ajax' : ''),
82 'field_type_names' => DynamicField
::getFieldsTypesNames()
85 if ($this->session
->dynamicfieldtype
) {
86 $params['df'] = $this->session
->dynamicfieldtype
;
87 $this->session
->dynamicfieldtype
= null;
93 'pages/configuration_dynamic_field_form.html.twig',
102 * @param Request $request PSR Request
103 * @param Response $response PSR Response
104 * @param string $form_name Form name
108 public function doAdd(Request
$request, Response
$response, string $form_name = null): Response
110 $post = $request->getParsedBody();
111 $post['form_name'] = $form_name;
113 $error_detected = [];
114 $warning_detected = [];
116 if (isset($post['cancel'])) {
119 ->withHeader('Location', $this->cancelUri($this->getArgs($request)));
122 $df = DynamicField
::getFieldType($this->zdb
, $post['field_type']);
126 $error_detected = $df->getErrors();
127 $warning_detected = $df->getWarnings();
128 } catch (Throwable
$e) {
129 $msg = 'An error occurred adding new dynamic field.';
135 if (GALETTE_MODE
== 'DEV') {
138 $error_detected[] = _T('An error occurred adding dynamic field :(');
142 if (count($error_detected) > 0) {
143 foreach ($error_detected as $error) {
144 $this->flash
->addMessage(
150 $this->flash
->addMessage(
152 _T('Dynamic field has been successfully stored!')
156 if (count($warning_detected) > 0) {
157 foreach ($warning_detected as $warning) {
158 $this->flash
->addMessage(
165 //handle redirections
166 if (count($error_detected) > 0) {
167 //something went wrong :'(
168 $this->session
->dynamicfieldtype
= $df;
173 $this->routeparser
->urlFor(
175 ['form_name' => $form_name]
179 if (!$df instanceof \Galette\DynamicFields\Separator
) {
184 $this->routeparser
->urlFor(
187 'form_name' => $form_name,
198 $this->routeparser
->urlFor(
199 'configureDynamicFields',
200 ['form_name' => $form_name]
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
220 public function list(
227 if (isset($_POST['form_name']) && trim($_POST['form_name']) != '') {
228 $form_name = $_POST['form_name'];
230 $fields = new DynamicFieldsSet($this->zdb
, $this->login
);
231 $fields_list = $fields->getList($form_name);
234 'fields_list' => $fields_list,
235 'form_name' => $form_name,
236 'form_title' => DynamicField
::getFormTitle($form_name),
237 'page_title' => _T("Dynamic fields configuration")
240 $tpl = 'pages/configuration_dynamic_fields.html.twig';
241 //Render directly template if we called from ajax,
242 //render in a full page otherwise
244 ($request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest')
245 ||
isset($request->getQueryParams()['ajax'])
246 && $request->getQueryParams()['ajax'] == 'true'
248 $tpl = 'elements/edit_dynamic_fields.html.twig';
250 $all_forms = DynamicField
::getFormsNames();
251 $params['all_forms'] = $all_forms;
266 * @param Request $request PSR Request
267 * @param Response $response PSR Response
271 public function filter(Request
$request, Response
$response): Response
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
290 public function getDynamicFile(
299 $object_class = DynamicFieldsSet
::getClasses()[$form_name];
300 if ($object_class === 'Galette\Entity\Adherent') {
301 $object = new $object_class($this->zdb
);
303 $object = new $object_class($this->zdb
, $this->login
);
308 ->enableDep('dynamics')
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
322 if ($denied === true) {
323 $this->flash
->addMessage(
325 _T("You do not have permission for requested URL.")
328 $route_name = 'member';
329 if ($form_name == 'contrib') {
330 $route_name = 'contribution';
331 } elseif ($route_name == 'trans') {
332 $route_name = 'transaction';
337 $this->routeparser
->urlFor(
344 $filename = str_replace(
357 '%form_%oid_field_%fid_value_%pos'
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 :(")
392 $this->routeparser
->urlFor('member', ['id' => $id])
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
410 public function edit(Request
$request, Response
$response, int $id, $form_name = null): Response
413 if ($this->session
->dynamicfieldtype
) {
414 $df = $this->session
->dynamicfieldtype
;
415 $this->session
->dynamicfieldtype
= null;
417 $df = DynamicField
::loadFieldType($this->zdb
, $id);
419 $this->flash
->addMessage(
421 _T("Unable to retrieve field information.")
425 ->withHeader('Location', $this->routeparser
->urlFor('configureDynamicFields'));
430 'page_title' => _T("Edit field"),
432 'form_name' => $form_name,
433 'perm_names' => DynamicField
::getPermsNames(),
434 'mode' => (($request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest') ?
'ajax' : ''),
441 'pages/configuration_dynamic_field_form.html.twig',
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
457 public function doEdit(Request
$request, Response
$response, int $id = null, string $form_name = null): Response
459 $post = $request->getParsedBody();
460 $post['form_name'] = $form_name;
462 if (isset($post['cancel'])) {
465 ->withHeader('Location', $this->cancelUri($this->getArgs($request)));
468 $error_detected = [];
469 $warning_detected = [];
472 $df = DynamicField
::loadFieldType($this->zdb
, $field_id);
476 $error_detected = $df->getErrors();
477 $warning_detected = $df->getWarnings();
478 } catch (Throwable
$e) {
479 $msg = 'An error occurred storing dynamic field ' . $df->getId() . '.';
485 if (GALETTE_MODE
== 'DEV') {
488 $error_detected[] = _T('An error occurred editing dynamic field :(');
492 if (count($error_detected) > 0) {
493 foreach ($error_detected as $error) {
494 $this->flash
->addMessage(
500 $this->flash
->addMessage(
502 _T('Dynamic field has been successfully stored!')
506 if (count($warning_detected) > 0) {
507 foreach ($warning_detected as $warning) {
508 $this->flash
->addMessage(
515 //handle redirections
516 if (count($error_detected) > 0) {
517 //something went wrong :'(
518 $this->session
->dynamicfieldtype
= $df;
523 $this->routeparser
->urlFor(
526 'form_name' => $form_name,
536 $this->routeparser
->urlFor(
537 'configureDynamicFields',
538 ['form_name' => $form_name]
548 * Get redirection URI
550 * @param array $args Route arguments
554 public function redirectUri(array $args)
556 return $this->routeparser
->urlFor('configureDynamicFields', ['form_name' => $args['form_name']]);
562 * @param array $args Route arguments
566 public function formUri(array $args)
568 return $this->routeparser
->urlFor(
569 'doRemoveDynamicField',
570 ['id' => $args['id'], 'form_name' => $args['form_name']]
575 * Get confirmation removal page title
577 * @param array $args Route arguments
581 public function confirmRemoveTitle(array $args)
583 $field = DynamicField
::loadFieldType($this->zdb
, (int)$args['id']);
584 if ($field === false) {
585 $this->flash
->addMessage(
587 _T("Requested field does not exists!")
589 return _T("Requested field does not exists!");
593 _T('Remove dynamic field %1$s'),
601 * @param array $args Route arguments
602 * @param array $post POST values
606 protected function doDelete(array $args, array $post)
608 $field_id = (int)$post['id'];
609 $field = DynamicField
::loadFieldType($this->zdb
, $field_id);
610 return $field->remove();
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_*
627 public function move(
634 $field = DynamicField
::loadFieldType($this->zdb
, $id);
635 if ($field->move($direction)) {
636 $this->flash
->addMessage(
638 _T("Field has been successfully moved")
641 $this->flash
->addMessage(
643 _T("An error occurred moving field :(")
649 ->withHeader('Location', $this->routeparser
->urlFor('configureDynamicFields', ['form_name' => $form_name]));