← All changes
|
backend/src/Controllers/Form/DeleteFormsController.php
+97
-0
0.6.1
→
1.5.1
View file →
| @@ -1,0 +1,97 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace IvyForms\Controllers\Form; | |
| 4 | + | |
| 5 | +use IvyForms\Common\Exceptions\ForbiddenException; | |
| 6 | +use IvyForms\Common\Exceptions\InvalidArgumentException; | |
| 7 | +use IvyForms\Common\Exceptions\QueryExecutionException; | |
| 8 | +use IvyForms\Common\Sanitizer\Sanitizer; | |
| 9 | +use IvyForms\Controllers\Controller; | |
| 10 | +use IvyForms\Services\Confirmation\ConfirmationService; | |
| 11 | +use IvyForms\Services\Entry\EntryService; | |
| 12 | +use IvyForms\Services\Field\FieldService; | |
| 13 | +use IvyForms\Services\Form\FormService; | |
| 14 | +use IvyForms\Services\Notification\NotificationService; | |
| 15 | +use IvyForms\Services\Permissions\RoutePermissionService; | |
| 16 | +use IvyForms\Services\Translations\BackendStrings; | |
| 17 | +use WP_REST_Request; | |
| 18 | +use WP_REST_Response; | |
| 19 | + | |
| 20 | +class DeleteFormsController extends Controller | |
| 21 | +{ | |
| 22 | + private FormService $formService; | |
| 23 | + private FieldService $fieldService; | |
| 24 | + private NotificationService $notificationService; | |
| 25 | + private ConfirmationService $confirmationService; | |
| 26 | + private EntryService $entryService; | |
| 27 | + | |
| 28 | + private RoutePermissionService $routePermissionService; | |
| 29 | + | |
| 30 | + public function __construct( | |
| 31 | + FormService $formService, | |
| 32 | + FieldService $fieldService, | |
| 33 | + NotificationService $notificationService, | |
| 34 | + ConfirmationService $confirmationService, | |
| 35 | + EntryService $entryService, | |
| 36 | + RoutePermissionService $routePermissionService | |
| 37 | + ) { | |
| 38 | + $this->formService = $formService; | |
| 39 | + $this->fieldService = $fieldService; | |
| 40 | + $this->notificationService = $notificationService; | |
| 41 | + $this->confirmationService = $confirmationService; | |
| 42 | + $this->entryService = $entryService; | |
| 43 | + $this->routePermissionService = $routePermissionService; | |
| 44 | + } | |
| 45 | + | |
| 46 | + /** | |
| 47 | + * Delete forms by IDs | |
| 48 | + * | |
| 49 | + * @param WP_REST_Request $data | |
| 50 | + * | |
| 51 | + * @return WP_REST_Response | |
| 52 | + * | |
| 53 | + * @throws InvalidArgumentException | |
| 54 | + * @throws ForbiddenException | |
| 55 | + * @throws QueryExecutionException | |
| 56 | + */ | |
| 57 | + public function handle(WP_REST_Request $data): WP_REST_Response | |
| 58 | + { | |
| 59 | + // Verify the nonce | |
| 60 | + Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); | |
| 61 | + | |
| 62 | + // Check if the form IDs are provided | |
| 63 | + if (empty($data->get_params())) { | |
| 64 | + throw new InvalidArgumentException( | |
| 65 | + BackendStrings::getExceptionStrings()['invalid_request_data'] | |
| 66 | + ); | |
| 67 | + } | |
| 68 | + | |
| 69 | + $ids = Sanitizer::sanitizeIds($data->get_param('ids')); | |
| 70 | + if ($ids === []) { | |
| 71 | + return new WP_REST_Response([ | |
| 72 | + 'success' => false, | |
| 73 | + 'message' => BackendStrings::getExceptionStrings()['invalid_or_missing_ids'], | |
| 74 | + ], 400); | |
| 75 | + } | |
| 76 | + | |
| 77 | + foreach ($ids as $formId) { | |
| 78 | + if (!$this->routePermissionService->canDeleteForms((int) $formId)) { | |
| 79 | + throw new ForbiddenException('forbidden'); | |
| 80 | + } | |
| 81 | + } | |
| 82 | + | |
| 83 | + $this->entryService->getDeletionManager()->deleteEntriesByFormIds($ids); | |
| 84 | + $this->confirmationService->deleteConfirmationsByFormIds($ids); | |
| 85 | + $this->notificationService->deleteNotificationsByFormIds($ids); | |
| 86 | + $this->fieldService->deleteFieldsByFormIds($ids); | |
| 87 | + $this->formService->deleteForms($ids); | |
| 88 | + | |
| 89 | + return new WP_REST_Response( | |
| 90 | + [ | |
| 91 | + 'message' => BackendStrings::getCommonStrings()['ok'], | |
| 92 | + 'data' => [], | |
| 93 | + ], | |
| 94 | + 200 | |
| 95 | + ); | |
| 96 | + } | |
| 97 | +} | |