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