Skip to content

Commit 0beb613

Browse files
committed
fix: make edition and retrieve visible elements compatible with envelop
Signed-off-by: Vitor Mattos <[email protected]>
1 parent 946b4b3 commit 0beb613

6 files changed

Lines changed: 55 additions & 46 deletions

File tree

lib/Controller/FileElementController.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ public function post(string $uuid, int $signRequestId, ?int $elementId = null, s
6969
'uuid' => $uuid,
7070
'userManager' => $this->userSession->getUser()
7171
]);
72-
$fileElement = $this->fileElementService->saveVisibleElement($visibleElement, $uuid);
73-
$statusCode = Http::STATUS_OK;
72+
$fileElement = $this->fileElementService->saveVisibleElement($visibleElement);
7473
return new DataResponse([
7574
'fileElementId' => $fileElement->getId(),
7675
]);

lib/Db/SignRequestMapper.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -459,18 +459,21 @@ public function getVisibleElementsFromSigners(array $signRequests): array {
459459
return [];
460460
}
461461
$qb = $this->db->getQueryBuilder();
462-
$qb->select('fe.*')
462+
463+
$qb->select('fe.*', 'f.metadata')
463464
->from('libresign_file_element', 'fe')
465+
->innerJoin('fe', 'libresign_file', 'f', $qb->expr()->eq('fe.file_id', 'f.id'))
464466
->where(
465467
$qb->expr()->in('fe.sign_request_id', $qb->createParameter('signRequestIds'))
466468
);
469+
467470
$return = [];
468471
foreach (array_chunk($signRequestIds, 1000) as $signRequestIdsChunk) {
469472
$qb->setParameter('signRequestIds', $signRequestIdsChunk, IQueryBuilder::PARAM_INT_ARRAY);
470473
$cursor = $qb->executeQuery();
471474
while ($row = $cursor->fetch()) {
472-
$fileElement = new FileElement();
473-
$return[$row['sign_request_id']][] = $fileElement->fromRow($row);
475+
$row['metadata'] = json_decode($row['metadata'], true);
476+
$return[$row['sign_request_id']][] = $row;
474477
}
475478
}
476479
return $return;

