3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
6 * Galette dynamic fields controller
10 * Copyright © 2020 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 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\Controllers\CrudController
;
41 use Slim\Http\Request
;
42 use Slim\Http\Response
;
43 use Galette\DynamicFields\DynamicField
;
47 * Galette dynamic fields controller
49 * @category Controllers
50 * @name DynamicFieldsController
52 * @author Johan Cwiklinski <johan@x-tnd.be>
53 * @copyright 2020 The Galette Team
54 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
55 * @link http://galette.tuxfamily.org
56 * @since Available since 0.9.4dev - 2020-05-02
59 class DynamicFieldsController
extends CrudController
66 * @param Request $request PSR Request
67 * @param Response $response PSR Response
68 * @param array $args Request arguments
72 public function add(Request
$request, Response
$response, array $args = []): Response
74 $form_name = $args['form_name'];
77 if ($this->session
->dynamicfieldtype
) {
78 $df = $this->session
->dynamicfieldtype
;
79 $this->session
->dynamicfieldtype
= null;
83 'page_title' => _T("Add field"),
84 'form_name' => $form_name,
86 'perm_names' => DynamicField
::getPermsNames(),
87 'mode' => ($request->isXhr() ?
'ajax' : ''),
88 'field_type_names' => DynamicField
::getFieldsTypesNames()
103 * @param Request $request PSR Request
104 * @param Response $response PSR Response
105 * @param array $args Request arguments
109 public function doAdd(Request
$request, Response
$response, array $args = []): Response
111 $post = $request->getParsedBody();
112 $post['form_name'] = $args['form_name'];
114 $error_detected = [];
115 $warning_detected = [];
117 $df = DynamicField
::getFieldType($this->zdb
, $post['field_type']);
121 $error_detected = $df->getErrors();
122 $warning_detected = $df->getWarnings();
123 } catch (Throwable
$e) {
124 $msg = 'An error occurred adding new dynamic field.';
130 if (GALETTE_MODE
== 'DEV') {
133 $error_detected[] = _T('An error occurred adding dynamic field :(');
137 if (count($error_detected) > 0) {
138 foreach ($error_detected as $error) {
139 $this->flash
->addMessage(
145 $this->flash
->addMessage(
147 _T('Dynamic field has been successfully stored!')
151 if (count($warning_detected) > 0) {
152 foreach ($warning_detected as $warning) {
153 $this->flash
->addMessage(
160 //handle redirections
161 if (count($error_detected) > 0) {
162 //something went wrong :'(
163 $this->session
->dynamicfieldtype
= $df;
168 $this->router
->pathFor(
174 if (!$df instanceof \Galette\DynamicFields\Separator
) {
179 $this->router
->pathFor(
182 'form_name' => $args['form_name'],
193 $this->router
->pathFor(
194 'configureDynamicFields',
195 ['form_name' => $args['form_name']]
207 * @param Request $request PSR Request
208 * @param Response $response PSR Response
209 * @param array $args Request arguments
213 public function list(Request
$request, Response
$response, array $args = []): Response
215 $form_name = $args['form'] ??
'adh';
216 if (isset($_POST['form_name']) && trim($_POST['form_name']) != '') {
217 $form_name = $_POST['form_name'];
219 $fields = new \Galette\Repository\
DynamicFieldsSet($this->zdb
, $this->login
);
220 $fields_list = $fields->getList($form_name, $this->login
);
222 $field_type_names = DynamicField
::getFieldsTypesNames();
225 'fields_list' => $fields_list,
226 'form_name' => $form_name,
227 'form_title' => DynamicField
::getFormTitle($form_name),
228 'page_title' => _T("Dynamic fields configuration")
231 $tpl = 'configurer_fiches.tpl';
232 //Render directly template if we called from ajax,
233 //render in a full page otherwise
236 ||
isset($request->getQueryParams()['ajax'])
237 && $request->getQueryParams()['ajax'] == 'true'
239 $tpl = 'configurer_fiche_content.tpl';
241 $all_forms = DynamicField
::getFormsNames();
242 $params['all_forms'] = $all_forms;
257 * @param Request $request PSR Request
258 * @param Response $response PSR Response
262 public function filter(Request
$request, Response
$response): Response
273 * @param Request $request PSR Request
274 * @param Response $response PSR Response
275 * @param array $args Request arguments
279 public function edit(Request
$request, Response
$response, array $args = []): Response
281 $id_dynf = (int)$args['id'];
282 $form_name = $args['form_name'];
285 if ($this->session
->dynamicfieldtype
) {
286 $df = $this->session
->dynamicfieldtype
;
287 $this->session
->dynamicfieldtype
= null;
289 $df = DynamicField
::loadFieldType($this->zdb
, $id_dynf);
291 $this->flash
->addMessage(
293 _T("Unable to retrieve field information.")
297 ->withHeader('Location', $this->router
->pathFor('configureDynamicFields'));
302 'page_title' => _T("Edit field"),
304 'form_name' => $form_name,
305 'perm_names' => DynamicField
::getPermsNames(),
306 'mode' => ($request->isXhr() ?
'ajax' : ''),
322 * @param Request $request PSR Request
323 * @param Response $response PSR Response
324 * @param array $args Request arguments
328 public function doEdit(Request
$request, Response
$response, array $args = []): Response
330 $post = $request->getParsedBody();
331 $post['form_name'] = $args['form_name'];
333 $error_detected = [];
334 $warning_detected = [];
336 $field_id = (int)$args['id'];
337 $df = DynamicField
::loadFieldType($this->zdb
, $field_id);
341 $error_detected = $df->getErrors();
342 $warning_detected = $df->getWarnings();
343 } catch (Throwable
$e) {
344 $msg = 'An error occurred storing dynamic field ' . $df->getId() . '.';
350 if (GALETTE_MODE
== 'DEV') {
353 $error_detected[] = _T('An error occurred editing dynamic field :(');
357 if (count($error_detected) > 0) {
358 foreach ($error_detected as $error) {
359 $this->flash
->addMessage(
365 $this->flash
->addMessage(
367 _T('Dynamic field has been successfully stored!')
371 if (count($warning_detected) > 0) {
372 foreach ($warning_detected as $warning) {
373 $this->flash
->addMessage(
380 //handle redirections
381 if (count($error_detected) > 0) {
382 //something went wrong :'(
383 $this->session
->dynamicfieldtype
= $df;
388 $this->router
->pathFor(
398 $this->router
->pathFor(
399 'configureDynamicFields',
400 ['form_name' => $args['form_name']]
410 * Get redirection URI
412 * @param array $args Route arguments
416 public function redirectUri(array $args = [])
418 return $this->router
->pathFor('configureDynamicFields');
424 * @param array $args Route arguments
428 public function formUri(array $args = [])
430 return $this->router
->pathFor(
431 'doRemoveDynamicField',
432 ['id' => $args['id'], 'form_name' => $args['form_name']]
437 * Get confirmation removal page title
439 * @param array $args Route arguments
443 public function confirmRemoveTitle(array $args = [])
445 $field = DynamicField
::loadFieldType($this->zdb
, (int)$args['id']);
446 if ($field === false) {
447 $this->flash
->addMessage(
449 _T("Requested field does not exists!")
453 ->withHeader('Location', $this->router
->pathFor('configureDynamicFields', ['form_name' => $args['form_name']]));
457 _T('Remove dynamic field %1$s'),
465 * @param array $args Route arguments
466 * @param array $post POST values
470 protected function doDelete(array $args, array $post)
472 $field_id = (int)$post['id'];
473 $field = DynamicField
::loadFieldType($this->zdb
, $field_id);
474 return $field->remove();
483 * @param Request $request PSR Request
484 * @param Response $response PSR Response
485 * @param array $args Request arguments
489 public function move(Request
$request, Response
$response, array $args = []): Response
491 $field_id = (int)$args['id'];
492 $form_name = $args['form_name'];
494 $field = DynamicField
::loadFieldType($this->zdb
, $field_id);
495 if ($field->move($args['direction'])) {
496 $this->flash
->addMessage(
498 _T("Field has been successfully moved")
501 $this->flash
->addMessage(
503 _T("An error occurred moving field :(")
509 ->withHeader('Location', $this->router
->pathFor('configureDynamicFields', ['form_name' => $form_name]));