Skip to content

Commit

Permalink
Merge pull request #105 from ConductionNL/feature/DIMOC340/fix-attach…
Browse files Browse the repository at this point in the history
…ments

Some small fixes/changes for front-end attachments
  • Loading branch information
WilcoLouwerse authored Oct 17, 2024
2 parents d8cd5b3 + 00f8e0e commit fa83f74
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
28 changes: 28 additions & 0 deletions lib/Controller/PublicationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
10 changes: 6 additions & 4 deletions lib/Service/DownloadService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down
11 changes: 10 additions & 1 deletion lib/Service/ObjectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit fa83f74

Please sign in to comment.