PluginProbe
The Innovative Form Builder – IvyForms / 0.3
The Innovative Form Builder – IvyForms v0.3
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / backend / src / Controllers / Form / GetFormController.php

GetFormController.php in The Innovative Form Builder – IvyForms 0.3, at backend/src/Controllers/Form/GetFormController.php

95 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * @copyright © TMS-Plugins. All rights reserved.
5 * @licence See COPYING.md for license details.
6 */
7
8 namespace IvyForms\Controllers\Form;
9
10 // phpcs:disable PSR1.Files.SideEffects
11 if (!defined('ABSPATH')) {
12 exit; // Exit if accessed directly
13 }
14
15 use IvyForms\Common\Exceptions\ForbiddenException;
16 use IvyForms\Common\Exceptions\InvalidArgumentException;
17 use IvyForms\Common\Exceptions\NotFoundException;
18 use IvyForms\Common\Sanitizer\Sanitizer;
19 use IvyForms\Controllers\Controller;
20 use IvyForms\Services\Confirmation\ConfirmationService;
21 use IvyForms\Services\Field\FieldService;
22 use IvyForms\Services\Form\FormService;
23 use IvyForms\Services\Translations\BackendStrings;
24 use WP_REST_Request;
25 use WP_REST_Response;
26
27 /**
28 * Class GetFormController
29 *
30 * @package IvyForms\Controllers\Form
31 */
32 class GetFormController extends Controller
33 {
34 private FormService $formService;
35 private FieldService $fieldService;
36 private ConfirmationService $confirmationService;
37 public function __construct(
38 FormService $formService,
39 FieldService $fieldService,
40 ConfirmationService $confirmationService
41 ) {
42 $this->formService = $formService;
43 $this->fieldService = $fieldService;
44 $this->confirmationService = $confirmationService;
45 }
46
47 /**
48 * @param WP_REST_Request $data
49 *
50 * @return WP_REST_Response
51 *
52 * @throws NotFoundException
53 * @throws InvalidArgumentException|ForbiddenException
54 */
55 public function handle(WP_REST_Request $data): WP_REST_Response
56 {
57 // Verify the nonce
58 Sanitizer::verifyNonce($data->get_header('X-WP-Nonce'));
59
60 // Check if the form ID is provided
61 if (empty($data->get_params())) {
62 throw new InvalidArgumentException(
63 BackendStrings::getExceptionStrings()['invalid_request_data']
64 );
65 }
66 // Sanitize the input data
67 $formId = Sanitizer::sanitizeId($data->get_param('id'));
68
69 $form = $this->formService->getFormById($formId);
70
71 $fields = $this->fieldService->getAllFields($form->getId());
72
73 $fieldsMap = [];
74
75 $confirmationsArr = $this->confirmationService->getConfirmationsById($formId);
76
77 $confirmations = [];
78
79 foreach ($confirmationsArr as $confirmation) {
80 $confirmations[] = $confirmation->toArray();
81 }
82
83 foreach ($fields as $field) {
84 $fieldsMap[] = $field->toArray();
85 }
86
87 $form->setFields($fieldsMap);
88
89 return new WP_REST_Response([
90 'message' => BackendStrings::getCommonStrings()['ok'],
91 'data' => array_merge($form->toArray(), ['confirmationId' => $confirmations[0]['id']])
92 ], 200);
93 }
94 }
95