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\Repository\DynamicFieldsSet
;
41 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 string $form_name Form name
73 public function add(Request
$request, Response
$response, string $form_name = null): Response
76 'page_title' => _T("Add field"),
77 'form_name' => $form_name,
79 'perm_names' => DynamicField
::getPermsNames(),
80 'mode' => ($request->isXhr() ?
'ajax' : ''),
81 'field_type_names' => DynamicField
::getFieldsTypesNames()
84 if ($this->session
->dynamicfieldtype
) {
85 $params['df'] = $this->session
->dynamicfieldtype
;
86 $this->session
->dynamicfieldtype
= null;
92 'modals/configuration_dynamic_field_form.html.twig',
101 * @param Request $request PSR Request
102 * @param Response $response PSR Response
103 * @param string $form_name Form name
107 public function doAdd(Request
$request, Response
$response, string $form_name = null): Response
109 $post = $request->getParsedBody();
110 $post['form_name'] = $form_name;
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 (Throwable
$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(
168 ['form_name' => $form_name]
172 if (!$df instanceof \Galette\DynamicFields\Separator
) {
177 $this->router
->pathFor(
180 'form_name' => $form_name,
191 $this->router
->pathFor(
192 'configureDynamicFields',
193 ['form_name' => $form_name]
205 * @param Request $request PSR Request
206 * @param Response $response PSR Response
207 * @param string $option One of 'page' or 'order'
208 * @param string|integer $value Value of the option
209 * @param string $form_name Form name
213 public function list(
220 if (isset($_POST['form_name']) && trim($_POST['form_name']) != '') {
221 $form_name = $_POST['form_name'];
223 $fields = new DynamicFieldsSet($this->zdb
, $this->login
);
224 $fields_list = $fields->getList($form_name);
227 'fields_list' => $fields_list,
228 'form_name' => $form_name,
229 'form_title' => DynamicField
::getFormTitle($form_name),
230 'page_title' => _T("Dynamic fields configuration")
233 $tpl = 'pages/configuration_dynamic_fields.html.twig';
234 //Render directly template if we called from ajax,
235 //render in a full page otherwise
238 ||
isset($request->getQueryParams()['ajax'])
239 && $request->getQueryParams()['ajax'] == 'true'
241 $tpl = 'elements/edit_dynamic_fields.html.twig';
243 $all_forms = DynamicField
::getFormsNames();
244 $params['all_forms'] = $all_forms;
259 * @param Request $request PSR Request
260 * @param Response $response PSR Response
264 public function filter(Request
$request, Response
$response): Response
275 * @param Request $request PSR Request
276 * @param Response $response PSR Response
277 * @param integer $id Dynamic field id
278 * @param string $form_name Form name
282 public function edit(Request
$request, Response
$response, int $id, $form_name = null): Response
285 if ($this->session
->dynamicfieldtype
) {
286 $df = $this->session
->dynamicfieldtype
;
287 $this->session
->dynamicfieldtype
= null;
289 $df = DynamicField
::loadFieldType($this->zdb
, $id);
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' : ''),
313 'modals/configuration_dynamic_field_form.html.twig',
322 * @param Request $request PSR Request
323 * @param Response $response PSR Response
324 * @param integer $id Dynamic field id
325 * @param string $form_name Form name
329 public function doEdit(Request
$request, Response
$response, int $id = null, string $form_name = null): Response
331 $post = $request->getParsedBody();
332 $post['form_name'] = $form_name;
334 $error_detected = [];
335 $warning_detected = [];
338 $df = DynamicField
::loadFieldType($this->zdb
, $field_id);
342 $error_detected = $df->getErrors();
343 $warning_detected = $df->getWarnings();
344 } catch (Throwable
$e) {
345 $msg = 'An error occurred storing dynamic field ' . $df->getId() . '.';
351 if (GALETTE_MODE
== 'DEV') {
354 $error_detected[] = _T('An error occurred editing dynamic field :(');
358 if (count($error_detected) > 0) {
359 foreach ($error_detected as $error) {
360 $this->flash
->addMessage(
366 $this->flash
->addMessage(
368 _T('Dynamic field has been successfully stored!')
372 if (count($warning_detected) > 0) {
373 foreach ($warning_detected as $warning) {
374 $this->flash
->addMessage(
381 //handle redirections
382 if (count($error_detected) > 0) {
383 //something went wrong :'(
384 $this->session
->dynamicfieldtype
= $df;
389 $this->router
->pathFor(
392 'form_name' => $form_name,
402 $this->router
->pathFor(
403 'configureDynamicFields',
404 ['form_name' => $form_name]
414 * Get redirection URI
416 * @param array $args Route arguments
420 public function redirectUri(array $args)
422 return $this->router
->pathFor('configureDynamicFields');
428 * @param array $args Route arguments
432 public function formUri(array $args)
434 return $this->router
->pathFor(
435 'doRemoveDynamicField',
436 ['id' => $args['id'], 'form_name' => $args['form_name']]
441 * Get confirmation removal page title
443 * @param array $args Route arguments
447 public function confirmRemoveTitle(array $args)
449 $field = DynamicField
::loadFieldType($this->zdb
, (int)$args['id']);
450 if ($field === false) {
451 $this->flash
->addMessage(
453 _T("Requested field does not exists!")
459 $this->router
->pathFor('configureDynamicFields', ['form_name' => $args['form_name']])
464 _T('Remove dynamic field %1$s'),
472 * @param array $args Route arguments
473 * @param array $post POST values
477 protected function doDelete(array $args, array $post)
479 $field_id = (int)$post['id'];
480 $field = DynamicField
::loadFieldType($this->zdb
, $field_id);
481 return $field->remove();
490 * @param Request $request PSR Request
491 * @param Response $response PSR Response
492 * @param integer $id Field id
493 * @param string $form_name Form name
494 * @param string $direction One of DynamicField::MOVE_*
498 public function move(
505 $field = DynamicField
::loadFieldType($this->zdb
, $id);
506 if ($field->move($direction)) {
507 $this->flash
->addMessage(
509 _T("Field has been successfully moved")
512 $this->flash
->addMessage(
514 _T("An error occurred moving field :(")
520 ->withHeader('Location', $this->router
->pathFor('configureDynamicFields', ['form_name' => $form_name]));