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
;
39 use Galette\Controllers\CrudController
;
40 use Slim\Http\Request
;
41 use Slim\Http\Response
;
42 use Galette\DynamicFields\DynamicField
;
46 * Galette dynamic fields controller
48 * @category Controllers
49 * @name DynamicFieldsController
51 * @author Johan Cwiklinski <johan@x-tnd.be>
52 * @copyright 2020 The Galette Team
53 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
54 * @link http://galette.tuxfamily.org
55 * @since Available since 0.9.4dev - 2020-05-02
58 class DynamicFieldsController
extends CrudController
65 * @param Request $request PSR Request
66 * @param Response $response PSR Response
67 * @param array $args Request arguments
71 public function add(Request
$request, Response
$response, array $args = []): Response
73 $form_name = $args['form'];
76 if ($this->session
->dynamicfieldtype
) {
77 $df = $this->session
->dynamicfieldtype
;
78 $this->session
->dynamicfieldtype
= null;
82 'page_title' => _T("Add field"),
83 'form_name' => $form_name,
85 'perm_names' => DynamicField
::getPermsNames(),
86 'mode' => ($request->isXhr() ?
'ajax' : ''),
87 'field_type_names' => DynamicField
::getFieldsTypesNames()
102 * @param Request $request PSR Request
103 * @param Response $response PSR Response
104 * @param array $args Request arguments
108 public function doAdd(Request
$request, Response
$response, array $args = []): Response
110 $post = $request->getParsedBody();
112 $error_detected = [];
113 $warning_detected = [];
115 $df = DynamicField
::getFieldType($this->zdb
, $post['field_type']);
119 $error_detected = $df->getErrors();
120 $warning_detected = $df->getWarnings();
121 } catch (\Exception
$e) {
122 $msg = 'An error occurred adding new dynamic field.';
128 if (GALETTE_MODE
== 'DEV') {
131 $error_detected[] = _T('An error occurred adding dynamic field :(');
135 if (count($error_detected) > 0) {
136 foreach ($error_detected as $error) {
137 $this->flash
->addMessage(
143 $this->flash
->addMessage(
145 _T('Dynamic field has been successfully stored!')
149 if (count($warning_detected) > 0) {
150 foreach ($warning_detected as $warning) {
151 $this->flash
->addMessage(
158 //handle redirections
159 if (count($error_detected) > 0) {
160 //something went wrong :'(
161 $this->session
->dynamicfieldtype
= $df;
166 $this->router
->pathFor(
172 if (!$df instanceof \Galette\DynamicFields\Separator
) {
177 $this->router
->pathFor(
180 'form' => $args['form'],
191 $this->router
->pathFor(
192 'configureDynamicFields',
193 ['form' => $args['form']]
205 * @param Request $request PSR Request
206 * @param Response $response PSR Response
207 * @param array $args Request arguments
211 public function list(Request
$request, Response
$response, array $args = []): Response
213 $form_name = $args['form'] ??
'adh';
214 if (isset($_POST['form']) && trim($_POST['form']) != '') {
215 $form_name = $_POST['form'];
217 $fields = new \Galette\Repository\
DynamicFieldsSet($this->zdb
, $this->login
);
218 $fields_list = $fields->getList($form_name, $this->login
);
220 $field_type_names = DynamicField
::getFieldsTypesNames();
223 'fields_list' => $fields_list,
224 'form_name' => $form_name,
225 'form_title' => DynamicField
::getFormTitle($form_name),
226 'page_title' => _T("Dynamic fields configuration")
229 $tpl = 'configurer_fiches.tpl';
230 //Render directly template if we called from ajax,
231 //render in a full page otherwise
234 ||
isset($request->getQueryParams()['ajax'])
235 && $request->getQueryParams()['ajax'] == 'true'
237 $tpl = 'configurer_fiche_content.tpl';
239 $all_forms = DynamicField
::getFormsNames();
240 $params['all_forms'] = $all_forms;
255 * @param Request $request PSR Request
256 * @param Response $response PSR Response
260 public function filter(Request
$request, Response
$response): Response
271 * @param Request $request PSR Request
272 * @param Response $response PSR Response
273 * @param array $args Request arguments
277 public function edit(Request
$request, Response
$response, array $args = []): Response
279 $id_dynf = (int)$args['id'];
280 $form_name = $args['form'];
283 if ($this->session
->dynamicfieldtype
) {
284 $df = $this->session
->dynamicfieldtype
;
285 $this->session
->dynamicfieldtype
= null;
287 $df = DynamicField
::loadFieldType($this->zdb
, $id_dynf);
289 $this->flash
->addMessage(
291 _T("Unable to retrieve field information.")
295 ->withHeader('Location', $this->router
->pathFor('configureDynamicFields'));
300 'page_title' => _T("Edit field"),
302 'form_name' => $form_name,
303 'perm_names' => DynamicField
::getPermsNames(),
304 'mode' => ($request->isXhr() ?
'ajax' : ''),
320 * @param Request $request PSR Request
321 * @param Response $response PSR Response
322 * @param array $args Request arguments
326 public function doEdit(Request
$request, Response
$response, array $args = []): Response
328 $post = $request->getParsedBody();
330 $error_detected = [];
331 $warning_detected = [];
333 $field_id = (int)$args['id'];
334 $df = DynamicField
::loadFieldType($this->zdb
, $field_id);
338 $error_detected = $df->getErrors();
339 $warning_detected = $df->getWarnings();
340 } catch (\Exception
$e) {
341 $msg = 'An error occurred storing dynamic field ' . $df->getId() . '.';
347 if (GALETTE_MODE
== 'DEV') {
350 $error_detected[] = _T('An error occurred editing dynamic field :(');
354 if (count($error_detected) > 0) {
355 foreach ($error_detected as $error) {
356 $this->flash
->addMessage(
362 $this->flash
->addMessage(
364 _T('Dynamic field has been successfully stored!')
368 if (count($warning_detected) > 0) {
369 foreach ($warning_detected as $warning) {
370 $this->flash
->addMessage(
377 //handle redirections
378 if (count($error_detected) > 0) {
379 //something went wrong :'(
380 $this->session
->dynamicfieldtype
= $df;
385 $this->router
->pathFor(
395 $this->router
->pathFor(
396 'configureDynamicFields',
397 ['form' => $args['form']]
407 * Get redirection URI
409 * @param array $args Route arguments
413 public function redirectUri(array $args = [])
415 return $this->router
->pathFor('configureDynamicFields');
421 * @param array $args Route arguments
425 public function formUri(array $args = [])
427 return $this->router
->pathFor(
428 'doRemoveDynamicField',
429 ['id' => $args['id'], 'form' => $args['form']]
434 * Get confirmation removal page title
436 * @param array $args Route arguments
440 public function confirmRemoveTitle(array $args = [])
442 $field = DynamicField
::loadFieldType($this->zdb
, (int)$args['id']);
443 if ($field === false) {
444 $this->flash
->addMessage(
446 _T("Requested field does not exists!")
450 ->withHeader('Location', $this->router
->pathFor('configureDynamicFields', ['form' => $args['form']]));
454 _T('Remove dynamic field %1$s'),
462 * @param array $args Route arguments
463 * @param array $post POST values
467 protected function doDelete(array $args, array $post)
469 $field_id = (int)$post['id'];
470 $field = DynamicField
::loadFieldType($this->zdb
, $field_id);
471 return $field->remove();
480 * @param Request $request PSR Request
481 * @param Response $response PSR Response
482 * @param array $args Request arguments
486 public function move(Request
$request, Response
$response, array $args = []): Response
488 $field_id = (int)$args['id'];
489 $form_name = $args['form'];
491 $field = DynamicField
::loadFieldType($this->zdb
, $field_id);
492 if ($field->move($args['direction'])) {
493 $this->flash
->addMessage(
495 _T("Field has been successfully moved")
498 $this->flash
->addMessage(
500 _T("An error occurred moving field :(")
506 ->withHeader('Location', $this->router
->pathFor('configureDynamicFields', ['form' => $form_name]));