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 / Form / DuplicateFormController.php

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

87 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 use IvyForms\Common\Exceptions\ForbiddenException;
11 use IvyForms\Common\Exceptions\InvalidArgumentException;
12 use IvyForms\Common\Exceptions\NotFoundException;
13 use IvyForms\Common\Exceptions\QueryExecutionException;
14 use IvyForms\Common\Exceptions\ValidationException;
15 use IvyForms\Common\Sanitizer\Sanitizer;
16 use IvyForms\Controllers\Controller;
17 use IvyForms\Factory\Form\FormFactory;
18 use IvyForms\Services\Confirmation\ConfirmationService;
19 use IvyForms\Services\Field\FieldService;
20 use IvyForms\Services\Form\FormService;
21 use IvyForms\Services\Notification\NotificationService;
22 use IvyForms\Services\Translations\BackendStrings;
23 use WP_REST_Request;
24 use WP_REST_Response;
25
26 /**
27 * Class DuplicateFormController
28 *
29 * @package IvyForms\Controllers\Form
30 */
31 class DuplicateFormController extends Controller
32 {
33 private FormService $formService;
34 private FieldService $fieldService;
35 private NotificationService $notificationService;
36 private ConfirmationService $confirmationService;
37
38 public function __construct(
39 FormService $formService,
40 FieldService $fieldService,
41 NotificationService $notificationService,
42 ConfirmationService $confirmationService
43 ) {
44 $this->formService = $formService;
45 $this->fieldService = $fieldService;
46 $this->notificationService = $notificationService;
47 $this->confirmationService = $confirmationService;
48 }
49
50 /**
51 * @param WP_REST_Request $data
52 *
53 * @return WP_REST_Response
54 *
55 * @throws QueryExecutionException
56 * @throws InvalidArgumentException
57 * @throws NotFoundException
58 * @throws ValidationException
59 * @throws ForbiddenException
60 */
61 public function handle(WP_REST_Request $data): WP_REST_Response
62 {
63 // Verify the nonce
64 Sanitizer::verifyNonce($data->get_header('X-WP-Nonce'));
65
66 // Check if the form ID is provided
67 if (empty($data->get_params())) {
68 throw new InvalidArgumentException(
69 BackendStrings::getExceptionStrings()['invalid_request_data']
70 );
71 }
72
73 $formId = Sanitizer::sanitizeId($data->get_param('id')) ;
74
75 $newFormId = $this->formService->duplicateForm($formId);
76
77 $this->fieldService->duplicateFields($formId, $newFormId);
78 $this->notificationService->duplicateNotifications($formId, $newFormId);
79 $this->confirmationService->duplicateConfirmations($formId, $newFormId);
80
81 return new WP_REST_Response([
82 'message' => BackendStrings::getCommonStrings()['ok'],
83 'data' => $this->formService->getFormById($newFormId)->toArray()
84 ], 200);
85 }
86 }
87