Skip to content

Commit

Permalink
Merge pull request #2681 from LibreSign/feat/return-visible-elements-…
Browse files Browse the repository at this point in the history
…at-list

Return visible elements at list
  • Loading branch information
vitormattos authored Apr 8, 2024
2 parents 22c1eb2 + b944ff9 commit 31adc45
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 6 deletions.
1 change: 0 additions & 1 deletion lib/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ public function sign(string $uuid): TemplateResponse {
->formatFile();
$this->initialState->provideInitialState('status', $file['status']);
$this->initialState->provideInitialState('statusText', $file['statusText']);
$this->initialState->provideInitialState('visibleElements', $file['visibleElements']);
$this->initialState->provideInitialState('signers', $file['signers']);
$this->provideSignerSignatues();
$signatureMethods = $this->identifyMethodService->getSignMethodsOfIdentifiedFactors($this->getSignRequestEntity()->getId());
Expand Down
69 changes: 66 additions & 3 deletions lib/Db/SignRequestMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

namespace OCA\Libresign\Db;

use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use OCA\Libresign\Helper\Pagination;
use OCA\Libresign\Service\IdentifyMethod\IIdentifyMethod;
use OCA\Libresign\Service\IdentifyMethodService;
Expand Down Expand Up @@ -368,11 +369,41 @@ public function getFilesAssociatedFilesWithMeFormatted(
}
$signers = $this->getByMultipleFileId($fileIds);
$identifyMethods = $this->getIdentifyMethodsFromSigners($signers);
$return['data'] = $this->associateAllAndFormat($user, $data, $signers, $identifyMethods);
$visibleElements = $this->getVisibleElementsFromSigners($signers);
$return['data'] = $this->associateAllAndFormat($user, $data, $signers, $identifyMethods, $visibleElements);
$return['pagination'] = $pagination;
return $return;
}

/**
* @param array<SignRequest> $signRequests
* @return FileElement[][]
*/
private function getVisibleElementsFromSigners(array $signRequests): array {
$signRequestIds = array_map(function (SignRequest $signRequest): int {
return $signRequest->getId();
}, $signRequests);
if (!$signRequestIds) {
return [];
}
$qb = $this->db->getQueryBuilder();
$qb->select('fe.*')
->from('libresign_file_element', 'fe')
->where(
$qb->expr()->in('fe.sign_request_id', $qb->createParameter('signRequestIds'))
);
$return = [];
foreach (array_chunk($signRequestIds, 1000) as $signRequestIdsChunk) {
$qb->setParameter('signRequestIds', $signRequestIdsChunk, IQueryBuilder::PARAM_INT_ARRAY);
$cursor = $qb->executeQuery();
while ($row = $cursor->fetch()) {
$fileElement = new FileElement();
$return[$row['sign_request_id']][] = $fileElement->fromRow($row);
}
}
return $return;
}

/**
* @param array<SignRequest> $signRequests
* @return array<array-key, array<array-key, \OCP\AppFramework\Db\Entity&\OCA\Libresign\Db\IdentifyMethod>>
Expand Down Expand Up @@ -435,6 +466,13 @@ private function getFilesAssociatedFilesWithMeQueryBuilder(string $userId, ?stri
'f.status',
'f.created_at',
);
// metadata is a json column, the right way is to use f.metadata::text
// when the database is PostgreSQL. The problem is that the command
// addGroupBy add quotes over all text send as argument. With
// PostgreSQL json columns don't have problem if not added to group by.
if (!$qb->getConnection()->getDatabasePlatform() instanceof PostgreSQLPlatform) {
$qb->addGroupBy('f.metadata');
}

$or = [
$qb->expr()->eq('f.user_id', $qb->createNamedParameter($userId)),
Expand Down Expand Up @@ -473,7 +511,8 @@ private function getFilesAssociatedFilesWithMeStmt(string $userId, ?string $emai
'f.user_id',
'f.uuid',
'f.name',
'f.status'
'f.status',
'f.metadata',
);
$qb->selectAlias('f.created_at', 'request_date');

Expand All @@ -497,8 +536,9 @@ private function getFilesAssociatedFilesWithMeStmt(string $userId, ?string $emai
* @param array $files
* @param array<SignRequest> $signers
* @param array<array-key, array<array-key, \OCP\AppFramework\Db\Entity&\OCA\Libresign\Db\IdentifyMethod>> $identifyMethods
* @param SignRequest[][]
*/
private function associateAllAndFormat(IUser $user, array $files, array $signers, array $identifyMethods): array {
private function associateAllAndFormat(IUser $user, array $files, array $signers, array $identifyMethods, array $visibleElements): array {
foreach ($files as $key => $file) {
$totalSigned = 0;
foreach ($signers as $signerKey => $signer) {
Expand Down Expand Up @@ -540,6 +580,29 @@ 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()] ?? []),
'identifyMethods' => array_map(function (IdentifyMethod $identifyMethod) use ($signer): array {
return [
'method' => $identifyMethod->getIdentifierKey(),
Expand Down
2 changes: 1 addition & 1 deletion lib/Service/FileElementService.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function translateCoordinatesFromInternalNotation(array $properties, File

$translated['left'] = $properties['coordinates']['llx'];
$translated['height'] = abs($properties['coordinates']['ury'] - $properties['coordinates']['lly']);
$translated['top'] = $dimension->h - $properties['coordinates']['ury'];
$translated['top'] = $dimension['h'] - $properties['coordinates']['ury'];
$translated['width'] = $properties['coordinates']['urx'] - $properties['coordinates']['llx'];

return $translated;
Expand Down
2 changes: 1 addition & 1 deletion lib/Service/FileService.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ private function getSigners(): array {
'signRequestId' => $signer->getId(),
'description' => $signer->getDescription(),
'identifyMethods' => $this->identifyMethodService->getIdentifyMethodsFromSignRequestId($signer->getId()),
'visibleElements' => $this->getVisibleElements(),
'request_sign_date' => (new \DateTime())
->setTimestamp($signer->getCreatedAt())
->format('Y-m-d H:i:s'),
Expand Down Expand Up @@ -391,7 +392,6 @@ private function getFile(): array {
if ($this->showSigners) {
$return['signers'] = $this->getSigners();
}
$return['visibleElements'] = $this->getVisibleElements();
ksort($return);
return $return;
}
Expand Down
8 changes: 8 additions & 0 deletions tests/psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@
<code>callable(QueryBuilder): void</code>
</UndefinedDocblockClass>
</file>
<file src="lib/Db/SignRequestMapper.php">
<UndefinedClass occurrences="1">
<code>PostgreSQLPlatform</code>
</UndefinedClass>
<UndefinedDocblockClass occurrences="1">
<code>$qb-&gt;getConnection()-&gt;getDatabasePlatform()</code>
</UndefinedDocblockClass>
</file>
<file src="lib/Files/TemplateLoader.php">
<UndefinedClass occurrences="2">
<code>LoadSidebar</code>
Expand Down

0 comments on commit 31adc45

Please sign in to comment.