Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/legalpad/controller/LegalpadDocumentSignatureAddController.php
13459 views
1
<?php
2
3
final class LegalpadDocumentSignatureAddController extends LegalpadController {
4
5
public function handleRequest(AphrontRequest $request) {
6
$request = $this->getRequest();
7
$viewer = $request->getUser();
8
9
$document = id(new LegalpadDocumentQuery())
10
->setViewer($viewer)
11
->needDocumentBodies(true)
12
->requireCapabilities(
13
array(
14
PhabricatorPolicyCapability::CAN_VIEW,
15
PhabricatorPolicyCapability::CAN_EDIT,
16
))
17
->withIDs(array($request->getURIData('id')))
18
->executeOne();
19
if (!$document) {
20
return new Aphront404Response();
21
}
22
23
$next_uri = $this->getApplicationURI('signatures/'.$document->getID().'/');
24
25
$e_name = true;
26
$e_user = true;
27
$v_users = array();
28
$v_notes = '';
29
$v_name = '';
30
$errors = array();
31
32
$type_individual = LegalpadDocument::SIGNATURE_TYPE_INDIVIDUAL;
33
$is_individual = ($document->getSignatureType() == $type_individual);
34
35
if ($request->isFormPost()) {
36
$v_notes = $request->getStr('notes');
37
$v_users = array_slice($request->getArr('users'), 0, 1);
38
$v_name = $request->getStr('name');
39
40
if ($is_individual) {
41
$user_phid = head($v_users);
42
if (!$user_phid) {
43
$e_user = pht('Required');
44
$errors[] = pht('You must choose a user to exempt.');
45
} else {
46
$user = id(new PhabricatorPeopleQuery())
47
->setViewer($viewer)
48
->withPHIDs(array($user_phid))
49
->executeOne();
50
51
if (!$user) {
52
$e_user = pht('Invalid');
53
$errors[] = pht('That user does not exist.');
54
} else {
55
$signature = id(new LegalpadDocumentSignatureQuery())
56
->setViewer($viewer)
57
->withDocumentPHIDs(array($document->getPHID()))
58
->withSignerPHIDs(array($user->getPHID()))
59
->executeOne();
60
if ($signature) {
61
$e_user = pht('Signed');
62
$errors[] = pht('That user has already signed this document.');
63
} else {
64
$e_user = null;
65
}
66
}
67
}
68
} else {
69
$company_name = $v_name;
70
if (!strlen($company_name)) {
71
$e_name = pht('Required');
72
$errors[] = pht('You must choose a company to add an exemption for.');
73
}
74
}
75
76
if (!$errors) {
77
if ($is_individual) {
78
$name = $user->getRealName();
79
$email = $user->loadPrimaryEmailAddress();
80
$signer_phid = $user->getPHID();
81
$signature_data = array(
82
'name' => $name,
83
'email' => $email,
84
'notes' => $v_notes,
85
);
86
} else {
87
$name = $company_name;
88
$email = '';
89
$signer_phid = null;
90
$signature_data = array(
91
'name' => $name,
92
'email' => null,
93
'notes' => $v_notes,
94
'actorPHID' => $viewer->getPHID(),
95
);
96
}
97
98
$signature = id(new LegalpadDocumentSignature())
99
->setDocumentPHID($document->getPHID())
100
->setDocumentVersion($document->getVersions())
101
->setSignerPHID($signer_phid)
102
->setSignerName($name)
103
->setSignerEmail($email)
104
->setSignatureType($document->getSignatureType())
105
->setIsExemption(1)
106
->setExemptionPHID($viewer->getPHID())
107
->setVerified(LegalpadDocumentSignature::VERIFIED)
108
->setSignatureData($signature_data);
109
110
$signature->save();
111
112
return id(new AphrontRedirectResponse())->setURI($next_uri);
113
}
114
}
115
116
$form = id(new AphrontFormView())
117
->setUser($viewer);
118
119
if ($is_individual) {
120
$form
121
->appendControl(
122
id(new AphrontFormTokenizerControl())
123
->setLabel(pht('Exempt User'))
124
->setName('users')
125
->setLimit(1)
126
->setDatasource(new PhabricatorPeopleDatasource())
127
->setValue($v_users)
128
->setError($e_user));
129
} else {
130
$form
131
->appendChild(
132
id(new AphrontFormTextControl())
133
->setLabel(pht('Company Name'))
134
->setName('name')
135
->setError($e_name)
136
->setValue($v_name));
137
}
138
139
$form
140
->appendChild(
141
id(new AphrontFormTextAreaControl())
142
->setLabel(pht('Notes'))
143
->setName('notes')
144
->setValue($v_notes));
145
146
return $this->newDialog()
147
->setTitle(pht('Add Signature Exemption'))
148
->setWidth(AphrontDialogView::WIDTH_FORM)
149
->setErrors($errors)
150
->appendParagraph(
151
pht(
152
'You can record a signature exemption if a user has signed an '.
153
'equivalent document. Other applications will behave as through the '.
154
'user has signed this document.'))
155
->appendParagraph(null)
156
->appendChild($form->buildLayoutView())
157
->addSubmitButton(pht('Add Exemption'))
158
->addCancelButton($next_uri);
159
}
160
161
}
162
163