PluginProbe
The Innovative Form Builder – IvyForms / 0.1.2
The Innovative Form Builder – IvyForms v0.1.2
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.1.2, at backend/src/Controllers/Form/GetFormController.php

92 lines 2.5 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 WP_REST_Request;
24 use WP_REST_Response;
25
26 /**
27 * Class GetFormController
28 *
29 * @package IvyForms\Controllers\Form
30 */
31 class GetFormController extends Controller
32 {
33 private FormService $formService;
34 private FieldService $fieldService;
35 private ConfirmationService $confirmationService;
36 public function __construct(
37 FormService $formService,
38 FieldService $fieldService,
39 ConfirmationService $confirmationService
40 ) {
41 $this->formService = $formService;
42 $this->fieldService = $fieldService;
43 $this->confirmationService = $confirmationService;
44 }
45
46 /**
47 * @param WP_REST_Request $data
48 *
49 * @return WP_REST_Response
50 *
51 * @throws NotFoundException
52 * @throws InvalidArgumentException|ForbiddenException
53 */
54 public function handle(WP_REST_Request $data): WP_REST_Response
55 {
56 // Verify the nonce
57 Sanitizer::verifyNonce($data->get_header('X-WP-Nonce'));
58
59 // Check if the form ID is provided
60 if (empty($data->get_params())) {
61 throw new InvalidArgumentException('Invalid request data.');
62 }
63 // Sanitize the input data
64 $formId = Sanitizer::sanitizeId($data->get_param('id'));
65
66 $form = $this->formService->getFormById($formId);
67
68 $fields = $this->fieldService->getAllFields($form->getId());
69
70 $fieldsMap = [];
71
72 $confirmationsArr = $this->confirmationService->getConfirmationsById($formId);
73
74 $confirmations = [];
75
76 foreach ($confirmationsArr as $confirmation) {
77 $confirmations[] = $confirmation->toArray();
78 }
79
80 foreach ($fields as $field) {
81 $fieldsMap[] = $field->toArray();
82 }
83
84 $form->setFields($fieldsMap);
85
86 return new WP_REST_Response([
87 'message' => 'OK',
88 'data' => array_merge($form->toArray(), ['confirmationId' => $confirmations[0]['id']])
89 ], 200);
90 }
91 }
92