Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return visible elements at list #2681

Merged
merged 4 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion lib/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ public function sign($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 @@ -367,11 +368,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 @@ -434,6 +465,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 @@ -467,7 +505,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 @@ -491,8 +530,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 @@ -534,6 +574,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