Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/legalpad/storage/LegalpadDocument.php
13459 views
1
<?php
2
3
final class LegalpadDocument extends LegalpadDAO
4
implements
5
PhabricatorPolicyInterface,
6
PhabricatorSubscribableInterface,
7
PhabricatorApplicationTransactionInterface,
8
PhabricatorDestructibleInterface {
9
10
protected $title;
11
protected $contributorCount;
12
protected $recentContributorPHIDs = array();
13
protected $creatorPHID;
14
protected $versions;
15
protected $documentBodyPHID;
16
protected $viewPolicy;
17
protected $editPolicy;
18
protected $mailKey;
19
protected $signatureType;
20
protected $preamble;
21
protected $requireSignature;
22
23
const SIGNATURE_TYPE_NONE = 'none';
24
const SIGNATURE_TYPE_INDIVIDUAL = 'user';
25
const SIGNATURE_TYPE_CORPORATION = 'corp';
26
27
private $documentBody = self::ATTACHABLE;
28
private $contributors = self::ATTACHABLE;
29
private $signatures = self::ATTACHABLE;
30
private $userSignatures = array();
31
32
public static function initializeNewDocument(PhabricatorUser $actor) {
33
$app = id(new PhabricatorApplicationQuery())
34
->setViewer($actor)
35
->withClasses(array('PhabricatorLegalpadApplication'))
36
->executeOne();
37
38
$view_policy = $app->getPolicy(LegalpadDefaultViewCapability::CAPABILITY);
39
$edit_policy = $app->getPolicy(LegalpadDefaultEditCapability::CAPABILITY);
40
41
return id(new LegalpadDocument())
42
->setVersions(0)
43
->setCreatorPHID($actor->getPHID())
44
->setContributorCount(0)
45
->setRecentContributorPHIDs(array())
46
->attachSignatures(array())
47
->setSignatureType(self::SIGNATURE_TYPE_INDIVIDUAL)
48
->setPreamble('')
49
->setRequireSignature(0)
50
->setViewPolicy($view_policy)
51
->setEditPolicy($edit_policy);
52
}
53
54
protected function getConfiguration() {
55
return array(
56
self::CONFIG_AUX_PHID => true,
57
self::CONFIG_SERIALIZATION => array(
58
'recentContributorPHIDs' => self::SERIALIZATION_JSON,
59
),
60
self::CONFIG_COLUMN_SCHEMA => array(
61
'title' => 'text255',
62
'contributorCount' => 'uint32',
63
'versions' => 'uint32',
64
'mailKey' => 'bytes20',
65
'signatureType' => 'text4',
66
'preamble' => 'text',
67
'requireSignature' => 'bool',
68
),
69
self::CONFIG_KEY_SCHEMA => array(
70
'key_creator' => array(
71
'columns' => array('creatorPHID', 'dateModified'),
72
),
73
'key_required' => array(
74
'columns' => array('requireSignature', 'dateModified'),
75
),
76
),
77
) + parent::getConfiguration();
78
}
79
80
public function generatePHID() {
81
return PhabricatorPHID::generateNewPHID(
82
PhabricatorLegalpadDocumentPHIDType::TYPECONST);
83
}
84
85
public function getDocumentBody() {
86
return $this->assertAttached($this->documentBody);
87
}
88
89
public function attachDocumentBody(LegalpadDocumentBody $body) {
90
$this->documentBody = $body;
91
return $this;
92
}
93
94
public function getContributors() {
95
return $this->assertAttached($this->contributors);
96
}
97
98
public function attachContributors(array $contributors) {
99
$this->contributors = $contributors;
100
return $this;
101
}
102
103
public function getSignatures() {
104
return $this->assertAttached($this->signatures);
105
}
106
107
public function attachSignatures(array $signatures) {
108
$this->signatures = $signatures;
109
return $this;
110
}
111
112
public function save() {
113
if (!$this->getMailKey()) {
114
$this->setMailKey(Filesystem::readRandomCharacters(20));
115
}
116
return parent::save();
117
}
118
119
public function getMonogram() {
120
return 'L'.$this->getID();
121
}
122
123
public function getURI() {
124
return '/'.$this->getMonogram();
125
}
126
127
public function getUserSignature($phid) {
128
return $this->assertAttachedKey($this->userSignatures, $phid);
129
}
130
131
public function attachUserSignature(
132
$user_phid,
133
LegalpadDocumentSignature $signature = null) {
134
$this->userSignatures[$user_phid] = $signature;
135
return $this;
136
}
137
138
public static function getSignatureTypeMap() {
139
return array(
140
self::SIGNATURE_TYPE_INDIVIDUAL => pht('Individuals'),
141
self::SIGNATURE_TYPE_CORPORATION => pht('Corporations'),
142
self::SIGNATURE_TYPE_NONE => pht('No One'),
143
);
144
}
145
146
public function getSignatureTypeName() {
147
$type = $this->getSignatureType();
148
return idx(self::getSignatureTypeMap(), $type, $type);
149
}
150
151
public function getSignatureTypeIcon() {
152
$type = $this->getSignatureType();
153
$map = array(
154
self::SIGNATURE_TYPE_NONE => '',
155
self::SIGNATURE_TYPE_INDIVIDUAL => 'fa-user grey',
156
self::SIGNATURE_TYPE_CORPORATION => 'fa-building-o grey',
157
);
158
159
return idx($map, $type, 'fa-user grey');
160
}
161
162
163
/* -( PhabricatorSubscribableInterface )----------------------------------- */
164
165
166
public function isAutomaticallySubscribed($phid) {
167
return ($this->creatorPHID == $phid);
168
}
169
170
171
/* -( PhabricatorPolicyInterface )----------------------------------------- */
172
173
174
public function getCapabilities() {
175
return array(
176
PhabricatorPolicyCapability::CAN_VIEW,
177
PhabricatorPolicyCapability::CAN_EDIT,
178
);
179
}
180
181
public function getPolicy($capability) {
182
switch ($capability) {
183
case PhabricatorPolicyCapability::CAN_VIEW:
184
$policy = $this->viewPolicy;
185
break;
186
case PhabricatorPolicyCapability::CAN_EDIT:
187
$policy = $this->editPolicy;
188
break;
189
default:
190
$policy = PhabricatorPolicies::POLICY_NOONE;
191
break;
192
}
193
return $policy;
194
}
195
196
public function hasAutomaticCapability($capability, PhabricatorUser $user) {
197
return ($user->getPHID() == $this->getCreatorPHID());
198
}
199
200
public function describeAutomaticCapability($capability) {
201
return pht('The author of a document can always view and edit it.');
202
}
203
204
205
/* -( PhabricatorApplicationTransactionInterface )------------------------- */
206
207
208
public function getApplicationTransactionEditor() {
209
return new LegalpadDocumentEditor();
210
}
211
212
public function getApplicationTransactionTemplate() {
213
return new LegalpadTransaction();
214
}
215
216
217
/* -( PhabricatorDestructibleInterface )----------------------------------- */
218
219
220
public function destroyObjectPermanently(
221
PhabricatorDestructionEngine $engine) {
222
223
$this->openTransaction();
224
$this->delete();
225
226
$bodies = id(new LegalpadDocumentBody())->loadAllWhere(
227
'documentPHID = %s',
228
$this->getPHID());
229
foreach ($bodies as $body) {
230
$body->delete();
231
}
232
233
$signatures = id(new LegalpadDocumentSignature())->loadAllWhere(
234
'documentPHID = %s',
235
$this->getPHID());
236
foreach ($signatures as $signature) {
237
$signature->delete();
238
}
239
240
$this->saveTransaction();
241
}
242
243
}
244
245