lib/Service/File/FileListService.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,10 @@ private function associateAllAndFormat(IUser $user, array $files, array $signers
115115
}
116116
return $carry;
117117
}, false),
118-
'visibleElements' => $this->formatVisibleElements(
119-
$visibleElements[$signer->getId()] ?? [],
120-
!empty($file['metadata'])?json_decode((string)$file['metadata'], true):[],
121-
$file['uuid'],
122-
),
118+
'visibleElements'
119+
=> $visibleElements[$signer->getId()]
120+
? $this->formatVisibleElements($visibleElements[$signer->getId()])
121+
: [],
123122
'identifyMethods' => array_map(fn (IdentifyMethod $identifyMethod): array => [
124123
'method' => $identifyMethod->getIdentifierKey(),
125124
'value' => $identifyMethod->getIdentifierValue(),
@@ -181,7 +180,8 @@ private function associateAllAndFormat(IUser $user, array $files, array $signers
181180

182181
$files[$key]['statusText'] = $this->fileMapper->getTextOfStatus((int)$files[$key]['status']);
183182
}
184-
unset($files[$key]['id'], $files[$key]['fileId']);
183+
$files[$key]['id'] = $files[$key]['fileId'];
184+
unset($files[$key]['fileId']);
185185
ksort($files[$key]);
186186
}
187187
return $files;
@@ -195,25 +195,25 @@ private function associateAllAndFormat(IUser $user, array $files, array $signers
195195
* @param string $uuid File UUID to include in response
196196
* @return array Formatted visible elements
197197
*/
198-
public function formatVisibleElements(array $visibleElements, array $metadata, string $uuid): array {
199-
return array_map(function (FileElement $visibleElement) use ($metadata, $uuid) {
200-
$page = $visibleElement->getPage();
201-
$urx = (int)$visibleElement->getUrx();
202-
$ury = (int)$visibleElement->getUry();
203-
$llx = (int)$visibleElement->getLlx();
204-
$lly = (int)$visibleElement->getLly();
205-
206-
$dimension = $metadata['d'][$page - 1];
198+
public function formatVisibleElements(array $visibleElements): array {
199+
return array_map(function ($visibleElement) {
200+
$page = $visibleElement['page'];
201+
$urx = (int)$visibleElement['urx'];
202+
$ury = (int)$visibleElement['ury'];
203+
$llx = (int)$visibleElement['llx'];
204+
$lly = (int)$visibleElement['lly'];
205+
206+
$dimension = $visibleElement['metadata']['d'][$page - 1];
207207
$height = abs($ury - $lly);
208208
$width = $urx - $llx;
209209
$top = (int)$dimension['h'] - $ury;
210210
$left = $llx;
211211

212212
return [
213-
'elementId' => $visibleElement->getId(),
214-
'signRequestId' => $visibleElement->getSignRequestId(),
215-
'type' => $visibleElement->getType(),
216-
'uuid' => $uuid,
213+
'elementId' => $visibleElement['id'],
214+
'signRequestId' => $visibleElement['sign_request_id'],
215+
'type' => $visibleElement['type'],
216+
'fileId' => $visibleElement['file_id'],
217217
'coordinates' => [
218218
'page' => $page,
219219
'urx' => $urx,

lib/Service/FileElementService.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ public function __construct(
2222
) {
2323
}
2424

25-
public function saveVisibleElement(array $element, string $uuid = ''): FileElement {
26-
$fileElement = $this->getVisibleElementFromProperties($element, $uuid);
25+
public function saveVisibleElement(array $element): FileElement {
26+
$fileElement = $this->getVisibleElementFromProperties($element);
2727
if ($fileElement->getId()) {
2828
$this->fileElementMapper->update($fileElement);
2929
} else {
@@ -32,21 +32,24 @@ public function saveVisibleElement(array $element, string $uuid = ''): FileEleme
3232
return $fileElement;
3333
}
3434

35-
private function getVisibleElementFromProperties(array $properties, string $uuid = ''): FileElement {
35+
private function getVisibleElementFromProperties(array $properties): FileElement {
3636
if (!empty($properties['elementId'])) {
3737
$fileElement = $this->fileElementMapper->getById($properties['elementId']);
3838
} else {
3939
$fileElement = new FileElement();
4040
$fileElement->setCreatedAt($this->timeFactory->getDateTime());
4141
}
4242
$file = null;
43-
if ($uuid) {
44-
$file = $this->fileMapper->getByUuid($uuid);
43+
if (!empty($properties['uuid'])) {
44+
$file = $this->fileMapper->getByUuid($properties['uuid']);
4545
$fileElement->setFileId($file->getId());
4646
} elseif (!empty($properties['fileId'])) {
4747
$file = $this->fileMapper->getById($properties['fileId']);
4848
$fileElement->setFileId($properties['fileId']);
4949
}
50+
if (!$file) {
51+
throw new \InvalidArgumentException('File not found for visible element');
52+
}
5053
$coordinates = $this->translateCoordinatesToInternalNotation($properties, $file);
5154
$fileElement->setSignRequestId($properties['signRequestId']);
5255
$fileElement->setType($properties['type']);

lib/Service/FileService.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -355,13 +355,16 @@ private function loadSigners(): void {
355355
return;
356356
}
357357

358-
if (!$this->options->isValidateFile() || !$this->file instanceof File || !$this->file->getSignedNodeId()) {
358+
if (!$this->options->isValidateFile() || !$this->file instanceof File) {
359359
return;
360360
}
361-
$fileNode = $this->getFile();
362-
$certData = $this->loadCertificateChain($fileNode, $this->file);
363-
if ($certData) {
364-
$this->signersLoader->loadSignersFromCertData($this->fileData, $certData, $this->options->getHost());
361+
362+
if ($this->file->getSignedNodeId()) {
363+
$fileNode = $this->getFile();
364+
$certData = $this->loadCertificateChain($fileNode, $this->file);
365+
if ($certData) {
366+
$this->signersLoader->loadSignersFromCertData($this->fileData, $certData, $this->options->getHost());
367+
}
365368
}
366369
$this->loadLibreSignSigners();
367370
}
@@ -530,12 +533,16 @@ private function loadLibreSignData(): void {
530533
$signers = $this->signRequestMapper->getByMultipleFileId([$this->file->getId()]);
531534
$this->fileData->visibleElements = [];
532535
foreach ($this->signRequestMapper->getVisibleElementsFromSigners($signers) as $visibleElements) {
536+
if (empty($visibleElements)) {
537+
continue;
538+
}
539+
$file = array_filter($this->fileData->files, fn (stdClass $file) => $file->id === $visibleElements[0]['file_id']);
540+
if (empty($file)) {
541+
continue;
542+
}
543+
$file = current($file);
533544
$this->fileData->visibleElements = array_merge(
534-
$this->fileListService->formatVisibleElements(
535-
$visibleElements,
536-
$this->file->getMetadata(),
537-
$this->file->getUuid(),
538-
),
545+
$this->fileListService->formatVisibleElements($visibleElements),
539546
$this->fileData->visibleElements
540547
);
541548
}
@@ -586,6 +593,7 @@ private function buildEnvelopeChildData(File $childFile): stdClass {
586593
$fileData->status = $childFile->getStatus();
587594
$fileData->statusText = $this->fileMapper->getTextOfStatus($childFile->getStatus());
588595
$fileData->nodeId = $childFile->getNodeId();
596+
$fileData->metadata = $childFile->getMetadata();
589597
$fileData->signers = [];
590598

591599
$signRequests = $this->signRequestMapper->getByFileId($childFile->getId());

lib/Service/RequestSignatureService.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public function saveFiles(array $data): array {
115115

116116
public function save(array $data): FileEntity {
117117
$file = $this->saveFile($data);
118-
$this->saveVisibleElements($data, $file);
118+
$this->saveVisibleElements($data);
119119
if (!isset($data['status'])) {
120120
$data['status'] = $file->getStatus();
121121
}
@@ -493,17 +493,13 @@ private function associateToSigners(array $data, FileEntity $file): array {
493493

494494

495495

496-
private function saveVisibleElements(array $data, FileEntity $file): array {
496+
private function saveVisibleElements(array $data): array {
497497
if (empty($data['visibleElements'])) {
498498
return [];
499499
}
500500
$elements = $data['visibleElements'];
501501
foreach ($elements as $key => $element) {
502-
if (empty($element['uuid']) && empty($element['fileId'])) {
503-
$element['fileId'] = $file->getId();
504-
}
505-
$uuid = $element['uuid'] ?? '';
506-
$elements[$key] = $this->fileElementService->saveVisibleElement($element, $uuid);
502+
$elements[$key] = $this->fileElementService->saveVisibleElement($element);
507503
}
508504
return $elements;
509505
}

0 commit comments

Comments
 (0)