From 6c5d6ee0beb20bc0d01b0406e68ddfee6854c3f6 Mon Sep 17 00:00:00 2001 From: Vitor Mattos Date: Tue, 19 Nov 2024 16:44:32 -0300 Subject: [PATCH] chore: return visible elements when this is necessary Signed-off-by: Vitor Mattos --- lib/Service/FileService.php | 70 +++++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 23 deletions(-) diff --git a/lib/Service/FileService.php b/lib/Service/FileService.php index 21f47e748a..caf52aa0fc 100644 --- a/lib/Service/FileService.php +++ b/lib/Service/FileService.php @@ -386,6 +386,19 @@ private function getFile(): array { 'displayName' => $this->userManager->get($this->file->getUserId())->getDisplayName(), ]; $return['file'] = $this->urlGenerator->linkToRoute('libresign.page.getPdf', ['uuid' => $this->file->getUuid()]); + if ($this->showVisibleElements) { + $signers = $this->signRequestMapper->getByMultipleFileId([$this->file->getId()]); + $return['visibleElements'] = []; + foreach ($this->signRequestMapper->getVisibleElementsFromSigners($signers) as $visibleElements) { + $return['visibleElements'] = array_merge( + $this->formatVisibleElementsToArray( + $visibleElements, + $this->file->getMetadata() + ), + $return['visibleElements'] + ); + } + } foreach ($this->getSigners() as $signer) { if ($signer['me']) { $return['url'] = $this->urlGenerator->linkToRoute('libresign.page.getPdfFile', ['uuid' => $signer['sign_uuid']]); @@ -520,29 +533,10 @@ private function associateAllAndFormat(IUser $user, array $files, array $signers } return $carry; }, false), - 'visibleElements' => array_map(function (FileElement $visibleElement) use ($file) { - $element = [ - 'elementId' => $visibleElement->getId(), - 'signRequestId' => $visibleElement->getSignRequestId(), - 'type' => $visibleElement->getType(), - 'coordinates' => [ - 'page' => $visibleElement->getPage(), - 'urx' => $visibleElement->getUrx(), - 'ury' => $visibleElement->getUry(), - 'llx' => $visibleElement->getLlx(), - 'lly' => $visibleElement->getLly() - ] - ]; - $metadata = json_decode($file['metadata'], true); - $dimension = $metadata['d'][$element['coordinates']['page'] - 1]; - - $element['coordinates']['left'] = $element['coordinates']['llx']; - $element['coordinates']['height'] = abs($element['coordinates']['ury'] - $element['coordinates']['lly']); - $element['coordinates']['top'] = $dimension['h'] - $element['coordinates']['ury']; - $element['coordinates']['width'] = $element['coordinates']['urx'] - $element['coordinates']['llx']; - - return $element; - }, $visibleElements[$signer->getId()] ?? []), + 'visibleElements' => $this->formatVisibleElementsToArray( + $visibleElements[$signer->getId()] ?? [], + json_decode($file['metadata'], true) + ), 'identifyMethods' => array_map(function (IdentifyMethod $identifyMethod) use ($signer): array { return [ 'method' => $identifyMethod->getIdentifierKey(), @@ -599,6 +593,36 @@ private function associateAllAndFormat(IUser $user, array $files, array $signers return $files; } + /** + * @param FileElement[] $visibleElements + * @param array + * @return array + */ + private function formatVisibleElementsToArray(array $visibleElements, array $metadata): array { + return array_map(function (FileElement $visibleElement) use ($metadata) { + $element = [ + 'elementId' => $visibleElement->getId(), + 'signRequestId' => $visibleElement->getSignRequestId(), + 'type' => $visibleElement->getType(), + 'coordinates' => [ + 'page' => $visibleElement->getPage(), + 'urx' => $visibleElement->getUrx(), + 'ury' => $visibleElement->getUry(), + 'llx' => $visibleElement->getLlx(), + 'lly' => $visibleElement->getLly() + ] + ]; + $dimension = $metadata['d'][$element['coordinates']['page'] - 1]; + + $element['coordinates']['left'] = $element['coordinates']['llx']; + $element['coordinates']['height'] = abs($element['coordinates']['ury'] - $element['coordinates']['lly']); + $element['coordinates']['top'] = $dimension['h'] - $element['coordinates']['ury']; + $element['coordinates']['width'] = $element['coordinates']['urx'] - $element['coordinates']['llx']; + + return $element; + }, $visibleElements); + } + public function getMyLibresignFile(int $nodeId): File { return $this->signRequestMapper->getMyLibresignFile( userId: $this->me->getUID(),