← All changes
|
backend/src/Controllers/Form/DuplicateFormController.php
+28
-41
0.1.2
→
1.5
View file →
| @@ -1,18 +1,13 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | /** |
| 4 | - * @copyright © TMS-Plugins. All rights reserved. | |
| 4 | + * @copyright © Melograno Venture Studio. All rights reserved. | |
| 5 | 5 | * @licence See COPYING.md for license details. |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace IvyForms\Controllers\Form; |
| 9 | 9 | |
| 10 | -// phpcs:disable PSR1.Files.SideEffects | |
| 11 | -if (!defined('ABSPATH')) { | |
| 12 | - exit; // Exit if accessed directly | |
| 13 | -} | |
| 14 | - | |
| 15 | 10 | use IvyForms\Common\Exceptions\ForbiddenException; |
| 16 | 11 | use IvyForms\Common\Exceptions\InvalidArgumentException; |
| 17 | 12 | use IvyForms\Common\Exceptions\NotFoundException; |
| 18 | 13 | use IvyForms\Common\Exceptions\QueryExecutionException; |
| @@ -18,12 +13,15 @@ | ||
| 18 | 13 | use IvyForms\Common\Exceptions\QueryExecutionException; |
| 19 | 14 | use IvyForms\Common\Exceptions\ValidationException; |
| 20 | 15 | use IvyForms\Common\Sanitizer\Sanitizer; |
| 21 | 16 | use IvyForms\Controllers\Controller; |
| 22 | -use IvyForms\Factory\Field\FieldFactory; | |
| 23 | 17 | use IvyForms\Factory\Form\FormFactory; |
| 18 | +use IvyForms\Services\Confirmation\ConfirmationService; | |
| 24 | 19 | use IvyForms\Services\Field\FieldService; |
| 25 | 20 | use IvyForms\Services\Form\FormService; |
| 21 | +use IvyForms\Services\Notification\NotificationService; | |
| 22 | +use IvyForms\Services\Permissions\AccessRulesFormScopeExtender; | |
| 23 | +use IvyForms\Services\Translations\BackendStrings; | |
| 26 | 24 | use WP_REST_Request; |
| 27 | 25 | use WP_REST_Response; |
| 28 | 26 | |
| 29 | 27 | /** |
| @@ -34,15 +32,25 @@ | ||
| 34 | 32 | class DuplicateFormController extends Controller |
| 35 | 33 | { |
| 36 | 34 | private FormService $formService; |
| 37 | 35 | private FieldService $fieldService; |
| 36 | + private NotificationService $notificationService; | |
| 37 | + private ConfirmationService $confirmationService; | |
| 38 | 38 | |
| 39 | + private AccessRulesFormScopeExtender $accessRulesFormScopeExtender; | |
| 40 | + | |
| 39 | 41 | public function __construct( |
| 40 | 42 | FormService $formService, |
| 41 | - FieldService $fieldService | |
| 43 | + FieldService $fieldService, | |
| 44 | + NotificationService $notificationService, | |
| 45 | + ConfirmationService $confirmationService, | |
| 46 | + AccessRulesFormScopeExtender $accessRulesFormScopeExtender | |
| 42 | 47 | ) { |
| 43 | - $this->formService = $formService; | |
| 44 | - $this->fieldService = $fieldService; | |
| 48 | + $this->formService = $formService; | |
| 49 | + $this->fieldService = $fieldService; | |
| 50 | + $this->notificationService = $notificationService; | |
| 51 | + $this->confirmationService = $confirmationService; | |
| 52 | + $this->accessRulesFormScopeExtender = $accessRulesFormScopeExtender; | |
| 45 | 53 | } |
| 46 | 54 | |
| 47 | 55 | /** |
| 48 | 56 | * @param WP_REST_Request $data |
| @@ -61,46 +69,25 @@ | ||
| 61 | 69 | Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); |
| 62 | 70 | |
| 63 | 71 | // Check if the form ID is provided |
| 64 | 72 | if (empty($data->get_params())) { |
| 65 | - throw new InvalidArgumentException('Invalid request data.'); | |
| 73 | + throw new InvalidArgumentException( | |
| 74 | + BackendStrings::getExceptionStrings()['invalid_request_data'] | |
| 75 | + ); | |
| 66 | 76 | } |
| 67 | 77 | |
| 68 | 78 | $formId = Sanitizer::sanitizeId($data->get_param('id')) ; |
| 69 | 79 | |
| 70 | - // Retrieve the original form | |
| 71 | - $originalForm = $this->formService->getFormById($formId); | |
| 80 | + $newFormId = $this->formService->duplicateForm($formId); | |
| 72 | 81 | |
| 73 | - // Duplicate the form's parameters, excluding the ID | |
| 74 | - $newFormData = $originalForm->toArray(); | |
| 75 | - unset($newFormData['id']); // Exclude the ID | |
| 76 | - $newFormData['name'] .= ' (Copy)'; // Append "(Copy)" to the name | |
| 77 | - $newForm = FormFactory::create($newFormData); | |
| 82 | + $this->fieldService->duplicateFields($formId, $newFormId); | |
| 83 | + $this->notificationService->duplicateNotifications($formId, $newFormId); | |
| 84 | + $this->confirmationService->duplicateConfirmations($formId, $newFormId); | |
| 78 | 85 | |
| 79 | - // Save the new form to the database | |
| 80 | - $newFormId = $this->formService->createForm($newForm); | |
| 81 | - if (!$newFormId) { | |
| 82 | - throw new QueryExecutionException('Failed to duplicate form.'); | |
| 83 | - } | |
| 86 | + $this->accessRulesFormScopeExtender->appendCreatedFormToCurrentUserScope($newFormId); | |
| 84 | 87 | |
| 85 | - // Retrieve and duplicate the fields associated with the original form | |
| 86 | - $originalFields = $this->fieldService->getAllFields($formId); | |
| 87 | - | |
| 88 | - if (!empty($originalFields)) { | |
| 89 | - foreach ($originalFields as $field) { | |
| 90 | - $fieldData = $field->toArray(); | |
| 91 | - $fieldData['formId'] = $newFormId; // Link to the new form | |
| 92 | - unset($fieldData['id']); // Exclude the field ID | |
| 93 | - $newField = FieldFactory::create($fieldData); | |
| 94 | - $this->fieldService->addField($newField); | |
| 95 | - } | |
| 96 | - } | |
| 97 | - | |
| 98 | - // Set the new form ID | |
| 99 | - $newForm->setId($newFormId); | |
| 100 | - | |
| 101 | 88 | return new WP_REST_Response([ |
| 102 | - 'message' => 'OK', | |
| 103 | - 'data' => $newForm->toArray() | |
| 89 | + 'message' => BackendStrings::getCommonStrings()['ok'], | |
| 90 | + 'data' => $this->formService->getFormById($newFormId)->toArray() | |
| 104 | 91 | ], 200); |
| 105 | 92 | } |
| 106 | 93 | } |