← All changes
|
backend/src/Controllers/Form/DuplicateFormController.php
+93
-0
0.6.1
→
1.5.1
View file →
| @@ -1,0 +1,93 @@ | ||
| 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\Permissions\AccessRulesFormScopeExtender; | |
| 23 | +use IvyForms\Services\Translations\BackendStrings; | |
| 24 | +use WP_REST_Request; | |
| 25 | +use WP_REST_Response; | |
| 26 | + | |
| 27 | +/** | |
| 28 | + * Class DuplicateFormController | |
| 29 | + * | |
| 30 | + * @package IvyForms\Controllers\Form | |
| 31 | + */ | |
| 32 | +class DuplicateFormController extends Controller | |
| 33 | +{ | |
| 34 | + private FormService $formService; | |
| 35 | + private FieldService $fieldService; | |
| 36 | + private NotificationService $notificationService; | |
| 37 | + private ConfirmationService $confirmationService; | |
| 38 | + | |
| 39 | + private AccessRulesFormScopeExtender $accessRulesFormScopeExtender; | |
| 40 | + | |
| 41 | + public function __construct( | |
| 42 | + FormService $formService, | |
| 43 | + FieldService $fieldService, | |
| 44 | + NotificationService $notificationService, | |
| 45 | + ConfirmationService $confirmationService, | |
| 46 | + AccessRulesFormScopeExtender $accessRulesFormScopeExtender | |
| 47 | + ) { | |
| 48 | + $this->formService = $formService; | |
| 49 | + $this->fieldService = $fieldService; | |
| 50 | + $this->notificationService = $notificationService; | |
| 51 | + $this->confirmationService = $confirmationService; | |
| 52 | + $this->accessRulesFormScopeExtender = $accessRulesFormScopeExtender; | |
| 53 | + } | |
| 54 | + | |
| 55 | + /** | |
| 56 | + * @param WP_REST_Request $data | |
| 57 | + * | |
| 58 | + * @return WP_REST_Response | |
| 59 | + * | |
| 60 | + * @throws QueryExecutionException | |
| 61 | + * @throws InvalidArgumentException | |
| 62 | + * @throws NotFoundException | |
| 63 | + * @throws ValidationException | |
| 64 | + * @throws ForbiddenException | |
| 65 | + */ | |
| 66 | + public function handle(WP_REST_Request $data): WP_REST_Response | |
| 67 | + { | |
| 68 | + // Verify the nonce | |
| 69 | + Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); | |
| 70 | + | |
| 71 | + // Check if the form ID is provided | |
| 72 | + if (empty($data->get_params())) { | |
| 73 | + throw new InvalidArgumentException( | |
| 74 | + BackendStrings::getExceptionStrings()['invalid_request_data'] | |
| 75 | + ); | |
| 76 | + } | |
| 77 | + | |
| 78 | + $formId = Sanitizer::sanitizeId($data->get_param('id')) ; | |
| 79 | + | |
| 80 | + $newFormId = $this->formService->duplicateForm($formId); | |
| 81 | + | |
| 82 | + $this->fieldService->duplicateFields($formId, $newFormId); | |
| 83 | + $this->notificationService->duplicateNotifications($formId, $newFormId); | |
| 84 | + $this->confirmationService->duplicateConfirmations($formId, $newFormId); | |
| 85 | + | |
| 86 | + $this->accessRulesFormScopeExtender->appendCreatedFormToCurrentUserScope($newFormId); | |
| 87 | + | |
| 88 | + return new WP_REST_Response([ | |
| 89 | + 'message' => BackendStrings::getCommonStrings()['ok'], | |
| 90 | + 'data' => $this->formService->getFormById($newFormId)->toArray() | |
| 91 | + ], 200); | |
| 92 | + } | |
| 93 | +} | |