| @@ -1,0 +1,70 @@ | ||
| 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 | +use IvyForms\Common\Exceptions\ForbiddenException; | |
| 11 | +use IvyForms\Common\Exceptions\InvalidArgumentException; | |
| 12 | +use IvyForms\Common\Exceptions\NotFoundException; | |
| 13 | +use IvyForms\Common\Sanitizer\Sanitizer; | |
| 14 | +use IvyForms\Controllers\Controller; | |
| 15 | +use IvyForms\Services\Amelia\AmeliaService; | |
| 16 | +use IvyForms\Services\Entry\EntryService; | |
| 17 | +use IvyForms\Services\Translations\BackendStrings; | |
| 18 | +use WP_REST_Request; | |
| 19 | +use WP_REST_Response; | |
| 20 | + | |
| 21 | +/** | |
| 22 | + * Class GetEntryController | |
| 23 | + * | |
| 24 | + * @package IvyForms\Controllers\Entry | |
| 25 | + */ | |
| 26 | +class GetEntryController extends Controller | |
| 27 | +{ | |
| 28 | + private EntryService $entryService; | |
| 29 | + private AmeliaService $ameliaService; | |
| 30 | + | |
| 31 | + public function __construct(EntryService $entryService, AmeliaService $ameliaService) | |
| 32 | + { | |
| 33 | + $this->entryService = $entryService; | |
| 34 | + $this->ameliaService = $ameliaService; | |
| 35 | + } | |
| 36 | + | |
| 37 | + /** | |
| 38 | + * @param WP_REST_Request $data | |
| 39 | + * | |
| 40 | + * @return WP_REST_Response | |
| 41 | + * | |
| 42 | + * @throws NotFoundException | |
| 43 | + * @throws InvalidArgumentException|ForbiddenException | |
| 44 | + */ | |
| 45 | + public function handle(WP_REST_Request $data): WP_REST_Response | |
| 46 | + { | |
| 47 | + // Verify the nonce | |
| 48 | + Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); | |
| 49 | + | |
| 50 | + // Check if the entry ID is provided | |
| 51 | + if (empty($data->get_params())) { | |
| 52 | + throw new InvalidArgumentException( | |
| 53 | + BackendStrings::getExceptionStrings()['entry_id_required'] | |
| 54 | + ); | |
| 55 | + } | |
| 56 | + | |
| 57 | + $entryId = Sanitizer::sanitizeId($data->get_param('id')); | |
| 58 | + | |
| 59 | + $entry = $this->entryService->getEntryManager()->getEntry($entryId); | |
| 60 | + $entryArray = $entry->toArray(); | |
| 61 | + $entryArray['ameliaBookingUrls'] = $this->ameliaService->getBookingUrlsByEntryId($entryId); | |
| 62 | + | |
| 63 | + $entryFields = $this->entryService->getEntryFieldManager()->getEntryFields([$entryArray]); | |
| 64 | + | |
| 65 | + return new WP_REST_Response([ | |
| 66 | + 'entry' => $entryArray, | |
| 67 | + 'fields' => $entryFields, | |
| 68 | + ], 200); | |
| 69 | + } | |
| 70 | +} | |