-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathPendingSignaturesWidget.php
More file actions
164 lines (139 loc) · 4.34 KB
/
PendingSignaturesWidget.php
File metadata and controls
164 lines (139 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 LibreSign
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Libresign\Dashboard;
use OCA\Libresign\AppInfo\Application;
use OCA\Libresign\Db\File;
use OCA\Libresign\Db\SignRequest;
use OCA\Libresign\Db\SignRequestMapper;
use OCA\Libresign\Enum\FileStatus;
use OCA\Libresign\Handler\CertificateEngine\CertificateEngineFactory;
use OCP\Dashboard\IAPIWidget;
use OCP\Dashboard\IAPIWidgetV2;
use OCP\Dashboard\IButtonWidget;
use OCP\Dashboard\IConditionalWidget;
use OCP\Dashboard\IIconWidget;
use OCP\Dashboard\Model\WidgetButton;
use OCP\Dashboard\Model\WidgetItem;
use OCP\Dashboard\Model\WidgetItems;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUserSession;
use Override;
class PendingSignaturesWidget implements IAPIWidget, IAPIWidgetV2, IButtonWidget, IConditionalWidget, IIconWidget {
public function __construct(
private IL10N $l10n,
private IURLGenerator $urlGenerator,
private SignRequestMapper $signRequestMapper,
private IUserSession $userSession,
private CertificateEngineFactory $certificateEngineFactory,
) {
}
#[Override]
public function getId(): string {
return 'libresign_pending_signatures';
}
#[Override]
public function getTitle(): string {
return $this->l10n->t('Pending signatures');
}
#[Override]
public function getOrder(): int {
return 10;
}
#[Override]
public function getIconClass(): string {
return 'icon-libresign';
}
#[Override]
public function getIconUrl(): string {
return $this->urlGenerator->getAbsoluteURL(
$this->urlGenerator->imagePath(Application::APP_ID, 'app-dark.svg')
);
}
#[Override]
public function getUrl(): ?string {
return $this->urlGenerator->linkToRouteAbsolute('libresign.page.index');
}
#[Override]
public function load(): void {
// No special loading required
}
#[Override]
public function isEnabled(): bool {
return $this->certificateEngineFactory->getEngine()->isSetupOk();
}
#[Override]
public function getItemsV2(string $userId, ?string $since = null, int $limit = 7): WidgetItems {
$user = $this->userSession->getUser();
if (!$user) {
return new WidgetItems([], $this->l10n->t('User not found'));
}
$result = $this->signRequestMapper->getFilesAssociatedFilesWithMe(
$user,
['status' => [FileStatus::ABLE_TO_SIGN->value, FileStatus::PARTIAL_SIGNED->value]],
1,
$limit,
['sortBy' => 'created_at', 'sortDirection' => 'desc']
);
$items = [];
foreach ($result['data'] as $fileEntity) {
$signRequests = $this->signRequestMapper->getByFileId($fileEntity->getId());
foreach ($signRequests as $signRequest) {
if ($signRequest->getSigned()) {
continue;
}
$item = new WidgetItem(
$fileEntity->getName(),
$this->getSubtitle($signRequest, $fileEntity),
$this->urlGenerator->linkToRouteAbsolute('libresign.page.signFPath', ['uuid' => $signRequest->getUuid(), 'path' => 'pdf']),
$this->urlGenerator->getAbsoluteURL(
$this->urlGenerator->imagePath('core', 'filetypes/application-pdf.svg')
),
$this->getTimestamp($fileEntity)
);
$items[] = $item;
}
}
return new WidgetItems(
$items,
empty($items) ? $this->l10n->t('No pending signatures') : '',
);
}
private function getSubtitle(SignRequest $signRequest, File $fileEntity): string {
$displayName = $signRequest->getDisplayName();
$createdAt = $fileEntity->getCreatedAt();
if ($createdAt instanceof \DateTime) {
$date = $createdAt->format('d/m/Y');
// TRANSLATORS %s is the sender name, %s is the date
return $this->l10n->t('From: %s • Date: %s', [$displayName, $date]);
}
// TRANSLATORS %s is the sender name
return $this->l10n->t('From: %s', [$displayName]);
}
private function getTimestamp(File $fileEntity): string {
$createdAt = $fileEntity->getCreatedAt();
if ($createdAt instanceof \DateTime) {
return (string)$createdAt->getTimestamp();
}
return '';
}
#[Override]
public function getItems(string $userId, ?string $since = null, int $limit = 7): array {
$widgetItems = $this->getItemsV2($userId, $since, $limit);
return array_values($widgetItems->getItems());
}
#[Override]
public function getWidgetButtons(string $userId): array {
return [
new WidgetButton(
WidgetButton::TYPE_MORE,
$this->urlGenerator->linkToRouteAbsolute('libresign.page.index'),
$this->l10n->t('View all documents')
),
];
}
}