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
34 * @link http://galette.tuxfamily.org
35 * @since Available since 0.9.4dev - 2020-05-02
38 namespace Galette\Controllers\Crud
;
40 use Galette\Controllers\CrudController
;
42 use Slim\Http\Request
;
43 use Slim\Http\Response
;
44 use Galette\DynamicFields\DynamicField
;
48 * Galette dynamic fields controller
50 * @category Controllers
51 * @name DynamicFieldsController
53 * @author Johan Cwiklinski <johan@x-tnd.be>
54 * @copyright 2020 The Galette Team
55 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
56 * @link http://galette.tuxfamily.org
57 * @since Available since 0.9.4dev - 2020-05-02
60 class DynamicFieldsController
extends CrudController
67 * @param Request $request PSR Request
68 * @param Response $response PSR Response
69 * @param array $args Request arguments
73 public function add(Request
$request, Response
$response, array $args = []) :Response
75 $form_name = $args['form'];
78 if ($this->session
->dynamicfieldtype
) {
79 $df = $this->session
->dynamicfieldtype
;
80 $this->session
->dynamicfieldtype
= null;
84 'page_title' => _T("Add field"),
85 'form_name' => $form_name,
87 'perm_names' => DynamicField
::getPermsNames(),
88 'mode' => ($request->isXhr() ?
'ajax' : ''),
89 'field_type_names' => DynamicField
::getFieldsTypesNames()
104 * @param Request $request PSR Request
105 * @param Response $response PSR Response
106 * @param array $args Request arguments
110 public function doAdd(Request
$request, Response
$response, array $args = []) :Response
112 $post = $request->getParsedBody();
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 (\Exception
$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' => $args['form'],
193 $this->router
->pathFor(
194 'configureDynamicFields',
195 ['form' => $args['form']]
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']) && trim($_POST['form']) != '') {
217 $form_name = $_POST['form'];
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
234 if ($request->isXhr()
235 ||
isset($request->getQueryParams()['ajax'])
236 && $request->getQueryParams()['ajax'] == 'true'
238 $tpl = 'configurer_fiche_content.tpl';
240 $all_forms = DynamicField
::getFormsNames();
241 $params['all_forms'] = $all_forms;
256 * @param Request $request PSR Request
257 * @param Response $response PSR Response
261 public function filter(Request
$request, Response
$response) :Response
272 * @param Request $request PSR Request
273 * @param Response $response PSR Response
274 * @param array $args Request arguments
278 public function edit(Request
$request, Response
$response, array $args = []) :Response
280 $id_dynf = (int)$args['id'];
281 $form_name = $args['form'];
284 if ($this->session
->dynamicfieldtype
) {
285 $df = $this->session
->dynamicfieldtype
;
286 $this->session
->dynamicfieldtype
= null;
288 $df = DynamicField
::loadFieldType($this->zdb
, $id_dynf);
290 $this->flash
->addMessage(
292 _T("Unable to retrieve field information.")
296 ->withHeader('Location', $this->router
->pathFor('configureDynamicFields'));
301 'page_title' => _T("Edit field"),
303 'form_name' => $form_name,
304 'perm_names' => DynamicField
::getPermsNames(),
305 'mode' => ($request->isXhr() ?
'ajax' : ''),
321 * @param Request $request PSR Request
322 * @param Response $response PSR Response
323 * @param array $args Request arguments
327 public function doEdit(Request
$request, Response
$response, array $args = []) :Response
329 $post = $request->getParsedBody();
331 $error_detected = [];
332 $warning_detected = [];
334 $field_id = (int)$args['id'];
335 $df = DynamicField
::loadFieldType($this->zdb
, $field_id);
339 $error_detected = $df->getErrors();
340 $warning_detected = $df->getWarnings();
341 } catch (\Exception
$e) {
342 $msg = 'An error occurred storing dynamic field ' . $df->getId() . '.';
348 if (GALETTE_MODE
== 'DEV') {
351 $error_detected[] = _T('An error occurred editing dynamic field :(');
355 if (count($error_detected) > 0) {
356 foreach ($error_detected as $error) {
357 $this->flash
->addMessage(
363 $this->flash
->addMessage(
365 _T('Dynamic field has been successfully stored!')
369 if (count($warning_detected) > 0) {
370 foreach ($warning_detected as $warning) {
371 $this->flash
->addMessage(
378 //handle redirections
379 if (count($error_detected) > 0) {
380 //something went wrong :'(
381 $this->session
->dynamicfieldtype
= $df;
386 $this->router
->pathFor(
396 $this->router
->pathFor(
397 'configureDynamicFields',
398 ['form' => $args['form']]
408 * Get redirection URI
410 * @param array $args Route arguments
414 public function redirectUri(array $args = [])
416 return $this->router
->pathFor('configureDynamicFields');
422 * @param array $args Route arguments
426 public function formUri(array $args = [])
428 return $this->router
->pathFor(
429 'doRemoveDynamicField',
430 ['id' => $args['id'], 'form' => $args['form']]
435 * Get confirmation removal page title
437 * @param array $args Route arguments
441 public function confirmRemoveTitle(array $args = [])
443 $field = DynamicField
::loadFieldType($this->zdb
, (int)$args['id']);
444 if ($field === false) {
445 $this->flash
->addMessage(
447 _T("Requested field does not exists!")
451 ->withHeader('Location', $this->router
->pathFor('configureDynamicFields', ['form' => $args['form']]));
455 _T('Remove dynamic field %1$s'),
463 * @param array $args Route arguments
464 * @param array $post POST values
468 protected function doDelete(array $args, array $post)
470 $field_id = (int)$post['id'];
471 $field = DynamicField
::loadFieldType($this->zdb
, $field_id);
472 return $field->remove();
481 * @param Request $request PSR Request
482 * @param Response $response PSR Response
483 * @param array $args Request arguments
487 public function move(Request
$request, Response
$response, array $args = []) :Response
489 $field_id = (int)$args['id'];
490 $form_name = $args['form'];
492 $field = DynamicField
::loadFieldType($this->zdb
, $field_id);
493 if ($field->move($args['direction'])) {
494 $this->flash
->addMessage(
496 _T("Field has been successfully moved")
499 $this->flash
->addMessage(
501 _T("An error occurred moving field :(")
507 ->withHeader('Location', $this->router
->pathFor('configureDynamicFields', ['form' => $form_name]));