diff --git a/lib/Controller/PublicationsController.php b/lib/Controller/PublicationsController.php index 79bc9891..3feda27d 100644 --- a/lib/Controller/PublicationsController.php +++ b/lib/Controller/PublicationsController.php @@ -109,6 +109,34 @@ public function show(string|int $id, ObjectService $objectService): JSONResponse return new JSONResponse($object); } + /** + * Return all attachments for given publication. + * + * @param string|int $id The id of the publication. + * + * @return JSONResponse The Response containing attachments. + * @throws DoesNotExistException|MultipleObjectsReturnedException|ContainerExceptionInterface|NotFoundExceptionInterface + * + * @NoAdminRequired + * @NoCSRFRequired + */ + public function attachments(string|int $id): JSONResponse + { + // Fetch the publication object by its ID + $object = $this->objectService->getObject('publication', $id); + + // Fetch attachment objects + $objects = $this->objectService->getMultipleObjects(objectType: 'attachment', ids: $object['attachments']); + + // Prepare response data + $data = [ + 'results' => $objects, + 'total' => count($objects) + ]; + + return new JSONResponse($data); + } + /** * Download a publication in either PDF or ZIP format. * diff --git a/lib/Service/DownloadService.php b/lib/Service/DownloadService.php index 591dc977..7d582b9a 100644 --- a/lib/Service/DownloadService.php +++ b/lib/Service/DownloadService.php @@ -190,7 +190,7 @@ private function prepareZip(string $tempFolder, array $attachments, array $publi } // Add all attachments to Bijlagen folder - foreach ($attachments['results'] as $attachment) { + foreach ($attachments as $attachment) { $attachment = $attachment->jsonSerialize(); $file_content = file_get_contents($attachment['downloadUrl']); if ($file_content !== false) { @@ -265,11 +265,13 @@ public function createPublicationZip(ObjectService $objectService, string|int $i */ public function publicationAttachments(string|int $id, ObjectService $objectService): array|JSONResponse { - $filters['publication'] = $id; - // Fetch attachment objects try { - return $objectService->getObjects('attachment', null, null, $filters); + // Fetch the publication object by its ID + $object = $objectService->getObject(objectType: 'publication', id: $id); + + // Fetch attachment objects + return $objectService->getMultipleObjects(objectType: 'attachment', ids: $object['attachments']); } catch (NotFoundExceptionInterface|MultipleObjectsReturnedException|ContainerExceptionInterface|DoesNotExistException $e) { return new JSONResponse(data: ['error' => $e->getMessage()], statusCode: 500); } diff --git a/lib/Service/ObjectService.php b/lib/Service/ObjectService.php index adb95744..0376cada 100644 --- a/lib/Service/ObjectService.php +++ b/lib/Service/ObjectService.php @@ -345,7 +345,16 @@ public function getResultArrayForRequest(string $objectType, array $requestParam unset($filters['extend'], $filters['limit'], $filters['offset'], $filters['order']); // Fetch objects based on filters and order - $objects = $this->getObjects($objectType, null, null, $filters, $limit, $offset, $order, $extend); + $objects = $this->getObjects( + objectType: $objectType, + limit: $limit, + offset: $offset, + filters: $filters, + searchConditions: null, + searchParams: null, + sort: $order, + extend: $extend + ); // Extend the objects if the extend array is not empty if (empty($extend) === false) {