]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Controllers/Crud/DynamicFieldsController.php
Fix controller
[galette.git] / galette / lib / Galette / Controllers / Crud / DynamicFieldsController.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Galette dynamic fields controller
7 *
8 * PHP version 5
9 *
10 * Copyright © 2020 The Galette Team
11 *
12 * This file is part of Galette (http://galette.tuxfamily.org).
13 *
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.
18 *
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.
23 *
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/>.
26 *
27 * @category Controllers
28 * @package Galette
29 *
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
35 */
36
37 namespace Galette\Controllers\Crud;
38
39 use Galette\Repository\DynamicFieldsSet;
40 use Throwable;
41 use Galette\Controllers\CrudController;
42 use Slim\Http\Request;
43 use Slim\Http\Response;
44 use Galette\DynamicFields\DynamicField;
45 use Analog\Analog;
46
47 /**
48 * Galette dynamic fields controller
49 *
50 * @category Controllers
51 * @name DynamicFieldsController
52 * @package Galette
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
58 */
59
60 class DynamicFieldsController extends CrudController
61 {
62 // CRUD - Create
63
64 /**
65 * Add page
66 *
67 * @param Request $request PSR Request
68 * @param Response $response PSR Response
69 * @param string $form_name Form name
70 *
71 * @return Response
72 */
73 public function add(Request $request, Response $response, string $form_name = null): Response
74 {
75 $params = [
76 'page_title' => _T("Add field"),
77 'form_name' => $form_name,
78 'action' => 'add',
79 'perm_names' => DynamicField::getPermsNames(),
80 'mode' => ($request->isXhr() ? 'ajax' : ''),
81 'field_type_names' => DynamicField::getFieldsTypesNames()
82 ];
83
84 if ($this->session->dynamicfieldtype) {
85 $params['df'] = $this->session->dynamicfieldtype;
86 $this->session->dynamicfieldtype = null;
87 }
88
89 // display page
90 $this->view->render(
91 $response,
92 'modals/configuration_dynamic_field_form.html.twig',
93 $params
94 );
95 return $response;
96 }
97
98 /**
99 * Add action
100 *
101 * @param Request $request PSR Request
102 * @param Response $response PSR Response
103 * @param string $form_name Form name
104 *
105 * @return Response
106 */
107 public function doAdd(Request $request, Response $response, string $form_name = null): Response
108 {
109 $post = $request->getParsedBody();
110 $post['form_name'] = $form_name;
111
112 $error_detected = [];
113 $warning_detected = [];
114
115 $df = DynamicField::getFieldType($this->zdb, $post['field_type']);
116
117 try {
118 $df->store($post);
119 $error_detected = $df->getErrors();
120 $warning_detected = $df->getWarnings();
121 } catch (Throwable $e) {
122 $msg = 'An error occurred adding new dynamic field.';
123 Analog::log(
124 $msg . ' | ' .
125 $e->getMessage(),
126 Analog::ERROR
127 );
128 if (GALETTE_MODE == 'DEV') {
129 throw $e;
130 }
131 $error_detected[] = _T('An error occurred adding dynamic field :(');
132 }
133
134 //flash messages
135 if (count($error_detected) > 0) {
136 foreach ($error_detected as $error) {
137 $this->flash->addMessage(
138 'error_detected',
139 $error
140 );
141 }
142 } else {
143 $this->flash->addMessage(
144 'success_detected',
145 _T('Dynamic field has been successfully stored!')
146 );
147 }
148
149 if (count($warning_detected) > 0) {
150 foreach ($warning_detected as $warning) {
151 $this->flash->addMessage(
152 'warning_detected',
153 $warning
154 );
155 }
156 }
157
158 //handle redirections
159 if (count($error_detected) > 0) {
160 //something went wrong :'(
161 $this->session->dynamicfieldtype = $df;
162 return $response
163 ->withStatus(301)
164 ->withHeader(
165 'Location',
166 $this->router->pathFor(
167 'addDynamicField',
168 ['form_name' => $form_name]
169 )
170 );
171 } else {
172 if (!$df instanceof \Galette\DynamicFields\Separator) {
173 return $response
174 ->withStatus(301)
175 ->withHeader(
176 'Location',
177 $this->router->pathFor(
178 'editDynamicField',
179 [
180 'form_name' => $form_name,
181 'id' => $df->getId()
182 ]
183 )
184 );
185 }
186
187 return $response
188 ->withStatus(301)
189 ->withHeader(
190 'Location',
191 $this->router->pathFor(
192 'configureDynamicFields',
193 ['form_name' => $form_name]
194 )
195 );
196 }
197 }
198
199 // /CRUD - Create
200 // CRUD - Read
201
202 /**
203 * List page
204 *
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
210 *
211 * @return Response
212 */
213 public function list(
214 Request $request,
215 Response $response,
216 $option = null,
217 $value = null,
218 $form_name = 'adh'
219 ): Response {
220 if (isset($_POST['form_name']) && trim($_POST['form_name']) != '') {
221 $form_name = $_POST['form_name'];
222 }
223 $fields = new DynamicFieldsSet($this->zdb, $this->login);
224 $fields_list = $fields->getList($form_name);
225
226 $params = [
227 'fields_list' => $fields_list,
228 'form_name' => $form_name,
229 'form_title' => DynamicField::getFormTitle($form_name),
230 'page_title' => _T("Dynamic fields configuration")
231 ];
232
233 $tpl = 'pages/configuration_dynamic_fields.html.twig';
234 //Render directly template if we called from ajax,
235 //render in a full page otherwise
236 if (
237 $request->isXhr()
238 || isset($request->getQueryParams()['ajax'])
239 && $request->getQueryParams()['ajax'] == 'true'
240 ) {
241 $tpl = 'elements/edit_dynamic_fields.html.twig';
242 } else {
243 $all_forms = DynamicField::getFormsNames();
244 $params['all_forms'] = $all_forms;
245 }
246
247 // display page
248 $this->view->render(
249 $response,
250 $tpl,
251 $params
252 );
253 return $response;
254 }
255
256 /**
257 * Filtering
258 *
259 * @param Request $request PSR Request
260 * @param Response $response PSR Response
261 *
262 * @return Response
263 */
264 public function filter(Request $request, Response $response): Response
265 {
266 //no filtering
267 }
268
269 // /CRUD - Read
270 // CRUD - Update
271
272 /**
273 * Edit page
274 *
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
279 *
280 * @return Response
281 */
282 public function edit(Request $request, Response $response, int $id, $form_name = null): Response
283 {
284 $df = null;
285 if ($this->session->dynamicfieldtype) {
286 $df = $this->session->dynamicfieldtype;
287 $this->session->dynamicfieldtype = null;
288 } else {
289 $df = DynamicField::loadFieldType($this->zdb, $id);
290 if ($df === false) {
291 $this->flash->addMessage(
292 'error_detected',
293 _T("Unable to retrieve field information.")
294 );
295 return $response
296 ->withStatus(301)
297 ->withHeader('Location', $this->router->pathFor('configureDynamicFields'));
298 }
299 }
300
301 $params = [
302 'page_title' => _T("Edit field"),
303 'action' => 'edit',
304 'form_name' => $form_name,
305 'perm_names' => DynamicField::getPermsNames(),
306 'mode' => ($request->isXhr() ? 'ajax' : ''),
307 'df' => $df
308 ];
309
310 // display page
311 $this->view->render(
312 $response,
313 'modals/configuration_dynamic_field_form.html.twig',
314 $params
315 );
316 return $response;
317 }
318
319 /**
320 * Edit action
321 *
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
326 *
327 * @return Response
328 */
329 public function doEdit(Request $request, Response $response, int $id = null, string $form_name = null): Response
330 {
331 $post = $request->getParsedBody();
332 $post['form_name'] = $form_name;
333
334 $error_detected = [];
335 $warning_detected = [];
336
337 $field_id = $id;
338 $df = DynamicField::loadFieldType($this->zdb, $field_id);
339
340 try {
341 $df->store($post);
342 $error_detected = $df->getErrors();
343 $warning_detected = $df->getWarnings();
344 } catch (Throwable $e) {
345 $msg = 'An error occurred storing dynamic field ' . $df->getId() . '.';
346 Analog::log(
347 $msg . ' | ' .
348 $e->getMessage(),
349 Analog::ERROR
350 );
351 if (GALETTE_MODE == 'DEV') {
352 throw $e;
353 }
354 $error_detected[] = _T('An error occurred editing dynamic field :(');
355 }
356
357 //flash messages
358 if (count($error_detected) > 0) {
359 foreach ($error_detected as $error) {
360 $this->flash->addMessage(
361 'error_detected',
362 $error
363 );
364 }
365 } else {
366 $this->flash->addMessage(
367 'success_detected',
368 _T('Dynamic field has been successfully stored!')
369 );
370 }
371
372 if (count($warning_detected) > 0) {
373 foreach ($warning_detected as $warning) {
374 $this->flash->addMessage(
375 'warning_detected',
376 $warning
377 );
378 }
379 }
380
381 //handle redirections
382 if (count($error_detected) > 0) {
383 //something went wrong :'(
384 $this->session->dynamicfieldtype = $df;
385 return $response
386 ->withStatus(301)
387 ->withHeader(
388 'Location',
389 $this->router->pathFor(
390 'editDynamicField',
391 [
392 'form_name' => $form_name,
393 'id' => $id
394 ]
395 )
396 );
397 } else {
398 return $response
399 ->withStatus(301)
400 ->withHeader(
401 'Location',
402 $this->router->pathFor(
403 'configureDynamicFields',
404 ['form_name' => $form_name]
405 )
406 );
407 }
408 }
409
410 // /CRUD - Update
411 // CRUD - Delete
412
413 /**
414 * Get redirection URI
415 *
416 * @param array $args Route arguments
417 *
418 * @return string
419 */
420 public function redirectUri(array $args)
421 {
422 return $this->router->pathFor('configureDynamicFields');
423 }
424
425 /**
426 * Get form URI
427 *
428 * @param array $args Route arguments
429 *
430 * @return string
431 */
432 public function formUri(array $args)
433 {
434 return $this->router->pathFor(
435 'doRemoveDynamicField',
436 ['id' => $args['id'], 'form_name' => $args['form_name']]
437 );
438 }
439
440 /**
441 * Get confirmation removal page title
442 *
443 * @param array $args Route arguments
444 *
445 * @return string
446 */
447 public function confirmRemoveTitle(array $args)
448 {
449 $field = DynamicField::loadFieldType($this->zdb, (int)$args['id']);
450 if ($field === false) {
451 $this->flash->addMessage(
452 'error_detected',
453 _T("Requested field does not exists!")
454 );
455 return $response
456 ->withStatus(301)
457 ->withHeader(
458 'Location',
459 $this->router->pathFor('configureDynamicFields', ['form_name' => $args['form_name']])
460 );
461 }
462
463 return sprintf(
464 _T('Remove dynamic field %1$s'),
465 $field->getName()
466 );
467 }
468
469 /**
470 * Remove object
471 *
472 * @param array $args Route arguments
473 * @param array $post POST values
474 *
475 * @return boolean
476 */
477 protected function doDelete(array $args, array $post)
478 {
479 $field_id = (int)$post['id'];
480 $field = DynamicField::loadFieldType($this->zdb, $field_id);
481 return $field->remove();
482 }
483
484 // /CRUD - Delete
485 // /CRUD
486
487 /**
488 * Move field
489 *
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_*
495 *
496 * @return Response
497 */
498 public function move(
499 Request $request,
500 Response $response,
501 int $id,
502 string $form_name,
503 string $direction
504 ): Response {
505 $field = DynamicField::loadFieldType($this->zdb, $id);
506 if ($field->move($direction)) {
507 $this->flash->addMessage(
508 'success_detected',
509 _T("Field has been successfully moved")
510 );
511 } else {
512 $this->flash->addMessage(
513 'error_detected',
514 _T("An error occurred moving field :(")
515 );
516 }
517
518 return $response
519 ->withStatus(301)
520 ->withHeader('Location', $this->router->pathFor('configureDynamicFields', ['form_name' => $form_name]));
521 }
522 }