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 / Entry / AddEntryController.php

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

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