PluginProbe
The Innovative Form Builder – IvyForms / 0.8
The Innovative Form Builder – IvyForms v0.8
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 1.2 1.3
ivyforms / backend / src / Controllers / Form / GetFormController.php

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

91 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * @copyright © Melograno Venture Studio. 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\Exceptions\QueryExecutionException;
19 use IvyForms\Common\Exceptions\ValidationException;
20 use IvyForms\Common\Sanitizer\Sanitizer;
21 use IvyForms\Controllers\Controller;
22 use IvyForms\Services\Confirmation\ConfirmationService;
23 use IvyForms\Services\Field\FieldService;
24 use IvyForms\Services\Form\FormService;
25 use IvyForms\Services\Translations\BackendStrings;
26 use WP_REST_Request;
27 use WP_REST_Response;
28
29 /**
30 * Class GetFormController
31 *
32 * @package IvyForms\Controllers\Form
33 */
34 class GetFormController extends Controller
35 {
36 private FormService $formService;
37 private FieldService $fieldService;
38 private ConfirmationService $confirmationService;
39
40 public function __construct(
41 FormService $formService,
42 FieldService $fieldService,
43 ConfirmationService $confirmationService
44 ) {
45 $this->formService = $formService;
46 $this->fieldService = $fieldService;
47 $this->confirmationService = $confirmationService;
48 }
49
50 /**
51 * @param WP_REST_Request $data
52 * @return WP_REST_Response
53 * @throws NotFoundException
54 * @throws InvalidArgumentException|ForbiddenException|ValidationException
55 * @throws QueryExecutionException
56 */
57 public function handle(WP_REST_Request $data): WP_REST_Response
58 {
59 // Verify the nonce
60 Sanitizer::verifyNonce($data->get_header('X-WP-Nonce'));
61
62 // Check if the form ID is provided
63 if (empty($data->get_params())) {
64 throw new InvalidArgumentException(
65 BackendStrings::getExceptionStrings()['invalid_request_data']
66 );
67 }
68 // Sanitize the input data
69 $formId = Sanitizer::sanitizeId($data->get_param('id'));
70
71 $form = $this->formService->getFormById($formId);
72
73 // Use the service to get fields with options
74 $fieldsMap = $this->fieldService->getAllFieldsWithOptions($form->getId());
75 $form->setFields($fieldsMap);
76
77 $confirmationsArr = $this->confirmationService->getConfirmationsById($formId);
78
79 $confirmations = [];
80
81 foreach ($confirmationsArr as $confirmation) {
82 $confirmations[] = $confirmation->toArray();
83 }
84
85 return new WP_REST_Response([
86 'message' => BackendStrings::getCommonStrings()['ok'],
87 'data' => array_merge($form->toArray(), ['confirmationId' => $confirmations[0]['id']])
88 ], 200);
89 }
90 }
91