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

94 lines 3.0 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\Exceptions\ValidationException;
19 use IvyForms\Common\Sanitizer\Sanitizer;
20 use IvyForms\Controllers\Controller;
21 use IvyForms\Services\Confirmation\ConfirmationService;
22 use IvyForms\Services\Field\FieldService;
23 use IvyForms\Services\Form\FormService;
24 use IvyForms\Services\Translations\BackendStrings;
25 use IvyForms\Repository\FieldOptions\FieldOptionsRepository;
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 private FieldOptionsRepository $fieldOptionsRepository;
40 public function __construct(
41 FormService $formService,
42 FieldService $fieldService,
43 ConfirmationService $confirmationService,
44 FieldOptionsRepository $fieldOptionsRepository
45 ) {
46 $this->formService = $formService;
47 $this->fieldService = $fieldService;
48 $this->confirmationService = $confirmationService;
49 $this->fieldOptionsRepository = $fieldOptionsRepository;
50 }
51
52 /**
53 * @param WP_REST_Request $data
54 *
55 * @return WP_REST_Response
56 *
57 * @throws NotFoundException
58 * @throws InvalidArgumentException|ForbiddenException|ValidationException
59 */
60 public function handle(WP_REST_Request $data): WP_REST_Response
61 {
62 // Verify the nonce
63 Sanitizer::verifyNonce($data->get_header('X-WP-Nonce'));
64
65 // Check if the form ID is provided
66 if (empty($data->get_params())) {
67 throw new InvalidArgumentException(
68 BackendStrings::getExceptionStrings()['invalid_request_data']
69 );
70 }
71 // Sanitize the input data
72 $formId = Sanitizer::sanitizeId($data->get_param('id'));
73
74 $form = $this->formService->getFormById($formId);
75
76 // Use the service to get fields with options
77 $fieldsMap = $this->fieldService->getAllFieldsWithOptions($form->getId(), $this->fieldOptionsRepository);
78 $form->setFields($fieldsMap);
79
80 $confirmationsArr = $this->confirmationService->getConfirmationsById($formId);
81
82 $confirmations = [];
83
84 foreach ($confirmationsArr as $confirmation) {
85 $confirmations[] = $confirmation->toArray();
86 }
87
88 return new WP_REST_Response([
89 'message' => BackendStrings::getCommonStrings()['ok'],
90 'data' => array_merge($form->toArray(), ['confirmationId' => $confirmations[0]['id']])
91 ], 200);
92 }
93 }
94