PluginProbe
The Innovative Form Builder – IvyForms / 1.1.1
The Innovative Form Builder – IvyForms v1.1.1
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 / Entry / AddEntryController.php

AddEntryController.php in The Innovative Form Builder – IvyForms 1.1.1, at backend/src/Controllers/Entry/AddEntryController.php

87 lines 2.6 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\Entry;
9
10 use IvyForms\Common\Exceptions\ForbiddenException;
11 use IvyForms\Common\Exceptions\InvalidArgumentException;
12 use IvyForms\Common\Exceptions\ValidationException;
13 use IvyForms\Common\Sanitizer\Sanitizer;
14 use IvyForms\Controllers\Controller;
15 use IvyForms\Factory\Entry\EntryFactory;
16 use IvyForms\Services\Entry\EntryService;
17 use IvyForms\Services\Field\FieldService;
18 use WP_REST_Request;
19 use WP_REST_Response;
20 use IvyForms\Services\Translations\BackendStrings;
21
22 /**
23 * Class AddEntryController
24 *
25 * @package IvyForms\Controllers\Entry
26 */
27 class AddEntryController extends Controller
28 {
29 private EntryService $entryService;
30 private FieldService $fieldService;
31
32 public function __construct(EntryService $entryService, FieldService $fieldService)
33 {
34 $this->entryService = $entryService;
35 $this->fieldService = $fieldService;
36 }
37
38 /**
39 * @param WP_REST_Request $data
40 *
41 * @return WP_REST_Response
42 *
43 * @throws ForbiddenException
44 * @throws InvalidArgumentException
45 * @throws ValidationException
46 */
47 protected function handle(WP_REST_Request $data): WP_REST_Response
48 {
49 // Verify the nonce
50 Sanitizer::verifyNonce($data->get_header('X-WP-Nonce'));
51
52 // Check if the entry data is provided
53 if (empty($data->get_params())) {
54 throw new InvalidArgumentException(
55 BackendStrings::getExceptionStrings()['invalid_request_data']
56 );
57 }
58
59 // Sanitize the input data
60 $params = Sanitizer::sanitizeEntryData($data->get_params());
61
62 // Extract formId and submission data
63 $formId = $params['formId'] ?? null;
64 if (!$formId) {
65 throw new InvalidArgumentException(
66 BackendStrings::getExceptionStrings()['invalid_form_id']
67 );
68 }
69 $formFields = $this->fieldService->getAllFields($formId);
70 $submissionData = $params['fields'] ?? [];
71
72 // Create Entry entity
73 $entry = EntryFactory::create($params);
74 $entryId = $this->entryService->getEntryManager()->createEntry($entry);
75
76 // Add EntryFields using the service method
77 $this->entryService->getEntryFieldManager()->addEntryFields($formFields, $entryId, $submissionData);
78
79 $entry->setId($entryId);
80
81 return new WP_REST_Response([
82 'message' => BackendStrings::getCommonStrings()['ok'],
83 'data' => $entry->toArray(),
84 ], 200);
85 }
86 }
87