-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathForm.php
More file actions
241 lines (221 loc) Β· 7.4 KB
/
Form.php
File metadata and controls
241 lines (221 loc) Β· 7.4 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Forms\Db;
use OCA\Forms\Constants;
use OCA\Forms\ResponseDefinitions;
use OCP\AppFramework\Db\Entity;
/**
* @psalm-import-type FormsAccess from ResponseDefinitions
* @method string getHash()
* @method void setHash(string $value)
* @method string getTitle()
* @method void setTitle(string $value)
* @method string getDescription()
* @method void setDescription(string $value)
* @method string getOwnerId()
* @method void setOwnerId(string $value)
* @method int|null getFileId()
* @method void setFileId(int|null $value)
* @method string|null getFileFormat()
* @method void setFileFormat(string|null $value)
* @method int getCreated()
* @method void setCreated(int $value)
* @method int getExpires()
* @method void setExpires(int $value)
* @method int getIsAnonymous()
* @method void setIsAnonymous(bool $value)
* @method int getSubmitMultiple()
* @method void setSubmitMultiple(bool $value)
* @method int getAllowEditSubmissions()
* @method void setAllowEditSubmissions(bool $value)
* @method int getShowExpiration()
* @method void setShowExpiration(bool $value)
* @method int getLastUpdated()
* @method void setLastUpdated(int $value)
* @method string|null getSubmissionMessage()
* @method void setSubmissionMessage(string|null $value)
* @method bool getNotifyOwnerOnSubmission()
* @method void setNotifyOwnerOnSubmission(bool $value)
* @method bool getAttachSubmissionPdf()
* @method void setAttachSubmissionPdf(bool $value)
* @method string|null getNotificationRecipientsJson()
* @method void setNotificationRecipientsJson(?string $value)
* @method int getState()
* @psalm-method 0|1|2 getState()
* @method void setState(int|null $value)
* @psalm-method void setState(0|1|2|null $value)
* @method string getLockedBy()
* @method void setLockedBy(string|null $value)
* @method int getLockedUntil()
* @method int|null getMaxSubmissions()
* @method void setMaxSubmissions(int|null $value)
* @method void setLockedUntil(int|null $value)
*/
class Form extends Entity {
protected $hash;
protected $title;
protected $description;
protected $ownerId;
protected $fileId;
protected $fileFormat;
protected $accessEnum;
protected $created;
protected $expires;
protected $isAnonymous;
protected $submitMultiple;
protected $allowEditSubmissions;
protected $showExpiration;
protected $submissionMessage;
protected $notifyOwnerOnSubmission;
protected $attachSubmissionPdf;
protected $notificationRecipientsJson;
protected $lastUpdated;
protected $state;
protected $lockedBy;
protected $lockedUntil;
protected $maxSubmissions;
/**
* Form constructor.
*/
public function __construct() {
$this->addType('created', 'integer');
$this->addType('expires', 'integer');
$this->addType('isAnonymous', 'boolean');
$this->addType('submitMultiple', 'boolean');
$this->addType('allowEditSubmissions', 'boolean');
$this->addType('showExpiration', 'boolean');
$this->addType('notifyOwnerOnSubmission', 'boolean');
$this->addType('attachSubmissionPdf', 'boolean');
$this->addType('lastUpdated', 'integer');
$this->addType('state', 'integer');
$this->addType('lockedBy', 'string');
$this->addType('lockedUntil', 'integer');
$this->addType('maxSubmissions', 'integer');
}
// JSON-Decoding of access-column.
/**
* @return FormsAccess
*/
public function getAccess(): array {
$accessEnum = $this->getAccessEnum();
$access = [];
switch ($accessEnum) {
case Constants::FORM_ACCESS_NOPUBLICSHARE:
$access['permitAllUsers'] = false;
$access['showToAllUsers'] = false;
break;
case Constants::FORM_ACCESS_PERMITALLUSERS:
$access['permitAllUsers'] = true;
$access['showToAllUsers'] = false;
break;
case Constants::FORM_ACCESS_SHOWTOALLUSERS:
$access['permitAllUsers'] = true;
$access['showToAllUsers'] = true;
break;
}
return $access;
}
// JSON-Encoding of access-column.
/**
* @param FormsAccess $access
*/
public function setAccess(array $access): void {
// No further permissions -> 0
// Permit all users, but don't show in navigation -> 1
// Permit all users and show in navigation -> 2
if (!$access['permitAllUsers']) {
// no permit means no public share
$value = Constants::FORM_ACCESS_NOPUBLICSHARE;
} elseif ($access['showToAllUsers']) {
// permit all + show to all
$value = Constants::FORM_ACCESS_SHOWTOALLUSERS;
} else {
// only permit all but not shown to all
$value = Constants::FORM_ACCESS_PERMITALLUSERS;
}
$this->setAccessEnum($value);
}
/**
* @return list<string>
*/
public function getNotificationRecipients(): array {
$encodedRecipients = $this->getNotificationRecipientsJson();
if ($encodedRecipients === null || $encodedRecipients === '') {
return [];
}
$decodedRecipients = json_decode($encodedRecipients, true, 512, JSON_THROW_ON_ERROR);
if (!is_array($decodedRecipients)) {
return [];
}
return array_values(array_filter(array_map(static fn (mixed $recipient): string => trim((string)$recipient), $decodedRecipients), static fn (string $recipient): bool => $recipient !== ''));
}
/**
* @param list<string> $recipients
*/
public function setNotificationRecipients(array $recipients): void {
$normalizedRecipients = array_values(array_filter(array_map(static fn (string $recipient): string => trim($recipient), $recipients), static fn (string $recipient): bool => $recipient !== ''));
if ($normalizedRecipients === []) {
$this->setNotificationRecipientsJson(null);
return;
}
$this->setNotificationRecipientsJson(json_encode($normalizedRecipients, JSON_THROW_ON_ERROR));
}
/**
* @return array{
* id: int,
* hash: string,
* title: string,
* description: string,
* ownerId: string,
* fileId: ?int,
* fileFormat: ?string,
* created: int,
* access: FormsAccess,
* expires: int,
* isAnonymous: bool,
* submitMultiple: bool,
* allowEditSubmissions: bool,
* showExpiration: bool,
* lastUpdated: int,
* submissionMessage: ?string,
* notifyOwnerOnSubmission: bool,
* attachSubmissionPdf: bool,
* notificationRecipients: list<string>,
* state: 0|1|2,
* lockedBy: ?string,
* lockedUntil: ?int,
* maxSubmissions: ?int,
* }
*/
public function read() {
return [
'id' => $this->getId(),
'hash' => $this->getHash(),
'title' => (string)$this->getTitle(),
'description' => (string)$this->getDescription(),
'ownerId' => $this->getOwnerId(),
'fileId' => $this->getFileId(),
'fileFormat' => $this->getFileFormat(),
'created' => $this->getCreated(),
'access' => $this->getAccess(),
'expires' => (int)$this->getExpires(),
'isAnonymous' => (bool)$this->getIsAnonymous(),
'submitMultiple' => (bool)$this->getSubmitMultiple(),
'allowEditSubmissions' => (bool)$this->getAllowEditSubmissions(),
'showExpiration' => (bool)$this->getShowExpiration(),
'lastUpdated' => (int)$this->getLastUpdated(),
'submissionMessage' => $this->getSubmissionMessage(),
'notifyOwnerOnSubmission' => (bool)$this->getNotifyOwnerOnSubmission(),
'attachSubmissionPdf' => (bool)$this->getAttachSubmissionPdf(),
'notificationRecipients' => $this->getNotificationRecipients(),
'state' => $this->getState(),
'lockedBy' => $this->getLockedBy(),
'lockedUntil' => $this->getLockedUntil(),
'maxSubmissions' => $this->getMaxSubmissions(),
];
}
}